xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 0885057cf9e0a155eecaa8c15cb1076eaa432057)
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 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_singleton.hpp"
207a1dbc48SGeorge Liu #include "dbus_utility.hpp"
21aaf08ac7SMatt Simmering #include "generated/enums/resource.hpp"
220ec8b83dSEd Tanous #include "generated/enums/sensor.hpp"
233ccb3adbSEd Tanous #include "query.hpp"
243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
2550ebd4afSEd Tanous #include "str_utility.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
283ccb3adbSEd Tanous #include "utils/query_param.hpp"
290ec8b83dSEd Tanous 
30e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
31ef4c65b7SEd Tanous #include <boost/url/format.hpp>
321e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
3386d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
341214b7e7SGunnar Mills 
357a1dbc48SGeorge Liu #include <array>
361214b7e7SGunnar Mills #include <cmath>
37fe04d49cSNan Zhou #include <iterator>
38283860f5SEd Tanous #include <limits>
39fe04d49cSNan Zhou #include <map>
403544d2a7SEd Tanous #include <ranges>
41fe04d49cSNan Zhou #include <set>
4218f8f608SEd Tanous #include <string>
437a1dbc48SGeorge Liu #include <string_view>
44b5a76932SEd Tanous #include <utility>
45abf2add6SEd Tanous #include <variant>
4608777fb0SLewanczyk, Dawid 
471abe55efSEd Tanous namespace redfish
481abe55efSEd Tanous {
4908777fb0SLewanczyk, Dawid 
50a0ec28b6SAdrian Ambrożewicz namespace sensors
51a0ec28b6SAdrian Ambrożewicz {
52a0ec28b6SAdrian Ambrożewicz namespace node
53a0ec28b6SAdrian Ambrożewicz {
54a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
55a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
56a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
57a0ec28b6SAdrian Ambrożewicz } // namespace node
58a0ec28b6SAdrian Ambrożewicz 
5902da7c5aSEd Tanous // clang-format off
60a0ec28b6SAdrian Ambrożewicz namespace dbus
61a0ec28b6SAdrian Ambrożewicz {
62cf9e417dSEd Tanous constexpr auto powerPaths = std::to_array<std::string_view>({
6302da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
6402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
6502da7c5aSEd Tanous });
66c2bf7f99SWludzik, Jozef 
67cf9e417dSEd Tanous constexpr auto sensorPaths = std::to_array<std::string_view>({
6802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power",
69a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/current",
707088690cSBasheer Ahmed Muddebihal     "/xyz/openbmc_project/sensors/airflow",
715deabed9SGunnar Mills     "/xyz/openbmc_project/sensors/humidity",
72e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
73e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/voltage",
74e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_tach",
75e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/temperature",
76e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_pwm",
77e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/altitude",
78e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/energy",
79e8204933SGeorge Liu #endif
8002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/utilization"
8102da7c5aSEd Tanous });
8202da7c5aSEd Tanous 
83cf9e417dSEd Tanous constexpr auto thermalPaths = std::to_array<std::string_view>({
8402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
85a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
8602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
8702da7c5aSEd Tanous });
8802da7c5aSEd Tanous 
89c2bf7f99SWludzik, Jozef } // namespace dbus
9002da7c5aSEd Tanous // clang-format on
9102da7c5aSEd Tanous 
92cf9e417dSEd Tanous using sensorPair =
93cf9e417dSEd Tanous     std::pair<std::string_view, std::span<const std::string_view>>;
9402da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
95cf9e417dSEd Tanous     {{node::power, dbus::powerPaths},
96cf9e417dSEd Tanous      {node::sensors, dbus::sensorPaths},
97cf9e417dSEd Tanous      {node::thermal, dbus::thermalPaths}}};
98c2bf7f99SWludzik, Jozef 
990ec8b83dSEd Tanous inline sensor::ReadingType toReadingType(std::string_view sensorType)
100c2bf7f99SWludzik, Jozef {
101c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
102c2bf7f99SWludzik, Jozef     {
1030ec8b83dSEd Tanous         return sensor::ReadingType::Voltage;
104c2bf7f99SWludzik, Jozef     }
105c2bf7f99SWludzik, Jozef     if (sensorType == "power")
106c2bf7f99SWludzik, Jozef     {
1070ec8b83dSEd Tanous         return sensor::ReadingType::Power;
108c2bf7f99SWludzik, Jozef     }
109c2bf7f99SWludzik, Jozef     if (sensorType == "current")
110c2bf7f99SWludzik, Jozef     {
1110ec8b83dSEd Tanous         return sensor::ReadingType::Current;
112c2bf7f99SWludzik, Jozef     }
113c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
114c2bf7f99SWludzik, Jozef     {
1150ec8b83dSEd Tanous         return sensor::ReadingType::Rotational;
116c2bf7f99SWludzik, Jozef     }
117c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
118c2bf7f99SWludzik, Jozef     {
1190ec8b83dSEd Tanous         return sensor::ReadingType::Temperature;
120c2bf7f99SWludzik, Jozef     }
121c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
122c2bf7f99SWludzik, Jozef     {
1230ec8b83dSEd Tanous         return sensor::ReadingType::Percent;
124c2bf7f99SWludzik, Jozef     }
1255deabed9SGunnar Mills     if (sensorType == "humidity")
1265deabed9SGunnar Mills     {
1270ec8b83dSEd Tanous         return sensor::ReadingType::Humidity;
1285deabed9SGunnar Mills     }
129c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
130c2bf7f99SWludzik, Jozef     {
1310ec8b83dSEd Tanous         return sensor::ReadingType::Altitude;
132c2bf7f99SWludzik, Jozef     }
133c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
134c2bf7f99SWludzik, Jozef     {
1350ec8b83dSEd Tanous         return sensor::ReadingType::AirFlow;
136c2bf7f99SWludzik, Jozef     }
137c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
138c2bf7f99SWludzik, Jozef     {
1390ec8b83dSEd Tanous         return sensor::ReadingType::EnergyJoules;
140c2bf7f99SWludzik, Jozef     }
1410ec8b83dSEd Tanous     return sensor::ReadingType::Invalid;
142c2bf7f99SWludzik, Jozef }
143c2bf7f99SWludzik, Jozef 
1441d7c0054SEd Tanous inline std::string_view toReadingUnits(std::string_view sensorType)
145c2bf7f99SWludzik, Jozef {
146c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
147c2bf7f99SWludzik, Jozef     {
148c2bf7f99SWludzik, Jozef         return "V";
149c2bf7f99SWludzik, Jozef     }
150c2bf7f99SWludzik, Jozef     if (sensorType == "power")
151c2bf7f99SWludzik, Jozef     {
152c2bf7f99SWludzik, Jozef         return "W";
153c2bf7f99SWludzik, Jozef     }
154c2bf7f99SWludzik, Jozef     if (sensorType == "current")
155c2bf7f99SWludzik, Jozef     {
156c2bf7f99SWludzik, Jozef         return "A";
157c2bf7f99SWludzik, Jozef     }
158c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
159c2bf7f99SWludzik, Jozef     {
160c2bf7f99SWludzik, Jozef         return "RPM";
161c2bf7f99SWludzik, Jozef     }
162c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
163c2bf7f99SWludzik, Jozef     {
164c2bf7f99SWludzik, Jozef         return "Cel";
165c2bf7f99SWludzik, Jozef     }
1665deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
1675deabed9SGunnar Mills         sensorType == "humidity")
168c2bf7f99SWludzik, Jozef     {
169c2bf7f99SWludzik, Jozef         return "%";
170c2bf7f99SWludzik, Jozef     }
171c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
172c2bf7f99SWludzik, Jozef     {
173c2bf7f99SWludzik, Jozef         return "m";
174c2bf7f99SWludzik, Jozef     }
175c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
176c2bf7f99SWludzik, Jozef     {
177c2bf7f99SWludzik, Jozef         return "cft_i/min";
178c2bf7f99SWludzik, Jozef     }
179c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
180c2bf7f99SWludzik, Jozef     {
181c2bf7f99SWludzik, Jozef         return "J";
182c2bf7f99SWludzik, Jozef     }
183c2bf7f99SWludzik, Jozef     return "";
184a0ec28b6SAdrian Ambrożewicz }
185a0ec28b6SAdrian Ambrożewicz } // namespace sensors
186a0ec28b6SAdrian Ambrożewicz 
18708777fb0SLewanczyk, Dawid /**
188588c3f0dSKowalski, Kamil  * SensorsAsyncResp
18908777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
19008777fb0SLewanczyk, Dawid  */
1911abe55efSEd Tanous class SensorsAsyncResp
1921abe55efSEd Tanous {
19308777fb0SLewanczyk, Dawid   public:
194a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
195a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
196fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
197a0ec28b6SAdrian Ambrożewicz 
198a0ec28b6SAdrian Ambrożewicz     struct SensorData
199a0ec28b6SAdrian Ambrożewicz     {
200a0ec28b6SAdrian Ambrożewicz         const std::string name;
201a0ec28b6SAdrian Ambrożewicz         std::string uri;
202a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
203a0ec28b6SAdrian Ambrożewicz     };
204a0ec28b6SAdrian Ambrożewicz 
2058a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2068d1b46d7Szhanghch05                      const std::string& chassisIdIn,
207cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
20802da7c5aSEd Tanous                      std::string_view subNode) :
2098a592810SEd Tanous         asyncResp(asyncRespIn),
210928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
211928fefb9SNan Zhou         efficientExpand(false)
2121214b7e7SGunnar Mills     {}
21308777fb0SLewanczyk, Dawid 
214a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
2158a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2168d1b46d7Szhanghch05                      const std::string& chassisIdIn,
217cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
21802da7c5aSEd Tanous                      std::string_view subNode,
219a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
2208a592810SEd Tanous         asyncResp(asyncRespIn),
221928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
222928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
223a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
224a0ec28b6SAdrian Ambrożewicz     {}
225a0ec28b6SAdrian Ambrożewicz 
226928fefb9SNan Zhou     // sensor collections expand
2278a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
228928fefb9SNan Zhou                      const std::string& chassisIdIn,
229cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
2308a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
2318a592810SEd Tanous         asyncResp(asyncRespIn),
232928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
2338a592810SEd Tanous         efficientExpand(efficientExpandIn)
234928fefb9SNan Zhou     {}
235928fefb9SNan Zhou 
2361abe55efSEd Tanous     ~SensorsAsyncResp()
2371abe55efSEd Tanous     {
2388d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2398d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2401abe55efSEd Tanous         {
2411abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2421abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2431abe55efSEd Tanous             // proper code
2448d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
24508777fb0SLewanczyk, Dawid         }
246a0ec28b6SAdrian Ambrożewicz 
247a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
248a0ec28b6SAdrian Ambrożewicz         {
249fe04d49cSNan Zhou             std::map<std::string, std::string> map;
2508d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
251a0ec28b6SAdrian Ambrożewicz             {
252a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
253a0ec28b6SAdrian Ambrożewicz                 {
254c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
255a0ec28b6SAdrian Ambrożewicz                 }
256a0ec28b6SAdrian Ambrożewicz             }
2578d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
258a0ec28b6SAdrian Ambrożewicz         }
25908777fb0SLewanczyk, Dawid     }
260588c3f0dSKowalski, Kamil 
261ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
262ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
263ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
264ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
265ecd6a3a2SEd Tanous 
266a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
267c1d019a6SEd Tanous                      const std::string& dbusPath)
268a0ec28b6SAdrian Ambrożewicz     {
269a0ec28b6SAdrian Ambrożewicz         if (metadata)
270a0ec28b6SAdrian Ambrożewicz         {
271c1d019a6SEd Tanous             metadata->emplace_back(SensorData{
272c1d019a6SEd Tanous                 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
273a0ec28b6SAdrian Ambrożewicz         }
274a0ec28b6SAdrian Ambrożewicz     }
275a0ec28b6SAdrian Ambrożewicz 
276a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
277a0ec28b6SAdrian Ambrożewicz     {
278a0ec28b6SAdrian Ambrożewicz         if (metadata)
279a0ec28b6SAdrian Ambrożewicz         {
280a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
281a0ec28b6SAdrian Ambrożewicz             {
282a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
283a0ec28b6SAdrian Ambrożewicz                 {
284a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
285a0ec28b6SAdrian Ambrożewicz                 }
286a0ec28b6SAdrian Ambrożewicz             }
287a0ec28b6SAdrian Ambrożewicz         }
288a0ec28b6SAdrian Ambrożewicz     }
289a0ec28b6SAdrian Ambrożewicz 
2908d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
291a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
292cf9e417dSEd Tanous     const std::span<const std::string_view> types;
293a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
294928fefb9SNan Zhou     const bool efficientExpand;
295a0ec28b6SAdrian Ambrożewicz 
296a0ec28b6SAdrian Ambrożewicz   private:
297a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
298a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
29908777fb0SLewanczyk, Dawid };
30008777fb0SLewanczyk, Dawid 
30108777fb0SLewanczyk, Dawid /**
302d500549bSAnthony Wilson  * Possible states for physical inventory leds
303d500549bSAnthony Wilson  */
304d500549bSAnthony Wilson enum class LedState
305d500549bSAnthony Wilson {
306d500549bSAnthony Wilson     OFF,
307d500549bSAnthony Wilson     ON,
308d500549bSAnthony Wilson     BLINK,
309d500549bSAnthony Wilson     UNKNOWN
310d500549bSAnthony Wilson };
311d500549bSAnthony Wilson 
312d500549bSAnthony Wilson /**
313adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
314adc4f0dbSShawn McCarney  */
315adc4f0dbSShawn McCarney class InventoryItem
316adc4f0dbSShawn McCarney {
317adc4f0dbSShawn McCarney   public:
3184e23a444SEd Tanous     explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
319adc4f0dbSShawn McCarney     {
320adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
32128aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
32228aa8de5SGeorge Liu         name = path.filename();
32328aa8de5SGeorge Liu         if (name.empty())
324adc4f0dbSShawn McCarney         {
32562598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath);
326adc4f0dbSShawn McCarney         }
327adc4f0dbSShawn McCarney     }
328adc4f0dbSShawn McCarney 
329adc4f0dbSShawn McCarney     std::string objectPath;
330adc4f0dbSShawn McCarney     std::string name;
331e05aec50SEd Tanous     bool isPresent = true;
332e05aec50SEd Tanous     bool isFunctional = true;
333e05aec50SEd Tanous     bool isPowerSupply = false;
334e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
335adc4f0dbSShawn McCarney     std::string manufacturer;
336adc4f0dbSShawn McCarney     std::string model;
337adc4f0dbSShawn McCarney     std::string partNumber;
338adc4f0dbSShawn McCarney     std::string serialNumber;
339adc4f0dbSShawn McCarney     std::set<std::string> sensors;
340d500549bSAnthony Wilson     std::string ledObjectPath;
341e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
342adc4f0dbSShawn McCarney };
343adc4f0dbSShawn McCarney 
344adc4f0dbSShawn McCarney /**
345413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
346588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
34708777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
34808777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
34908777fb0SLewanczyk, Dawid  */
35008777fb0SLewanczyk, Dawid template <typename Callback>
351413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
35281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
353fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
3541abe55efSEd Tanous     Callback&& callback)
3551abe55efSEd Tanous {
35662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
35703b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
358e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
35908777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
36008777fb0SLewanczyk, Dawid 
361e99073f5SGeorge Liu     // Make call to ObjectMapper to find all sensors objects
362e99073f5SGeorge Liu     dbus::utility::getSubTree(
363e99073f5SGeorge Liu         path, 2, interfaces,
364002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
365e99073f5SGeorge Liu          sensorNames](const boost::system::error_code& ec,
366002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
367e99073f5SGeorge Liu         // Response handler for parsing objects subtree
36862598e31SEd Tanous         BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
3691abe55efSEd Tanous         if (ec)
3701abe55efSEd Tanous         {
3718d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
37262598e31SEd Tanous             BMCWEB_LOG_ERROR(
37362598e31SEd Tanous                 "getObjectsWithConnection resp_handler: Dbus error {}", ec);
37408777fb0SLewanczyk, Dawid             return;
37508777fb0SLewanczyk, Dawid         }
37608777fb0SLewanczyk, Dawid 
37762598e31SEd Tanous         BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
37808777fb0SLewanczyk, Dawid 
37908777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
38008777fb0SLewanczyk, Dawid         // found in the chassis
381fe04d49cSNan Zhou         std::set<std::string> connections;
382413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
38308777fb0SLewanczyk, Dawid 
38462598e31SEd Tanous         BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
38549c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3861abe55efSEd Tanous         {
38762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
38808777fb0SLewanczyk, Dawid         }
38908777fb0SLewanczyk, Dawid 
39008777fb0SLewanczyk, Dawid         for (const std::pair<
39108777fb0SLewanczyk, Dawid                  std::string,
39208777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3931abe55efSEd Tanous                  object : subtree)
3941abe55efSEd Tanous         {
39549c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3961abe55efSEd Tanous             {
39749c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3981abe55efSEd Tanous                          objData : object.second)
3991abe55efSEd Tanous                 {
40062598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Adding connection: {}", objData.first);
40108777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
402de629b6eSShawn McCarney                     objectsWithConnection.insert(
403de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
40408777fb0SLewanczyk, Dawid                 }
40508777fb0SLewanczyk, Dawid             }
40608777fb0SLewanczyk, Dawid         }
40762598e31SEd Tanous         BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
408413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
40962598e31SEd Tanous         BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
410e99073f5SGeorge Liu     });
41162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
412413961deSRichard Marian Thomaiyar }
413413961deSRichard Marian Thomaiyar 
414413961deSRichard Marian Thomaiyar /**
415413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
416413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
417413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
418413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
419413961deSRichard Marian Thomaiyar  */
420413961deSRichard Marian Thomaiyar template <typename Callback>
421fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
422fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
423413961deSRichard Marian Thomaiyar                     Callback&& callback)
424413961deSRichard Marian Thomaiyar {
425413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
426fe04d49cSNan Zhou         [callback](const std::set<std::string>& connections,
427413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4283174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
42981ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
430413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
43108777fb0SLewanczyk, Dawid }
43208777fb0SLewanczyk, Dawid 
43308777fb0SLewanczyk, Dawid /**
43449c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43549c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43649c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
43749c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
43849c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
43949c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
44049c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
44149c53ac9SJohnathan Mantey  */
44223a21a1cSEd Tanous inline void reduceSensorList(
4437f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
444cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
44549c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
446fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
44749c53ac9SJohnathan Mantey {
44849c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
44949c53ac9SJohnathan Mantey     {
4507f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
4517f1cc26dSEd Tanous                                    chassisSubNode == sensors::node::thermal
452a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
45349c53ac9SJohnathan Mantey                                        : "Voltages");
45449c53ac9SJohnathan Mantey 
45549c53ac9SJohnathan Mantey         return;
45649c53ac9SJohnathan Mantey     }
45749c53ac9SJohnathan Mantey     if (allSensors->empty())
45849c53ac9SJohnathan Mantey     {
45949c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
46049c53ac9SJohnathan Mantey         return;
46149c53ac9SJohnathan Mantey     }
46249c53ac9SJohnathan Mantey 
4637f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
46449c53ac9SJohnathan Mantey     {
46549c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46649c53ac9SJohnathan Mantey         {
46711ba3979SEd Tanous             if (sensor.starts_with(type))
46849c53ac9SJohnathan Mantey             {
46949c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
47049c53ac9SJohnathan Mantey             }
47149c53ac9SJohnathan Mantey         }
47249c53ac9SJohnathan Mantey     }
47349c53ac9SJohnathan Mantey }
47449c53ac9SJohnathan Mantey 
4757f1cc26dSEd Tanous /*
4767f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
4777f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
4787f1cc26dSEd Tanous  *
4797f1cc26dSEd Tanous  * */
4807f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
4817f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
4827f1cc26dSEd Tanous {
4837f1cc26dSEd Tanous     if (chassisSubNode == sensors::node::power)
4847f1cc26dSEd Tanous     {
4857f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
4867f1cc26dSEd Tanous     }
4877f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::thermal)
4887f1cc26dSEd Tanous     {
4897f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
4907f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
4917f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
4927f1cc26dSEd Tanous     }
4937f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::sensors)
4947f1cc26dSEd Tanous     {
4957f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
4967f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
4977f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
4987f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
4997f1cc26dSEd Tanous     }
5007f1cc26dSEd Tanous 
5017f1cc26dSEd Tanous     if (chassisSubNode != sensors::node::sensors)
5027f1cc26dSEd Tanous     {
5037f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
5047f1cc26dSEd Tanous     }
5057f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
5067f1cc26dSEd Tanous }
5077f1cc26dSEd Tanous 
50849c53ac9SJohnathan Mantey /**
50908777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
510588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
51108777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
51208777fb0SLewanczyk, Dawid  */
51308777fb0SLewanczyk, Dawid template <typename Callback>
5147f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5157f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
516cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
517cf9e417dSEd Tanous                 Callback&& callback)
5181abe55efSEd Tanous {
51962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis enter");
5207a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
52149c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
522adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
5237a1dbc48SGeorge Liu 
5247a1dbc48SGeorge Liu     // Get the Chassis Collection
5257a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
5267a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
5277f1cc26dSEd Tanous         [callback{std::forward<Callback>(callback)}, asyncResp,
5287f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
5297f1cc26dSEd Tanous          chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
5307a1dbc48SGeorge Liu             const boost::system::error_code& ec,
531002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
53262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getChassis respHandler enter");
5331abe55efSEd Tanous         if (ec)
5341abe55efSEd Tanous         {
53562598e31SEd Tanous             BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
5367f1cc26dSEd Tanous             messages::internalError(asyncResp->res);
53708777fb0SLewanczyk, Dawid             return;
53808777fb0SLewanczyk, Dawid         }
53949c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
54049c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5411abe55efSEd Tanous         {
54228aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
543f8fe53e7SEd Tanous             std::string chassisName = path.filename();
54428aa8de5SGeorge Liu             if (chassisName.empty())
5451abe55efSEd Tanous             {
54662598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
547daf36e2eSEd Tanous                 continue;
548daf36e2eSEd Tanous             }
5497f1cc26dSEd Tanous             if (chassisName == chassisIdStr)
5501abe55efSEd Tanous             {
55149c53ac9SJohnathan Mantey                 chassisPath = &chassis;
55249c53ac9SJohnathan Mantey                 break;
553daf36e2eSEd Tanous             }
55449c53ac9SJohnathan Mantey         }
55549c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5561abe55efSEd Tanous         {
5577f1cc26dSEd Tanous             messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
55849c53ac9SJohnathan Mantey             return;
5591abe55efSEd Tanous         }
5607f1cc26dSEd Tanous         populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
56108777fb0SLewanczyk, Dawid 
562ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
563ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
56495a3ecadSAnthony Wilson 
5658fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5668fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
5676c3e9451SGeorge Liu         dbus::utility::getAssociationEndPoints(
5686c3e9451SGeorge Liu             sensorPath,
5697f1cc26dSEd Tanous             [asyncResp, chassisSubNode, sensorTypes,
570f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
5718b24275dSEd Tanous                 const boost::system::error_code& ec2,
5726c3e9451SGeorge Liu                 const dbus::utility::MapperEndPoints& nodeSensorList) {
5738b24275dSEd Tanous             if (ec2)
57449c53ac9SJohnathan Mantey             {
5758b24275dSEd Tanous                 if (ec2.value() != EBADR)
57649c53ac9SJohnathan Mantey                 {
5777f1cc26dSEd Tanous                     messages::internalError(asyncResp->res);
57849c53ac9SJohnathan Mantey                     return;
57949c53ac9SJohnathan Mantey                 }
58049c53ac9SJohnathan Mantey             }
581fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>> culledSensorList =
582fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
5837f1cc26dSEd Tanous             reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
5847f1cc26dSEd Tanous                              &nodeSensorList, culledSensorList);
58562598e31SEd Tanous             BMCWEB_LOG_DEBUG("Finishing with {}", culledSensorList->size());
58649c53ac9SJohnathan Mantey             callback(culledSensorList);
5871e1e598dSJonathan Doman         });
5887a1dbc48SGeorge Liu     });
58962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis exit");
59008777fb0SLewanczyk, Dawid }
59108777fb0SLewanczyk, Dawid 
59208777fb0SLewanczyk, Dawid /**
593adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
594adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
595aaf08ac7SMatt Simmering  * @param sensorAvailable Boolean representing if D-Bus sensor is marked as
596aaf08ac7SMatt Simmering  * available.
597adc4f0dbSShawn McCarney  * @return State value for inventory item.
59834dd179eSJames Feist  */
599aaf08ac7SMatt Simmering inline resource::State getState(const InventoryItem* inventoryItem,
600aaf08ac7SMatt Simmering                                 const bool sensorAvailable)
601adc4f0dbSShawn McCarney {
602adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
603adc4f0dbSShawn McCarney     {
604aaf08ac7SMatt Simmering         return resource::State::Absent;
605adc4f0dbSShawn McCarney     }
60634dd179eSJames Feist 
607aaf08ac7SMatt Simmering     if (!sensorAvailable)
608aaf08ac7SMatt Simmering     {
609aaf08ac7SMatt Simmering         return resource::State::UnavailableOffline;
610aaf08ac7SMatt Simmering     }
611aaf08ac7SMatt Simmering 
612aaf08ac7SMatt Simmering     return resource::State::Enabled;
613adc4f0dbSShawn McCarney }
614adc4f0dbSShawn McCarney 
615adc4f0dbSShawn McCarney /**
616adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
617adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
6181d7c0054SEd Tanous  * @param valuesDict Map of all sensor DBus values.
619adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
620adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
621adc4f0dbSShawn McCarney  * @return Health value for sensor.
622adc4f0dbSShawn McCarney  */
6231d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson,
6241d7c0054SEd Tanous                              const dbus::utility::DBusPropertiesMap& valuesDict,
625adc4f0dbSShawn McCarney                              const InventoryItem* inventoryItem)
62634dd179eSJames Feist {
627adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
628adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
629adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
630adc4f0dbSShawn McCarney     std::string currentHealth;
631adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
632adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
633adc4f0dbSShawn McCarney     {
634adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
635adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
636adc4f0dbSShawn McCarney         {
637adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
638adc4f0dbSShawn McCarney             if (health != nullptr)
639adc4f0dbSShawn McCarney             {
640adc4f0dbSShawn McCarney                 currentHealth = *health;
641adc4f0dbSShawn McCarney             }
642adc4f0dbSShawn McCarney         }
643adc4f0dbSShawn McCarney     }
644adc4f0dbSShawn McCarney 
645adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
646adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
647adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
648adc4f0dbSShawn McCarney     {
649adc4f0dbSShawn McCarney         return "Critical";
650adc4f0dbSShawn McCarney     }
651adc4f0dbSShawn McCarney 
652c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmHigh = nullptr;
653c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmLow = nullptr;
654c1343bf6SKrzysztof Grobelny     const bool* warningAlarmHigh = nullptr;
655c1343bf6SKrzysztof Grobelny     const bool* warningAlarmLow = nullptr;
656711ac7a9SEd Tanous 
657c1343bf6SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
658c1343bf6SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
659c1343bf6SKrzysztof Grobelny         criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
660c1343bf6SKrzysztof Grobelny         "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
661c1343bf6SKrzysztof Grobelny         warningAlarmLow);
662c1343bf6SKrzysztof Grobelny 
663c1343bf6SKrzysztof Grobelny     if (success)
66434dd179eSJames Feist     {
665c1343bf6SKrzysztof Grobelny         // Check if sensor has critical threshold alarm
666c1343bf6SKrzysztof Grobelny         if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
667c1343bf6SKrzysztof Grobelny             (criticalAlarmLow != nullptr && *criticalAlarmLow))
66834dd179eSJames Feist         {
66934dd179eSJames Feist             return "Critical";
67034dd179eSJames Feist         }
67134dd179eSJames Feist     }
67234dd179eSJames Feist 
673adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
674adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
675adc4f0dbSShawn McCarney     {
676adc4f0dbSShawn McCarney         return "Critical";
677adc4f0dbSShawn McCarney     }
678adc4f0dbSShawn McCarney 
679adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that. This
680adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
681adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
682adc4f0dbSShawn McCarney     {
683adc4f0dbSShawn McCarney         return "Warning";
684adc4f0dbSShawn McCarney     }
685adc4f0dbSShawn McCarney 
686c1343bf6SKrzysztof Grobelny     if (success)
687c1343bf6SKrzysztof Grobelny     {
688adc4f0dbSShawn McCarney         // Check if sensor has warning threshold alarm
689c1343bf6SKrzysztof Grobelny         if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
690c1343bf6SKrzysztof Grobelny             (warningAlarmLow != nullptr && *warningAlarmLow))
69134dd179eSJames Feist         {
692ebe4d91eSEd Tanous             return "Warning";
69334dd179eSJames Feist         }
69434dd179eSJames Feist     }
695adc4f0dbSShawn McCarney 
69634dd179eSJames Feist     return "OK";
69734dd179eSJames Feist }
69834dd179eSJames Feist 
69923a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
700d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
701d500549bSAnthony Wilson {
702d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
703d500549bSAnthony Wilson     {
704d500549bSAnthony Wilson         switch (inventoryItem->ledState)
705d500549bSAnthony Wilson         {
706d500549bSAnthony Wilson             case LedState::OFF:
707d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
708d500549bSAnthony Wilson                 break;
709d500549bSAnthony Wilson             case LedState::ON:
710d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
711d500549bSAnthony Wilson                 break;
712d500549bSAnthony Wilson             case LedState::BLINK:
713d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
714d500549bSAnthony Wilson                 break;
71523a21a1cSEd Tanous             case LedState::UNKNOWN:
716d500549bSAnthony Wilson                 break;
717d500549bSAnthony Wilson         }
718d500549bSAnthony Wilson     }
719d500549bSAnthony Wilson }
720d500549bSAnthony Wilson 
72134dd179eSJames Feist /**
72208777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
72308777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
724274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
72508777fb0SLewanczyk, Dawid  * build
7268ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
7271d7c0054SEd Tanous  * @param propertiesDict A dictionary of the properties to build the sensor
7281d7c0054SEd Tanous  * from.
7291d7c0054SEd Tanous  * @param sensorJson  The json object to fill
730adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
731adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
73208777fb0SLewanczyk, Dawid  */
7331d7c0054SEd Tanous inline void objectPropertiesToJson(
7341d7c0054SEd Tanous     std::string_view sensorName, std::string_view sensorType,
7351d7c0054SEd Tanous     std::string_view chassisSubNode,
7361d7c0054SEd Tanous     const dbus::utility::DBusPropertiesMap& propertiesDict,
73781ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
7381abe55efSEd Tanous {
7391d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
740adc4f0dbSShawn McCarney     {
741c71d6125SEd Tanous         std::string subNodeEscaped(sensorType);
7423544d2a7SEd Tanous         auto remove = std::ranges::remove(subNodeEscaped, '_');
7433544d2a7SEd Tanous         subNodeEscaped.erase(std::ranges::begin(remove), subNodeEscaped.end());
744c1d019a6SEd Tanous 
745c1d019a6SEd Tanous         // For sensors in SensorCollection we set Id instead of MemberId,
746c1d019a6SEd Tanous         // including power sensors.
747c1d019a6SEd Tanous         subNodeEscaped += '_';
748c1d019a6SEd Tanous         subNodeEscaped += sensorName;
749c1d019a6SEd Tanous         sensorJson["Id"] = std::move(subNodeEscaped);
750c1d019a6SEd Tanous 
7511d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7521d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7531d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
75495a3ecadSAnthony Wilson     }
75595a3ecadSAnthony Wilson     else if (sensorType != "power")
75695a3ecadSAnthony Wilson     {
75795a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
75895a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
75995a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
7601d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7611d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7621d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
763adc4f0dbSShawn McCarney     }
764e742b6ccSEd Tanous 
765aaf08ac7SMatt Simmering     const bool* checkAvailable = nullptr;
766aaf08ac7SMatt Simmering     bool available = true;
767aaf08ac7SMatt Simmering     const bool success = sdbusplus::unpackPropertiesNoThrow(
768aaf08ac7SMatt Simmering         dbus_utils::UnpackErrorPrinter(), propertiesDict, "Available",
769aaf08ac7SMatt Simmering         checkAvailable);
770aaf08ac7SMatt Simmering     if (!success)
771aaf08ac7SMatt Simmering     {
772aaf08ac7SMatt Simmering         messages::internalError();
773aaf08ac7SMatt Simmering     }
774aaf08ac7SMatt Simmering     if (checkAvailable != nullptr)
775aaf08ac7SMatt Simmering     {
776aaf08ac7SMatt Simmering         available = *checkAvailable;
777aaf08ac7SMatt Simmering     }
778aaf08ac7SMatt Simmering 
779aaf08ac7SMatt Simmering     sensorJson["Status"]["State"] = getState(inventoryItem, available);
78089492a15SPatrick Williams     sensorJson["Status"]["Health"] = getHealth(sensorJson, propertiesDict,
78189492a15SPatrick Williams                                                inventoryItem);
78208777fb0SLewanczyk, Dawid 
78308777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
78408777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
78508777fb0SLewanczyk, Dawid     // that require integers, not floats.
78608777fb0SLewanczyk, Dawid     bool forceToInt = false;
78708777fb0SLewanczyk, Dawid 
7883929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
7891d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
79095a3ecadSAnthony Wilson     {
7912a4ba195SShounak Mitra         sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
792c2bf7f99SWludzik, Jozef 
7930ec8b83dSEd Tanous         sensor::ReadingType readingType = sensors::toReadingType(sensorType);
7940ec8b83dSEd Tanous         if (readingType == sensor::ReadingType::Invalid)
79595a3ecadSAnthony Wilson         {
79662598e31SEd Tanous             BMCWEB_LOG_ERROR("Redfish cannot map reading type for {}",
79762598e31SEd Tanous                              sensorType);
79895a3ecadSAnthony Wilson         }
799c2bf7f99SWludzik, Jozef         else
80095a3ecadSAnthony Wilson         {
801c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
80295a3ecadSAnthony Wilson         }
803c2bf7f99SWludzik, Jozef 
8041d7c0054SEd Tanous         std::string_view readingUnits = sensors::toReadingUnits(sensorType);
805c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
806f8ede15eSAdrian Ambrożewicz         {
80762598e31SEd Tanous             BMCWEB_LOG_ERROR("Redfish cannot map reading unit for {}",
80862598e31SEd Tanous                              sensorType);
809c2bf7f99SWludzik, Jozef         }
810c2bf7f99SWludzik, Jozef         else
811c2bf7f99SWludzik, Jozef         {
812c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
813f8ede15eSAdrian Ambrożewicz         }
81495a3ecadSAnthony Wilson     }
81595a3ecadSAnthony Wilson     else if (sensorType == "temperature")
8161abe55efSEd Tanous     {
8173929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
81881ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
81908777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
82008777fb0SLewanczyk, Dawid         // implementation seems to implement fan
8211abe55efSEd Tanous     }
8221abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
8231abe55efSEd Tanous     {
8243929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
82581ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
82681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
82781ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
82808777fb0SLewanczyk, Dawid         forceToInt = true;
8291abe55efSEd Tanous     }
8306f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
8316f6d0d32SEd Tanous     {
8323929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
83381ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
83481ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
83581ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
8366f6d0d32SEd Tanous         forceToInt = true;
8376f6d0d32SEd Tanous     }
8381abe55efSEd Tanous     else if (sensorType == "voltage")
8391abe55efSEd Tanous     {
8403929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
84181ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
8421abe55efSEd Tanous     }
8432474adfaSEd Tanous     else if (sensorType == "power")
8442474adfaSEd Tanous     {
84518f8f608SEd Tanous         std::string lower;
84618f8f608SEd Tanous         std::ranges::transform(sensorName, std::back_inserter(lower),
84718f8f608SEd Tanous                                bmcweb::asciiToLower);
84818f8f608SEd Tanous         if (lower == "total_power")
849028f7ebcSEddie James         {
85081ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
8517ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
8527ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
85381ce609eSEd Tanous             sensorJson["MemberId"] = "0";
85481ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
8553929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
856028f7ebcSEddie James         }
85718f8f608SEd Tanous         else if (lower.find("input") != std::string::npos)
85849c53ac9SJohnathan Mantey         {
8593929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
86049c53ac9SJohnathan Mantey         }
86149c53ac9SJohnathan Mantey         else
86249c53ac9SJohnathan Mantey         {
8633929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
86449c53ac9SJohnathan Mantey         }
8652474adfaSEd Tanous     }
8661abe55efSEd Tanous     else
8671abe55efSEd Tanous     {
86862598e31SEd Tanous         BMCWEB_LOG_ERROR("Redfish cannot map object type for {}", sensorName);
86908777fb0SLewanczyk, Dawid         return;
87008777fb0SLewanczyk, Dawid     }
87108777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
8723929aca1SAnthony Wilson     std::vector<
8733929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
8743929aca1SAnthony Wilson         properties;
87508777fb0SLewanczyk, Dawid     properties.reserve(7);
87608777fb0SLewanczyk, Dawid 
87708777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
878de629b6eSShawn McCarney 
8791d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
8803929aca1SAnthony Wilson     {
8813929aca1SAnthony Wilson         properties.emplace_back(
8823929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
8833929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
8843929aca1SAnthony Wilson         properties.emplace_back(
8853929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
8863929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
8873929aca1SAnthony Wilson         properties.emplace_back(
8883929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
8893929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
8903929aca1SAnthony Wilson         properties.emplace_back(
8913929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
8923929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
8933929aca1SAnthony Wilson     }
8943929aca1SAnthony Wilson     else if (sensorType != "power")
895de629b6eSShawn McCarney     {
89608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8973929aca1SAnthony Wilson                                 "WarningHigh",
8983929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
89908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
9003929aca1SAnthony Wilson                                 "WarningLow",
9013929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
90208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
9033929aca1SAnthony Wilson                                 "CriticalHigh",
9043929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
90508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
9063929aca1SAnthony Wilson                                 "CriticalLow",
9073929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
908de629b6eSShawn McCarney     }
90908777fb0SLewanczyk, Dawid 
9102474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
9112474adfaSEd Tanous 
9121d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
91395a3ecadSAnthony Wilson     {
91495a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9153929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
91695a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9173929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
91851c35a8fSGeorge Liu         properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
91951c35a8fSGeorge Liu                                 "Accuracy", "/Accuracy"_json_pointer);
92095a3ecadSAnthony Wilson     }
92195a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9221abe55efSEd Tanous     {
92308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9243929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
92508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9263929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
9271abe55efSEd Tanous     }
928adc4f0dbSShawn McCarney     else if (sensorType != "power")
9291abe55efSEd Tanous     {
93008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9313929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
93208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9333929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
93408777fb0SLewanczyk, Dawid     }
93508777fb0SLewanczyk, Dawid 
9363929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
9373929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
9381abe55efSEd Tanous     {
9391d7c0054SEd Tanous         for (const auto& [valueName, valueVariant] : propertiesDict)
940711ac7a9SEd Tanous         {
941711ac7a9SEd Tanous             if (valueName != std::get<1>(p))
942711ac7a9SEd Tanous             {
943711ac7a9SEd Tanous                 continue;
944711ac7a9SEd Tanous             }
9453929aca1SAnthony Wilson 
9463929aca1SAnthony Wilson             // The property we want to set may be nested json, so use
9473929aca1SAnthony Wilson             // a json_pointer for easy indexing into the json structure.
9483929aca1SAnthony Wilson             const nlohmann::json::json_pointer& key = std::get<2>(p);
9493929aca1SAnthony Wilson 
950abf2add6SEd Tanous             const double* doubleValue = std::get_if<double>(&valueVariant);
95140e4f380SEd Tanous             if (doubleValue == nullptr)
9521abe55efSEd Tanous             {
95362598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got value interface that wasn't double");
9546f6d0d32SEd Tanous                 continue;
95508777fb0SLewanczyk, Dawid             }
956283860f5SEd Tanous             if (!std::isfinite(*doubleValue))
957283860f5SEd Tanous             {
958283860f5SEd Tanous                 if (valueName == "Value")
959283860f5SEd Tanous                 {
960283860f5SEd Tanous                     // Readings are allowed to be NAN for unavailable;  coerce
961283860f5SEd Tanous                     // them to null in the json response.
962283860f5SEd Tanous                     sensorJson[key] = nullptr;
963283860f5SEd Tanous                     continue;
964283860f5SEd Tanous                 }
96562598e31SEd Tanous                 BMCWEB_LOG_WARNING("Sensor value for {} was unexpectedly {}",
96662598e31SEd Tanous                                    valueName, *doubleValue);
967283860f5SEd Tanous                 continue;
968283860f5SEd Tanous             }
9696f6d0d32SEd Tanous             if (forceToInt)
9706f6d0d32SEd Tanous             {
97140e4f380SEd Tanous                 sensorJson[key] = static_cast<int64_t>(*doubleValue);
9726f6d0d32SEd Tanous             }
9736f6d0d32SEd Tanous             else
9746f6d0d32SEd Tanous             {
97540e4f380SEd Tanous                 sensorJson[key] = *doubleValue;
97608777fb0SLewanczyk, Dawid             }
97708777fb0SLewanczyk, Dawid         }
97808777fb0SLewanczyk, Dawid     }
97908777fb0SLewanczyk, Dawid }
98008777fb0SLewanczyk, Dawid 
9811d7c0054SEd Tanous /**
9821d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
9831d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
9841d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
9851d7c0054SEd Tanous  * build
9868ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
9871d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
9881d7c0054SEd Tanous  * interfaces to be built from
9891d7c0054SEd Tanous  * @param sensorJson  The json object to fill
9901d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
9911d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
9921d7c0054SEd Tanous  */
9931d7c0054SEd Tanous inline void objectInterfacesToJson(
9941d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
9951d7c0054SEd Tanous     const std::string& chassisSubNode,
99680f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict,
9971d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
9981d7c0054SEd Tanous {
9991d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
10001d7c0054SEd Tanous     {
10011d7c0054SEd Tanous         objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
10021d7c0054SEd Tanous                                valuesDict, sensorJson, inventoryItem);
10031d7c0054SEd Tanous     }
100462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
10051d7c0054SEd Tanous }
10061d7c0054SEd Tanous 
1007b5a76932SEd Tanous inline void populateFanRedundancy(
1008b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
10098bd25ccdSJames Feist {
1010e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1011e99073f5SGeorge Liu         "xyz.openbmc_project.Control.FanRedundancy"};
1012e99073f5SGeorge Liu     dbus::utility::getSubTree(
1013e99073f5SGeorge Liu         "/xyz/openbmc_project/control", 2, interfaces,
1014b9d36b47SEd Tanous         [sensorsAsyncResp](
1015e99073f5SGeorge Liu             const boost::system::error_code& ec,
1016b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
10178bd25ccdSJames Feist         if (ec)
10188bd25ccdSJames Feist         {
10198bd25ccdSJames Feist             return; // don't have to have this interface
10208bd25ccdSJames Feist         }
10216c3e9451SGeorge Liu         for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
1022e278c18fSEd Tanous                  pathPair : resp)
10238bd25ccdSJames Feist         {
1024e278c18fSEd Tanous             const std::string& path = pathPair.first;
10256c3e9451SGeorge Liu             const dbus::utility::MapperServiceMap& objDict = pathPair.second;
10268bd25ccdSJames Feist             if (objDict.empty())
10278bd25ccdSJames Feist             {
10288bd25ccdSJames Feist                 continue; // this should be impossible
10298bd25ccdSJames Feist             }
10308bd25ccdSJames Feist 
10318bd25ccdSJames Feist             const std::string& owner = objDict.begin()->first;
10326c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
10336c3e9451SGeorge Liu                 path + "/chassis",
10346c3e9451SGeorge Liu                 [path, owner, sensorsAsyncResp](
10358b24275dSEd Tanous                     const boost::system::error_code& ec2,
10366c3e9451SGeorge Liu                     const dbus::utility::MapperEndPoints& endpoints) {
10378b24275dSEd Tanous                 if (ec2)
10388bd25ccdSJames Feist                 {
10398bd25ccdSJames Feist                     return; // if they don't have an association we
10408bd25ccdSJames Feist                             // can't tell what chassis is
10418bd25ccdSJames Feist                 }
10423544d2a7SEd Tanous                 auto found = std::ranges::find_if(
10433544d2a7SEd Tanous                     endpoints, [sensorsAsyncResp](const std::string& entry) {
1044002d39b4SEd Tanous                     return entry.find(sensorsAsyncResp->chassisId) !=
10458bd25ccdSJames Feist                            std::string::npos;
10468bd25ccdSJames Feist                 });
10478bd25ccdSJames Feist 
10481e1e598dSJonathan Doman                 if (found == endpoints.end())
10498bd25ccdSJames Feist                 {
10508bd25ccdSJames Feist                     return;
10518bd25ccdSJames Feist                 }
105286d89ed7SKrzysztof Grobelny                 sdbusplus::asio::getAllProperties(
105386d89ed7SKrzysztof Grobelny                     *crow::connections::systemBus, owner, path,
105486d89ed7SKrzysztof Grobelny                     "xyz.openbmc_project.Control.FanRedundancy",
10558bd25ccdSJames Feist                     [path, sensorsAsyncResp](
10568b24275dSEd Tanous                         const boost::system::error_code& ec3,
105786d89ed7SKrzysztof Grobelny                         const dbus::utility::DBusPropertiesMap& ret) {
10588b24275dSEd Tanous                     if (ec3)
10598bd25ccdSJames Feist                     {
10608bd25ccdSJames Feist                         return; // don't have to have this
10618bd25ccdSJames Feist                                 // interface
10628bd25ccdSJames Feist                     }
10638bd25ccdSJames Feist 
106486d89ed7SKrzysztof Grobelny                     const uint8_t* allowedFailures = nullptr;
106586d89ed7SKrzysztof Grobelny                     const std::vector<std::string>* collection = nullptr;
106686d89ed7SKrzysztof Grobelny                     const std::string* status = nullptr;
106786d89ed7SKrzysztof Grobelny 
106886d89ed7SKrzysztof Grobelny                     const bool success = sdbusplus::unpackPropertiesNoThrow(
106986d89ed7SKrzysztof Grobelny                         dbus_utils::UnpackErrorPrinter(), ret,
107086d89ed7SKrzysztof Grobelny                         "AllowedFailures", allowedFailures, "Collection",
107186d89ed7SKrzysztof Grobelny                         collection, "Status", status);
107286d89ed7SKrzysztof Grobelny 
107386d89ed7SKrzysztof Grobelny                     if (!success)
107486d89ed7SKrzysztof Grobelny                     {
107586d89ed7SKrzysztof Grobelny                         messages::internalError(
107686d89ed7SKrzysztof Grobelny                             sensorsAsyncResp->asyncResp->res);
107786d89ed7SKrzysztof Grobelny                         return;
107886d89ed7SKrzysztof Grobelny                     }
107986d89ed7SKrzysztof Grobelny 
108086d89ed7SKrzysztof Grobelny                     if (allowedFailures == nullptr || collection == nullptr ||
108186d89ed7SKrzysztof Grobelny                         status == nullptr)
10828bd25ccdSJames Feist                     {
108362598e31SEd Tanous                         BMCWEB_LOG_ERROR("Invalid redundancy interface");
10848bd25ccdSJames Feist                         messages::internalError(
10858d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
10868bd25ccdSJames Feist                         return;
10878bd25ccdSJames Feist                     }
10888bd25ccdSJames Feist 
1089002d39b4SEd Tanous                     sdbusplus::message::object_path objectPath(path);
109028aa8de5SGeorge Liu                     std::string name = objectPath.filename();
109128aa8de5SGeorge Liu                     if (name.empty())
10928bd25ccdSJames Feist                     {
10938bd25ccdSJames Feist                         // this should be impossible
10948bd25ccdSJames Feist                         messages::internalError(
10958d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
10968bd25ccdSJames Feist                         return;
10978bd25ccdSJames Feist                     }
109818f8f608SEd Tanous                     std::ranges::replace(name, '_', ' ');
10998bd25ccdSJames Feist 
11008bd25ccdSJames Feist                     std::string health;
11018bd25ccdSJames Feist 
110211ba3979SEd Tanous                     if (status->ends_with("Full"))
11038bd25ccdSJames Feist                     {
11048bd25ccdSJames Feist                         health = "OK";
11058bd25ccdSJames Feist                     }
110611ba3979SEd Tanous                     else if (status->ends_with("Degraded"))
11078bd25ccdSJames Feist                     {
11088bd25ccdSJames Feist                         health = "Warning";
11098bd25ccdSJames Feist                     }
11108bd25ccdSJames Feist                     else
11118bd25ccdSJames Feist                     {
11128bd25ccdSJames Feist                         health = "Critical";
11138bd25ccdSJames Feist                     }
11141476687dSEd Tanous                     nlohmann::json::array_t redfishCollection;
11158bd25ccdSJames Feist                     const auto& fanRedfish =
1116002d39b4SEd Tanous                         sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
11178bd25ccdSJames Feist                     for (const std::string& item : *collection)
11188bd25ccdSJames Feist                     {
11198a592810SEd Tanous                         sdbusplus::message::object_path itemPath(item);
11208a592810SEd Tanous                         std::string itemName = itemPath.filename();
112128aa8de5SGeorge Liu                         if (itemName.empty())
112228aa8de5SGeorge Liu                         {
112328aa8de5SGeorge Liu                             continue;
112428aa8de5SGeorge Liu                         }
11258bd25ccdSJames Feist                         /*
11268bd25ccdSJames Feist                         todo(ed): merge patch that fixes the names
11278bd25ccdSJames Feist                         std::replace(itemName.begin(),
11288bd25ccdSJames Feist                                      itemName.end(), '_', ' ');*/
11293544d2a7SEd Tanous                         auto schemaItem = std::ranges::find_if(
11303544d2a7SEd Tanous                             fanRedfish, [itemName](const nlohmann::json& fan) {
11313e35c761SGeorge Liu                             return fan["Name"] == itemName;
11328bd25ccdSJames Feist                         });
11338bd25ccdSJames Feist                         if (schemaItem != fanRedfish.end())
11348bd25ccdSJames Feist                         {
11358a592810SEd Tanous                             nlohmann::json::object_t collectionId;
11368a592810SEd Tanous                             collectionId["@odata.id"] =
11371476687dSEd Tanous                                 (*schemaItem)["@odata.id"];
11381476687dSEd Tanous                             redfishCollection.emplace_back(
11398a592810SEd Tanous                                 std::move(collectionId));
11408bd25ccdSJames Feist                         }
11418bd25ccdSJames Feist                         else
11428bd25ccdSJames Feist                         {
114362598e31SEd Tanous                             BMCWEB_LOG_ERROR("failed to find fan in schema");
11448bd25ccdSJames Feist                             messages::internalError(
11458d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
11468bd25ccdSJames Feist                             return;
11478bd25ccdSJames Feist                         }
11488bd25ccdSJames Feist                     }
11498bd25ccdSJames Feist 
115089492a15SPatrick Williams                     size_t minNumNeeded = collection->empty()
115126f6976fSEd Tanous                                               ? 0
115289492a15SPatrick Williams                                               : collection->size() -
115389492a15SPatrick Williams                                                     *allowedFailures;
1154002d39b4SEd Tanous                     nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
11558bd25ccdSJames Feist                                                 .jsonValue["Redundancy"];
11561476687dSEd Tanous 
11571476687dSEd Tanous                     nlohmann::json::object_t redundancy;
1158ef4c65b7SEd Tanous                     boost::urls::url url =
1159ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}/{}",
1160ef4c65b7SEd Tanous                                             sensorsAsyncResp->chassisId,
1161eddfc437SWilly Tu                                             sensorsAsyncResp->chassisSubNode);
1162eddfc437SWilly Tu                     url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1163eddfc437SWilly Tu                                          .to_string());
1164eddfc437SWilly Tu                     redundancy["@odata.id"] = std::move(url);
1165002d39b4SEd Tanous                     redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
11661476687dSEd Tanous                     redundancy["MinNumNeeded"] = minNumNeeded;
11671476687dSEd Tanous                     redundancy["Mode"] = "N+m";
11681476687dSEd Tanous                     redundancy["Name"] = name;
11691476687dSEd Tanous                     redundancy["RedundancySet"] = redfishCollection;
11701476687dSEd Tanous                     redundancy["Status"]["Health"] = health;
11711476687dSEd Tanous                     redundancy["Status"]["State"] = "Enabled";
11721476687dSEd Tanous 
1173b2ba3072SPatrick Williams                     jResp.emplace_back(std::move(redundancy));
117486d89ed7SKrzysztof Grobelny                 });
11751e1e598dSJonathan Doman             });
11768bd25ccdSJames Feist         }
1177e99073f5SGeorge Liu     });
11788bd25ccdSJames Feist }
11798bd25ccdSJames Feist 
1180b5a76932SEd Tanous inline void
118181ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
118249c53ac9SJohnathan Mantey {
11838d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
118449c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
118581ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
118649c53ac9SJohnathan Mantey     {
118749c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
118849c53ac9SJohnathan Mantey     }
118949c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
119049c53ac9SJohnathan Mantey     {
119149c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
119249c53ac9SJohnathan Mantey         if (entry != response.end())
119349c53ac9SJohnathan Mantey         {
119449c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
119502cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
119649c53ac9SJohnathan Mantey                 return c1["Name"] < c2["Name"];
119749c53ac9SJohnathan Mantey             });
119849c53ac9SJohnathan Mantey 
119949c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
120049c53ac9SJohnathan Mantey             size_t count = 0;
120149c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
120249c53ac9SJohnathan Mantey             {
120349c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
120449c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
120549c53ac9SJohnathan Mantey                 {
120649c53ac9SJohnathan Mantey                     continue;
120749c53ac9SJohnathan Mantey                 }
120849c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
120949c53ac9SJohnathan Mantey                 if (value != nullptr)
121049c53ac9SJohnathan Mantey                 {
1211eddfc437SWilly Tu                     *value += "/" + std::to_string(count);
12123e35c761SGeorge Liu                     sensorJson["MemberId"] = std::to_string(count);
121349c53ac9SJohnathan Mantey                     count++;
121481ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
121549c53ac9SJohnathan Mantey                 }
121649c53ac9SJohnathan Mantey             }
121749c53ac9SJohnathan Mantey         }
121849c53ac9SJohnathan Mantey     }
121949c53ac9SJohnathan Mantey }
122049c53ac9SJohnathan Mantey 
122108777fb0SLewanczyk, Dawid /**
1222adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1223adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1224adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1225adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12268fb49dd6SShawn McCarney  */
122723a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1228b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1229adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
12308fb49dd6SShawn McCarney {
1231adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
12328fb49dd6SShawn McCarney     {
1233adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
12348fb49dd6SShawn McCarney         {
1235adc4f0dbSShawn McCarney             return &inventoryItem;
12368fb49dd6SShawn McCarney         }
12378fb49dd6SShawn McCarney     }
12388fb49dd6SShawn McCarney     return nullptr;
12398fb49dd6SShawn McCarney }
12408fb49dd6SShawn McCarney 
12418fb49dd6SShawn McCarney /**
1242adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1243adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1244adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1245adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12468fb49dd6SShawn McCarney  */
124723a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1248b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1249adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1250adc4f0dbSShawn McCarney {
1251adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1252adc4f0dbSShawn McCarney     {
1253db0d36efSEd Tanous         if (inventoryItem.sensors.contains(sensorObjPath))
1254adc4f0dbSShawn McCarney         {
1255adc4f0dbSShawn McCarney             return &inventoryItem;
1256adc4f0dbSShawn McCarney         }
1257adc4f0dbSShawn McCarney     }
1258adc4f0dbSShawn McCarney     return nullptr;
1259adc4f0dbSShawn McCarney }
1260adc4f0dbSShawn McCarney 
1261adc4f0dbSShawn McCarney /**
1262d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1263d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1264d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1265d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1266d500549bSAnthony Wilson  */
1267d500549bSAnthony Wilson inline InventoryItem*
1268d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1269d500549bSAnthony Wilson                             const std::string& ledObjPath)
1270d500549bSAnthony Wilson {
1271d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1272d500549bSAnthony Wilson     {
1273d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1274d500549bSAnthony Wilson         {
1275d500549bSAnthony Wilson             return &inventoryItem;
1276d500549bSAnthony Wilson         }
1277d500549bSAnthony Wilson     }
1278d500549bSAnthony Wilson     return nullptr;
1279d500549bSAnthony Wilson }
1280d500549bSAnthony Wilson 
1281d500549bSAnthony Wilson /**
1282adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1283adc4f0dbSShawn McCarney  *
1284adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1285adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1286adc4f0dbSShawn McCarney  * added to the vector.
1287adc4f0dbSShawn McCarney  *
1288adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1289adc4f0dbSShawn McCarney  * InventoryItem.
1290adc4f0dbSShawn McCarney  *
1291adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1292adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1293adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1294adc4f0dbSShawn McCarney  */
1295b5a76932SEd Tanous inline void addInventoryItem(
1296b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1297b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1298adc4f0dbSShawn McCarney {
1299adc4f0dbSShawn McCarney     // Look for inventory item in vector
130089492a15SPatrick Williams     InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
130189492a15SPatrick Williams                                                      invItemObjPath);
1302adc4f0dbSShawn McCarney 
1303adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1304adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1305adc4f0dbSShawn McCarney     {
1306adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1307adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1308adc4f0dbSShawn McCarney     }
1309adc4f0dbSShawn McCarney 
1310adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1311adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1312adc4f0dbSShawn McCarney }
1313adc4f0dbSShawn McCarney 
1314adc4f0dbSShawn McCarney /**
1315adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1316adc4f0dbSShawn McCarney  *
1317adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1318adc4f0dbSShawn McCarney  * specified InventoryItem.
1319adc4f0dbSShawn McCarney  *
1320adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1321adc4f0dbSShawn McCarney  * response.
1322adc4f0dbSShawn McCarney  *
1323adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1324adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1325adc4f0dbSShawn McCarney  * for the specified inventory item.
1326adc4f0dbSShawn McCarney  */
132723a21a1cSEd Tanous inline void storeInventoryItemData(
1328adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
132980f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict)
13308fb49dd6SShawn McCarney {
1331adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1332711ac7a9SEd Tanous 
13339eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
13348fb49dd6SShawn McCarney     {
1335711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
13368fb49dd6SShawn McCarney         {
13379eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1338711ac7a9SEd Tanous             {
1339711ac7a9SEd Tanous                 if (name == "Present")
1340711ac7a9SEd Tanous                 {
1341711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1342adc4f0dbSShawn McCarney                     if (value != nullptr)
13438fb49dd6SShawn McCarney                     {
1344adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
13458fb49dd6SShawn McCarney                     }
13468fb49dd6SShawn McCarney                 }
13478fb49dd6SShawn McCarney             }
1348711ac7a9SEd Tanous         }
1349adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1350711ac7a9SEd Tanous 
1351711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
13528fb49dd6SShawn McCarney         {
1353adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
13548fb49dd6SShawn McCarney         }
1355adc4f0dbSShawn McCarney 
1356adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1357711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1358adc4f0dbSShawn McCarney         {
13599eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1360711ac7a9SEd Tanous             {
1361711ac7a9SEd Tanous                 if (name == "Manufacturer")
1362adc4f0dbSShawn McCarney                 {
1363adc4f0dbSShawn McCarney                     const std::string* value =
1364711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1365adc4f0dbSShawn McCarney                     if (value != nullptr)
1366adc4f0dbSShawn McCarney                     {
1367adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1368adc4f0dbSShawn McCarney                     }
1369adc4f0dbSShawn McCarney                 }
1370711ac7a9SEd Tanous                 if (name == "Model")
1371adc4f0dbSShawn McCarney                 {
1372adc4f0dbSShawn McCarney                     const std::string* value =
1373711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1374adc4f0dbSShawn McCarney                     if (value != nullptr)
1375adc4f0dbSShawn McCarney                     {
1376adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1377adc4f0dbSShawn McCarney                     }
1378adc4f0dbSShawn McCarney                 }
1379711ac7a9SEd Tanous                 if (name == "SerialNumber")
1380adc4f0dbSShawn McCarney                 {
1381adc4f0dbSShawn McCarney                     const std::string* value =
1382711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1383adc4f0dbSShawn McCarney                     if (value != nullptr)
1384adc4f0dbSShawn McCarney                     {
1385adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1386adc4f0dbSShawn McCarney                     }
1387adc4f0dbSShawn McCarney                 }
1388711ac7a9SEd Tanous                 if (name == "PartNumber")
1389711ac7a9SEd Tanous                 {
1390711ac7a9SEd Tanous                     const std::string* value =
1391711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1392711ac7a9SEd Tanous                     if (value != nullptr)
1393711ac7a9SEd Tanous                     {
1394711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1395711ac7a9SEd Tanous                     }
1396711ac7a9SEd Tanous                 }
1397711ac7a9SEd Tanous             }
1398adc4f0dbSShawn McCarney         }
1399adc4f0dbSShawn McCarney 
1400711ac7a9SEd Tanous         if (interface ==
1401711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1402adc4f0dbSShawn McCarney         {
14039eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1404adc4f0dbSShawn McCarney             {
1405711ac7a9SEd Tanous                 if (name == "Functional")
1406711ac7a9SEd Tanous                 {
1407711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1408adc4f0dbSShawn McCarney                     if (value != nullptr)
1409adc4f0dbSShawn McCarney                     {
1410adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
14118fb49dd6SShawn McCarney                     }
14128fb49dd6SShawn McCarney                 }
14138fb49dd6SShawn McCarney             }
14148fb49dd6SShawn McCarney         }
1415711ac7a9SEd Tanous     }
1416711ac7a9SEd Tanous }
14178fb49dd6SShawn McCarney 
14188fb49dd6SShawn McCarney /**
1419adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
14208fb49dd6SShawn McCarney  *
1421adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1422adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1423adc4f0dbSShawn McCarney  * inventoryItems vector.
14248fb49dd6SShawn McCarney  *
1425adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1426adc4f0dbSShawn McCarney  * response.
1427adc4f0dbSShawn McCarney  *
1428adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1429adc4f0dbSShawn McCarney  * been obtained.
1430adc4f0dbSShawn McCarney  *
1431adc4f0dbSShawn McCarney  * The callback must have the following signature:
1432adc4f0dbSShawn McCarney  *   @code
1433d500549bSAnthony Wilson  *   callback(void)
1434adc4f0dbSShawn McCarney  *   @endcode
1435adc4f0dbSShawn McCarney  *
1436adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1437adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1438adc4f0dbSShawn McCarney  * last asynchronous function has completed.
14398fb49dd6SShawn McCarney  *
14408fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1441adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1442adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
14438fb49dd6SShawn McCarney  * implements ObjectManager.
1444adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1445adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1446adc4f0dbSShawn McCarney  * in recursive calls to this function.
14478fb49dd6SShawn McCarney  */
1448adc4f0dbSShawn McCarney template <typename Callback>
1449adc4f0dbSShawn McCarney static void getInventoryItemsData(
14508fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1451adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1452d0090733SEd Tanous     std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1453d0090733SEd Tanous     size_t invConnectionsIndex = 0)
14548fb49dd6SShawn McCarney {
145562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
14568fb49dd6SShawn McCarney 
1457adc4f0dbSShawn McCarney     // If no more connections left, call callback
1458adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
14598fb49dd6SShawn McCarney     {
1460d500549bSAnthony Wilson         callback();
146162598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
1462adc4f0dbSShawn McCarney         return;
1463adc4f0dbSShawn McCarney     }
1464adc4f0dbSShawn McCarney 
1465adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1466fe04d49cSNan Zhou     auto it = invConnections->begin();
1467fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1468adc4f0dbSShawn McCarney     if (it != invConnections->end())
1469adc4f0dbSShawn McCarney     {
1470adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1471adc4f0dbSShawn McCarney 
14725eb468daSGeorge Liu         // Get all object paths and their interfaces for current connection
14735eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
14745eb468daSGeorge Liu         dbus::utility::getManagedObjects(
14755eb468daSGeorge Liu             invConnection, path,
14765eb468daSGeorge Liu             [sensorsAsyncResp, inventoryItems, invConnections,
14775eb468daSGeorge Liu              callback{std::forward<Callback>(callback)}, invConnectionsIndex](
14785e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
147902cad96eSEd Tanous                 const dbus::utility::ManagedObjectType& resp) {
148062598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
14818fb49dd6SShawn McCarney             if (ec)
14828fb49dd6SShawn McCarney             {
148362598e31SEd Tanous                 BMCWEB_LOG_ERROR(
148462598e31SEd Tanous                     "getInventoryItemsData respHandler DBus error {}", ec);
14858d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
14868fb49dd6SShawn McCarney                 return;
14878fb49dd6SShawn McCarney             }
14888fb49dd6SShawn McCarney 
14898fb49dd6SShawn McCarney             // Loop through returned object paths
14908fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
14918fb49dd6SShawn McCarney             {
14928fb49dd6SShawn McCarney                 const std::string& objPath =
14938fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
14948fb49dd6SShawn McCarney 
1495adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
149689492a15SPatrick Williams                 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
149789492a15SPatrick Williams                                                                  objPath);
1498adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
14998fb49dd6SShawn McCarney                 {
1500adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1501adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
15028fb49dd6SShawn McCarney                 }
15038fb49dd6SShawn McCarney             }
15048fb49dd6SShawn McCarney 
1505adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1506adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1507d0090733SEd Tanous                                   invConnections, std::move(callback),
1508d0090733SEd Tanous                                   invConnectionsIndex + 1);
1509adc4f0dbSShawn McCarney 
151062598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
15115eb468daSGeorge Liu         });
15128fb49dd6SShawn McCarney     }
15138fb49dd6SShawn McCarney 
151462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
15158fb49dd6SShawn McCarney }
15168fb49dd6SShawn McCarney 
15178fb49dd6SShawn McCarney /**
1518adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
15198fb49dd6SShawn McCarney  *
1520adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1521adc4f0dbSShawn McCarney  * items that are associated with sensors.
15228fb49dd6SShawn McCarney  *
15238fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
15248fb49dd6SShawn McCarney  * been obtained.
15258fb49dd6SShawn McCarney  *
15268fb49dd6SShawn McCarney  * The callback must have the following signature:
15278fb49dd6SShawn McCarney  *   @code
1528fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
15298fb49dd6SShawn McCarney  *   @endcode
15308fb49dd6SShawn McCarney  *
15318fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1532adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
15338fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
15348fb49dd6SShawn McCarney  */
15358fb49dd6SShawn McCarney template <typename Callback>
15368fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1537b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1538b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
15398fb49dd6SShawn McCarney     Callback&& callback)
15408fb49dd6SShawn McCarney {
154162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
15428fb49dd6SShawn McCarney 
15438fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1544e99073f5SGeorge Liu     constexpr std::array<std::string_view, 4> interfaces = {
15458fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1546adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1547adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
15488fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
15498fb49dd6SShawn McCarney 
1550e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1551e99073f5SGeorge Liu     dbus::utility::getSubTree(
1552e99073f5SGeorge Liu         path, 0, interfaces,
1553002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1554002d39b4SEd Tanous          inventoryItems](
1555e99073f5SGeorge Liu             const boost::system::error_code& ec,
1556002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1557e99073f5SGeorge Liu         // Response handler for parsing output from GetSubTree
155862598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
15598fb49dd6SShawn McCarney         if (ec)
15608fb49dd6SShawn McCarney         {
15618d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
156262598e31SEd Tanous             BMCWEB_LOG_ERROR(
156362598e31SEd Tanous                 "getInventoryItemsConnections respHandler DBus error {}", ec);
15648fb49dd6SShawn McCarney             return;
15658fb49dd6SShawn McCarney         }
15668fb49dd6SShawn McCarney 
15678fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
1568fe04d49cSNan Zhou         std::shared_ptr<std::set<std::string>> invConnections =
1569fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
15708fb49dd6SShawn McCarney 
15718fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
15728fb49dd6SShawn McCarney         for (const std::pair<
15738fb49dd6SShawn McCarney                  std::string,
15748fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
15758fb49dd6SShawn McCarney                  object : subtree)
15768fb49dd6SShawn McCarney         {
1577adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
15788fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1579adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
15808fb49dd6SShawn McCarney             {
15818fb49dd6SShawn McCarney                 // Store all connections to inventory item
15828fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
15838fb49dd6SShawn McCarney                          objData : object.second)
15848fb49dd6SShawn McCarney                 {
15858fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
15868fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
15878fb49dd6SShawn McCarney                 }
15888fb49dd6SShawn McCarney             }
15898fb49dd6SShawn McCarney         }
1590d500549bSAnthony Wilson 
15918fb49dd6SShawn McCarney         callback(invConnections);
159262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1593e99073f5SGeorge Liu     });
159462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
15958fb49dd6SShawn McCarney }
15968fb49dd6SShawn McCarney 
15978fb49dd6SShawn McCarney /**
1598adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
15998fb49dd6SShawn McCarney  *
16008fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1601d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1602d500549bSAnthony Wilson  * their LEDs, if any.
16038fb49dd6SShawn McCarney  *
16048fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
16058fb49dd6SShawn McCarney  * has been obtained.
16068fb49dd6SShawn McCarney  *
16078fb49dd6SShawn McCarney  * The callback must have the following signature:
16088fb49dd6SShawn McCarney  *   @code
1609adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
16108fb49dd6SShawn McCarney  *   @endcode
16118fb49dd6SShawn McCarney  *
16128fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
16138fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
16148fb49dd6SShawn McCarney  * implements ObjectManager.
16158fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
16168fb49dd6SShawn McCarney  */
16178fb49dd6SShawn McCarney template <typename Callback>
1618adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1619b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1620fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
16218fb49dd6SShawn McCarney     Callback&& callback)
16228fb49dd6SShawn McCarney {
162362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
16248fb49dd6SShawn McCarney 
16255eb468daSGeorge Liu     // Call GetManagedObjects on the ObjectMapper to get all associations
16265eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
16275eb468daSGeorge Liu     dbus::utility::getManagedObjects(
16285eb468daSGeorge Liu         "xyz.openbmc_project.ObjectMapper", path,
162902cad96eSEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
16305e7e2dc5SEd Tanous          sensorNames](const boost::system::error_code& ec,
163102cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
163262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
16338fb49dd6SShawn McCarney         if (ec)
16348fb49dd6SShawn McCarney         {
163562598e31SEd Tanous             BMCWEB_LOG_ERROR(
163662598e31SEd Tanous                 "getInventoryItemAssociations respHandler DBus error {}", ec);
16378d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
16388fb49dd6SShawn McCarney             return;
16398fb49dd6SShawn McCarney         }
16408fb49dd6SShawn McCarney 
1641adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1642adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1643adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1644adc4f0dbSShawn McCarney 
16458fb49dd6SShawn McCarney         // Loop through returned object paths
16468fb49dd6SShawn McCarney         std::string sensorAssocPath;
16478fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
16488fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
16498fb49dd6SShawn McCarney         {
16508fb49dd6SShawn McCarney             const std::string& objPath =
16518fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
16528fb49dd6SShawn McCarney 
16538fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
16548fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
16558fb49dd6SShawn McCarney             {
16568fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
16578fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
16588fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
16598fb49dd6SShawn McCarney                 {
16608fb49dd6SShawn McCarney                     // Get Association interface for object path
1661711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
16628fb49dd6SShawn McCarney                     {
1663711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1664711ac7a9SEd Tanous                         {
1665711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1666711ac7a9SEd Tanous                             {
1667711ac7a9SEd Tanous                                 if (valueName == "endpoints")
16688fb49dd6SShawn McCarney                                 {
16698fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
16708fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1671711ac7a9SEd Tanous                                             &value);
1672711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1673711ac7a9SEd Tanous                                         !endpoints->empty())
16748fb49dd6SShawn McCarney                                     {
1675adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1676adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1677adc4f0dbSShawn McCarney                                             endpoints->front();
1678711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1679711ac7a9SEd Tanous                                                          invItemPath,
1680adc4f0dbSShawn McCarney                                                          sensorName);
16818fb49dd6SShawn McCarney                                     }
16828fb49dd6SShawn McCarney                                 }
16838fb49dd6SShawn McCarney                             }
1684711ac7a9SEd Tanous                         }
1685711ac7a9SEd Tanous                     }
16868fb49dd6SShawn McCarney                     break;
16878fb49dd6SShawn McCarney                 }
16888fb49dd6SShawn McCarney             }
16898fb49dd6SShawn McCarney         }
16908fb49dd6SShawn McCarney 
1691d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1692d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1693d500549bSAnthony Wilson         std::string inventoryAssocPath;
1694d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1695d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1696d500549bSAnthony Wilson         {
1697d500549bSAnthony Wilson             const std::string& objPath =
1698d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1699d500549bSAnthony Wilson 
1700d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1701d500549bSAnthony Wilson             {
1702d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1703d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1704d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1705d500549bSAnthony Wilson                 {
1706711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1707d500549bSAnthony Wilson                     {
1708711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1709711ac7a9SEd Tanous                         {
1710711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1711711ac7a9SEd Tanous                             {
1712711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1713d500549bSAnthony Wilson                                 {
1714d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1715d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1716711ac7a9SEd Tanous                                             &value);
1717711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1718711ac7a9SEd Tanous                                         !endpoints->empty())
1719d500549bSAnthony Wilson                                     {
1720711ac7a9SEd Tanous                                         // Add inventory item to vector
1721d500549bSAnthony Wilson                                         // Store LED path in inventory item
1722711ac7a9SEd Tanous                                         const std::string& ledPath =
1723711ac7a9SEd Tanous                                             endpoints->front();
1724d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1725d500549bSAnthony Wilson                                     }
1726d500549bSAnthony Wilson                                 }
1727d500549bSAnthony Wilson                             }
1728711ac7a9SEd Tanous                         }
1729711ac7a9SEd Tanous                     }
1730711ac7a9SEd Tanous 
1731d500549bSAnthony Wilson                     break;
1732d500549bSAnthony Wilson                 }
1733d500549bSAnthony Wilson             }
1734d500549bSAnthony Wilson         }
1735adc4f0dbSShawn McCarney         callback(inventoryItems);
173662598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
17375eb468daSGeorge Liu     });
17388fb49dd6SShawn McCarney 
173962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
17408fb49dd6SShawn McCarney }
17418fb49dd6SShawn McCarney 
17428fb49dd6SShawn McCarney /**
1743d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1744d500549bSAnthony Wilson  *
1745d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1746d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1747d500549bSAnthony Wilson  * inventoryItems vector.
1748d500549bSAnthony Wilson  *
1749d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1750d500549bSAnthony Wilson  * response.
1751d500549bSAnthony Wilson  *
1752d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1753d500549bSAnthony Wilson  * has been obtained.
1754d500549bSAnthony Wilson  *
1755d500549bSAnthony Wilson  * The callback must have the following signature:
1756d500549bSAnthony Wilson  *   @code
175742cbe538SGunnar Mills  *   callback()
1758d500549bSAnthony Wilson  *   @endcode
1759d500549bSAnthony Wilson  *
1760d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1761d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1762d500549bSAnthony Wilson  * last asynchronous function has completed.
1763d500549bSAnthony Wilson  *
1764d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1765d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1766d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1767d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1768d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1769d500549bSAnthony Wilson  * in recursive calls to this function.
1770d500549bSAnthony Wilson  */
1771d500549bSAnthony Wilson template <typename Callback>
1772d500549bSAnthony Wilson void getInventoryLedData(
1773d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1774d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1775fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1776d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1777d500549bSAnthony Wilson {
177862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData enter");
1779d500549bSAnthony Wilson 
1780d500549bSAnthony Wilson     // If no more connections left, call callback
1781d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1782d500549bSAnthony Wilson     {
178342cbe538SGunnar Mills         callback();
178462598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1785d500549bSAnthony Wilson         return;
1786d500549bSAnthony Wilson     }
1787d500549bSAnthony Wilson 
1788d500549bSAnthony Wilson     // Get inventory item data from current connection
1789fe04d49cSNan Zhou     auto it = ledConnections->begin();
1790fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1791d500549bSAnthony Wilson     if (it != ledConnections->end())
1792d500549bSAnthony Wilson     {
1793d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1794d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1795d500549bSAnthony Wilson         // Response handler for Get State property
17961e1e598dSJonathan Doman         auto respHandler =
17971e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1798f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
17995e7e2dc5SEd Tanous                 const boost::system::error_code& ec, const std::string& state) {
180062598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1801d500549bSAnthony Wilson             if (ec)
1802d500549bSAnthony Wilson             {
180362598e31SEd Tanous                 BMCWEB_LOG_ERROR(
180462598e31SEd Tanous                     "getInventoryLedData respHandler DBus error {}", ec);
18058d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1806d500549bSAnthony Wilson                 return;
1807d500549bSAnthony Wilson             }
1808d500549bSAnthony Wilson 
180962598e31SEd Tanous             BMCWEB_LOG_DEBUG("Led state: {}", state);
1810d500549bSAnthony Wilson             // Find inventory item with this LED object path
1811d500549bSAnthony Wilson             InventoryItem* inventoryItem =
1812d500549bSAnthony Wilson                 findInventoryItemForLed(*inventoryItems, ledPath);
1813d500549bSAnthony Wilson             if (inventoryItem != nullptr)
1814d500549bSAnthony Wilson             {
1815d500549bSAnthony Wilson                 // Store LED state in InventoryItem
181611ba3979SEd Tanous                 if (state.ends_with("On"))
1817d500549bSAnthony Wilson                 {
1818d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::ON;
1819d500549bSAnthony Wilson                 }
182011ba3979SEd Tanous                 else if (state.ends_with("Blink"))
1821d500549bSAnthony Wilson                 {
1822d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::BLINK;
1823d500549bSAnthony Wilson                 }
182411ba3979SEd Tanous                 else if (state.ends_with("Off"))
1825d500549bSAnthony Wilson                 {
1826d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::OFF;
1827d500549bSAnthony Wilson                 }
1828d500549bSAnthony Wilson                 else
1829d500549bSAnthony Wilson                 {
1830d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::UNKNOWN;
1831d500549bSAnthony Wilson                 }
1832d500549bSAnthony Wilson             }
1833d500549bSAnthony Wilson 
1834d500549bSAnthony Wilson             // Recurse to get LED data from next connection
1835d500549bSAnthony Wilson             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1836d500549bSAnthony Wilson                                 ledConnections, std::move(callback),
1837d500549bSAnthony Wilson                                 ledConnectionsIndex + 1);
1838d500549bSAnthony Wilson 
183962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1840d500549bSAnthony Wilson         };
1841d500549bSAnthony Wilson 
1842d500549bSAnthony Wilson         // Get the State property for the current LED
18431e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
18441e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
18451e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
18461e1e598dSJonathan Doman             std::move(respHandler));
1847d500549bSAnthony Wilson     }
1848d500549bSAnthony Wilson 
184962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1850d500549bSAnthony Wilson }
1851d500549bSAnthony Wilson 
1852d500549bSAnthony Wilson /**
1853d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1854d500549bSAnthony Wilson  *
1855d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1856d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1857d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1858d500549bSAnthony Wilson  *
1859d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1860d500549bSAnthony Wilson  * response.
1861d500549bSAnthony Wilson  *
1862d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1863d500549bSAnthony Wilson  * been obtained.
1864d500549bSAnthony Wilson  *
1865d500549bSAnthony Wilson  * The callback must have the following signature:
1866d500549bSAnthony Wilson  *   @code
186742cbe538SGunnar Mills  *   callback()
1868d500549bSAnthony Wilson  *   @endcode
1869d500549bSAnthony Wilson  *
1870d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1871d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1872d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1873d500549bSAnthony Wilson  */
1874d500549bSAnthony Wilson template <typename Callback>
1875d500549bSAnthony Wilson void getInventoryLeds(
1876d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1877d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1878d500549bSAnthony Wilson     Callback&& callback)
1879d500549bSAnthony Wilson {
188062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds enter");
1881d500549bSAnthony Wilson 
1882d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1883e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1884d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1885d500549bSAnthony Wilson 
1886e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1887e99073f5SGeorge Liu     dbus::utility::getSubTree(
1888e99073f5SGeorge Liu         path, 0, interfaces,
1889002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1890002d39b4SEd Tanous          inventoryItems](
1891e99073f5SGeorge Liu             const boost::system::error_code& ec,
1892002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1893e99073f5SGeorge Liu         // Response handler for parsing output from GetSubTree
189462598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1895d500549bSAnthony Wilson         if (ec)
1896d500549bSAnthony Wilson         {
18978d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
189862598e31SEd Tanous             BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}", ec);
1899d500549bSAnthony Wilson             return;
1900d500549bSAnthony Wilson         }
1901d500549bSAnthony Wilson 
1902d500549bSAnthony Wilson         // Build map of LED object paths to connections
1903fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1904fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
1905d500549bSAnthony Wilson 
1906d500549bSAnthony Wilson         // Loop through objects from GetSubTree
1907d500549bSAnthony Wilson         for (const std::pair<
1908d500549bSAnthony Wilson                  std::string,
1909d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1910d500549bSAnthony Wilson                  object : subtree)
1911d500549bSAnthony Wilson         {
1912d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
1913d500549bSAnthony Wilson             // items
1914d500549bSAnthony Wilson             const std::string& ledPath = object.first;
1915d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1916d500549bSAnthony Wilson             {
1917d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
1918d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
1919d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
192062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath, connection);
1921d500549bSAnthony Wilson             }
1922d500549bSAnthony Wilson         }
1923d500549bSAnthony Wilson 
1924d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1925d500549bSAnthony Wilson                             std::move(callback));
192662598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1927e99073f5SGeorge Liu     });
192862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds exit");
1929d500549bSAnthony Wilson }
1930d500549bSAnthony Wilson 
1931d500549bSAnthony Wilson /**
193242cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
193342cbe538SGunnar Mills  *
193442cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
193542cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
193642cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
193742cbe538SGunnar Mills  *
193842cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
193942cbe538SGunnar Mills  * response.
194042cbe538SGunnar Mills  *
194142cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
194242cbe538SGunnar Mills  * when data has been obtained.
194342cbe538SGunnar Mills  *
194442cbe538SGunnar Mills  * The callback must have the following signature:
194542cbe538SGunnar Mills  *   @code
194642cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
194742cbe538SGunnar Mills  *   @endcode
194842cbe538SGunnar Mills  *
194942cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
195042cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
195142cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
195242cbe538SGunnar Mills  *        Supply Attributes
195342cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
195442cbe538SGunnar Mills  */
195542cbe538SGunnar Mills template <typename Callback>
195642cbe538SGunnar Mills void getPowerSupplyAttributesData(
1957b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
195842cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1959fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
196042cbe538SGunnar Mills     Callback&& callback)
196142cbe538SGunnar Mills {
196262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
196342cbe538SGunnar Mills 
196442cbe538SGunnar Mills     if (psAttributesConnections.empty())
196542cbe538SGunnar Mills     {
196662598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
196742cbe538SGunnar Mills         callback(inventoryItems);
196842cbe538SGunnar Mills         return;
196942cbe538SGunnar Mills     }
197042cbe538SGunnar Mills 
197142cbe538SGunnar Mills     // Assuming just one connection (service) for now
1972fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
197342cbe538SGunnar Mills 
197442cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
197542cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
197642cbe538SGunnar Mills 
197742cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
19785a39f77aSPatrick Williams     auto respHandler = [sensorsAsyncResp, inventoryItems,
1979f94c4ecfSEd Tanous                         callback{std::forward<Callback>(callback)}](
19805a39f77aSPatrick Williams                            const boost::system::error_code& ec,
19815a39f77aSPatrick Williams                            const uint32_t value) {
198262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
198342cbe538SGunnar Mills         if (ec)
198442cbe538SGunnar Mills         {
198562598e31SEd Tanous             BMCWEB_LOG_ERROR(
198662598e31SEd Tanous                 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
19878d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
198842cbe538SGunnar Mills             return;
198942cbe538SGunnar Mills         }
199042cbe538SGunnar Mills 
199162598e31SEd Tanous         BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
199242cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
199342cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
199442cbe538SGunnar Mills         {
199555f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
199642cbe538SGunnar Mills             {
199742cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
19981e1e598dSJonathan Doman                     static_cast<int>(value);
199942cbe538SGunnar Mills             }
200042cbe538SGunnar Mills         }
200142cbe538SGunnar Mills 
200262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
200342cbe538SGunnar Mills         callback(inventoryItems);
200442cbe538SGunnar Mills     };
200542cbe538SGunnar Mills 
200642cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
200742cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
20081e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
20091e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
20101e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
20111e1e598dSJonathan Doman         std::move(respHandler));
201242cbe538SGunnar Mills 
201362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
201442cbe538SGunnar Mills }
201542cbe538SGunnar Mills 
201642cbe538SGunnar Mills /**
201742cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
201842cbe538SGunnar Mills  *
201942cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
202042cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
202142cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
202242cbe538SGunnar Mills  * item.
202342cbe538SGunnar Mills  *
202442cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
202542cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
202642cbe538SGunnar Mills  *
202742cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
202842cbe538SGunnar Mills  * when information has been obtained.
202942cbe538SGunnar Mills  *
203042cbe538SGunnar Mills  * The callback must have the following signature:
203142cbe538SGunnar Mills  *   @code
203242cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
203342cbe538SGunnar Mills  *   @endcode
203442cbe538SGunnar Mills  *
203542cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
203642cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
203742cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
203842cbe538SGunnar Mills  */
203942cbe538SGunnar Mills template <typename Callback>
204042cbe538SGunnar Mills void getPowerSupplyAttributes(
204142cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
204242cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
204342cbe538SGunnar Mills     Callback&& callback)
204442cbe538SGunnar Mills {
204562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
204642cbe538SGunnar Mills 
204742cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2048a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
204942cbe538SGunnar Mills     {
205062598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
205142cbe538SGunnar Mills         callback(inventoryItems);
205242cbe538SGunnar Mills         return;
205342cbe538SGunnar Mills     }
205442cbe538SGunnar Mills 
2055e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
205642cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
205742cbe538SGunnar Mills 
2058e99073f5SGeorge Liu     // Make call to ObjectMapper to find the PowerSupplyAttributes service
2059e99073f5SGeorge Liu     dbus::utility::getSubTree(
2060e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, interfaces,
2061b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2062b9d36b47SEd Tanous          inventoryItems](
2063e99073f5SGeorge Liu             const boost::system::error_code& ec,
2064b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
2065e99073f5SGeorge Liu         // Response handler for parsing output from GetSubTree
206662598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
206742cbe538SGunnar Mills         if (ec)
206842cbe538SGunnar Mills         {
20698d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
207062598e31SEd Tanous             BMCWEB_LOG_ERROR(
207162598e31SEd Tanous                 "getPowerSupplyAttributes respHandler DBus error {}", ec);
207242cbe538SGunnar Mills             return;
207342cbe538SGunnar Mills         }
207426f6976fSEd Tanous         if (subtree.empty())
207542cbe538SGunnar Mills         {
207662598e31SEd Tanous             BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
207742cbe538SGunnar Mills             callback(inventoryItems);
207842cbe538SGunnar Mills             return;
207942cbe538SGunnar Mills         }
208042cbe538SGunnar Mills 
208142cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
208242cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
208342cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
2084fe04d49cSNan Zhou         std::map<std::string, std::string> psAttributesConnections;
208542cbe538SGunnar Mills 
208642cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
208742cbe538SGunnar Mills         {
208862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
208942cbe538SGunnar Mills             callback(inventoryItems);
209042cbe538SGunnar Mills             return;
209142cbe538SGunnar Mills         }
209242cbe538SGunnar Mills 
209342cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
209442cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
209542cbe538SGunnar Mills 
209642cbe538SGunnar Mills         if (connection.empty())
209742cbe538SGunnar Mills         {
209862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
209942cbe538SGunnar Mills             callback(inventoryItems);
210042cbe538SGunnar Mills             return;
210142cbe538SGunnar Mills         }
210242cbe538SGunnar Mills 
210342cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
210462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
210562598e31SEd Tanous                          connection);
210642cbe538SGunnar Mills 
210742cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
210842cbe538SGunnar Mills                                      psAttributesConnections,
210942cbe538SGunnar Mills                                      std::move(callback));
211062598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
2111e99073f5SGeorge Liu     });
211262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
211342cbe538SGunnar Mills }
211442cbe538SGunnar Mills 
211542cbe538SGunnar Mills /**
2116adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
21178fb49dd6SShawn McCarney  *
21188fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2119adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
21208fb49dd6SShawn McCarney  *
2121adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2122adc4f0dbSShawn McCarney  * response.
21238fb49dd6SShawn McCarney  *
2124adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2125adc4f0dbSShawn McCarney  * inventory items have been obtained.
2126adc4f0dbSShawn McCarney  *
2127adc4f0dbSShawn McCarney  * The callback must have the following signature:
2128adc4f0dbSShawn McCarney  *   @code
2129adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2130adc4f0dbSShawn McCarney  *   @endcode
21318fb49dd6SShawn McCarney  *
21328fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
21338fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
21348fb49dd6SShawn McCarney  * implements ObjectManager.
2135adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
21368fb49dd6SShawn McCarney  */
2137adc4f0dbSShawn McCarney template <typename Callback>
2138d0090733SEd Tanous static void
2139d0090733SEd Tanous     getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2140fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>> sensorNames,
2141adc4f0dbSShawn McCarney                       Callback&& callback)
21428fb49dd6SShawn McCarney {
214362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems enter");
2144adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2145d0090733SEd Tanous         [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
2146adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
214762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
21488fb49dd6SShawn McCarney         auto getInventoryItemsConnectionsCb =
2149d0090733SEd Tanous             [sensorsAsyncResp, inventoryItems,
2150f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
2151fe04d49cSNan Zhou                 std::shared_ptr<std::set<std::string>> invConnections) {
215262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
2153002d39b4SEd Tanous             auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2154d500549bSAnthony Wilson                                             callback{std::move(callback)}]() {
215562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
215642cbe538SGunnar Mills 
2157002d39b4SEd Tanous                 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2158002d39b4SEd Tanous                                            callback{std::move(callback)}]() {
215962598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryLedsCb enter");
216042cbe538SGunnar Mills                     // Find Power Supply Attributes and get the data
2161002d39b4SEd Tanous                     getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
216242cbe538SGunnar Mills                                              std::move(callback));
216362598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
216442cbe538SGunnar Mills                 };
216542cbe538SGunnar Mills 
2166d500549bSAnthony Wilson                 // Find led connections and get the data
2167d500549bSAnthony Wilson                 getInventoryLeds(sensorsAsyncResp, inventoryItems,
216842cbe538SGunnar Mills                                  std::move(getInventoryLedsCb));
216962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
2170d500549bSAnthony Wilson             };
21718fb49dd6SShawn McCarney 
2172adc4f0dbSShawn McCarney             // Get inventory item data from connections
2173adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2174d0090733SEd Tanous                                   invConnections,
2175d500549bSAnthony Wilson                                   std::move(getInventoryItemsDataCb));
217662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
21778fb49dd6SShawn McCarney         };
21788fb49dd6SShawn McCarney 
2179adc4f0dbSShawn McCarney         // Get connections that provide inventory item data
2180002d39b4SEd Tanous         getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
21818fb49dd6SShawn McCarney                                      std::move(getInventoryItemsConnectionsCb));
218262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
21838fb49dd6SShawn McCarney     };
21848fb49dd6SShawn McCarney 
2185adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2186d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
2187adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
218862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems exit");
2189adc4f0dbSShawn McCarney }
2190adc4f0dbSShawn McCarney 
2191adc4f0dbSShawn McCarney /**
2192adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2193adc4f0dbSShawn McCarney  *
2194adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2195adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2196adc4f0dbSShawn McCarney  * array.
2197adc4f0dbSShawn McCarney  *
2198adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2199adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2200adc4f0dbSShawn McCarney  * object.
2201adc4f0dbSShawn McCarney  *
2202adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2203adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2204adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2205adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2206adc4f0dbSShawn McCarney  */
220723a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2208adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2209adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2210adc4f0dbSShawn McCarney {
221118f8f608SEd Tanous     std::string nameS;
22126f4bd290SAlexander Hansen     nameS.resize(inventoryItem.name.size());
221318f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
2214adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2215adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2216adc4f0dbSShawn McCarney     {
221718f8f608SEd Tanous         nlohmann::json::iterator nameIt = powerSupply.find("Name");
221818f8f608SEd Tanous         if (nameIt == powerSupply.end())
221918f8f608SEd Tanous         {
222018f8f608SEd Tanous             continue;
222118f8f608SEd Tanous         }
222218f8f608SEd Tanous         const std::string* name = nameIt->get_ptr<std::string*>();
222318f8f608SEd Tanous         if (name == nullptr)
222418f8f608SEd Tanous         {
222518f8f608SEd Tanous             continue;
222618f8f608SEd Tanous         }
222718f8f608SEd Tanous         if (nameS == *name)
2228adc4f0dbSShawn McCarney         {
2229adc4f0dbSShawn McCarney             return powerSupply;
2230adc4f0dbSShawn McCarney         }
2231adc4f0dbSShawn McCarney     }
2232adc4f0dbSShawn McCarney 
2233adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2234adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2235adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2236ef4c65b7SEd Tanous     boost::urls::url url = boost::urls::format("/redfish/v1/Chassis/{}/Power",
2237ef4c65b7SEd Tanous                                                chassisId);
2238eddfc437SWilly Tu     url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2239eddfc437SWilly Tu     powerSupply["@odata.id"] = std::move(url);
224018f8f608SEd Tanous     std::string escaped;
22416f4bd290SAlexander Hansen     escaped.resize(inventoryItem.name.size());
224218f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
224318f8f608SEd Tanous     powerSupply["Name"] = std::move(escaped);
2244adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2245adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2246adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2247adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2248d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2249adc4f0dbSShawn McCarney 
225042cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
225142cbe538SGunnar Mills     {
225242cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
225342cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
225442cbe538SGunnar Mills     }
225542cbe538SGunnar Mills 
2256aaf08ac7SMatt Simmering     powerSupply["Status"]["State"] = getState(&inventoryItem, true);
2257adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2258adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2259adc4f0dbSShawn McCarney 
2260adc4f0dbSShawn McCarney     return powerSupply;
22618fb49dd6SShawn McCarney }
22628fb49dd6SShawn McCarney 
22638fb49dd6SShawn McCarney /**
2264de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2265de629b6eSShawn McCarney  *
2266de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2267de629b6eSShawn McCarney  *
2268de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2269de629b6eSShawn McCarney  * information has been obtained.
2270de629b6eSShawn McCarney  *
2271adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2272de629b6eSShawn McCarney  *
2273de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2274de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2275de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2276de629b6eSShawn McCarney  *
2277de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2278de629b6eSShawn McCarney  *
2279adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2280adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2281adc4f0dbSShawn McCarney  *
2282de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2283adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2284de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2285de629b6eSShawn McCarney  * implements ObjectManager.
2286adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2287de629b6eSShawn McCarney  */
228823a21a1cSEd Tanous inline void getSensorData(
228981ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2290fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2291fe04d49cSNan Zhou     const std::set<std::string>& connections,
2292b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2293de629b6eSShawn McCarney {
229462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData enter");
2295de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2296de629b6eSShawn McCarney     for (const std::string& connection : connections)
2297de629b6eSShawn McCarney     {
22985eb468daSGeorge Liu         sdbusplus::message::object_path sensorPath(
22995eb468daSGeorge Liu             "/xyz/openbmc_project/sensors");
23005eb468daSGeorge Liu         dbus::utility::getManagedObjects(
23015eb468daSGeorge Liu             connection, sensorPath,
2302002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
23035e7e2dc5SEd Tanous              inventoryItems](const boost::system::error_code& ec,
230402cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
230562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
2306de629b6eSShawn McCarney             if (ec)
2307de629b6eSShawn McCarney             {
230862598e31SEd Tanous                 BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
23098d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2310de629b6eSShawn McCarney                 return;
2311de629b6eSShawn McCarney             }
2312de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2313de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2314de629b6eSShawn McCarney             {
2315de629b6eSShawn McCarney                 const std::string& objPath =
2316de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
231762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
231862598e31SEd Tanous                                  objPath);
2319de629b6eSShawn McCarney 
2320de629b6eSShawn McCarney                 std::vector<std::string> split;
2321de629b6eSShawn McCarney                 // Reserve space for
2322de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2323de629b6eSShawn McCarney                 split.reserve(6);
232450ebd4afSEd Tanous                 // NOLINTNEXTLINE
232550ebd4afSEd Tanous                 bmcweb::split(split, objPath, '/');
2326de629b6eSShawn McCarney                 if (split.size() < 6)
2327de629b6eSShawn McCarney                 {
232862598e31SEd Tanous                     BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
232962598e31SEd Tanous                                      objPath);
2330de629b6eSShawn McCarney                     continue;
2331de629b6eSShawn McCarney                 }
233250ebd4afSEd Tanous                 // These indexes aren't intuitive, as split puts an empty
2333de629b6eSShawn McCarney                 // string at the beginning
2334de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2335de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
233662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
233762598e31SEd Tanous                                  sensorType);
233849c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2339de629b6eSShawn McCarney                 {
234062598e31SEd Tanous                     BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
2341de629b6eSShawn McCarney                     continue;
2342de629b6eSShawn McCarney                 }
2343de629b6eSShawn McCarney 
2344adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2345adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2346adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2347adc4f0dbSShawn McCarney 
234895a3ecadSAnthony Wilson                 const std::string& sensorSchema =
234981ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
235095a3ecadSAnthony Wilson 
235195a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
235295a3ecadSAnthony Wilson 
2353928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2354928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
235595a3ecadSAnthony Wilson                 {
2356c1d019a6SEd Tanous                     std::string sensorTypeEscaped(sensorType);
23573544d2a7SEd Tanous                     auto remove = std::ranges::remove(sensorTypeEscaped, '_');
23583544d2a7SEd Tanous 
23593544d2a7SEd Tanous                     sensorTypeEscaped.erase(std::ranges::begin(remove),
2360c1d019a6SEd Tanous                                             sensorTypeEscaped.end());
2361c1d019a6SEd Tanous                     std::string sensorId(sensorTypeEscaped);
2362c1d019a6SEd Tanous                     sensorId += "_";
2363c1d019a6SEd Tanous                     sensorId += sensorName;
2364c1d019a6SEd Tanous 
23658d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
2366ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}/{}/{}",
2367c1d019a6SEd Tanous                                             sensorsAsyncResp->chassisId,
2368ef4c65b7SEd Tanous                                             sensorsAsyncResp->chassisSubNode,
2369ef4c65b7SEd Tanous                                             sensorId);
23708d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
237195a3ecadSAnthony Wilson                 }
237295a3ecadSAnthony Wilson                 else
237395a3ecadSAnthony Wilson                 {
2374271584abSEd Tanous                     std::string fieldName;
2375928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2376928fefb9SNan Zhou                     {
2377928fefb9SNan Zhou                         fieldName = "Members";
2378928fefb9SNan Zhou                     }
2379928fefb9SNan Zhou                     else if (sensorType == "temperature")
2380de629b6eSShawn McCarney                     {
2381de629b6eSShawn McCarney                         fieldName = "Temperatures";
2382de629b6eSShawn McCarney                     }
2383de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2384de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2385de629b6eSShawn McCarney                     {
2386de629b6eSShawn McCarney                         fieldName = "Fans";
2387de629b6eSShawn McCarney                     }
2388de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2389de629b6eSShawn McCarney                     {
2390de629b6eSShawn McCarney                         fieldName = "Voltages";
2391de629b6eSShawn McCarney                     }
2392de629b6eSShawn McCarney                     else if (sensorType == "power")
2393de629b6eSShawn McCarney                     {
239455f79e6fSEd Tanous                         if (sensorName == "total_power")
2395028f7ebcSEddie James                         {
2396028f7ebcSEddie James                             fieldName = "PowerControl";
2397028f7ebcSEddie James                         }
2398adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2399adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2400028f7ebcSEddie James                         {
2401de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2402de629b6eSShawn McCarney                         }
2403adc4f0dbSShawn McCarney                         else
2404adc4f0dbSShawn McCarney                         {
2405adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2406adc4f0dbSShawn McCarney                             continue;
2407adc4f0dbSShawn McCarney                         }
2408028f7ebcSEddie James                     }
2409de629b6eSShawn McCarney                     else
2410de629b6eSShawn McCarney                     {
241162598e31SEd Tanous                         BMCWEB_LOG_ERROR("Unsure how to handle sensorType {}",
241262598e31SEd Tanous                                          sensorType);
2413de629b6eSShawn McCarney                         continue;
2414de629b6eSShawn McCarney                     }
2415de629b6eSShawn McCarney 
2416de629b6eSShawn McCarney                     nlohmann::json& tempArray =
24178d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2418adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
241949c53ac9SJohnathan Mantey                     {
2420adc4f0dbSShawn McCarney                         if (tempArray.empty())
24217ab06f49SGunnar Mills                         {
242295a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
242395a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
242495a3ecadSAnthony Wilson                             // naming in power.hpp.
24251476687dSEd Tanous                             nlohmann::json::object_t power;
2426ef4c65b7SEd Tanous                             boost::urls::url url = boost::urls::format(
2427ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}",
2428eddfc437SWilly Tu                                 sensorsAsyncResp->chassisId,
2429eddfc437SWilly Tu                                 sensorsAsyncResp->chassisSubNode);
2430eddfc437SWilly Tu                             url.set_fragment((""_json_pointer / fieldName / "0")
2431eddfc437SWilly Tu                                                  .to_string());
2432eddfc437SWilly Tu                             power["@odata.id"] = std::move(url);
2433b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(power));
2434adc4f0dbSShawn McCarney                         }
2435adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2436adc4f0dbSShawn McCarney                     }
2437adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2438adc4f0dbSShawn McCarney                     {
2439adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2440adc4f0dbSShawn McCarney                         {
2441adc4f0dbSShawn McCarney                             sensorJson =
2442adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
244381ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2444adc4f0dbSShawn McCarney                         }
244549c53ac9SJohnathan Mantey                     }
2446928fefb9SNan Zhou                     else if (fieldName == "Members")
2447928fefb9SNan Zhou                     {
2448677bb756SEd Tanous                         std::string sensorTypeEscaped(sensorType);
24493544d2a7SEd Tanous                         auto remove = std::ranges::remove(sensorTypeEscaped,
24503544d2a7SEd Tanous                                                           '_');
24513544d2a7SEd Tanous                         sensorTypeEscaped.erase(std::ranges::begin(remove),
2452677bb756SEd Tanous                                                 sensorTypeEscaped.end());
2453677bb756SEd Tanous                         std::string sensorId(sensorTypeEscaped);
2454677bb756SEd Tanous                         sensorId += "_";
2455677bb756SEd Tanous                         sensorId += sensorName;
2456677bb756SEd Tanous 
24571476687dSEd Tanous                         nlohmann::json::object_t member;
2458ef4c65b7SEd Tanous                         member["@odata.id"] = boost::urls::format(
2459ef4c65b7SEd Tanous                             "/redfish/v1/Chassis/{}/{}/{}",
2460677bb756SEd Tanous                             sensorsAsyncResp->chassisId,
2461677bb756SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
2462b2ba3072SPatrick Williams                         tempArray.emplace_back(std::move(member));
2463928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2464928fefb9SNan Zhou                     }
246549c53ac9SJohnathan Mantey                     else
246649c53ac9SJohnathan Mantey                     {
24671476687dSEd Tanous                         nlohmann::json::object_t member;
2468ef4c65b7SEd Tanous                         boost::urls::url url = boost::urls::format(
2469ef4c65b7SEd Tanous                             "/redfish/v1/Chassis/{}/{}",
2470eddfc437SWilly Tu                             sensorsAsyncResp->chassisId,
2471eddfc437SWilly Tu                             sensorsAsyncResp->chassisSubNode);
2472eddfc437SWilly Tu                         url.set_fragment(
2473eddfc437SWilly Tu                             (""_json_pointer / fieldName).to_string());
2474eddfc437SWilly Tu                         member["@odata.id"] = std::move(url);
2475b2ba3072SPatrick Williams                         tempArray.emplace_back(std::move(member));
2476adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
247749c53ac9SJohnathan Mantey                     }
247895a3ecadSAnthony Wilson                 }
2479de629b6eSShawn McCarney 
2480adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2481adc4f0dbSShawn McCarney                 {
24821d7c0054SEd Tanous                     objectInterfacesToJson(sensorName, sensorType,
24831d7c0054SEd Tanous                                            sensorsAsyncResp->chassisSubNode,
24841d7c0054SEd Tanous                                            objDictEntry.second, *sensorJson,
24851d7c0054SEd Tanous                                            inventoryItem);
24861d7c0054SEd Tanous 
24871d7c0054SEd Tanous                     std::string path = "/xyz/openbmc_project/sensors/";
24881d7c0054SEd Tanous                     path += sensorType;
24891d7c0054SEd Tanous                     path += "/";
24901d7c0054SEd Tanous                     path += sensorName;
2491c1d019a6SEd Tanous                     sensorsAsyncResp->addMetadata(*sensorJson, path);
2492adc4f0dbSShawn McCarney                 }
2493de629b6eSShawn McCarney             }
249481ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
249549c53ac9SJohnathan Mantey             {
249681ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2497928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2498928fefb9SNan Zhou                         sensors::node::sensors &&
2499928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2500928fefb9SNan Zhou                 {
2501928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2502928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2503928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2504928fefb9SNan Zhou                             .size();
2505928fefb9SNan Zhou                 }
2506928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2507928fefb9SNan Zhou                          sensors::node::thermal)
25088bd25ccdSJames Feist                 {
250981ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
25108bd25ccdSJames Feist                 }
251149c53ac9SJohnathan Mantey             }
251262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
25135eb468daSGeorge Liu         });
251423a21a1cSEd Tanous     }
251562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData exit");
2516de629b6eSShawn McCarney }
2517de629b6eSShawn McCarney 
2518fe04d49cSNan Zhou inline void
2519fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2520fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
25211abe55efSEd Tanous {
2522fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2523fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
252462598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb enter");
2525adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2526d0090733SEd Tanous             [sensorsAsyncResp, sensorNames,
2527d0090733SEd Tanous              connections](const std::shared_ptr<std::vector<InventoryItem>>&
2528adc4f0dbSShawn McCarney                               inventoryItems) {
252962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
253049c53ac9SJohnathan Mantey             // Get sensor data and store results in JSON
2531002d39b4SEd Tanous             getSensorData(sensorsAsyncResp, sensorNames, connections,
2532d0090733SEd Tanous                           inventoryItems);
253362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2534adc4f0dbSShawn McCarney         };
2535adc4f0dbSShawn McCarney 
2536adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2537d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2538adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2539adc4f0dbSShawn McCarney 
254062598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb exit");
254108777fb0SLewanczyk, Dawid     };
2542de629b6eSShawn McCarney 
2543de629b6eSShawn McCarney     // Get set of connections that provide sensor values
254481ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
254595a3ecadSAnthony Wilson }
254695a3ecadSAnthony Wilson 
254795a3ecadSAnthony Wilson /**
254895a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
254995a3ecadSAnthony Wilson  *        chassis.
255095a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
255195a3ecadSAnthony Wilson  */
2552b5a76932SEd Tanous inline void
255381ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
255495a3ecadSAnthony Wilson {
255562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData enter");
255695a3ecadSAnthony Wilson     auto getChassisCb =
255781ce609eSEd Tanous         [sensorsAsyncResp](
2558fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
255962598e31SEd Tanous         BMCWEB_LOG_DEBUG("getChassisCb enter");
256081ce609eSEd Tanous         processSensorList(sensorsAsyncResp, sensorNames);
256162598e31SEd Tanous         BMCWEB_LOG_DEBUG("getChassisCb exit");
256208777fb0SLewanczyk, Dawid     };
2563928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2564928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2565928fefb9SNan Zhou     {
25668d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
25678d1b46d7Szhanghch05             nlohmann::json::array();
2568928fefb9SNan Zhou     }
256926f03899SShawn McCarney     // Get set of sensors in chassis
25707f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
25717f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
25727f1cc26dSEd Tanous                std::move(getChassisCb));
257362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData exit");
2574271584abSEd Tanous }
257508777fb0SLewanczyk, Dawid 
2576413961deSRichard Marian Thomaiyar /**
257749c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
257849c53ac9SJohnathan Mantey  * the chassis node
257949c53ac9SJohnathan Mantey  *
258049c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
258149c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
258249c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
258349c53ac9SJohnathan Mantey  *                         repeated calls to this function
258449c53ac9SJohnathan Mantey  */
2585fe04d49cSNan Zhou inline bool
2586fe04d49cSNan Zhou     findSensorNameUsingSensorPath(std::string_view sensorName,
258702cad96eSEd Tanous                                   const std::set<std::string>& sensorsList,
2588fe04d49cSNan Zhou                                   std::set<std::string>& sensorsModified)
258949c53ac9SJohnathan Mantey {
2590fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
259149c53ac9SJohnathan Mantey     {
259228aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2593b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
259428aa8de5SGeorge Liu         if (thisSensorName.empty())
259549c53ac9SJohnathan Mantey         {
259649c53ac9SJohnathan Mantey             continue;
259749c53ac9SJohnathan Mantey         }
259849c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
259949c53ac9SJohnathan Mantey         {
260049c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
260149c53ac9SJohnathan Mantey             return true;
260249c53ac9SJohnathan Mantey         }
260349c53ac9SJohnathan Mantey     }
260449c53ac9SJohnathan Mantey     return false;
260549c53ac9SJohnathan Mantey }
260649c53ac9SJohnathan Mantey 
2607c71d6125SEd Tanous inline std::pair<std::string, std::string>
2608c71d6125SEd Tanous     splitSensorNameAndType(std::string_view sensorId)
2609c71d6125SEd Tanous {
2610c71d6125SEd Tanous     size_t index = sensorId.find('_');
2611c71d6125SEd Tanous     if (index == std::string::npos)
2612c71d6125SEd Tanous     {
2613c71d6125SEd Tanous         return std::make_pair<std::string, std::string>("", "");
2614c71d6125SEd Tanous     }
2615c71d6125SEd Tanous     std::string sensorType{sensorId.substr(0, index)};
2616c71d6125SEd Tanous     std::string sensorName{sensorId.substr(index + 1)};
2617c71d6125SEd Tanous     // fan_pwm and fan_tach need special handling
2618c71d6125SEd Tanous     if (sensorType == "fantach" || sensorType == "fanpwm")
2619c71d6125SEd Tanous     {
2620c71d6125SEd Tanous         sensorType.insert(3, 1, '_');
2621c71d6125SEd Tanous     }
2622c71d6125SEd Tanous     return std::make_pair(sensorType, sensorName);
2623c71d6125SEd Tanous }
2624c71d6125SEd Tanous 
262549c53ac9SJohnathan Mantey /**
2626413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2627413961deSRichard Marian Thomaiyar  *
26288d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
26294bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2630413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2631413961deSRichard Marian Thomaiyar  */
263223a21a1cSEd Tanous inline void setSensorsOverride(
2633b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
2634*0885057cSEd Tanous     std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
2635397fd61fSjayaprakash Mutyala         allCollections)
2636413961deSRichard Marian Thomaiyar {
263762598e31SEd Tanous     BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
263862598e31SEd Tanous                     sensorAsyncResp->chassisSubNode);
2639413961deSRichard Marian Thomaiyar 
2640543f4400SEd Tanous     const char* propertyValueName = nullptr;
2641f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2642413961deSRichard Marian Thomaiyar     std::string memberId;
2643543f4400SEd Tanous     double value = 0.0;
2644f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2645f65af9e8SRichard Marian Thomaiyar     {
2646f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2647f65af9e8SRichard Marian Thomaiyar         {
2648f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2649f65af9e8SRichard Marian Thomaiyar         }
2650f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2651f65af9e8SRichard Marian Thomaiyar         {
2652f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2653f65af9e8SRichard Marian Thomaiyar         }
2654f65af9e8SRichard Marian Thomaiyar         else
2655f65af9e8SRichard Marian Thomaiyar         {
2656f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2657f65af9e8SRichard Marian Thomaiyar         }
2658f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2659f65af9e8SRichard Marian Thomaiyar         {
2660*0885057cSEd Tanous             if (!json_util::readJsonObject(
2661*0885057cSEd Tanous                     item, sensorAsyncResp->asyncResp->res, "MemberId", memberId,
2662*0885057cSEd Tanous                     propertyValueName, value))
2663413961deSRichard Marian Thomaiyar             {
2664413961deSRichard Marian Thomaiyar                 return;
2665413961deSRichard Marian Thomaiyar             }
2666f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2667f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2668f65af9e8SRichard Marian Thomaiyar         }
2669f65af9e8SRichard Marian Thomaiyar     }
26704bb3dc34SCarol Wang 
2671002d39b4SEd Tanous     auto getChassisSensorListCb =
2672002d39b4SEd Tanous         [sensorAsyncResp, overrideMap](
2673fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorsList) {
267449c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
267549c53ac9SJohnathan Mantey         // chassis node
2676fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2677fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2678f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2679413961deSRichard Marian Thomaiyar         {
2680f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2681c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
2682c71d6125SEd Tanous                 splitSensorNameAndType(sensor);
2683c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2684c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2685f65af9e8SRichard Marian Thomaiyar             {
268662598e31SEd Tanous                 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
26878d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2688f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2689413961deSRichard Marian Thomaiyar                 return;
2690413961deSRichard Marian Thomaiyar             }
2691f65af9e8SRichard Marian Thomaiyar         }
2692413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2693002d39b4SEd Tanous         auto getObjectsWithConnectionCb =
2694fe04d49cSNan Zhou             [sensorAsyncResp,
2695fe04d49cSNan Zhou              overrideMap](const std::set<std::string>& /*connections*/,
2696002d39b4SEd Tanous                           const std::set<std::pair<std::string, std::string>>&
2697413961deSRichard Marian Thomaiyar                               objectsWithConnection) {
2698f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2699413961deSRichard Marian Thomaiyar             {
270062598e31SEd Tanous                 BMCWEB_LOG_INFO(
270162598e31SEd Tanous                     "Unable to find all objects with proper connection {} requested {}",
270262598e31SEd Tanous                     objectsWithConnection.size(), overrideMap.size());
27034f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2704a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2705a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2706413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2707413961deSRichard Marian Thomaiyar                                                : "Voltages",
2708f65af9e8SRichard Marian Thomaiyar                                            "Count");
2709f65af9e8SRichard Marian Thomaiyar                 return;
2710f65af9e8SRichard Marian Thomaiyar             }
2711f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2712f65af9e8SRichard Marian Thomaiyar             {
271328aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
271428aa8de5SGeorge Liu                 std::string sensorName = path.filename();
271528aa8de5SGeorge Liu                 if (sensorName.empty())
2716f65af9e8SRichard Marian Thomaiyar                 {
27174f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2718f65af9e8SRichard Marian Thomaiyar                     return;
2719f65af9e8SRichard Marian Thomaiyar                 }
27203f5eb755SBan Feng                 std::string id = path.parent_path().filename();
27213544d2a7SEd Tanous                 auto remove = std::ranges::remove(id, '_');
27223544d2a7SEd Tanous                 id.erase(std::ranges::begin(remove), id.end());
27233f5eb755SBan Feng                 id += "_";
27243f5eb755SBan Feng                 id += sensorName;
2725f65af9e8SRichard Marian Thomaiyar 
27263f5eb755SBan Feng                 const auto& iterator = overrideMap.find(id);
2727f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2728f65af9e8SRichard Marian Thomaiyar                 {
272962598e31SEd Tanous                     BMCWEB_LOG_INFO("Unable to find sensor object{}",
273062598e31SEd Tanous                                     item.first);
27314f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2732413961deSRichard Marian Thomaiyar                     return;
2733413961deSRichard Marian Thomaiyar                 }
27349ae226faSGeorge Liu                 sdbusplus::asio::setProperty(
27359ae226faSGeorge Liu                     *crow::connections::systemBus, item.second, item.first,
27369ae226faSGeorge Liu                     "xyz.openbmc_project.Sensor.Value", "Value",
27379ae226faSGeorge Liu                     iterator->second.first,
27385e7e2dc5SEd Tanous                     [sensorAsyncResp](const boost::system::error_code& ec) {
2739413961deSRichard Marian Thomaiyar                     if (ec)
2740413961deSRichard Marian Thomaiyar                     {
27414f277b54SJayaprakash Mutyala                         if (ec.value() ==
27424f277b54SJayaprakash Mutyala                             boost::system::errc::permission_denied)
27434f277b54SJayaprakash Mutyala                         {
274462598e31SEd Tanous                             BMCWEB_LOG_WARNING(
274562598e31SEd Tanous                                 "Manufacturing mode is not Enabled...can't "
274662598e31SEd Tanous                                 "Override the sensor value. ");
27474f277b54SJayaprakash Mutyala 
27484f277b54SJayaprakash Mutyala                             messages::insufficientPrivilege(
27498d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
2750413961deSRichard Marian Thomaiyar                             return;
2751413961deSRichard Marian Thomaiyar                         }
275262598e31SEd Tanous                         BMCWEB_LOG_DEBUG(
275362598e31SEd Tanous                             "setOverrideValueStatus DBUS error: {}", ec);
27544f277b54SJayaprakash Mutyala                         messages::internalError(
27554f277b54SJayaprakash Mutyala                             sensorAsyncResp->asyncResp->res);
27564f277b54SJayaprakash Mutyala                     }
27579ae226faSGeorge Liu                 });
2758f65af9e8SRichard Marian Thomaiyar             }
2759413961deSRichard Marian Thomaiyar         };
2760413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2761413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2762413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2763413961deSRichard Marian Thomaiyar     };
2764413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
27657f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
27667f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
27677f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2768413961deSRichard Marian Thomaiyar }
2769413961deSRichard Marian Thomaiyar 
2770a0ec28b6SAdrian Ambrożewicz /**
2771a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2772a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2773a0ec28b6SAdrian Ambrożewicz  *
2774a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2775a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2776a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2777a0ec28b6SAdrian Ambrożewicz  *
2778a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2779a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2780a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2781a0ec28b6SAdrian Ambrożewicz  */
2782931edc79SEd Tanous template <typename Callback>
2783021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2784021d32cfSKrzysztof Grobelny                                  const std::string& node,
2785931edc79SEd Tanous                                  Callback&& mapComplete)
2786a0ec28b6SAdrian Ambrożewicz {
278702da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
278802da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
278902da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
279002da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2791a0ec28b6SAdrian Ambrożewicz     {
279262598e31SEd Tanous         BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
27936804b5c8SEd Tanous         std::map<std::string, std::string> noop;
27946804b5c8SEd Tanous         mapComplete(boost::beast::http::status::bad_request, noop);
2795a0ec28b6SAdrian Ambrożewicz         return;
2796a0ec28b6SAdrian Ambrożewicz     }
2797d51e072fSKrzysztof Grobelny 
279872374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2799931edc79SEd Tanous     auto callback = [asyncResp,
2800931edc79SEd Tanous                      mapCompleteCb{std::forward<Callback>(mapComplete)}](
2801a0ec28b6SAdrian Ambrożewicz                         const boost::beast::http::status status,
2802fe04d49cSNan Zhou                         const std::map<std::string, std::string>& uriToDbus) {
2803fe04d49cSNan Zhou         mapCompleteCb(status, uriToDbus);
2804fe04d49cSNan Zhou     };
2805a0ec28b6SAdrian Ambrożewicz 
2806a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2807d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2808a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2809a0ec28b6SAdrian Ambrożewicz }
2810a0ec28b6SAdrian Ambrożewicz 
2811bacb2162SNan Zhou namespace sensors
2812bacb2162SNan Zhou {
2813928fefb9SNan Zhou 
2814bacb2162SNan Zhou inline void getChassisCallback(
2815c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2816c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2817fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2818bacb2162SNan Zhou {
281962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback enter ");
2820bacb2162SNan Zhou 
2821c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2822c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2823bacb2162SNan Zhou     {
282462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
2825bacb2162SNan Zhou 
2826bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2827bacb2162SNan Zhou         std::string sensorName = path.filename();
2828bacb2162SNan Zhou         if (sensorName.empty())
2829bacb2162SNan Zhou         {
283062598e31SEd Tanous             BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
2831c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2832bacb2162SNan Zhou             return;
2833bacb2162SNan Zhou         }
2834c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
2835c1d019a6SEd Tanous         // fan_tach has an underscore in it, so remove it to "normalize" the
2836c1d019a6SEd Tanous         // type in the URI
28373544d2a7SEd Tanous         auto remove = std::ranges::remove(type, '_');
28383544d2a7SEd Tanous         type.erase(std::ranges::begin(remove), type.end());
2839c1d019a6SEd Tanous 
28401476687dSEd Tanous         nlohmann::json::object_t member;
2841c1d019a6SEd Tanous         std::string id = type;
2842c1d019a6SEd Tanous         id += "_";
2843c1d019a6SEd Tanous         id += sensorName;
2844ef4c65b7SEd Tanous         member["@odata.id"] = boost::urls::format(
2845ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
2846c1d019a6SEd Tanous 
2847b2ba3072SPatrick Williams         entriesArray.emplace_back(std::move(member));
2848bacb2162SNan Zhou     }
2849bacb2162SNan Zhou 
2850c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
285162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback exit");
2852bacb2162SNan Zhou }
2853e6bd846dSNan Zhou 
2854ac106bf6SEd Tanous inline void handleSensorCollectionGet(
2855ac106bf6SEd Tanous     App& app, const crow::Request& req,
2856ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2857de167a6fSNan Zhou     const std::string& chassisId)
2858de167a6fSNan Zhou {
2859de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2860de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2861de167a6fSNan Zhou     };
2862de167a6fSNan Zhou     query_param::Query delegatedQuery;
2863ac106bf6SEd Tanous     if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
2864de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2865de167a6fSNan Zhou     {
2866de167a6fSNan Zhou         return;
2867de167a6fSNan Zhou     }
2868de167a6fSNan Zhou 
2869de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2870de167a6fSNan Zhou     {
2871de167a6fSNan Zhou         // we perform efficient expand.
2872ac106bf6SEd Tanous         auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2873ac106bf6SEd Tanous             asyncResp, chassisId, sensors::dbus::sensorPaths,
2874de167a6fSNan Zhou             sensors::node::sensors,
2875de167a6fSNan Zhou             /*efficientExpand=*/true);
2876ac106bf6SEd Tanous         getChassisData(sensorsAsyncResp);
2877de167a6fSNan Zhou 
287862598e31SEd Tanous         BMCWEB_LOG_DEBUG(
287962598e31SEd Tanous             "SensorCollection doGet exit via efficient expand handler");
2880de167a6fSNan Zhou         return;
28810bad320cSEd Tanous     }
2882de167a6fSNan Zhou 
2883de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2884de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
2885ac106bf6SEd Tanous     getChassis(asyncResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2886ac106bf6SEd Tanous                std::bind_front(sensors::getChassisCallback, asyncResp,
2887ac106bf6SEd Tanous                                chassisId, sensors::node::sensors));
2888c1d019a6SEd Tanous }
28897f1cc26dSEd Tanous 
2890c1d019a6SEd Tanous inline void
2891c1d019a6SEd Tanous     getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2892c1d019a6SEd Tanous                       const std::string& sensorPath,
2893c1d019a6SEd Tanous                       const ::dbus::utility::MapperGetObject& mapperResponse)
2894c1d019a6SEd Tanous {
2895c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2896c1d019a6SEd Tanous     {
2897c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2898c1d019a6SEd Tanous         return;
2899c1d019a6SEd Tanous     }
2900c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2901c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
290262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
290362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Path {}", sensorPath);
2904c1343bf6SKrzysztof Grobelny 
2905c1343bf6SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2906c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2907c1d019a6SEd Tanous         [asyncResp,
29085e7e2dc5SEd Tanous          sensorPath](const boost::system::error_code& ec,
2909c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2910c1d019a6SEd Tanous         if (ec)
2911c1d019a6SEd Tanous         {
2912c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2913c1d019a6SEd Tanous             return;
2914c1d019a6SEd Tanous         }
2915c1d019a6SEd Tanous         sdbusplus::message::object_path path(sensorPath);
2916c1d019a6SEd Tanous         std::string name = path.filename();
2917c1d019a6SEd Tanous         path = path.parent_path();
2918c1d019a6SEd Tanous         std::string type = path.filename();
2919c1d019a6SEd Tanous         objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2920c1d019a6SEd Tanous                                asyncResp->res.jsonValue, nullptr);
2921c1343bf6SKrzysztof Grobelny     });
2922de167a6fSNan Zhou }
2923de167a6fSNan Zhou 
2924e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2925c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2926677bb756SEd Tanous                             const std::string& chassisId,
2927c1d019a6SEd Tanous                             const std::string& sensorId)
2928e6bd846dSNan Zhou {
2929c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2930e6bd846dSNan Zhou     {
2931e6bd846dSNan Zhou         return;
2932e6bd846dSNan Zhou     }
2933c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
2934c71d6125SEd Tanous         splitSensorNameAndType(sensorId);
2935c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2936c1d019a6SEd Tanous     {
2937c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2938c1d019a6SEd Tanous         return;
2939c1d019a6SEd Tanous     }
2940c71d6125SEd Tanous 
2941ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2942ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
2943c1d019a6SEd Tanous 
294462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Sensor doGet enter");
2945e6bd846dSNan Zhou 
29462b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2947e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2948c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2949c71d6125SEd Tanous                              '/' + nameType.second;
2950e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2951e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
29522b73119cSGeorge Liu     ::dbus::utility::getDbusObject(
29532b73119cSGeorge Liu         sensorPath, interfaces,
2954aec0ec30SMyung Bae         [asyncResp, sensorId,
29552b73119cSGeorge Liu          sensorPath](const boost::system::error_code& ec,
2956c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
295762598e31SEd Tanous         BMCWEB_LOG_DEBUG("respHandler1 enter");
2958aec0ec30SMyung Bae         if (ec == boost::system::errc::io_error)
2959aec0ec30SMyung Bae         {
296062598e31SEd Tanous             BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
2961aec0ec30SMyung Bae             messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2962aec0ec30SMyung Bae             return;
2963aec0ec30SMyung Bae         }
2964e6bd846dSNan Zhou         if (ec)
2965e6bd846dSNan Zhou         {
2966c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
296762598e31SEd Tanous             BMCWEB_LOG_ERROR(
296862598e31SEd Tanous                 "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
2969e6bd846dSNan Zhou             return;
2970e6bd846dSNan Zhou         }
2971c1d019a6SEd Tanous         getSensorFromDbus(asyncResp, sensorPath, subtree);
297262598e31SEd Tanous         BMCWEB_LOG_DEBUG("respHandler1 exit");
29732b73119cSGeorge Liu     });
2974e6bd846dSNan Zhou }
2975e6bd846dSNan Zhou 
2976bacb2162SNan Zhou } // namespace sensors
2977bacb2162SNan Zhou 
29787e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
297995a3ecadSAnthony Wilson {
29807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2981ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2982002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2983de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
298495a3ecadSAnthony Wilson }
298595a3ecadSAnthony Wilson 
29867e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
298795a3ecadSAnthony Wilson {
29887e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2989ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2990002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2991e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
299295a3ecadSAnthony Wilson }
299395a3ecadSAnthony Wilson 
299408777fb0SLewanczyk, Dawid } // namespace redfish
2995