xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision bd79bce8c3f1deb1fb2773868b9ece25233cf27b)
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"
21539d8c6bSEd Tanous #include "generated/enums/redundancy.hpp"
22aaf08ac7SMatt Simmering #include "generated/enums/resource.hpp"
230ec8b83dSEd Tanous #include "generated/enums/sensor.hpp"
24539d8c6bSEd Tanous #include "generated/enums/thermal.hpp"
253ccb3adbSEd Tanous #include "query.hpp"
263ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
2750ebd4afSEd Tanous #include "str_utility.hpp"
283ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
293ccb3adbSEd Tanous #include "utils/json_utils.hpp"
303ccb3adbSEd Tanous #include "utils/query_param.hpp"
310ec8b83dSEd Tanous 
32e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
33ef4c65b7SEd Tanous #include <boost/url/format.hpp>
341e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
3586d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
361214b7e7SGunnar Mills 
377a1dbc48SGeorge Liu #include <array>
381214b7e7SGunnar Mills #include <cmath>
39fe04d49cSNan Zhou #include <iterator>
40283860f5SEd Tanous #include <limits>
41fe04d49cSNan Zhou #include <map>
423544d2a7SEd Tanous #include <ranges>
43fe04d49cSNan Zhou #include <set>
4418f8f608SEd Tanous #include <string>
457a1dbc48SGeorge Liu #include <string_view>
46b5a76932SEd Tanous #include <utility>
47abf2add6SEd Tanous #include <variant>
4808777fb0SLewanczyk, Dawid 
491abe55efSEd Tanous namespace redfish
501abe55efSEd Tanous {
5108777fb0SLewanczyk, Dawid 
52a0ec28b6SAdrian Ambrożewicz namespace sensors
53a0ec28b6SAdrian Ambrożewicz {
54a0ec28b6SAdrian Ambrożewicz namespace node
55a0ec28b6SAdrian Ambrożewicz {
56a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
57a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
58a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
59a0ec28b6SAdrian Ambrożewicz } // namespace node
60a0ec28b6SAdrian Ambrożewicz 
6102da7c5aSEd Tanous // clang-format off
62a0ec28b6SAdrian Ambrożewicz namespace dbus
63a0ec28b6SAdrian Ambrożewicz {
64cf9e417dSEd Tanous constexpr auto powerPaths = std::to_array<std::string_view>({
6502da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
6602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
6702da7c5aSEd Tanous });
68c2bf7f99SWludzik, Jozef 
6925b54dbaSEd Tanous constexpr auto getSensorPaths(){
7025b54dbaSEd Tanous     if constexpr(BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM){
7125b54dbaSEd Tanous     return std::to_array<std::string_view>({
7202da7c5aSEd Tanous         "/xyz/openbmc_project/sensors/power",
73a0ec28b6SAdrian Ambrożewicz         "/xyz/openbmc_project/sensors/current",
747088690cSBasheer Ahmed Muddebihal         "/xyz/openbmc_project/sensors/airflow",
755deabed9SGunnar Mills         "/xyz/openbmc_project/sensors/humidity",
76e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/voltage",
77e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/fan_tach",
78e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/temperature",
79e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/fan_pwm",
80e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/altitude",
81e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/energy",
8225b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/utilization"});
8325b54dbaSEd Tanous     } else {
8425b54dbaSEd Tanous       return  std::to_array<std::string_view>({"/xyz/openbmc_project/sensors/power",
8525b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/current",
8625b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/airflow",
8725b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/humidity",
8825b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/utilization"});
8925b54dbaSEd Tanous }
9025b54dbaSEd Tanous }
9125b54dbaSEd Tanous 
9225b54dbaSEd Tanous constexpr auto sensorPaths = getSensorPaths();
9302da7c5aSEd Tanous 
94cf9e417dSEd Tanous constexpr auto thermalPaths = std::to_array<std::string_view>({
9502da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
96a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
9702da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
9802da7c5aSEd Tanous });
9902da7c5aSEd Tanous 
100c2bf7f99SWludzik, Jozef } // namespace dbus
10102da7c5aSEd Tanous // clang-format on
10202da7c5aSEd Tanous 
103cf9e417dSEd Tanous using sensorPair =
104cf9e417dSEd Tanous     std::pair<std::string_view, std::span<const std::string_view>>;
10502da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
106cf9e417dSEd Tanous     {{node::power, dbus::powerPaths},
107cf9e417dSEd Tanous      {node::sensors, dbus::sensorPaths},
108cf9e417dSEd Tanous      {node::thermal, dbus::thermalPaths}}};
109c2bf7f99SWludzik, Jozef 
1100ec8b83dSEd Tanous inline sensor::ReadingType toReadingType(std::string_view sensorType)
111c2bf7f99SWludzik, Jozef {
112c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
113c2bf7f99SWludzik, Jozef     {
1140ec8b83dSEd Tanous         return sensor::ReadingType::Voltage;
115c2bf7f99SWludzik, Jozef     }
116c2bf7f99SWludzik, Jozef     if (sensorType == "power")
117c2bf7f99SWludzik, Jozef     {
1180ec8b83dSEd Tanous         return sensor::ReadingType::Power;
119c2bf7f99SWludzik, Jozef     }
120c2bf7f99SWludzik, Jozef     if (sensorType == "current")
121c2bf7f99SWludzik, Jozef     {
1220ec8b83dSEd Tanous         return sensor::ReadingType::Current;
123c2bf7f99SWludzik, Jozef     }
124c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
125c2bf7f99SWludzik, Jozef     {
1260ec8b83dSEd Tanous         return sensor::ReadingType::Rotational;
127c2bf7f99SWludzik, Jozef     }
128c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
129c2bf7f99SWludzik, Jozef     {
1300ec8b83dSEd Tanous         return sensor::ReadingType::Temperature;
131c2bf7f99SWludzik, Jozef     }
132c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
133c2bf7f99SWludzik, Jozef     {
1340ec8b83dSEd Tanous         return sensor::ReadingType::Percent;
135c2bf7f99SWludzik, Jozef     }
1365deabed9SGunnar Mills     if (sensorType == "humidity")
1375deabed9SGunnar Mills     {
1380ec8b83dSEd Tanous         return sensor::ReadingType::Humidity;
1395deabed9SGunnar Mills     }
140c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
141c2bf7f99SWludzik, Jozef     {
1420ec8b83dSEd Tanous         return sensor::ReadingType::Altitude;
143c2bf7f99SWludzik, Jozef     }
144c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
145c2bf7f99SWludzik, Jozef     {
1460ec8b83dSEd Tanous         return sensor::ReadingType::AirFlow;
147c2bf7f99SWludzik, Jozef     }
148c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
149c2bf7f99SWludzik, Jozef     {
1500ec8b83dSEd Tanous         return sensor::ReadingType::EnergyJoules;
151c2bf7f99SWludzik, Jozef     }
1520ec8b83dSEd Tanous     return sensor::ReadingType::Invalid;
153c2bf7f99SWludzik, Jozef }
154c2bf7f99SWludzik, Jozef 
1551d7c0054SEd Tanous inline std::string_view toReadingUnits(std::string_view sensorType)
156c2bf7f99SWludzik, Jozef {
157c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
158c2bf7f99SWludzik, Jozef     {
159c2bf7f99SWludzik, Jozef         return "V";
160c2bf7f99SWludzik, Jozef     }
161c2bf7f99SWludzik, Jozef     if (sensorType == "power")
162c2bf7f99SWludzik, Jozef     {
163c2bf7f99SWludzik, Jozef         return "W";
164c2bf7f99SWludzik, Jozef     }
165c2bf7f99SWludzik, Jozef     if (sensorType == "current")
166c2bf7f99SWludzik, Jozef     {
167c2bf7f99SWludzik, Jozef         return "A";
168c2bf7f99SWludzik, Jozef     }
169c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
170c2bf7f99SWludzik, Jozef     {
171c2bf7f99SWludzik, Jozef         return "RPM";
172c2bf7f99SWludzik, Jozef     }
173c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
174c2bf7f99SWludzik, Jozef     {
175c2bf7f99SWludzik, Jozef         return "Cel";
176c2bf7f99SWludzik, Jozef     }
1775deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
1785deabed9SGunnar Mills         sensorType == "humidity")
179c2bf7f99SWludzik, Jozef     {
180c2bf7f99SWludzik, Jozef         return "%";
181c2bf7f99SWludzik, Jozef     }
182c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
183c2bf7f99SWludzik, Jozef     {
184c2bf7f99SWludzik, Jozef         return "m";
185c2bf7f99SWludzik, Jozef     }
186c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
187c2bf7f99SWludzik, Jozef     {
188c2bf7f99SWludzik, Jozef         return "cft_i/min";
189c2bf7f99SWludzik, Jozef     }
190c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
191c2bf7f99SWludzik, Jozef     {
192c2bf7f99SWludzik, Jozef         return "J";
193c2bf7f99SWludzik, Jozef     }
194c2bf7f99SWludzik, Jozef     return "";
195a0ec28b6SAdrian Ambrożewicz }
196a0ec28b6SAdrian Ambrożewicz } // namespace sensors
197a0ec28b6SAdrian Ambrożewicz 
19808777fb0SLewanczyk, Dawid /**
199588c3f0dSKowalski, Kamil  * SensorsAsyncResp
20008777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
20108777fb0SLewanczyk, Dawid  */
2021abe55efSEd Tanous class SensorsAsyncResp
2031abe55efSEd Tanous {
20408777fb0SLewanczyk, Dawid   public:
205a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
206a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
207fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
208a0ec28b6SAdrian Ambrożewicz 
209a0ec28b6SAdrian Ambrożewicz     struct SensorData
210a0ec28b6SAdrian Ambrożewicz     {
211a0ec28b6SAdrian Ambrożewicz         const std::string name;
212a0ec28b6SAdrian Ambrożewicz         std::string uri;
213a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
214a0ec28b6SAdrian Ambrożewicz     };
215a0ec28b6SAdrian Ambrożewicz 
2168a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2178d1b46d7Szhanghch05                      const std::string& chassisIdIn,
218cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
21902da7c5aSEd Tanous                      std::string_view subNode) :
220*bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
221*bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false)
2221214b7e7SGunnar Mills     {}
22308777fb0SLewanczyk, Dawid 
224a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
2258a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2268d1b46d7Szhanghch05                      const std::string& chassisIdIn,
227cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
22802da7c5aSEd Tanous                      std::string_view subNode,
229a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
230*bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
231*bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false),
232*bd79bce8SPatrick Williams         metadata{std::vector<SensorData>()},
233a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
234a0ec28b6SAdrian Ambrożewicz     {}
235a0ec28b6SAdrian Ambrożewicz 
236928fefb9SNan Zhou     // sensor collections expand
2378a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
238928fefb9SNan Zhou                      const std::string& chassisIdIn,
239cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
2408a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
241*bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
242*bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(efficientExpandIn)
243928fefb9SNan Zhou     {}
244928fefb9SNan Zhou 
2451abe55efSEd Tanous     ~SensorsAsyncResp()
2461abe55efSEd Tanous     {
2478d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2488d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2491abe55efSEd Tanous         {
2501abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2511abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2521abe55efSEd Tanous             // proper code
2538d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
25408777fb0SLewanczyk, Dawid         }
255a0ec28b6SAdrian Ambrożewicz 
256a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
257a0ec28b6SAdrian Ambrożewicz         {
258fe04d49cSNan Zhou             std::map<std::string, std::string> map;
2598d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
260a0ec28b6SAdrian Ambrożewicz             {
261a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
262a0ec28b6SAdrian Ambrożewicz                 {
263c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
264a0ec28b6SAdrian Ambrożewicz                 }
265a0ec28b6SAdrian Ambrożewicz             }
2668d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
267a0ec28b6SAdrian Ambrożewicz         }
26808777fb0SLewanczyk, Dawid     }
269588c3f0dSKowalski, Kamil 
270ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
271ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
272ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
273ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
274ecd6a3a2SEd Tanous 
275a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
276c1d019a6SEd Tanous                      const std::string& dbusPath)
277a0ec28b6SAdrian Ambrożewicz     {
278a0ec28b6SAdrian Ambrożewicz         if (metadata)
279a0ec28b6SAdrian Ambrożewicz         {
280c1d019a6SEd Tanous             metadata->emplace_back(SensorData{
281c1d019a6SEd Tanous                 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
282a0ec28b6SAdrian Ambrożewicz         }
283a0ec28b6SAdrian Ambrożewicz     }
284a0ec28b6SAdrian Ambrożewicz 
285a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
286a0ec28b6SAdrian Ambrożewicz     {
287a0ec28b6SAdrian Ambrożewicz         if (metadata)
288a0ec28b6SAdrian Ambrożewicz         {
289a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
290a0ec28b6SAdrian Ambrożewicz             {
291a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
292a0ec28b6SAdrian Ambrożewicz                 {
293a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
294a0ec28b6SAdrian Ambrożewicz                 }
295a0ec28b6SAdrian Ambrożewicz             }
296a0ec28b6SAdrian Ambrożewicz         }
297a0ec28b6SAdrian Ambrożewicz     }
298a0ec28b6SAdrian Ambrożewicz 
2998d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
300a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
301cf9e417dSEd Tanous     const std::span<const std::string_view> types;
302a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
303928fefb9SNan Zhou     const bool efficientExpand;
304a0ec28b6SAdrian Ambrożewicz 
305a0ec28b6SAdrian Ambrożewicz   private:
306a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
307a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
30808777fb0SLewanczyk, Dawid };
30908777fb0SLewanczyk, Dawid 
31008777fb0SLewanczyk, Dawid /**
311d500549bSAnthony Wilson  * Possible states for physical inventory leds
312d500549bSAnthony Wilson  */
313d500549bSAnthony Wilson enum class LedState
314d500549bSAnthony Wilson {
315d500549bSAnthony Wilson     OFF,
316d500549bSAnthony Wilson     ON,
317d500549bSAnthony Wilson     BLINK,
318d500549bSAnthony Wilson     UNKNOWN
319d500549bSAnthony Wilson };
320d500549bSAnthony Wilson 
321d500549bSAnthony Wilson /**
322adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
323adc4f0dbSShawn McCarney  */
324adc4f0dbSShawn McCarney class InventoryItem
325adc4f0dbSShawn McCarney {
326adc4f0dbSShawn McCarney   public:
3274e23a444SEd Tanous     explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
328adc4f0dbSShawn McCarney     {
329adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
33028aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
33128aa8de5SGeorge Liu         name = path.filename();
33228aa8de5SGeorge Liu         if (name.empty())
333adc4f0dbSShawn McCarney         {
33462598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath);
335adc4f0dbSShawn McCarney         }
336adc4f0dbSShawn McCarney     }
337adc4f0dbSShawn McCarney 
338adc4f0dbSShawn McCarney     std::string objectPath;
339adc4f0dbSShawn McCarney     std::string name;
340e05aec50SEd Tanous     bool isPresent = true;
341e05aec50SEd Tanous     bool isFunctional = true;
342e05aec50SEd Tanous     bool isPowerSupply = false;
343e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
344adc4f0dbSShawn McCarney     std::string manufacturer;
345adc4f0dbSShawn McCarney     std::string model;
346adc4f0dbSShawn McCarney     std::string partNumber;
347adc4f0dbSShawn McCarney     std::string serialNumber;
348adc4f0dbSShawn McCarney     std::set<std::string> sensors;
349d500549bSAnthony Wilson     std::string ledObjectPath;
350e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
351adc4f0dbSShawn McCarney };
352adc4f0dbSShawn McCarney 
353adc4f0dbSShawn McCarney /**
354413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
355588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
35608777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
35708777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
35808777fb0SLewanczyk, Dawid  */
35908777fb0SLewanczyk, Dawid template <typename Callback>
360413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
36181ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
362fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
3631abe55efSEd Tanous     Callback&& callback)
3641abe55efSEd Tanous {
36562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
36603b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
367e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
36808777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
36908777fb0SLewanczyk, Dawid 
370e99073f5SGeorge Liu     // Make call to ObjectMapper to find all sensors objects
371e99073f5SGeorge Liu     dbus::utility::getSubTree(
372e99073f5SGeorge Liu         path, 2, interfaces,
3738cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
374e99073f5SGeorge Liu          sensorNames](const boost::system::error_code& ec,
375002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
376e99073f5SGeorge Liu             // Response handler for parsing objects subtree
37762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
3781abe55efSEd Tanous             if (ec)
3791abe55efSEd Tanous             {
3808d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
38162598e31SEd Tanous                 BMCWEB_LOG_ERROR(
38262598e31SEd Tanous                     "getObjectsWithConnection resp_handler: Dbus error {}", ec);
38308777fb0SLewanczyk, Dawid                 return;
38408777fb0SLewanczyk, Dawid             }
38508777fb0SLewanczyk, Dawid 
38662598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
38708777fb0SLewanczyk, Dawid 
388*bd79bce8SPatrick Williams             // Make unique list of connections only for requested sensor types
389*bd79bce8SPatrick Williams             // and found in the chassis
390fe04d49cSNan Zhou             std::set<std::string> connections;
391413961deSRichard Marian Thomaiyar             std::set<std::pair<std::string, std::string>> objectsWithConnection;
39208777fb0SLewanczyk, Dawid 
39362598e31SEd Tanous             BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
39449c53ac9SJohnathan Mantey             for (const std::string& tsensor : *sensorNames)
3951abe55efSEd Tanous             {
39662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
39708777fb0SLewanczyk, Dawid             }
39808777fb0SLewanczyk, Dawid 
399*bd79bce8SPatrick Williams             for (const std::pair<std::string,
400*bd79bce8SPatrick Williams                                  std::vector<std::pair<
401*bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
4021abe55efSEd Tanous                      object : subtree)
4031abe55efSEd Tanous             {
40449c53ac9SJohnathan Mantey                 if (sensorNames->find(object.first) != sensorNames->end())
4051abe55efSEd Tanous                 {
40649c53ac9SJohnathan Mantey                     for (const std::pair<std::string, std::vector<std::string>>&
4071abe55efSEd Tanous                              objData : object.second)
4081abe55efSEd Tanous                     {
409*bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG("Adding connection: {}",
410*bd79bce8SPatrick Williams                                          objData.first);
41108777fb0SLewanczyk, Dawid                         connections.insert(objData.first);
412de629b6eSShawn McCarney                         objectsWithConnection.insert(
413de629b6eSShawn McCarney                             std::make_pair(object.first, objData.first));
41408777fb0SLewanczyk, Dawid                     }
41508777fb0SLewanczyk, Dawid                 }
41608777fb0SLewanczyk, Dawid             }
41762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
418413961deSRichard Marian Thomaiyar             callback(std::move(connections), std::move(objectsWithConnection));
41962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
420e99073f5SGeorge Liu         });
42162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
422413961deSRichard Marian Thomaiyar }
423413961deSRichard Marian Thomaiyar 
424413961deSRichard Marian Thomaiyar /**
425413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
426413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
427413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
428413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
429413961deSRichard Marian Thomaiyar  */
430413961deSRichard Marian Thomaiyar template <typename Callback>
431fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
432fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
433413961deSRichard Marian Thomaiyar                     Callback&& callback)
434413961deSRichard Marian Thomaiyar {
435413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
4368cb2c024SEd Tanous         [callback = std::forward<Callback>(callback)](
4378cb2c024SEd Tanous             const std::set<std::string>& connections,
438413961deSRichard Marian Thomaiyar             const std::set<std::pair<std::string, std::string>>&
4393174e4dfSEd Tanous             /*objectsWithConnection*/) { callback(connections); };
44081ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
441413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
44208777fb0SLewanczyk, Dawid }
44308777fb0SLewanczyk, Dawid 
44408777fb0SLewanczyk, Dawid /**
44549c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
44649c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
44749c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
44849c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
44949c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
45049c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
45149c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
45249c53ac9SJohnathan Mantey  */
45323a21a1cSEd Tanous inline void reduceSensorList(
4547f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
455cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
45649c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
457fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
45849c53ac9SJohnathan Mantey {
45949c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
46049c53ac9SJohnathan Mantey     {
4617f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
4627f1cc26dSEd Tanous                                    chassisSubNode == sensors::node::thermal
463a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
46449c53ac9SJohnathan Mantey                                        : "Voltages");
46549c53ac9SJohnathan Mantey 
46649c53ac9SJohnathan Mantey         return;
46749c53ac9SJohnathan Mantey     }
46849c53ac9SJohnathan Mantey     if (allSensors->empty())
46949c53ac9SJohnathan Mantey     {
47049c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
47149c53ac9SJohnathan Mantey         return;
47249c53ac9SJohnathan Mantey     }
47349c53ac9SJohnathan Mantey 
4747f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
47549c53ac9SJohnathan Mantey     {
47649c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
47749c53ac9SJohnathan Mantey         {
47811ba3979SEd Tanous             if (sensor.starts_with(type))
47949c53ac9SJohnathan Mantey             {
48049c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
48149c53ac9SJohnathan Mantey             }
48249c53ac9SJohnathan Mantey         }
48349c53ac9SJohnathan Mantey     }
48449c53ac9SJohnathan Mantey }
48549c53ac9SJohnathan Mantey 
4867f1cc26dSEd Tanous /*
4877f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
4887f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
4897f1cc26dSEd Tanous  *
4907f1cc26dSEd Tanous  * */
4917f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
4927f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
4937f1cc26dSEd Tanous {
4947f1cc26dSEd Tanous     if (chassisSubNode == sensors::node::power)
4957f1cc26dSEd Tanous     {
4967f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
4977f1cc26dSEd Tanous     }
4987f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::thermal)
4997f1cc26dSEd Tanous     {
5007f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
5017f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
5027f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
5037f1cc26dSEd Tanous     }
5047f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::sensors)
5057f1cc26dSEd Tanous     {
5067f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
5077f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
5087f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
5097f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
5107f1cc26dSEd Tanous     }
5117f1cc26dSEd Tanous 
5127f1cc26dSEd Tanous     if (chassisSubNode != sensors::node::sensors)
5137f1cc26dSEd Tanous     {
5147f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
5157f1cc26dSEd Tanous     }
5167f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
5177f1cc26dSEd Tanous }
5187f1cc26dSEd Tanous 
51949c53ac9SJohnathan Mantey /**
52008777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
521588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
52208777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
52308777fb0SLewanczyk, Dawid  */
52408777fb0SLewanczyk, Dawid template <typename Callback>
5257f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5267f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
527cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
528cf9e417dSEd Tanous                 Callback&& callback)
5291abe55efSEd Tanous {
53062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis enter");
5317a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
53249c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
533adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
5347a1dbc48SGeorge Liu 
5357a1dbc48SGeorge Liu     // Get the Chassis Collection
5367a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
5377a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
5388cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
5397f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
5407f1cc26dSEd Tanous          chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
5417a1dbc48SGeorge Liu             const boost::system::error_code& ec,
542002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
54362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassis respHandler enter");
5441abe55efSEd Tanous             if (ec)
5451abe55efSEd Tanous             {
54662598e31SEd Tanous                 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
5477f1cc26dSEd Tanous                 messages::internalError(asyncResp->res);
54808777fb0SLewanczyk, Dawid                 return;
54908777fb0SLewanczyk, Dawid             }
55049c53ac9SJohnathan Mantey             const std::string* chassisPath = nullptr;
55149c53ac9SJohnathan Mantey             for (const std::string& chassis : chassisPaths)
5521abe55efSEd Tanous             {
55328aa8de5SGeorge Liu                 sdbusplus::message::object_path path(chassis);
554f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
55528aa8de5SGeorge Liu                 if (chassisName.empty())
5561abe55efSEd Tanous                 {
55762598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
558daf36e2eSEd Tanous                     continue;
559daf36e2eSEd Tanous                 }
5607f1cc26dSEd Tanous                 if (chassisName == chassisIdStr)
5611abe55efSEd Tanous                 {
56249c53ac9SJohnathan Mantey                     chassisPath = &chassis;
56349c53ac9SJohnathan Mantey                     break;
564daf36e2eSEd Tanous                 }
56549c53ac9SJohnathan Mantey             }
56649c53ac9SJohnathan Mantey             if (chassisPath == nullptr)
5671abe55efSEd Tanous             {
568*bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Chassis",
569*bd79bce8SPatrick Williams                                            chassisIdStr);
57049c53ac9SJohnathan Mantey                 return;
5711abe55efSEd Tanous             }
5727f1cc26dSEd Tanous             populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
57308777fb0SLewanczyk, Dawid 
574ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
575ef4c65b7SEd Tanous                 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
57695a3ecadSAnthony Wilson 
5778fb49dd6SShawn McCarney             // Get the list of all sensors for this Chassis element
5788fb49dd6SShawn McCarney             std::string sensorPath = *chassisPath + "/all_sensors";
5796c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
5806c3e9451SGeorge Liu                 sensorPath,
5817f1cc26dSEd Tanous                 [asyncResp, chassisSubNode, sensorTypes,
5828cb2c024SEd Tanous                  callback = std::forward<const Callback>(callback)](
5838b24275dSEd Tanous                     const boost::system::error_code& ec2,
5846c3e9451SGeorge Liu                     const dbus::utility::MapperEndPoints& nodeSensorList) {
5858b24275dSEd Tanous                     if (ec2)
58649c53ac9SJohnathan Mantey                     {
5878b24275dSEd Tanous                         if (ec2.value() != EBADR)
58849c53ac9SJohnathan Mantey                         {
5897f1cc26dSEd Tanous                             messages::internalError(asyncResp->res);
59049c53ac9SJohnathan Mantey                             return;
59149c53ac9SJohnathan Mantey                         }
59249c53ac9SJohnathan Mantey                     }
593*bd79bce8SPatrick Williams                     const std::shared_ptr<std::set<std::string>>
594*bd79bce8SPatrick Williams                         culledSensorList =
595fe04d49cSNan Zhou                             std::make_shared<std::set<std::string>>();
596*bd79bce8SPatrick Williams                     reduceSensorList(asyncResp->res, chassisSubNode,
597*bd79bce8SPatrick Williams                                      sensorTypes, &nodeSensorList,
598*bd79bce8SPatrick Williams                                      culledSensorList);
599*bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Finishing with {}",
600*bd79bce8SPatrick Williams                                      culledSensorList->size());
60149c53ac9SJohnathan Mantey                     callback(culledSensorList);
6021e1e598dSJonathan Doman                 });
6037a1dbc48SGeorge Liu         });
60462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis exit");
60508777fb0SLewanczyk, Dawid }
60608777fb0SLewanczyk, Dawid 
60708777fb0SLewanczyk, Dawid /**
608adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
609adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
610aaf08ac7SMatt Simmering  * @param sensorAvailable Boolean representing if D-Bus sensor is marked as
611aaf08ac7SMatt Simmering  * available.
612adc4f0dbSShawn McCarney  * @return State value for inventory item.
61334dd179eSJames Feist  */
614aaf08ac7SMatt Simmering inline resource::State getState(const InventoryItem* inventoryItem,
615aaf08ac7SMatt Simmering                                 const bool sensorAvailable)
616adc4f0dbSShawn McCarney {
617adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
618adc4f0dbSShawn McCarney     {
619aaf08ac7SMatt Simmering         return resource::State::Absent;
620adc4f0dbSShawn McCarney     }
62134dd179eSJames Feist 
622aaf08ac7SMatt Simmering     if (!sensorAvailable)
623aaf08ac7SMatt Simmering     {
624aaf08ac7SMatt Simmering         return resource::State::UnavailableOffline;
625aaf08ac7SMatt Simmering     }
626aaf08ac7SMatt Simmering 
627aaf08ac7SMatt Simmering     return resource::State::Enabled;
628adc4f0dbSShawn McCarney }
629adc4f0dbSShawn McCarney 
630adc4f0dbSShawn McCarney /**
631adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
632adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
6331d7c0054SEd Tanous  * @param valuesDict Map of all sensor DBus values.
634adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
635adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
636adc4f0dbSShawn McCarney  * @return Health value for sensor.
637adc4f0dbSShawn McCarney  */
6381d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson,
6391d7c0054SEd Tanous                              const dbus::utility::DBusPropertiesMap& valuesDict,
640adc4f0dbSShawn McCarney                              const InventoryItem* inventoryItem)
64134dd179eSJames Feist {
642adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
643adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
644adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
645adc4f0dbSShawn McCarney     std::string currentHealth;
646adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
647adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
648adc4f0dbSShawn McCarney     {
649adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
650adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
651adc4f0dbSShawn McCarney         {
652adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
653adc4f0dbSShawn McCarney             if (health != nullptr)
654adc4f0dbSShawn McCarney             {
655adc4f0dbSShawn McCarney                 currentHealth = *health;
656adc4f0dbSShawn McCarney             }
657adc4f0dbSShawn McCarney         }
658adc4f0dbSShawn McCarney     }
659adc4f0dbSShawn McCarney 
660adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
661adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
662adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
663adc4f0dbSShawn McCarney     {
664adc4f0dbSShawn McCarney         return "Critical";
665adc4f0dbSShawn McCarney     }
666adc4f0dbSShawn McCarney 
667c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmHigh = nullptr;
668c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmLow = nullptr;
669c1343bf6SKrzysztof Grobelny     const bool* warningAlarmHigh = nullptr;
670c1343bf6SKrzysztof Grobelny     const bool* warningAlarmLow = nullptr;
671711ac7a9SEd Tanous 
672c1343bf6SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
673c1343bf6SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
674c1343bf6SKrzysztof Grobelny         criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
675c1343bf6SKrzysztof Grobelny         "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
676c1343bf6SKrzysztof Grobelny         warningAlarmLow);
677c1343bf6SKrzysztof Grobelny 
678c1343bf6SKrzysztof Grobelny     if (success)
67934dd179eSJames Feist     {
680c1343bf6SKrzysztof Grobelny         // Check if sensor has critical threshold alarm
681c1343bf6SKrzysztof Grobelny         if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
682c1343bf6SKrzysztof Grobelny             (criticalAlarmLow != nullptr && *criticalAlarmLow))
68334dd179eSJames Feist         {
68434dd179eSJames Feist             return "Critical";
68534dd179eSJames Feist         }
68634dd179eSJames Feist     }
68734dd179eSJames Feist 
688adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
689adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
690adc4f0dbSShawn McCarney     {
691adc4f0dbSShawn McCarney         return "Critical";
692adc4f0dbSShawn McCarney     }
693adc4f0dbSShawn McCarney 
694adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that. This
695adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
696adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
697adc4f0dbSShawn McCarney     {
698adc4f0dbSShawn McCarney         return "Warning";
699adc4f0dbSShawn McCarney     }
700adc4f0dbSShawn McCarney 
701c1343bf6SKrzysztof Grobelny     if (success)
702c1343bf6SKrzysztof Grobelny     {
703adc4f0dbSShawn McCarney         // Check if sensor has warning threshold alarm
704c1343bf6SKrzysztof Grobelny         if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
705c1343bf6SKrzysztof Grobelny             (warningAlarmLow != nullptr && *warningAlarmLow))
70634dd179eSJames Feist         {
707ebe4d91eSEd Tanous             return "Warning";
70834dd179eSJames Feist         }
70934dd179eSJames Feist     }
710adc4f0dbSShawn McCarney 
71134dd179eSJames Feist     return "OK";
71234dd179eSJames Feist }
71334dd179eSJames Feist 
71423a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
715d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
716d500549bSAnthony Wilson {
717d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
718d500549bSAnthony Wilson     {
719d500549bSAnthony Wilson         switch (inventoryItem->ledState)
720d500549bSAnthony Wilson         {
721d500549bSAnthony Wilson             case LedState::OFF:
722539d8c6bSEd Tanous                 sensorJson["IndicatorLED"] = resource::IndicatorLED::Off;
723d500549bSAnthony Wilson                 break;
724d500549bSAnthony Wilson             case LedState::ON:
725539d8c6bSEd Tanous                 sensorJson["IndicatorLED"] = resource::IndicatorLED::Lit;
726d500549bSAnthony Wilson                 break;
727d500549bSAnthony Wilson             case LedState::BLINK:
728539d8c6bSEd Tanous                 sensorJson["IndicatorLED"] = resource::IndicatorLED::Blinking;
729d500549bSAnthony Wilson                 break;
7304da0490bSEd Tanous             default:
731d500549bSAnthony Wilson                 break;
732d500549bSAnthony Wilson         }
733d500549bSAnthony Wilson     }
734d500549bSAnthony Wilson }
735d500549bSAnthony Wilson 
73634dd179eSJames Feist /**
73708777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
73808777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
739274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
74008777fb0SLewanczyk, Dawid  * build
7418ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
7421d7c0054SEd Tanous  * @param propertiesDict A dictionary of the properties to build the sensor
7431d7c0054SEd Tanous  * from.
7441d7c0054SEd Tanous  * @param sensorJson  The json object to fill
745adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
746adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
74708777fb0SLewanczyk, Dawid  */
7481d7c0054SEd Tanous inline void objectPropertiesToJson(
7491d7c0054SEd Tanous     std::string_view sensorName, std::string_view sensorType,
7501d7c0054SEd Tanous     std::string_view chassisSubNode,
7511d7c0054SEd Tanous     const dbus::utility::DBusPropertiesMap& propertiesDict,
75281ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
7531abe55efSEd Tanous {
7541d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
755adc4f0dbSShawn McCarney     {
756c71d6125SEd Tanous         std::string subNodeEscaped(sensorType);
7573544d2a7SEd Tanous         auto remove = std::ranges::remove(subNodeEscaped, '_');
7583544d2a7SEd Tanous         subNodeEscaped.erase(std::ranges::begin(remove), subNodeEscaped.end());
759c1d019a6SEd Tanous 
760c1d019a6SEd Tanous         // For sensors in SensorCollection we set Id instead of MemberId,
761c1d019a6SEd Tanous         // including power sensors.
762c1d019a6SEd Tanous         subNodeEscaped += '_';
763c1d019a6SEd Tanous         subNodeEscaped += sensorName;
764c1d019a6SEd Tanous         sensorJson["Id"] = std::move(subNodeEscaped);
765c1d019a6SEd Tanous 
7661d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7671d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7681d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
76995a3ecadSAnthony Wilson     }
77095a3ecadSAnthony Wilson     else if (sensorType != "power")
77195a3ecadSAnthony Wilson     {
77295a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
77395a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
77495a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
7751d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7761d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7771d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
778adc4f0dbSShawn McCarney     }
779e742b6ccSEd Tanous 
780aaf08ac7SMatt Simmering     const bool* checkAvailable = nullptr;
781aaf08ac7SMatt Simmering     bool available = true;
782aaf08ac7SMatt Simmering     const bool success = sdbusplus::unpackPropertiesNoThrow(
783aaf08ac7SMatt Simmering         dbus_utils::UnpackErrorPrinter(), propertiesDict, "Available",
784aaf08ac7SMatt Simmering         checkAvailable);
785aaf08ac7SMatt Simmering     if (!success)
786aaf08ac7SMatt Simmering     {
787aaf08ac7SMatt Simmering         messages::internalError();
788aaf08ac7SMatt Simmering     }
789aaf08ac7SMatt Simmering     if (checkAvailable != nullptr)
790aaf08ac7SMatt Simmering     {
791aaf08ac7SMatt Simmering         available = *checkAvailable;
792aaf08ac7SMatt Simmering     }
793aaf08ac7SMatt Simmering 
794aaf08ac7SMatt Simmering     sensorJson["Status"]["State"] = getState(inventoryItem, available);
795*bd79bce8SPatrick Williams     sensorJson["Status"]["Health"] =
796*bd79bce8SPatrick Williams         getHealth(sensorJson, propertiesDict, inventoryItem);
79708777fb0SLewanczyk, Dawid 
79808777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
79908777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
80008777fb0SLewanczyk, Dawid     // that require integers, not floats.
80108777fb0SLewanczyk, Dawid     bool forceToInt = false;
80208777fb0SLewanczyk, Dawid 
8033929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
8041d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
80595a3ecadSAnthony Wilson     {
8062a4ba195SShounak Mitra         sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
807c2bf7f99SWludzik, Jozef 
8080ec8b83dSEd Tanous         sensor::ReadingType readingType = sensors::toReadingType(sensorType);
8090ec8b83dSEd Tanous         if (readingType == sensor::ReadingType::Invalid)
81095a3ecadSAnthony Wilson         {
81162598e31SEd Tanous             BMCWEB_LOG_ERROR("Redfish cannot map reading type for {}",
81262598e31SEd Tanous                              sensorType);
81395a3ecadSAnthony Wilson         }
814c2bf7f99SWludzik, Jozef         else
81595a3ecadSAnthony Wilson         {
816c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
81795a3ecadSAnthony Wilson         }
818c2bf7f99SWludzik, Jozef 
8191d7c0054SEd Tanous         std::string_view readingUnits = sensors::toReadingUnits(sensorType);
820c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
821f8ede15eSAdrian Ambrożewicz         {
82262598e31SEd Tanous             BMCWEB_LOG_ERROR("Redfish cannot map reading unit for {}",
82362598e31SEd Tanous                              sensorType);
824c2bf7f99SWludzik, Jozef         }
825c2bf7f99SWludzik, Jozef         else
826c2bf7f99SWludzik, Jozef         {
827c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
828f8ede15eSAdrian Ambrożewicz         }
82995a3ecadSAnthony Wilson     }
83095a3ecadSAnthony Wilson     else if (sensorType == "temperature")
8311abe55efSEd Tanous     {
8323929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
83381ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
83408777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
83508777fb0SLewanczyk, Dawid         // implementation seems to implement fan
8361abe55efSEd Tanous     }
8371abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
8381abe55efSEd Tanous     {
8393929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
840539d8c6bSEd Tanous         sensorJson["ReadingUnits"] = thermal::ReadingUnits::RPM;
84181ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
84281ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
84308777fb0SLewanczyk, Dawid         forceToInt = true;
8441abe55efSEd Tanous     }
8456f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
8466f6d0d32SEd Tanous     {
8473929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
848539d8c6bSEd Tanous         sensorJson["ReadingUnits"] = thermal::ReadingUnits::Percent;
84981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
85081ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
8516f6d0d32SEd Tanous         forceToInt = true;
8526f6d0d32SEd Tanous     }
8531abe55efSEd Tanous     else if (sensorType == "voltage")
8541abe55efSEd Tanous     {
8553929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
85681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
8571abe55efSEd Tanous     }
8582474adfaSEd Tanous     else if (sensorType == "power")
8592474adfaSEd Tanous     {
86018f8f608SEd Tanous         std::string lower;
86118f8f608SEd Tanous         std::ranges::transform(sensorName, std::back_inserter(lower),
86218f8f608SEd Tanous                                bmcweb::asciiToLower);
86318f8f608SEd Tanous         if (lower == "total_power")
864028f7ebcSEddie James         {
86581ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
8667ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
8677ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
86881ce609eSEd Tanous             sensorJson["MemberId"] = "0";
86981ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
8703929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
871028f7ebcSEddie James         }
87218f8f608SEd Tanous         else if (lower.find("input") != std::string::npos)
87349c53ac9SJohnathan Mantey         {
8743929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
87549c53ac9SJohnathan Mantey         }
87649c53ac9SJohnathan Mantey         else
87749c53ac9SJohnathan Mantey         {
8783929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
87949c53ac9SJohnathan Mantey         }
8802474adfaSEd Tanous     }
8811abe55efSEd Tanous     else
8821abe55efSEd Tanous     {
88362598e31SEd Tanous         BMCWEB_LOG_ERROR("Redfish cannot map object type for {}", sensorName);
88408777fb0SLewanczyk, Dawid         return;
88508777fb0SLewanczyk, Dawid     }
88608777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
8873929aca1SAnthony Wilson     std::vector<
8883929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
8893929aca1SAnthony Wilson         properties;
89008777fb0SLewanczyk, Dawid     properties.reserve(7);
89108777fb0SLewanczyk, Dawid 
89208777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
893de629b6eSShawn McCarney 
8941d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
8953929aca1SAnthony Wilson     {
8963929aca1SAnthony Wilson         properties.emplace_back(
8973929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
8983929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
8993929aca1SAnthony Wilson         properties.emplace_back(
9003929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
9013929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
9023929aca1SAnthony Wilson         properties.emplace_back(
9033929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
9043929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
9053929aca1SAnthony Wilson         properties.emplace_back(
9063929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
9073929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
9083929aca1SAnthony Wilson     }
9093929aca1SAnthony Wilson     else if (sensorType != "power")
910de629b6eSShawn McCarney     {
91108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
9123929aca1SAnthony Wilson                                 "WarningHigh",
9133929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
91408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
9153929aca1SAnthony Wilson                                 "WarningLow",
9163929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
91708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
9183929aca1SAnthony Wilson                                 "CriticalHigh",
9193929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
92008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
9213929aca1SAnthony Wilson                                 "CriticalLow",
9223929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
923de629b6eSShawn McCarney     }
92408777fb0SLewanczyk, Dawid 
9252474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
9262474adfaSEd Tanous 
9271d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
92895a3ecadSAnthony Wilson     {
92995a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9303929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
93195a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9323929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
93351c35a8fSGeorge Liu         properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
93451c35a8fSGeorge Liu                                 "Accuracy", "/Accuracy"_json_pointer);
93595a3ecadSAnthony Wilson     }
93695a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9371abe55efSEd Tanous     {
93808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9393929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
94008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9413929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
9421abe55efSEd Tanous     }
943adc4f0dbSShawn McCarney     else if (sensorType != "power")
9441abe55efSEd Tanous     {
94508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9463929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
94708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9483929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
94908777fb0SLewanczyk, Dawid     }
95008777fb0SLewanczyk, Dawid 
9513929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
9523929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
9531abe55efSEd Tanous     {
9541d7c0054SEd Tanous         for (const auto& [valueName, valueVariant] : propertiesDict)
955711ac7a9SEd Tanous         {
956711ac7a9SEd Tanous             if (valueName != std::get<1>(p))
957711ac7a9SEd Tanous             {
958711ac7a9SEd Tanous                 continue;
959711ac7a9SEd Tanous             }
9603929aca1SAnthony Wilson 
9613929aca1SAnthony Wilson             // The property we want to set may be nested json, so use
9623929aca1SAnthony Wilson             // a json_pointer for easy indexing into the json structure.
9633929aca1SAnthony Wilson             const nlohmann::json::json_pointer& key = std::get<2>(p);
9643929aca1SAnthony Wilson 
965abf2add6SEd Tanous             const double* doubleValue = std::get_if<double>(&valueVariant);
96640e4f380SEd Tanous             if (doubleValue == nullptr)
9671abe55efSEd Tanous             {
96862598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got value interface that wasn't double");
9696f6d0d32SEd Tanous                 continue;
97008777fb0SLewanczyk, Dawid             }
971283860f5SEd Tanous             if (!std::isfinite(*doubleValue))
972283860f5SEd Tanous             {
973283860f5SEd Tanous                 if (valueName == "Value")
974283860f5SEd Tanous                 {
975283860f5SEd Tanous                     // Readings are allowed to be NAN for unavailable;  coerce
976283860f5SEd Tanous                     // them to null in the json response.
977283860f5SEd Tanous                     sensorJson[key] = nullptr;
978283860f5SEd Tanous                     continue;
979283860f5SEd Tanous                 }
98062598e31SEd Tanous                 BMCWEB_LOG_WARNING("Sensor value for {} was unexpectedly {}",
98162598e31SEd Tanous                                    valueName, *doubleValue);
982283860f5SEd Tanous                 continue;
983283860f5SEd Tanous             }
9846f6d0d32SEd Tanous             if (forceToInt)
9856f6d0d32SEd Tanous             {
98640e4f380SEd Tanous                 sensorJson[key] = static_cast<int64_t>(*doubleValue);
9876f6d0d32SEd Tanous             }
9886f6d0d32SEd Tanous             else
9896f6d0d32SEd Tanous             {
99040e4f380SEd Tanous                 sensorJson[key] = *doubleValue;
99108777fb0SLewanczyk, Dawid             }
99208777fb0SLewanczyk, Dawid         }
99308777fb0SLewanczyk, Dawid     }
99408777fb0SLewanczyk, Dawid }
99508777fb0SLewanczyk, Dawid 
9961d7c0054SEd Tanous /**
9971d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
9981d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
9991d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
10001d7c0054SEd Tanous  * build
10018ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
10021d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
10031d7c0054SEd Tanous  * interfaces to be built from
10041d7c0054SEd Tanous  * @param sensorJson  The json object to fill
10051d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
10061d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
10071d7c0054SEd Tanous  */
10081d7c0054SEd Tanous inline void objectInterfacesToJson(
10091d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
10101d7c0054SEd Tanous     const std::string& chassisSubNode,
101180f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict,
10121d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
10131d7c0054SEd Tanous {
10141d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
10151d7c0054SEd Tanous     {
10161d7c0054SEd Tanous         objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
10171d7c0054SEd Tanous                                valuesDict, sensorJson, inventoryItem);
10181d7c0054SEd Tanous     }
101962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
10201d7c0054SEd Tanous }
10211d7c0054SEd Tanous 
1022b5a76932SEd Tanous inline void populateFanRedundancy(
1023b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
10248bd25ccdSJames Feist {
1025e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1026e99073f5SGeorge Liu         "xyz.openbmc_project.Control.FanRedundancy"};
1027e99073f5SGeorge Liu     dbus::utility::getSubTree(
1028e99073f5SGeorge Liu         "/xyz/openbmc_project/control", 2, interfaces,
1029b9d36b47SEd Tanous         [sensorsAsyncResp](
1030e99073f5SGeorge Liu             const boost::system::error_code& ec,
1031b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
10328bd25ccdSJames Feist             if (ec)
10338bd25ccdSJames Feist             {
10348bd25ccdSJames Feist                 return; // don't have to have this interface
10358bd25ccdSJames Feist             }
10366c3e9451SGeorge Liu             for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
1037e278c18fSEd Tanous                      pathPair : resp)
10388bd25ccdSJames Feist             {
1039e278c18fSEd Tanous                 const std::string& path = pathPair.first;
1040*bd79bce8SPatrick Williams                 const dbus::utility::MapperServiceMap& objDict =
1041*bd79bce8SPatrick Williams                     pathPair.second;
10428bd25ccdSJames Feist                 if (objDict.empty())
10438bd25ccdSJames Feist                 {
10448bd25ccdSJames Feist                     continue; // this should be impossible
10458bd25ccdSJames Feist                 }
10468bd25ccdSJames Feist 
10478bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
10486c3e9451SGeorge Liu                 dbus::utility::getAssociationEndPoints(
10496c3e9451SGeorge Liu                     path + "/chassis",
10506c3e9451SGeorge Liu                     [path, owner, sensorsAsyncResp](
10518b24275dSEd Tanous                         const boost::system::error_code& ec2,
10526c3e9451SGeorge Liu                         const dbus::utility::MapperEndPoints& endpoints) {
10538b24275dSEd Tanous                         if (ec2)
10548bd25ccdSJames Feist                         {
10558bd25ccdSJames Feist                             return; // if they don't have an association we
10568bd25ccdSJames Feist                                     // can't tell what chassis is
10578bd25ccdSJames Feist                         }
10583544d2a7SEd Tanous                         auto found = std::ranges::find_if(
1059*bd79bce8SPatrick Williams                             endpoints,
1060*bd79bce8SPatrick Williams                             [sensorsAsyncResp](const std::string& entry) {
1061*bd79bce8SPatrick Williams                                 return entry.find(
1062*bd79bce8SPatrick Williams                                            sensorsAsyncResp->chassisId) !=
10638bd25ccdSJames Feist                                        std::string::npos;
10648bd25ccdSJames Feist                             });
10658bd25ccdSJames Feist 
10661e1e598dSJonathan Doman                         if (found == endpoints.end())
10678bd25ccdSJames Feist                         {
10688bd25ccdSJames Feist                             return;
10698bd25ccdSJames Feist                         }
107086d89ed7SKrzysztof Grobelny                         sdbusplus::asio::getAllProperties(
107186d89ed7SKrzysztof Grobelny                             *crow::connections::systemBus, owner, path,
107286d89ed7SKrzysztof Grobelny                             "xyz.openbmc_project.Control.FanRedundancy",
10738bd25ccdSJames Feist                             [path, sensorsAsyncResp](
10748b24275dSEd Tanous                                 const boost::system::error_code& ec3,
107586d89ed7SKrzysztof Grobelny                                 const dbus::utility::DBusPropertiesMap& ret) {
10768b24275dSEd Tanous                                 if (ec3)
10778bd25ccdSJames Feist                                 {
10788bd25ccdSJames Feist                                     return; // don't have to have this
10798bd25ccdSJames Feist                                             // interface
10808bd25ccdSJames Feist                                 }
10818bd25ccdSJames Feist 
108286d89ed7SKrzysztof Grobelny                                 const uint8_t* allowedFailures = nullptr;
1083*bd79bce8SPatrick Williams                                 const std::vector<std::string>* collection =
1084*bd79bce8SPatrick Williams                                     nullptr;
108586d89ed7SKrzysztof Grobelny                                 const std::string* status = nullptr;
108686d89ed7SKrzysztof Grobelny 
1087*bd79bce8SPatrick Williams                                 const bool success =
1088*bd79bce8SPatrick Williams                                     sdbusplus::unpackPropertiesNoThrow(
108986d89ed7SKrzysztof Grobelny                                         dbus_utils::UnpackErrorPrinter(), ret,
1090*bd79bce8SPatrick Williams                                         "AllowedFailures", allowedFailures,
1091*bd79bce8SPatrick Williams                                         "Collection", collection, "Status",
1092*bd79bce8SPatrick Williams                                         status);
109386d89ed7SKrzysztof Grobelny 
109486d89ed7SKrzysztof Grobelny                                 if (!success)
109586d89ed7SKrzysztof Grobelny                                 {
109686d89ed7SKrzysztof Grobelny                                     messages::internalError(
109786d89ed7SKrzysztof Grobelny                                         sensorsAsyncResp->asyncResp->res);
109886d89ed7SKrzysztof Grobelny                                     return;
109986d89ed7SKrzysztof Grobelny                                 }
110086d89ed7SKrzysztof Grobelny 
1101*bd79bce8SPatrick Williams                                 if (allowedFailures == nullptr ||
1102*bd79bce8SPatrick Williams                                     collection == nullptr || status == nullptr)
11038bd25ccdSJames Feist                                 {
1104*bd79bce8SPatrick Williams                                     BMCWEB_LOG_ERROR(
1105*bd79bce8SPatrick Williams                                         "Invalid redundancy interface");
11068bd25ccdSJames Feist                                     messages::internalError(
11078d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
11088bd25ccdSJames Feist                                     return;
11098bd25ccdSJames Feist                                 }
11108bd25ccdSJames Feist 
1111*bd79bce8SPatrick Williams                                 sdbusplus::message::object_path objectPath(
1112*bd79bce8SPatrick Williams                                     path);
111328aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
111428aa8de5SGeorge Liu                                 if (name.empty())
11158bd25ccdSJames Feist                                 {
11168bd25ccdSJames Feist                                     // this should be impossible
11178bd25ccdSJames Feist                                     messages::internalError(
11188d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
11198bd25ccdSJames Feist                                     return;
11208bd25ccdSJames Feist                                 }
112118f8f608SEd Tanous                                 std::ranges::replace(name, '_', ' ');
11228bd25ccdSJames Feist 
11238bd25ccdSJames Feist                                 std::string health;
11248bd25ccdSJames Feist 
112511ba3979SEd Tanous                                 if (status->ends_with("Full"))
11268bd25ccdSJames Feist                                 {
11278bd25ccdSJames Feist                                     health = "OK";
11288bd25ccdSJames Feist                                 }
112911ba3979SEd Tanous                                 else if (status->ends_with("Degraded"))
11308bd25ccdSJames Feist                                 {
11318bd25ccdSJames Feist                                     health = "Warning";
11328bd25ccdSJames Feist                                 }
11338bd25ccdSJames Feist                                 else
11348bd25ccdSJames Feist                                 {
11358bd25ccdSJames Feist                                     health = "Critical";
11368bd25ccdSJames Feist                                 }
11371476687dSEd Tanous                                 nlohmann::json::array_t redfishCollection;
11388bd25ccdSJames Feist                                 const auto& fanRedfish =
1139*bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
1140*bd79bce8SPatrick Williams                                         .jsonValue["Fans"];
11418bd25ccdSJames Feist                                 for (const std::string& item : *collection)
11428bd25ccdSJames Feist                                 {
1143*bd79bce8SPatrick Williams                                     sdbusplus::message::object_path itemPath(
1144*bd79bce8SPatrick Williams                                         item);
11458a592810SEd Tanous                                     std::string itemName = itemPath.filename();
114628aa8de5SGeorge Liu                                     if (itemName.empty())
114728aa8de5SGeorge Liu                                     {
114828aa8de5SGeorge Liu                                         continue;
114928aa8de5SGeorge Liu                                     }
11508bd25ccdSJames Feist                                     /*
11518bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
11528bd25ccdSJames Feist                                     std::replace(itemName.begin(),
11538bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
11543544d2a7SEd Tanous                                     auto schemaItem = std::ranges::find_if(
1155*bd79bce8SPatrick Williams                                         fanRedfish,
1156*bd79bce8SPatrick Williams                                         [itemName](const nlohmann::json& fan) {
11573e35c761SGeorge Liu                                             return fan["Name"] == itemName;
11588bd25ccdSJames Feist                                         });
11598bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
11608bd25ccdSJames Feist                                     {
11618a592810SEd Tanous                                         nlohmann::json::object_t collectionId;
11628a592810SEd Tanous                                         collectionId["@odata.id"] =
11631476687dSEd Tanous                                             (*schemaItem)["@odata.id"];
11641476687dSEd Tanous                                         redfishCollection.emplace_back(
11658a592810SEd Tanous                                             std::move(collectionId));
11668bd25ccdSJames Feist                                     }
11678bd25ccdSJames Feist                                     else
11688bd25ccdSJames Feist                                     {
1169*bd79bce8SPatrick Williams                                         BMCWEB_LOG_ERROR(
1170*bd79bce8SPatrick Williams                                             "failed to find fan in schema");
11718bd25ccdSJames Feist                                         messages::internalError(
11728d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
11738bd25ccdSJames Feist                                         return;
11748bd25ccdSJames Feist                                     }
11758bd25ccdSJames Feist                                 }
11768bd25ccdSJames Feist 
1177*bd79bce8SPatrick Williams                                 size_t minNumNeeded =
1178*bd79bce8SPatrick Williams                                     collection->empty()
117926f6976fSEd Tanous                                         ? 0
1180*bd79bce8SPatrick Williams                                         : collection->size() - *allowedFailures;
1181*bd79bce8SPatrick Williams                                 nlohmann::json& jResp =
1182*bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
11838bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
11841476687dSEd Tanous 
11851476687dSEd Tanous                                 nlohmann::json::object_t redundancy;
1186*bd79bce8SPatrick Williams                                 boost::urls::url url = boost::urls::format(
1187*bd79bce8SPatrick Williams                                     "/redfish/v1/Chassis/{}/{}",
1188ef4c65b7SEd Tanous                                     sensorsAsyncResp->chassisId,
1189eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
1190*bd79bce8SPatrick Williams                                 url.set_fragment(
1191*bd79bce8SPatrick Williams                                     ("/Redundancy"_json_pointer / jResp.size())
1192eddfc437SWilly Tu                                         .to_string());
1193eddfc437SWilly Tu                                 redundancy["@odata.id"] = std::move(url);
1194*bd79bce8SPatrick Williams                                 redundancy["@odata.type"] =
1195*bd79bce8SPatrick Williams                                     "#Redundancy.v1_3_2.Redundancy";
11961476687dSEd Tanous                                 redundancy["MinNumNeeded"] = minNumNeeded;
1197*bd79bce8SPatrick Williams                                 redundancy["Mode"] =
1198*bd79bce8SPatrick Williams                                     redundancy::RedundancyType::NPlusM;
11991476687dSEd Tanous                                 redundancy["Name"] = name;
12001476687dSEd Tanous                                 redundancy["RedundancySet"] = redfishCollection;
12011476687dSEd Tanous                                 redundancy["Status"]["Health"] = health;
1202*bd79bce8SPatrick Williams                                 redundancy["Status"]["State"] =
1203*bd79bce8SPatrick Williams                                     resource::State::Enabled;
12041476687dSEd Tanous 
1205b2ba3072SPatrick Williams                                 jResp.emplace_back(std::move(redundancy));
120686d89ed7SKrzysztof Grobelny                             });
12071e1e598dSJonathan Doman                     });
12088bd25ccdSJames Feist             }
1209e99073f5SGeorge Liu         });
12108bd25ccdSJames Feist }
12118bd25ccdSJames Feist 
1212b5a76932SEd Tanous inline void
121381ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
121449c53ac9SJohnathan Mantey {
12158d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
121649c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
121781ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
121849c53ac9SJohnathan Mantey     {
121949c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
122049c53ac9SJohnathan Mantey     }
122149c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
122249c53ac9SJohnathan Mantey     {
122349c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
122449c53ac9SJohnathan Mantey         if (entry != response.end())
122549c53ac9SJohnathan Mantey         {
122649c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
122702cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
122849c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
122949c53ac9SJohnathan Mantey                       });
123049c53ac9SJohnathan Mantey 
123149c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
123249c53ac9SJohnathan Mantey             size_t count = 0;
123349c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
123449c53ac9SJohnathan Mantey             {
123549c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
123649c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
123749c53ac9SJohnathan Mantey                 {
123849c53ac9SJohnathan Mantey                     continue;
123949c53ac9SJohnathan Mantey                 }
124049c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
124149c53ac9SJohnathan Mantey                 if (value != nullptr)
124249c53ac9SJohnathan Mantey                 {
1243eddfc437SWilly Tu                     *value += "/" + std::to_string(count);
12443e35c761SGeorge Liu                     sensorJson["MemberId"] = std::to_string(count);
124549c53ac9SJohnathan Mantey                     count++;
124681ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
124749c53ac9SJohnathan Mantey                 }
124849c53ac9SJohnathan Mantey             }
124949c53ac9SJohnathan Mantey         }
125049c53ac9SJohnathan Mantey     }
125149c53ac9SJohnathan Mantey }
125249c53ac9SJohnathan Mantey 
125308777fb0SLewanczyk, Dawid /**
1254adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1255adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1256adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1257adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12588fb49dd6SShawn McCarney  */
125923a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1260b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1261adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
12628fb49dd6SShawn McCarney {
1263adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
12648fb49dd6SShawn McCarney     {
1265adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
12668fb49dd6SShawn McCarney         {
1267adc4f0dbSShawn McCarney             return &inventoryItem;
12688fb49dd6SShawn McCarney         }
12698fb49dd6SShawn McCarney     }
12708fb49dd6SShawn McCarney     return nullptr;
12718fb49dd6SShawn McCarney }
12728fb49dd6SShawn McCarney 
12738fb49dd6SShawn McCarney /**
1274adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1275adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1276adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1277adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12788fb49dd6SShawn McCarney  */
127923a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1280b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1281adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1282adc4f0dbSShawn McCarney {
1283adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1284adc4f0dbSShawn McCarney     {
1285db0d36efSEd Tanous         if (inventoryItem.sensors.contains(sensorObjPath))
1286adc4f0dbSShawn McCarney         {
1287adc4f0dbSShawn McCarney             return &inventoryItem;
1288adc4f0dbSShawn McCarney         }
1289adc4f0dbSShawn McCarney     }
1290adc4f0dbSShawn McCarney     return nullptr;
1291adc4f0dbSShawn McCarney }
1292adc4f0dbSShawn McCarney 
1293adc4f0dbSShawn McCarney /**
1294d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1295d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1296d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1297d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1298d500549bSAnthony Wilson  */
1299*bd79bce8SPatrick Williams inline InventoryItem* findInventoryItemForLed(
1300*bd79bce8SPatrick Williams     std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath)
1301d500549bSAnthony Wilson {
1302d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1303d500549bSAnthony Wilson     {
1304d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1305d500549bSAnthony Wilson         {
1306d500549bSAnthony Wilson             return &inventoryItem;
1307d500549bSAnthony Wilson         }
1308d500549bSAnthony Wilson     }
1309d500549bSAnthony Wilson     return nullptr;
1310d500549bSAnthony Wilson }
1311d500549bSAnthony Wilson 
1312d500549bSAnthony Wilson /**
1313adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1314adc4f0dbSShawn McCarney  *
1315adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1316adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1317adc4f0dbSShawn McCarney  * added to the vector.
1318adc4f0dbSShawn McCarney  *
1319adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1320adc4f0dbSShawn McCarney  * InventoryItem.
1321adc4f0dbSShawn McCarney  *
1322adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1323adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1324adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1325adc4f0dbSShawn McCarney  */
1326b5a76932SEd Tanous inline void addInventoryItem(
1327b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1328b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1329adc4f0dbSShawn McCarney {
1330adc4f0dbSShawn McCarney     // Look for inventory item in vector
1331*bd79bce8SPatrick Williams     InventoryItem* inventoryItem =
1332*bd79bce8SPatrick Williams         findInventoryItem(inventoryItems, invItemObjPath);
1333adc4f0dbSShawn McCarney 
1334adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1335adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1336adc4f0dbSShawn McCarney     {
1337adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1338adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1339adc4f0dbSShawn McCarney     }
1340adc4f0dbSShawn McCarney 
1341adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1342adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1343adc4f0dbSShawn McCarney }
1344adc4f0dbSShawn McCarney 
1345adc4f0dbSShawn McCarney /**
1346adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1347adc4f0dbSShawn McCarney  *
1348adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1349adc4f0dbSShawn McCarney  * specified InventoryItem.
1350adc4f0dbSShawn McCarney  *
1351adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1352adc4f0dbSShawn McCarney  * response.
1353adc4f0dbSShawn McCarney  *
1354adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1355adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1356adc4f0dbSShawn McCarney  * for the specified inventory item.
1357adc4f0dbSShawn McCarney  */
135823a21a1cSEd Tanous inline void storeInventoryItemData(
1359adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
136080f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict)
13618fb49dd6SShawn McCarney {
1362adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1363711ac7a9SEd Tanous 
13649eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
13658fb49dd6SShawn McCarney     {
1366711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
13678fb49dd6SShawn McCarney         {
13689eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1369711ac7a9SEd Tanous             {
1370711ac7a9SEd Tanous                 if (name == "Present")
1371711ac7a9SEd Tanous                 {
1372711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1373adc4f0dbSShawn McCarney                     if (value != nullptr)
13748fb49dd6SShawn McCarney                     {
1375adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
13768fb49dd6SShawn McCarney                     }
13778fb49dd6SShawn McCarney                 }
13788fb49dd6SShawn McCarney             }
1379711ac7a9SEd Tanous         }
1380adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1381711ac7a9SEd Tanous 
1382711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
13838fb49dd6SShawn McCarney         {
1384adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
13858fb49dd6SShawn McCarney         }
1386adc4f0dbSShawn McCarney 
1387adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1388711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1389adc4f0dbSShawn McCarney         {
13909eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1391711ac7a9SEd Tanous             {
1392711ac7a9SEd Tanous                 if (name == "Manufacturer")
1393adc4f0dbSShawn McCarney                 {
1394adc4f0dbSShawn McCarney                     const std::string* value =
1395711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1396adc4f0dbSShawn McCarney                     if (value != nullptr)
1397adc4f0dbSShawn McCarney                     {
1398adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1399adc4f0dbSShawn McCarney                     }
1400adc4f0dbSShawn McCarney                 }
1401711ac7a9SEd Tanous                 if (name == "Model")
1402adc4f0dbSShawn McCarney                 {
1403adc4f0dbSShawn McCarney                     const std::string* value =
1404711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1405adc4f0dbSShawn McCarney                     if (value != nullptr)
1406adc4f0dbSShawn McCarney                     {
1407adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1408adc4f0dbSShawn McCarney                     }
1409adc4f0dbSShawn McCarney                 }
1410711ac7a9SEd Tanous                 if (name == "SerialNumber")
1411adc4f0dbSShawn McCarney                 {
1412adc4f0dbSShawn McCarney                     const std::string* value =
1413711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1414adc4f0dbSShawn McCarney                     if (value != nullptr)
1415adc4f0dbSShawn McCarney                     {
1416adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1417adc4f0dbSShawn McCarney                     }
1418adc4f0dbSShawn McCarney                 }
1419711ac7a9SEd Tanous                 if (name == "PartNumber")
1420711ac7a9SEd Tanous                 {
1421711ac7a9SEd Tanous                     const std::string* value =
1422711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1423711ac7a9SEd Tanous                     if (value != nullptr)
1424711ac7a9SEd Tanous                     {
1425711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1426711ac7a9SEd Tanous                     }
1427711ac7a9SEd Tanous                 }
1428711ac7a9SEd Tanous             }
1429adc4f0dbSShawn McCarney         }
1430adc4f0dbSShawn McCarney 
1431711ac7a9SEd Tanous         if (interface ==
1432711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1433adc4f0dbSShawn McCarney         {
14349eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1435adc4f0dbSShawn McCarney             {
1436711ac7a9SEd Tanous                 if (name == "Functional")
1437711ac7a9SEd Tanous                 {
1438711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1439adc4f0dbSShawn McCarney                     if (value != nullptr)
1440adc4f0dbSShawn McCarney                     {
1441adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
14428fb49dd6SShawn McCarney                     }
14438fb49dd6SShawn McCarney                 }
14448fb49dd6SShawn McCarney             }
14458fb49dd6SShawn McCarney         }
1446711ac7a9SEd Tanous     }
1447711ac7a9SEd Tanous }
14488fb49dd6SShawn McCarney 
14498fb49dd6SShawn McCarney /**
1450adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
14518fb49dd6SShawn McCarney  *
1452adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1453adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1454adc4f0dbSShawn McCarney  * inventoryItems vector.
14558fb49dd6SShawn McCarney  *
1456adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1457adc4f0dbSShawn McCarney  * response.
1458adc4f0dbSShawn McCarney  *
1459adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1460adc4f0dbSShawn McCarney  * been obtained.
1461adc4f0dbSShawn McCarney  *
1462adc4f0dbSShawn McCarney  * The callback must have the following signature:
1463adc4f0dbSShawn McCarney  *   @code
1464d500549bSAnthony Wilson  *   callback(void)
1465adc4f0dbSShawn McCarney  *   @endcode
1466adc4f0dbSShawn McCarney  *
1467adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1468adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1469adc4f0dbSShawn McCarney  * last asynchronous function has completed.
14708fb49dd6SShawn McCarney  *
14718fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1472adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1473adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
14748fb49dd6SShawn McCarney  * implements ObjectManager.
1475adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1476adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1477adc4f0dbSShawn McCarney  * in recursive calls to this function.
14788fb49dd6SShawn McCarney  */
1479adc4f0dbSShawn McCarney template <typename Callback>
1480adc4f0dbSShawn McCarney static void getInventoryItemsData(
14818fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1482adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1483d0090733SEd Tanous     std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1484d0090733SEd Tanous     size_t invConnectionsIndex = 0)
14858fb49dd6SShawn McCarney {
148662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
14878fb49dd6SShawn McCarney 
1488adc4f0dbSShawn McCarney     // If no more connections left, call callback
1489adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
14908fb49dd6SShawn McCarney     {
1491d500549bSAnthony Wilson         callback();
149262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
1493adc4f0dbSShawn McCarney         return;
1494adc4f0dbSShawn McCarney     }
1495adc4f0dbSShawn McCarney 
1496adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1497fe04d49cSNan Zhou     auto it = invConnections->begin();
1498fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1499adc4f0dbSShawn McCarney     if (it != invConnections->end())
1500adc4f0dbSShawn McCarney     {
1501adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1502adc4f0dbSShawn McCarney 
15035eb468daSGeorge Liu         // Get all object paths and their interfaces for current connection
15045eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
15055eb468daSGeorge Liu         dbus::utility::getManagedObjects(
15065eb468daSGeorge Liu             invConnection, path,
15075eb468daSGeorge Liu             [sensorsAsyncResp, inventoryItems, invConnections,
15088cb2c024SEd Tanous              callback = std::forward<Callback>(callback), invConnectionsIndex](
15095e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
151002cad96eSEd Tanous                 const dbus::utility::ManagedObjectType& resp) {
151162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
15128fb49dd6SShawn McCarney                 if (ec)
15138fb49dd6SShawn McCarney                 {
151462598e31SEd Tanous                     BMCWEB_LOG_ERROR(
151562598e31SEd Tanous                         "getInventoryItemsData respHandler DBus error {}", ec);
15168d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
15178fb49dd6SShawn McCarney                     return;
15188fb49dd6SShawn McCarney                 }
15198fb49dd6SShawn McCarney 
15208fb49dd6SShawn McCarney                 // Loop through returned object paths
15218fb49dd6SShawn McCarney                 for (const auto& objDictEntry : resp)
15228fb49dd6SShawn McCarney                 {
15238fb49dd6SShawn McCarney                     const std::string& objPath =
15248fb49dd6SShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
15258fb49dd6SShawn McCarney 
1526*bd79bce8SPatrick Williams                     // If this object path is one of the specified inventory
1527*bd79bce8SPatrick Williams                     // items
1528*bd79bce8SPatrick Williams                     InventoryItem* inventoryItem =
1529*bd79bce8SPatrick Williams                         findInventoryItem(inventoryItems, objPath);
1530adc4f0dbSShawn McCarney                     if (inventoryItem != nullptr)
15318fb49dd6SShawn McCarney                     {
1532adc4f0dbSShawn McCarney                         // Store inventory data in InventoryItem
1533*bd79bce8SPatrick Williams                         storeInventoryItemData(*inventoryItem,
1534*bd79bce8SPatrick Williams                                                objDictEntry.second);
15358fb49dd6SShawn McCarney                     }
15368fb49dd6SShawn McCarney                 }
15378fb49dd6SShawn McCarney 
1538adc4f0dbSShawn McCarney                 // Recurse to get inventory item data from next connection
1539adc4f0dbSShawn McCarney                 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1540d0090733SEd Tanous                                       invConnections, std::move(callback),
1541d0090733SEd Tanous                                       invConnectionsIndex + 1);
1542adc4f0dbSShawn McCarney 
154362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
15445eb468daSGeorge Liu             });
15458fb49dd6SShawn McCarney     }
15468fb49dd6SShawn McCarney 
154762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
15488fb49dd6SShawn McCarney }
15498fb49dd6SShawn McCarney 
15508fb49dd6SShawn McCarney /**
1551adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
15528fb49dd6SShawn McCarney  *
1553adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1554adc4f0dbSShawn McCarney  * items that are associated with sensors.
15558fb49dd6SShawn McCarney  *
15568fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
15578fb49dd6SShawn McCarney  * been obtained.
15588fb49dd6SShawn McCarney  *
15598fb49dd6SShawn McCarney  * The callback must have the following signature:
15608fb49dd6SShawn McCarney  *   @code
1561fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
15628fb49dd6SShawn McCarney  *   @endcode
15638fb49dd6SShawn McCarney  *
15648fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1565adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
15668fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
15678fb49dd6SShawn McCarney  */
15688fb49dd6SShawn McCarney template <typename Callback>
15698fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1570b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1571b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
15728fb49dd6SShawn McCarney     Callback&& callback)
15738fb49dd6SShawn McCarney {
157462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
15758fb49dd6SShawn McCarney 
15768fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1577e99073f5SGeorge Liu     constexpr std::array<std::string_view, 4> interfaces = {
15788fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1579adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1580adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
15818fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
15828fb49dd6SShawn McCarney 
1583e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1584e99073f5SGeorge Liu     dbus::utility::getSubTree(
1585e99073f5SGeorge Liu         path, 0, interfaces,
15868cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1587002d39b4SEd Tanous          inventoryItems](
1588e99073f5SGeorge Liu             const boost::system::error_code& ec,
1589002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1590e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
159162598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
15928fb49dd6SShawn McCarney             if (ec)
15938fb49dd6SShawn McCarney             {
15948d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
159562598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1596*bd79bce8SPatrick Williams                     "getInventoryItemsConnections respHandler DBus error {}",
1597*bd79bce8SPatrick Williams                     ec);
15988fb49dd6SShawn McCarney                 return;
15998fb49dd6SShawn McCarney             }
16008fb49dd6SShawn McCarney 
16018fb49dd6SShawn McCarney             // Make unique list of connections for desired inventory items
1602fe04d49cSNan Zhou             std::shared_ptr<std::set<std::string>> invConnections =
1603fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
16048fb49dd6SShawn McCarney 
16058fb49dd6SShawn McCarney             // Loop through objects from GetSubTree
1606*bd79bce8SPatrick Williams             for (const std::pair<std::string,
1607*bd79bce8SPatrick Williams                                  std::vector<std::pair<
1608*bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
16098fb49dd6SShawn McCarney                      object : subtree)
16108fb49dd6SShawn McCarney             {
1611adc4f0dbSShawn McCarney                 // Check if object path is one of the specified inventory items
16128fb49dd6SShawn McCarney                 const std::string& objPath = object.first;
1613adc4f0dbSShawn McCarney                 if (findInventoryItem(inventoryItems, objPath) != nullptr)
16148fb49dd6SShawn McCarney                 {
16158fb49dd6SShawn McCarney                     // Store all connections to inventory item
16168fb49dd6SShawn McCarney                     for (const std::pair<std::string, std::vector<std::string>>&
16178fb49dd6SShawn McCarney                              objData : object.second)
16188fb49dd6SShawn McCarney                     {
16198fb49dd6SShawn McCarney                         const std::string& invConnection = objData.first;
16208fb49dd6SShawn McCarney                         invConnections->insert(invConnection);
16218fb49dd6SShawn McCarney                     }
16228fb49dd6SShawn McCarney                 }
16238fb49dd6SShawn McCarney             }
1624d500549bSAnthony Wilson 
16258fb49dd6SShawn McCarney             callback(invConnections);
162662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1627e99073f5SGeorge Liu         });
162862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
16298fb49dd6SShawn McCarney }
16308fb49dd6SShawn McCarney 
16318fb49dd6SShawn McCarney /**
1632adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
16338fb49dd6SShawn McCarney  *
16348fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1635d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1636d500549bSAnthony Wilson  * their LEDs, if any.
16378fb49dd6SShawn McCarney  *
16388fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
16398fb49dd6SShawn McCarney  * has been obtained.
16408fb49dd6SShawn McCarney  *
16418fb49dd6SShawn McCarney  * The callback must have the following signature:
16428fb49dd6SShawn McCarney  *   @code
1643adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
16448fb49dd6SShawn McCarney  *   @endcode
16458fb49dd6SShawn McCarney  *
16468fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
16478fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
16488fb49dd6SShawn McCarney  * implements ObjectManager.
16498fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
16508fb49dd6SShawn McCarney  */
16518fb49dd6SShawn McCarney template <typename Callback>
1652adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1653b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1654fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
16558fb49dd6SShawn McCarney     Callback&& callback)
16568fb49dd6SShawn McCarney {
165762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
16588fb49dd6SShawn McCarney 
16595eb468daSGeorge Liu     // Call GetManagedObjects on the ObjectMapper to get all associations
16605eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
16615eb468daSGeorge Liu     dbus::utility::getManagedObjects(
16625eb468daSGeorge Liu         "xyz.openbmc_project.ObjectMapper", path,
16638cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
16645e7e2dc5SEd Tanous          sensorNames](const boost::system::error_code& ec,
166502cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
166662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
16678fb49dd6SShawn McCarney             if (ec)
16688fb49dd6SShawn McCarney             {
166962598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1670*bd79bce8SPatrick Williams                     "getInventoryItemAssociations respHandler DBus error {}",
1671*bd79bce8SPatrick Williams                     ec);
16728d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16738fb49dd6SShawn McCarney                 return;
16748fb49dd6SShawn McCarney             }
16758fb49dd6SShawn McCarney 
1676adc4f0dbSShawn McCarney             // Create vector to hold list of inventory items
1677adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1678adc4f0dbSShawn McCarney                 std::make_shared<std::vector<InventoryItem>>();
1679adc4f0dbSShawn McCarney 
16808fb49dd6SShawn McCarney             // Loop through returned object paths
16818fb49dd6SShawn McCarney             std::string sensorAssocPath;
16828fb49dd6SShawn McCarney             sensorAssocPath.reserve(128); // avoid memory allocations
16838fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16848fb49dd6SShawn McCarney             {
16858fb49dd6SShawn McCarney                 const std::string& objPath =
16868fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16878fb49dd6SShawn McCarney 
1688*bd79bce8SPatrick Williams                 // If path is inventory association for one of the specified
1689*bd79bce8SPatrick Williams                 // sensors
16908fb49dd6SShawn McCarney                 for (const std::string& sensorName : *sensorNames)
16918fb49dd6SShawn McCarney                 {
16928fb49dd6SShawn McCarney                     sensorAssocPath = sensorName;
16938fb49dd6SShawn McCarney                     sensorAssocPath += "/inventory";
16948fb49dd6SShawn McCarney                     if (objPath == sensorAssocPath)
16958fb49dd6SShawn McCarney                     {
16968fb49dd6SShawn McCarney                         // Get Association interface for object path
1697*bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1698*bd79bce8SPatrick Williams                              objDictEntry.second)
16998fb49dd6SShawn McCarney                         {
1700711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1701711ac7a9SEd Tanous                             {
1702711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1703711ac7a9SEd Tanous                                 {
1704711ac7a9SEd Tanous                                     if (valueName == "endpoints")
17058fb49dd6SShawn McCarney                                     {
1706*bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1707*bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1708*bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1709711ac7a9SEd Tanous                                                 &value);
1710711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1711711ac7a9SEd Tanous                                             !endpoints->empty())
17128fb49dd6SShawn McCarney                                         {
1713adc4f0dbSShawn McCarney                                             // Add inventory item to vector
1714adc4f0dbSShawn McCarney                                             const std::string& invItemPath =
1715adc4f0dbSShawn McCarney                                                 endpoints->front();
1716711ac7a9SEd Tanous                                             addInventoryItem(inventoryItems,
1717711ac7a9SEd Tanous                                                              invItemPath,
1718adc4f0dbSShawn McCarney                                                              sensorName);
17198fb49dd6SShawn McCarney                                         }
17208fb49dd6SShawn McCarney                                     }
17218fb49dd6SShawn McCarney                                 }
1722711ac7a9SEd Tanous                             }
1723711ac7a9SEd Tanous                         }
17248fb49dd6SShawn McCarney                         break;
17258fb49dd6SShawn McCarney                     }
17268fb49dd6SShawn McCarney                 }
17278fb49dd6SShawn McCarney             }
17288fb49dd6SShawn McCarney 
1729d500549bSAnthony Wilson             // Now loop through the returned object paths again, this time to
1730d500549bSAnthony Wilson             // find the leds associated with the inventory items we just found
1731d500549bSAnthony Wilson             std::string inventoryAssocPath;
1732d500549bSAnthony Wilson             inventoryAssocPath.reserve(128); // avoid memory allocations
1733d500549bSAnthony Wilson             for (const auto& objDictEntry : resp)
1734d500549bSAnthony Wilson             {
1735d500549bSAnthony Wilson                 const std::string& objPath =
1736d500549bSAnthony Wilson                     static_cast<const std::string&>(objDictEntry.first);
1737d500549bSAnthony Wilson 
1738d500549bSAnthony Wilson                 for (InventoryItem& inventoryItem : *inventoryItems)
1739d500549bSAnthony Wilson                 {
1740d500549bSAnthony Wilson                     inventoryAssocPath = inventoryItem.objectPath;
1741d500549bSAnthony Wilson                     inventoryAssocPath += "/leds";
1742d500549bSAnthony Wilson                     if (objPath == inventoryAssocPath)
1743d500549bSAnthony Wilson                     {
1744*bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1745*bd79bce8SPatrick Williams                              objDictEntry.second)
1746d500549bSAnthony Wilson                         {
1747711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1748711ac7a9SEd Tanous                             {
1749711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1750711ac7a9SEd Tanous                                 {
1751711ac7a9SEd Tanous                                     if (valueName == "endpoints")
1752d500549bSAnthony Wilson                                     {
1753*bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1754*bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1755*bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1756711ac7a9SEd Tanous                                                 &value);
1757711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1758711ac7a9SEd Tanous                                             !endpoints->empty())
1759d500549bSAnthony Wilson                                         {
1760711ac7a9SEd Tanous                                             // Add inventory item to vector
1761d500549bSAnthony Wilson                                             // Store LED path in inventory item
1762711ac7a9SEd Tanous                                             const std::string& ledPath =
1763711ac7a9SEd Tanous                                                 endpoints->front();
1764*bd79bce8SPatrick Williams                                             inventoryItem.ledObjectPath =
1765*bd79bce8SPatrick Williams                                                 ledPath;
1766d500549bSAnthony Wilson                                         }
1767d500549bSAnthony Wilson                                     }
1768d500549bSAnthony Wilson                                 }
1769711ac7a9SEd Tanous                             }
1770711ac7a9SEd Tanous                         }
1771711ac7a9SEd Tanous 
1772d500549bSAnthony Wilson                         break;
1773d500549bSAnthony Wilson                     }
1774d500549bSAnthony Wilson                 }
1775d500549bSAnthony Wilson             }
1776adc4f0dbSShawn McCarney             callback(inventoryItems);
177762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
17785eb468daSGeorge Liu         });
17798fb49dd6SShawn McCarney 
178062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
17818fb49dd6SShawn McCarney }
17828fb49dd6SShawn McCarney 
17838fb49dd6SShawn McCarney /**
1784d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1785d500549bSAnthony Wilson  *
1786d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1787d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1788d500549bSAnthony Wilson  * inventoryItems vector.
1789d500549bSAnthony Wilson  *
1790d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1791d500549bSAnthony Wilson  * response.
1792d500549bSAnthony Wilson  *
1793d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1794d500549bSAnthony Wilson  * has been obtained.
1795d500549bSAnthony Wilson  *
1796d500549bSAnthony Wilson  * The callback must have the following signature:
1797d500549bSAnthony Wilson  *   @code
179842cbe538SGunnar Mills  *   callback()
1799d500549bSAnthony Wilson  *   @endcode
1800d500549bSAnthony Wilson  *
1801d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1802d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1803d500549bSAnthony Wilson  * last asynchronous function has completed.
1804d500549bSAnthony Wilson  *
1805d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1806d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1807d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1808d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1809d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1810d500549bSAnthony Wilson  * in recursive calls to this function.
1811d500549bSAnthony Wilson  */
1812d500549bSAnthony Wilson template <typename Callback>
1813d500549bSAnthony Wilson void getInventoryLedData(
1814d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1815d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1816fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1817d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1818d500549bSAnthony Wilson {
181962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData enter");
1820d500549bSAnthony Wilson 
1821d500549bSAnthony Wilson     // If no more connections left, call callback
1822d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1823d500549bSAnthony Wilson     {
182442cbe538SGunnar Mills         callback();
182562598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1826d500549bSAnthony Wilson         return;
1827d500549bSAnthony Wilson     }
1828d500549bSAnthony Wilson 
1829d500549bSAnthony Wilson     // Get inventory item data from current connection
1830fe04d49cSNan Zhou     auto it = ledConnections->begin();
1831fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1832d500549bSAnthony Wilson     if (it != ledConnections->end())
1833d500549bSAnthony Wilson     {
1834d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1835d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1836d500549bSAnthony Wilson         // Response handler for Get State property
18371e1e598dSJonathan Doman         auto respHandler =
18381e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
18398cb2c024SEd Tanous              callback = std::forward<Callback>(callback), ledConnectionsIndex](
18405e7e2dc5SEd Tanous                 const boost::system::error_code& ec, const std::string& state) {
184162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1842d500549bSAnthony Wilson                 if (ec)
1843d500549bSAnthony Wilson                 {
184462598e31SEd Tanous                     BMCWEB_LOG_ERROR(
184562598e31SEd Tanous                         "getInventoryLedData respHandler DBus error {}", ec);
18468d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1847d500549bSAnthony Wilson                     return;
1848d500549bSAnthony Wilson                 }
1849d500549bSAnthony Wilson 
185062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Led state: {}", state);
1851d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1852d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1853d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1854d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1855d500549bSAnthony Wilson                 {
1856d500549bSAnthony Wilson                     // Store LED state in InventoryItem
185711ba3979SEd Tanous                     if (state.ends_with("On"))
1858d500549bSAnthony Wilson                     {
1859d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::ON;
1860d500549bSAnthony Wilson                     }
186111ba3979SEd Tanous                     else if (state.ends_with("Blink"))
1862d500549bSAnthony Wilson                     {
1863d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::BLINK;
1864d500549bSAnthony Wilson                     }
186511ba3979SEd Tanous                     else if (state.ends_with("Off"))
1866d500549bSAnthony Wilson                     {
1867d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::OFF;
1868d500549bSAnthony Wilson                     }
1869d500549bSAnthony Wilson                     else
1870d500549bSAnthony Wilson                     {
1871d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::UNKNOWN;
1872d500549bSAnthony Wilson                     }
1873d500549bSAnthony Wilson                 }
1874d500549bSAnthony Wilson 
1875d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1876d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1877d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1878d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1879d500549bSAnthony Wilson 
188062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1881d500549bSAnthony Wilson             };
1882d500549bSAnthony Wilson 
1883d500549bSAnthony Wilson         // Get the State property for the current LED
18841e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
18851e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
18861e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
18871e1e598dSJonathan Doman             std::move(respHandler));
1888d500549bSAnthony Wilson     }
1889d500549bSAnthony Wilson 
189062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1891d500549bSAnthony Wilson }
1892d500549bSAnthony Wilson 
1893d500549bSAnthony Wilson /**
1894d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1895d500549bSAnthony Wilson  *
1896d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1897d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1898d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1899d500549bSAnthony Wilson  *
1900d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1901d500549bSAnthony Wilson  * response.
1902d500549bSAnthony Wilson  *
1903d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1904d500549bSAnthony Wilson  * been obtained.
1905d500549bSAnthony Wilson  *
1906d500549bSAnthony Wilson  * The callback must have the following signature:
1907d500549bSAnthony Wilson  *   @code
190842cbe538SGunnar Mills  *   callback()
1909d500549bSAnthony Wilson  *   @endcode
1910d500549bSAnthony Wilson  *
1911d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1912d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1913d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1914d500549bSAnthony Wilson  */
1915d500549bSAnthony Wilson template <typename Callback>
1916d500549bSAnthony Wilson void getInventoryLeds(
1917d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1918d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1919d500549bSAnthony Wilson     Callback&& callback)
1920d500549bSAnthony Wilson {
192162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds enter");
1922d500549bSAnthony Wilson 
1923d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1924e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1925d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1926d500549bSAnthony Wilson 
1927e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1928e99073f5SGeorge Liu     dbus::utility::getSubTree(
1929e99073f5SGeorge Liu         path, 0, interfaces,
19308cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1931002d39b4SEd Tanous          inventoryItems](
1932e99073f5SGeorge Liu             const boost::system::error_code& ec,
1933002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1934e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
193562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1936d500549bSAnthony Wilson             if (ec)
1937d500549bSAnthony Wilson             {
19388d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1939*bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}",
1940*bd79bce8SPatrick Williams                                  ec);
1941d500549bSAnthony Wilson                 return;
1942d500549bSAnthony Wilson             }
1943d500549bSAnthony Wilson 
1944d500549bSAnthony Wilson             // Build map of LED object paths to connections
1945fe04d49cSNan Zhou             std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1946fe04d49cSNan Zhou                 std::make_shared<std::map<std::string, std::string>>();
1947d500549bSAnthony Wilson 
1948d500549bSAnthony Wilson             // Loop through objects from GetSubTree
1949*bd79bce8SPatrick Williams             for (const std::pair<std::string,
1950*bd79bce8SPatrick Williams                                  std::vector<std::pair<
1951*bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
1952d500549bSAnthony Wilson                      object : subtree)
1953d500549bSAnthony Wilson             {
1954*bd79bce8SPatrick Williams                 // Check if object path is LED for one of the specified
1955*bd79bce8SPatrick Williams                 // inventory items
1956d500549bSAnthony Wilson                 const std::string& ledPath = object.first;
1957*bd79bce8SPatrick Williams                 if (findInventoryItemForLed(*inventoryItems, ledPath) !=
1958*bd79bce8SPatrick Williams                     nullptr)
1959d500549bSAnthony Wilson                 {
1960d500549bSAnthony Wilson                     // Add mapping from ledPath to connection
1961*bd79bce8SPatrick Williams                     const std::string& connection =
1962*bd79bce8SPatrick Williams                         object.second.begin()->first;
1963d500549bSAnthony Wilson                     (*ledConnections)[ledPath] = connection;
1964*bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath,
1965*bd79bce8SPatrick Williams                                      connection);
1966d500549bSAnthony Wilson                 }
1967d500549bSAnthony Wilson             }
1968d500549bSAnthony Wilson 
1969*bd79bce8SPatrick Williams             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1970*bd79bce8SPatrick Williams                                 ledConnections, std::move(callback));
197162598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1972e99073f5SGeorge Liu         });
197362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds exit");
1974d500549bSAnthony Wilson }
1975d500549bSAnthony Wilson 
1976d500549bSAnthony Wilson /**
197742cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
197842cbe538SGunnar Mills  *
197942cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
198042cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
198142cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
198242cbe538SGunnar Mills  *
198342cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
198442cbe538SGunnar Mills  * response.
198542cbe538SGunnar Mills  *
198642cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
198742cbe538SGunnar Mills  * when data has been obtained.
198842cbe538SGunnar Mills  *
198942cbe538SGunnar Mills  * The callback must have the following signature:
199042cbe538SGunnar Mills  *   @code
199142cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
199242cbe538SGunnar Mills  *   @endcode
199342cbe538SGunnar Mills  *
199442cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
199542cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
199642cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
199742cbe538SGunnar Mills  *        Supply Attributes
199842cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
199942cbe538SGunnar Mills  */
200042cbe538SGunnar Mills template <typename Callback>
200142cbe538SGunnar Mills void getPowerSupplyAttributesData(
2002b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
200342cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2004fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
200542cbe538SGunnar Mills     Callback&& callback)
200642cbe538SGunnar Mills {
200762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
200842cbe538SGunnar Mills 
200942cbe538SGunnar Mills     if (psAttributesConnections.empty())
201042cbe538SGunnar Mills     {
201162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
201242cbe538SGunnar Mills         callback(inventoryItems);
201342cbe538SGunnar Mills         return;
201442cbe538SGunnar Mills     }
201542cbe538SGunnar Mills 
201642cbe538SGunnar Mills     // Assuming just one connection (service) for now
2017fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
201842cbe538SGunnar Mills 
201942cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
202042cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
202142cbe538SGunnar Mills 
202242cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
20235a39f77aSPatrick Williams     auto respHandler = [sensorsAsyncResp, inventoryItems,
20248cb2c024SEd Tanous                         callback = std::forward<Callback>(callback)](
20255a39f77aSPatrick Williams                            const boost::system::error_code& ec,
20265a39f77aSPatrick Williams                            const uint32_t value) {
202762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
202842cbe538SGunnar Mills         if (ec)
202942cbe538SGunnar Mills         {
203062598e31SEd Tanous             BMCWEB_LOG_ERROR(
203162598e31SEd Tanous                 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
20328d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
203342cbe538SGunnar Mills             return;
203442cbe538SGunnar Mills         }
203542cbe538SGunnar Mills 
203662598e31SEd Tanous         BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
203742cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
203842cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
203942cbe538SGunnar Mills         {
204055f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
204142cbe538SGunnar Mills             {
204242cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
20431e1e598dSJonathan Doman                     static_cast<int>(value);
204442cbe538SGunnar Mills             }
204542cbe538SGunnar Mills         }
204642cbe538SGunnar Mills 
204762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
204842cbe538SGunnar Mills         callback(inventoryItems);
204942cbe538SGunnar Mills     };
205042cbe538SGunnar Mills 
205142cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
205242cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
20531e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
20541e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
20551e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
20561e1e598dSJonathan Doman         std::move(respHandler));
205742cbe538SGunnar Mills 
205862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
205942cbe538SGunnar Mills }
206042cbe538SGunnar Mills 
206142cbe538SGunnar Mills /**
206242cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
206342cbe538SGunnar Mills  *
206442cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
206542cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
206642cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
206742cbe538SGunnar Mills  * item.
206842cbe538SGunnar Mills  *
206942cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
207042cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
207142cbe538SGunnar Mills  *
207242cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
207342cbe538SGunnar Mills  * when information has been obtained.
207442cbe538SGunnar Mills  *
207542cbe538SGunnar Mills  * The callback must have the following signature:
207642cbe538SGunnar Mills  *   @code
207742cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
207842cbe538SGunnar Mills  *   @endcode
207942cbe538SGunnar Mills  *
208042cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
208142cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
208242cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
208342cbe538SGunnar Mills  */
208442cbe538SGunnar Mills template <typename Callback>
208542cbe538SGunnar Mills void getPowerSupplyAttributes(
208642cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
208742cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
208842cbe538SGunnar Mills     Callback&& callback)
208942cbe538SGunnar Mills {
209062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
209142cbe538SGunnar Mills 
209242cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2093a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
209442cbe538SGunnar Mills     {
209562598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
209642cbe538SGunnar Mills         callback(inventoryItems);
209742cbe538SGunnar Mills         return;
209842cbe538SGunnar Mills     }
209942cbe538SGunnar Mills 
2100e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
210142cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
210242cbe538SGunnar Mills 
2103e99073f5SGeorge Liu     // Make call to ObjectMapper to find the PowerSupplyAttributes service
2104e99073f5SGeorge Liu     dbus::utility::getSubTree(
2105e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, interfaces,
21068cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
2107b9d36b47SEd Tanous          inventoryItems](
2108e99073f5SGeorge Liu             const boost::system::error_code& ec,
2109b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
2110e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
211162598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
211242cbe538SGunnar Mills             if (ec)
211342cbe538SGunnar Mills             {
21148d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
211562598e31SEd Tanous                 BMCWEB_LOG_ERROR(
211662598e31SEd Tanous                     "getPowerSupplyAttributes respHandler DBus error {}", ec);
211742cbe538SGunnar Mills                 return;
211842cbe538SGunnar Mills             }
211926f6976fSEd Tanous             if (subtree.empty())
212042cbe538SGunnar Mills             {
212162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
212242cbe538SGunnar Mills                 callback(inventoryItems);
212342cbe538SGunnar Mills                 return;
212442cbe538SGunnar Mills             }
212542cbe538SGunnar Mills 
212642cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
212742cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
212842cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
2129fe04d49cSNan Zhou             std::map<std::string, std::string> psAttributesConnections;
213042cbe538SGunnar Mills 
213142cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
213242cbe538SGunnar Mills             {
213362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
213442cbe538SGunnar Mills                 callback(inventoryItems);
213542cbe538SGunnar Mills                 return;
213642cbe538SGunnar Mills             }
213742cbe538SGunnar Mills 
213842cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
213942cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
214042cbe538SGunnar Mills 
214142cbe538SGunnar Mills             if (connection.empty())
214242cbe538SGunnar Mills             {
214362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
214442cbe538SGunnar Mills                 callback(inventoryItems);
214542cbe538SGunnar Mills                 return;
214642cbe538SGunnar Mills             }
214742cbe538SGunnar Mills 
214842cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
214962598e31SEd Tanous             BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
215062598e31SEd Tanous                              connection);
215142cbe538SGunnar Mills 
215242cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
215342cbe538SGunnar Mills                                          psAttributesConnections,
215442cbe538SGunnar Mills                                          std::move(callback));
215562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
2156e99073f5SGeorge Liu         });
215762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
215842cbe538SGunnar Mills }
215942cbe538SGunnar Mills 
216042cbe538SGunnar Mills /**
2161adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
21628fb49dd6SShawn McCarney  *
21638fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2164adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
21658fb49dd6SShawn McCarney  *
2166adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2167adc4f0dbSShawn McCarney  * response.
21688fb49dd6SShawn McCarney  *
2169adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2170adc4f0dbSShawn McCarney  * inventory items have been obtained.
2171adc4f0dbSShawn McCarney  *
2172adc4f0dbSShawn McCarney  * The callback must have the following signature:
2173adc4f0dbSShawn McCarney  *   @code
2174adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2175adc4f0dbSShawn McCarney  *   @endcode
21768fb49dd6SShawn McCarney  *
21778fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
21788fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
21798fb49dd6SShawn McCarney  * implements ObjectManager.
2180adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
21818fb49dd6SShawn McCarney  */
2182adc4f0dbSShawn McCarney template <typename Callback>
2183d0090733SEd Tanous static void
2184d0090733SEd Tanous     getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2185fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>> sensorNames,
2186adc4f0dbSShawn McCarney                       Callback&& callback)
21878fb49dd6SShawn McCarney {
218862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems enter");
2189adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
21908cb2c024SEd Tanous         [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
2191adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
219262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
21938fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2194d0090733SEd Tanous                 [sensorsAsyncResp, inventoryItems,
21958cb2c024SEd Tanous                  callback = std::forward<const Callback>(callback)](
2196fe04d49cSNan Zhou                     std::shared_ptr<std::set<std::string>> invConnections) {
219762598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
2198*bd79bce8SPatrick Williams                     auto getInventoryItemsDataCb =
2199*bd79bce8SPatrick Williams                         [sensorsAsyncResp, inventoryItems,
2200d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
220162598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
220242cbe538SGunnar Mills 
2203*bd79bce8SPatrick Williams                             auto getInventoryLedsCb =
2204*bd79bce8SPatrick Williams                                 [sensorsAsyncResp, inventoryItems,
2205002d39b4SEd Tanous                                  callback{std::move(callback)}]() {
2206*bd79bce8SPatrick Williams                                     BMCWEB_LOG_DEBUG(
2207*bd79bce8SPatrick Williams                                         "getInventoryLedsCb enter");
2208*bd79bce8SPatrick Williams                                     // Find Power Supply Attributes and get the
2209*bd79bce8SPatrick Williams                                     // data
2210*bd79bce8SPatrick Williams                                     getPowerSupplyAttributes(
2211*bd79bce8SPatrick Williams                                         sensorsAsyncResp, inventoryItems,
221242cbe538SGunnar Mills                                         std::move(callback));
221362598e31SEd Tanous                                     BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
221442cbe538SGunnar Mills                                 };
221542cbe538SGunnar Mills 
2216d500549bSAnthony Wilson                             // Find led connections and get the data
2217d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
221842cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
221962598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
2220d500549bSAnthony Wilson                         };
22218fb49dd6SShawn McCarney 
2222adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2223adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2224d0090733SEd Tanous                                           invConnections,
2225d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
222662598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
22278fb49dd6SShawn McCarney                 };
22288fb49dd6SShawn McCarney 
2229adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
2230*bd79bce8SPatrick Williams             getInventoryItemsConnections(
2231*bd79bce8SPatrick Williams                 sensorsAsyncResp, inventoryItems,
22328fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
223362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
22348fb49dd6SShawn McCarney         };
22358fb49dd6SShawn McCarney 
2236adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2237d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
2238adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
223962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems exit");
2240adc4f0dbSShawn McCarney }
2241adc4f0dbSShawn McCarney 
2242adc4f0dbSShawn McCarney /**
2243adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2244adc4f0dbSShawn McCarney  *
2245adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2246adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2247adc4f0dbSShawn McCarney  * array.
2248adc4f0dbSShawn McCarney  *
2249adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2250adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2251adc4f0dbSShawn McCarney  * object.
2252adc4f0dbSShawn McCarney  *
2253adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2254adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2255adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2256adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2257adc4f0dbSShawn McCarney  */
225823a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2259adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2260adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2261adc4f0dbSShawn McCarney {
226218f8f608SEd Tanous     std::string nameS;
22636f4bd290SAlexander Hansen     nameS.resize(inventoryItem.name.size());
226418f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
2265adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2266adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2267adc4f0dbSShawn McCarney     {
226818f8f608SEd Tanous         nlohmann::json::iterator nameIt = powerSupply.find("Name");
226918f8f608SEd Tanous         if (nameIt == powerSupply.end())
227018f8f608SEd Tanous         {
227118f8f608SEd Tanous             continue;
227218f8f608SEd Tanous         }
227318f8f608SEd Tanous         const std::string* name = nameIt->get_ptr<std::string*>();
227418f8f608SEd Tanous         if (name == nullptr)
227518f8f608SEd Tanous         {
227618f8f608SEd Tanous             continue;
227718f8f608SEd Tanous         }
227818f8f608SEd Tanous         if (nameS == *name)
2279adc4f0dbSShawn McCarney         {
2280adc4f0dbSShawn McCarney             return powerSupply;
2281adc4f0dbSShawn McCarney         }
2282adc4f0dbSShawn McCarney     }
2283adc4f0dbSShawn McCarney 
2284adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2285adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2286adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2287*bd79bce8SPatrick Williams     boost::urls::url url =
2288*bd79bce8SPatrick Williams         boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
2289eddfc437SWilly Tu     url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2290eddfc437SWilly Tu     powerSupply["@odata.id"] = std::move(url);
229118f8f608SEd Tanous     std::string escaped;
22926f4bd290SAlexander Hansen     escaped.resize(inventoryItem.name.size());
229318f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
229418f8f608SEd Tanous     powerSupply["Name"] = std::move(escaped);
2295adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2296adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2297adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2298adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2299d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2300adc4f0dbSShawn McCarney 
230142cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
230242cbe538SGunnar Mills     {
230342cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
230442cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
230542cbe538SGunnar Mills     }
230642cbe538SGunnar Mills 
2307aaf08ac7SMatt Simmering     powerSupply["Status"]["State"] = getState(&inventoryItem, true);
2308adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2309adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2310adc4f0dbSShawn McCarney 
2311adc4f0dbSShawn McCarney     return powerSupply;
23128fb49dd6SShawn McCarney }
23138fb49dd6SShawn McCarney 
23148fb49dd6SShawn McCarney /**
2315de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2316de629b6eSShawn McCarney  *
2317de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2318de629b6eSShawn McCarney  *
2319de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2320de629b6eSShawn McCarney  * information has been obtained.
2321de629b6eSShawn McCarney  *
2322adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2323de629b6eSShawn McCarney  *
2324de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2325de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2326de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2327de629b6eSShawn McCarney  *
2328de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2329de629b6eSShawn McCarney  *
2330adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2331adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2332adc4f0dbSShawn McCarney  *
2333de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2334adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2335de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2336de629b6eSShawn McCarney  * implements ObjectManager.
2337adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2338de629b6eSShawn McCarney  */
233923a21a1cSEd Tanous inline void getSensorData(
234081ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2341fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2342fe04d49cSNan Zhou     const std::set<std::string>& connections,
2343b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2344de629b6eSShawn McCarney {
234562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData enter");
2346de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2347de629b6eSShawn McCarney     for (const std::string& connection : connections)
2348de629b6eSShawn McCarney     {
23495eb468daSGeorge Liu         sdbusplus::message::object_path sensorPath(
23505eb468daSGeorge Liu             "/xyz/openbmc_project/sensors");
23515eb468daSGeorge Liu         dbus::utility::getManagedObjects(
23525eb468daSGeorge Liu             connection, sensorPath,
2353002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
23545e7e2dc5SEd Tanous              inventoryItems](const boost::system::error_code& ec,
235502cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
235662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
2357de629b6eSShawn McCarney                 if (ec)
2358de629b6eSShawn McCarney                 {
235962598e31SEd Tanous                     BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
23608d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
2361de629b6eSShawn McCarney                     return;
2362de629b6eSShawn McCarney                 }
2363de629b6eSShawn McCarney                 // Go through all objects and update response with sensor data
2364de629b6eSShawn McCarney                 for (const auto& objDictEntry : resp)
2365de629b6eSShawn McCarney                 {
2366de629b6eSShawn McCarney                     const std::string& objPath =
2367de629b6eSShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
236862598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
236962598e31SEd Tanous                                      objPath);
2370de629b6eSShawn McCarney 
2371de629b6eSShawn McCarney                     std::vector<std::string> split;
2372de629b6eSShawn McCarney                     // Reserve space for
2373de629b6eSShawn McCarney                     // /xyz/openbmc_project/sensors/<name>/<subname>
2374de629b6eSShawn McCarney                     split.reserve(6);
237550ebd4afSEd Tanous                     // NOLINTNEXTLINE
237650ebd4afSEd Tanous                     bmcweb::split(split, objPath, '/');
2377de629b6eSShawn McCarney                     if (split.size() < 6)
2378de629b6eSShawn McCarney                     {
237962598e31SEd Tanous                         BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
238062598e31SEd Tanous                                          objPath);
2381de629b6eSShawn McCarney                         continue;
2382de629b6eSShawn McCarney                     }
238350ebd4afSEd Tanous                     // These indexes aren't intuitive, as split puts an empty
2384de629b6eSShawn McCarney                     // string at the beginning
2385de629b6eSShawn McCarney                     const std::string& sensorType = split[4];
2386de629b6eSShawn McCarney                     const std::string& sensorName = split[5];
238762598e31SEd Tanous                     BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
238862598e31SEd Tanous                                      sensorType);
238949c53ac9SJohnathan Mantey                     if (sensorNames->find(objPath) == sensorNames->end())
2390de629b6eSShawn McCarney                     {
239162598e31SEd Tanous                         BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
2392de629b6eSShawn McCarney                         continue;
2393de629b6eSShawn McCarney                     }
2394de629b6eSShawn McCarney 
2395adc4f0dbSShawn McCarney                     // Find inventory item (if any) associated with sensor
2396adc4f0dbSShawn McCarney                     InventoryItem* inventoryItem =
2397adc4f0dbSShawn McCarney                         findInventoryItemForSensor(inventoryItems, objPath);
2398adc4f0dbSShawn McCarney 
239995a3ecadSAnthony Wilson                     const std::string& sensorSchema =
240081ce609eSEd Tanous                         sensorsAsyncResp->chassisSubNode;
240195a3ecadSAnthony Wilson 
240295a3ecadSAnthony Wilson                     nlohmann::json* sensorJson = nullptr;
240395a3ecadSAnthony Wilson 
2404928fefb9SNan Zhou                     if (sensorSchema == sensors::node::sensors &&
2405928fefb9SNan Zhou                         !sensorsAsyncResp->efficientExpand)
240695a3ecadSAnthony Wilson                     {
2407c1d019a6SEd Tanous                         std::string sensorTypeEscaped(sensorType);
2408*bd79bce8SPatrick Williams                         auto remove =
2409*bd79bce8SPatrick Williams                             std::ranges::remove(sensorTypeEscaped, '_');
24103544d2a7SEd Tanous 
24113544d2a7SEd Tanous                         sensorTypeEscaped.erase(std::ranges::begin(remove),
2412c1d019a6SEd Tanous                                                 sensorTypeEscaped.end());
2413c1d019a6SEd Tanous                         std::string sensorId(sensorTypeEscaped);
2414c1d019a6SEd Tanous                         sensorId += "_";
2415c1d019a6SEd Tanous                         sensorId += sensorName;
2416c1d019a6SEd Tanous 
2417*bd79bce8SPatrick Williams                         sensorsAsyncResp->asyncResp->res
2418*bd79bce8SPatrick Williams                             .jsonValue["@odata.id"] = boost::urls::format(
2419*bd79bce8SPatrick Williams                             "/redfish/v1/Chassis/{}/{}/{}",
2420c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
2421*bd79bce8SPatrick Williams                             sensorsAsyncResp->chassisSubNode, sensorId);
2422*bd79bce8SPatrick Williams                         sensorJson =
2423*bd79bce8SPatrick Williams                             &(sensorsAsyncResp->asyncResp->res.jsonValue);
242495a3ecadSAnthony Wilson                     }
242595a3ecadSAnthony Wilson                     else
242695a3ecadSAnthony Wilson                     {
2427271584abSEd Tanous                         std::string fieldName;
2428928fefb9SNan Zhou                         if (sensorsAsyncResp->efficientExpand)
2429928fefb9SNan Zhou                         {
2430928fefb9SNan Zhou                             fieldName = "Members";
2431928fefb9SNan Zhou                         }
2432928fefb9SNan Zhou                         else if (sensorType == "temperature")
2433de629b6eSShawn McCarney                         {
2434de629b6eSShawn McCarney                             fieldName = "Temperatures";
2435de629b6eSShawn McCarney                         }
2436*bd79bce8SPatrick Williams                         else if (sensorType == "fan" ||
2437*bd79bce8SPatrick Williams                                  sensorType == "fan_tach" ||
2438de629b6eSShawn McCarney                                  sensorType == "fan_pwm")
2439de629b6eSShawn McCarney                         {
2440de629b6eSShawn McCarney                             fieldName = "Fans";
2441de629b6eSShawn McCarney                         }
2442de629b6eSShawn McCarney                         else if (sensorType == "voltage")
2443de629b6eSShawn McCarney                         {
2444de629b6eSShawn McCarney                             fieldName = "Voltages";
2445de629b6eSShawn McCarney                         }
2446de629b6eSShawn McCarney                         else if (sensorType == "power")
2447de629b6eSShawn McCarney                         {
244855f79e6fSEd Tanous                             if (sensorName == "total_power")
2449028f7ebcSEddie James                             {
2450028f7ebcSEddie James                                 fieldName = "PowerControl";
2451028f7ebcSEddie James                             }
2452adc4f0dbSShawn McCarney                             else if ((inventoryItem != nullptr) &&
2453adc4f0dbSShawn McCarney                                      (inventoryItem->isPowerSupply))
2454028f7ebcSEddie James                             {
2455de629b6eSShawn McCarney                                 fieldName = "PowerSupplies";
2456de629b6eSShawn McCarney                             }
2457adc4f0dbSShawn McCarney                             else
2458adc4f0dbSShawn McCarney                             {
2459adc4f0dbSShawn McCarney                                 // Other power sensors are in SensorCollection
2460adc4f0dbSShawn McCarney                                 continue;
2461adc4f0dbSShawn McCarney                             }
2462028f7ebcSEddie James                         }
2463de629b6eSShawn McCarney                         else
2464de629b6eSShawn McCarney                         {
2465*bd79bce8SPatrick Williams                             BMCWEB_LOG_ERROR(
2466*bd79bce8SPatrick Williams                                 "Unsure how to handle sensorType {}",
246762598e31SEd Tanous                                 sensorType);
2468de629b6eSShawn McCarney                             continue;
2469de629b6eSShawn McCarney                         }
2470de629b6eSShawn McCarney 
2471de629b6eSShawn McCarney                         nlohmann::json& tempArray =
2472*bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2473*bd79bce8SPatrick Williams                                 .jsonValue[fieldName];
2474adc4f0dbSShawn McCarney                         if (fieldName == "PowerControl")
247549c53ac9SJohnathan Mantey                         {
2476adc4f0dbSShawn McCarney                             if (tempArray.empty())
24777ab06f49SGunnar Mills                             {
247895a3ecadSAnthony Wilson                                 // Put multiple "sensors" into a single
247995a3ecadSAnthony Wilson                                 // PowerControl. Follows MemberId naming and
248095a3ecadSAnthony Wilson                                 // naming in power.hpp.
24811476687dSEd Tanous                                 nlohmann::json::object_t power;
2482ef4c65b7SEd Tanous                                 boost::urls::url url = boost::urls::format(
2483ef4c65b7SEd Tanous                                     "/redfish/v1/Chassis/{}/{}",
2484eddfc437SWilly Tu                                     sensorsAsyncResp->chassisId,
2485eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
2486*bd79bce8SPatrick Williams                                 url.set_fragment(
2487*bd79bce8SPatrick Williams                                     (""_json_pointer / fieldName / "0")
2488eddfc437SWilly Tu                                         .to_string());
2489eddfc437SWilly Tu                                 power["@odata.id"] = std::move(url);
2490b2ba3072SPatrick Williams                                 tempArray.emplace_back(std::move(power));
2491adc4f0dbSShawn McCarney                             }
2492adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
2493adc4f0dbSShawn McCarney                         }
2494adc4f0dbSShawn McCarney                         else if (fieldName == "PowerSupplies")
2495adc4f0dbSShawn McCarney                         {
2496adc4f0dbSShawn McCarney                             if (inventoryItem != nullptr)
2497adc4f0dbSShawn McCarney                             {
2498*bd79bce8SPatrick Williams                                 sensorJson = &(getPowerSupply(
2499*bd79bce8SPatrick Williams                                     tempArray, *inventoryItem,
250081ce609eSEd Tanous                                     sensorsAsyncResp->chassisId));
2501adc4f0dbSShawn McCarney                             }
250249c53ac9SJohnathan Mantey                         }
2503928fefb9SNan Zhou                         else if (fieldName == "Members")
2504928fefb9SNan Zhou                         {
2505677bb756SEd Tanous                             std::string sensorTypeEscaped(sensorType);
2506*bd79bce8SPatrick Williams                             auto remove =
2507*bd79bce8SPatrick Williams                                 std::ranges::remove(sensorTypeEscaped, '_');
25083544d2a7SEd Tanous                             sensorTypeEscaped.erase(std::ranges::begin(remove),
2509677bb756SEd Tanous                                                     sensorTypeEscaped.end());
2510677bb756SEd Tanous                             std::string sensorId(sensorTypeEscaped);
2511677bb756SEd Tanous                             sensorId += "_";
2512677bb756SEd Tanous                             sensorId += sensorName;
2513677bb756SEd Tanous 
25141476687dSEd Tanous                             nlohmann::json::object_t member;
2515ef4c65b7SEd Tanous                             member["@odata.id"] = boost::urls::format(
2516ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}/{}",
2517677bb756SEd Tanous                                 sensorsAsyncResp->chassisId,
2518677bb756SEd Tanous                                 sensorsAsyncResp->chassisSubNode, sensorId);
2519b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2520928fefb9SNan Zhou                             sensorJson = &(tempArray.back());
2521928fefb9SNan Zhou                         }
252249c53ac9SJohnathan Mantey                         else
252349c53ac9SJohnathan Mantey                         {
25241476687dSEd Tanous                             nlohmann::json::object_t member;
2525ef4c65b7SEd Tanous                             boost::urls::url url = boost::urls::format(
2526ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}",
2527eddfc437SWilly Tu                                 sensorsAsyncResp->chassisId,
2528eddfc437SWilly Tu                                 sensorsAsyncResp->chassisSubNode);
2529eddfc437SWilly Tu                             url.set_fragment(
2530eddfc437SWilly Tu                                 (""_json_pointer / fieldName).to_string());
2531eddfc437SWilly Tu                             member["@odata.id"] = std::move(url);
2532b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2533adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
253449c53ac9SJohnathan Mantey                         }
253595a3ecadSAnthony Wilson                     }
2536de629b6eSShawn McCarney 
2537adc4f0dbSShawn McCarney                     if (sensorJson != nullptr)
2538adc4f0dbSShawn McCarney                     {
25391d7c0054SEd Tanous                         objectInterfacesToJson(sensorName, sensorType,
25401d7c0054SEd Tanous                                                sensorsAsyncResp->chassisSubNode,
25411d7c0054SEd Tanous                                                objDictEntry.second, *sensorJson,
25421d7c0054SEd Tanous                                                inventoryItem);
25431d7c0054SEd Tanous 
25441d7c0054SEd Tanous                         std::string path = "/xyz/openbmc_project/sensors/";
25451d7c0054SEd Tanous                         path += sensorType;
25461d7c0054SEd Tanous                         path += "/";
25471d7c0054SEd Tanous                         path += sensorName;
2548c1d019a6SEd Tanous                         sensorsAsyncResp->addMetadata(*sensorJson, path);
2549adc4f0dbSShawn McCarney                     }
2550de629b6eSShawn McCarney                 }
255181ce609eSEd Tanous                 if (sensorsAsyncResp.use_count() == 1)
255249c53ac9SJohnathan Mantey                 {
255381ce609eSEd Tanous                     sortJSONResponse(sensorsAsyncResp);
2554928fefb9SNan Zhou                     if (sensorsAsyncResp->chassisSubNode ==
2555928fefb9SNan Zhou                             sensors::node::sensors &&
2556928fefb9SNan Zhou                         sensorsAsyncResp->efficientExpand)
2557928fefb9SNan Zhou                     {
2558928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res
2559928fefb9SNan Zhou                             .jsonValue["Members@odata.count"] =
2560*bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2561*bd79bce8SPatrick Williams                                 .jsonValue["Members"]
2562928fefb9SNan Zhou                                 .size();
2563928fefb9SNan Zhou                     }
2564928fefb9SNan Zhou                     else if (sensorsAsyncResp->chassisSubNode ==
2565928fefb9SNan Zhou                              sensors::node::thermal)
25668bd25ccdSJames Feist                     {
256781ce609eSEd Tanous                         populateFanRedundancy(sensorsAsyncResp);
25688bd25ccdSJames Feist                     }
256949c53ac9SJohnathan Mantey                 }
257062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
25715eb468daSGeorge Liu             });
257223a21a1cSEd Tanous     }
257362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData exit");
2574de629b6eSShawn McCarney }
2575de629b6eSShawn McCarney 
2576fe04d49cSNan Zhou inline void
2577fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2578fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
25791abe55efSEd Tanous {
2580fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2581fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
258262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb enter");
2583adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2584*bd79bce8SPatrick Williams             [sensorsAsyncResp, sensorNames, connections](
2585*bd79bce8SPatrick Williams                 const std::shared_ptr<std::vector<InventoryItem>>&
2586adc4f0dbSShawn McCarney                     inventoryItems) {
258762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
258849c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2589002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2590d0090733SEd Tanous                               inventoryItems);
259162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2592adc4f0dbSShawn McCarney             };
2593adc4f0dbSShawn McCarney 
2594adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2595d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2596adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2597adc4f0dbSShawn McCarney 
259862598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb exit");
259908777fb0SLewanczyk, Dawid     };
2600de629b6eSShawn McCarney 
2601de629b6eSShawn McCarney     // Get set of connections that provide sensor values
260281ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
260395a3ecadSAnthony Wilson }
260495a3ecadSAnthony Wilson 
260595a3ecadSAnthony Wilson /**
260695a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
260795a3ecadSAnthony Wilson  *        chassis.
260895a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
260995a3ecadSAnthony Wilson  */
2610b5a76932SEd Tanous inline void
261181ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
261295a3ecadSAnthony Wilson {
261362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData enter");
261495a3ecadSAnthony Wilson     auto getChassisCb =
261581ce609eSEd Tanous         [sensorsAsyncResp](
2616fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
261762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb enter");
261881ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
261962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb exit");
262008777fb0SLewanczyk, Dawid         };
2621928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2622928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2623928fefb9SNan Zhou     {
26248d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
26258d1b46d7Szhanghch05             nlohmann::json::array();
2626928fefb9SNan Zhou     }
262726f03899SShawn McCarney     // Get set of sensors in chassis
26287f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
26297f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
26307f1cc26dSEd Tanous                std::move(getChassisCb));
263162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData exit");
2632271584abSEd Tanous }
263308777fb0SLewanczyk, Dawid 
2634413961deSRichard Marian Thomaiyar /**
263549c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
263649c53ac9SJohnathan Mantey  * the chassis node
263749c53ac9SJohnathan Mantey  *
263849c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
263949c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
264049c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
264149c53ac9SJohnathan Mantey  *                         repeated calls to this function
264249c53ac9SJohnathan Mantey  */
2643*bd79bce8SPatrick Williams inline bool findSensorNameUsingSensorPath(
2644*bd79bce8SPatrick Williams     std::string_view sensorName, const std::set<std::string>& sensorsList,
2645fe04d49cSNan Zhou     std::set<std::string>& sensorsModified)
264649c53ac9SJohnathan Mantey {
2647fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
264849c53ac9SJohnathan Mantey     {
264928aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2650b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
265128aa8de5SGeorge Liu         if (thisSensorName.empty())
265249c53ac9SJohnathan Mantey         {
265349c53ac9SJohnathan Mantey             continue;
265449c53ac9SJohnathan Mantey         }
265549c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
265649c53ac9SJohnathan Mantey         {
265749c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
265849c53ac9SJohnathan Mantey             return true;
265949c53ac9SJohnathan Mantey         }
266049c53ac9SJohnathan Mantey     }
266149c53ac9SJohnathan Mantey     return false;
266249c53ac9SJohnathan Mantey }
266349c53ac9SJohnathan Mantey 
2664c71d6125SEd Tanous inline std::pair<std::string, std::string>
2665c71d6125SEd Tanous     splitSensorNameAndType(std::string_view sensorId)
2666c71d6125SEd Tanous {
2667c71d6125SEd Tanous     size_t index = sensorId.find('_');
2668c71d6125SEd Tanous     if (index == std::string::npos)
2669c71d6125SEd Tanous     {
2670c71d6125SEd Tanous         return std::make_pair<std::string, std::string>("", "");
2671c71d6125SEd Tanous     }
2672c71d6125SEd Tanous     std::string sensorType{sensorId.substr(0, index)};
2673c71d6125SEd Tanous     std::string sensorName{sensorId.substr(index + 1)};
2674c71d6125SEd Tanous     // fan_pwm and fan_tach need special handling
2675c71d6125SEd Tanous     if (sensorType == "fantach" || sensorType == "fanpwm")
2676c71d6125SEd Tanous     {
2677c71d6125SEd Tanous         sensorType.insert(3, 1, '_');
2678c71d6125SEd Tanous     }
2679c71d6125SEd Tanous     return std::make_pair(sensorType, sensorName);
2680c71d6125SEd Tanous }
2681c71d6125SEd Tanous 
268249c53ac9SJohnathan Mantey /**
2683413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2684413961deSRichard Marian Thomaiyar  *
26858d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
26864bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2687413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2688413961deSRichard Marian Thomaiyar  */
268923a21a1cSEd Tanous inline void setSensorsOverride(
2690b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
26910885057cSEd Tanous     std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
2692397fd61fSjayaprakash Mutyala         allCollections)
2693413961deSRichard Marian Thomaiyar {
269462598e31SEd Tanous     BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
269562598e31SEd Tanous                     sensorAsyncResp->chassisSubNode);
2696413961deSRichard Marian Thomaiyar 
2697d02aad39SEd Tanous     std::string_view propertyValueName;
2698f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2699413961deSRichard Marian Thomaiyar     std::string memberId;
2700543f4400SEd Tanous     double value = 0.0;
2701f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2702f65af9e8SRichard Marian Thomaiyar     {
2703f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2704f65af9e8SRichard Marian Thomaiyar         {
2705f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2706f65af9e8SRichard Marian Thomaiyar         }
2707f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2708f65af9e8SRichard Marian Thomaiyar         {
2709f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2710f65af9e8SRichard Marian Thomaiyar         }
2711f65af9e8SRichard Marian Thomaiyar         else
2712f65af9e8SRichard Marian Thomaiyar         {
2713f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2714f65af9e8SRichard Marian Thomaiyar         }
2715f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2716f65af9e8SRichard Marian Thomaiyar         {
27170885057cSEd Tanous             if (!json_util::readJsonObject(
27180885057cSEd Tanous                     item, sensorAsyncResp->asyncResp->res, "MemberId", memberId,
27190885057cSEd Tanous                     propertyValueName, value))
2720413961deSRichard Marian Thomaiyar             {
2721413961deSRichard Marian Thomaiyar                 return;
2722413961deSRichard Marian Thomaiyar             }
2723f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2724f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2725f65af9e8SRichard Marian Thomaiyar         }
2726f65af9e8SRichard Marian Thomaiyar     }
27274bb3dc34SCarol Wang 
2728*bd79bce8SPatrick Williams     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap,
2729*bd79bce8SPatrick Williams                                    propertyValueNameStr =
2730*bd79bce8SPatrick Williams                                        std::string(propertyValueName)](
2731*bd79bce8SPatrick Williams                                       const std::shared_ptr<
2732*bd79bce8SPatrick Williams                                           std::set<std::string>>& sensorsList) {
273349c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
273449c53ac9SJohnathan Mantey         // chassis node
2735fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2736fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2737f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2738413961deSRichard Marian Thomaiyar         {
2739f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2740c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
2741c71d6125SEd Tanous                 splitSensorNameAndType(sensor);
2742c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2743c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2744f65af9e8SRichard Marian Thomaiyar             {
274562598e31SEd Tanous                 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
27468d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2747f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2748413961deSRichard Marian Thomaiyar                 return;
2749413961deSRichard Marian Thomaiyar             }
2750f65af9e8SRichard Marian Thomaiyar         }
2751413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2752*bd79bce8SPatrick Williams         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap,
2753*bd79bce8SPatrick Williams                                            propertyValueNameStr](
2754*bd79bce8SPatrick Williams                                               const std::set<
2755*bd79bce8SPatrick Williams                                                   std::string>& /*connections*/,
2756*bd79bce8SPatrick Williams                                               const std::set<std::pair<
2757*bd79bce8SPatrick Williams                                                   std::string, std::string>>&
2758413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2759f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2760413961deSRichard Marian Thomaiyar             {
276162598e31SEd Tanous                 BMCWEB_LOG_INFO(
276262598e31SEd Tanous                     "Unable to find all objects with proper connection {} requested {}",
276362598e31SEd Tanous                     objectsWithConnection.size(), overrideMap.size());
2764*bd79bce8SPatrick Williams                 messages::resourceNotFound(
2765*bd79bce8SPatrick Williams                     sensorAsyncResp->asyncResp->res,
2766*bd79bce8SPatrick Williams                     sensorAsyncResp->chassisSubNode == sensors::node::thermal
2767413961deSRichard Marian Thomaiyar                         ? "Temperatures"
2768413961deSRichard Marian Thomaiyar                         : "Voltages",
2769f65af9e8SRichard Marian Thomaiyar                     "Count");
2770f65af9e8SRichard Marian Thomaiyar                 return;
2771f65af9e8SRichard Marian Thomaiyar             }
2772f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2773f65af9e8SRichard Marian Thomaiyar             {
277428aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
277528aa8de5SGeorge Liu                 std::string sensorName = path.filename();
277628aa8de5SGeorge Liu                 if (sensorName.empty())
2777f65af9e8SRichard Marian Thomaiyar                 {
27784f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2779f65af9e8SRichard Marian Thomaiyar                     return;
2780f65af9e8SRichard Marian Thomaiyar                 }
27813f5eb755SBan Feng                 std::string id = path.parent_path().filename();
27823544d2a7SEd Tanous                 auto remove = std::ranges::remove(id, '_');
27833544d2a7SEd Tanous                 id.erase(std::ranges::begin(remove), id.end());
27843f5eb755SBan Feng                 id += "_";
27853f5eb755SBan Feng                 id += sensorName;
2786f65af9e8SRichard Marian Thomaiyar 
27873f5eb755SBan Feng                 const auto& iterator = overrideMap.find(id);
2788f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2789f65af9e8SRichard Marian Thomaiyar                 {
279062598e31SEd Tanous                     BMCWEB_LOG_INFO("Unable to find sensor object{}",
279162598e31SEd Tanous                                     item.first);
27924f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2793413961deSRichard Marian Thomaiyar                     return;
2794413961deSRichard Marian Thomaiyar                 }
2795e93abac6SGinu George                 setDbusProperty(sensorAsyncResp->asyncResp,
2796e93abac6SGinu George                                 propertyValueNameStr, item.second, item.first,
2797e93abac6SGinu George                                 "xyz.openbmc_project.Sensor.Value", "Value",
2798d02aad39SEd Tanous                                 iterator->second.first);
2799f65af9e8SRichard Marian Thomaiyar             }
2800413961deSRichard Marian Thomaiyar         };
2801413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2802413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2803413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2804413961deSRichard Marian Thomaiyar     };
2805413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
28067f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
28077f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
28087f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2809413961deSRichard Marian Thomaiyar }
2810413961deSRichard Marian Thomaiyar 
2811a0ec28b6SAdrian Ambrożewicz /**
2812a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2813a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2814a0ec28b6SAdrian Ambrożewicz  *
2815a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2816a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2817a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2818a0ec28b6SAdrian Ambrożewicz  *
2819a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2820a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2821a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2822a0ec28b6SAdrian Ambrożewicz  */
2823931edc79SEd Tanous template <typename Callback>
2824*bd79bce8SPatrick Williams inline void retrieveUriToDbusMap(
2825*bd79bce8SPatrick Williams     const std::string& chassis, const std::string& node, Callback&& mapComplete)
2826a0ec28b6SAdrian Ambrożewicz {
282702da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
282802da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
282902da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
283002da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2831a0ec28b6SAdrian Ambrożewicz     {
283262598e31SEd Tanous         BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
28336804b5c8SEd Tanous         std::map<std::string, std::string> noop;
28346804b5c8SEd Tanous         mapComplete(boost::beast::http::status::bad_request, noop);
2835a0ec28b6SAdrian Ambrożewicz         return;
2836a0ec28b6SAdrian Ambrożewicz     }
2837d51e072fSKrzysztof Grobelny 
283872374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2839*bd79bce8SPatrick Williams     auto callback =
2840*bd79bce8SPatrick Williams         [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)](
2841a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2842fe04d49cSNan Zhou             const std::map<std::string, std::string>& uriToDbus) {
2843fe04d49cSNan Zhou             mapCompleteCb(status, uriToDbus);
2844fe04d49cSNan Zhou         };
2845a0ec28b6SAdrian Ambrożewicz 
2846a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2847d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2848a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2849a0ec28b6SAdrian Ambrożewicz }
2850a0ec28b6SAdrian Ambrożewicz 
2851bacb2162SNan Zhou namespace sensors
2852bacb2162SNan Zhou {
2853928fefb9SNan Zhou 
2854bacb2162SNan Zhou inline void getChassisCallback(
2855c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2856c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2857fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2858bacb2162SNan Zhou {
285962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback enter ");
2860bacb2162SNan Zhou 
2861c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2862c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2863bacb2162SNan Zhou     {
286462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
2865bacb2162SNan Zhou 
2866bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2867bacb2162SNan Zhou         std::string sensorName = path.filename();
2868bacb2162SNan Zhou         if (sensorName.empty())
2869bacb2162SNan Zhou         {
287062598e31SEd Tanous             BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
2871c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2872bacb2162SNan Zhou             return;
2873bacb2162SNan Zhou         }
2874c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
2875c1d019a6SEd Tanous         // fan_tach has an underscore in it, so remove it to "normalize" the
2876c1d019a6SEd Tanous         // type in the URI
28773544d2a7SEd Tanous         auto remove = std::ranges::remove(type, '_');
28783544d2a7SEd Tanous         type.erase(std::ranges::begin(remove), type.end());
2879c1d019a6SEd Tanous 
28801476687dSEd Tanous         nlohmann::json::object_t member;
2881c1d019a6SEd Tanous         std::string id = type;
2882c1d019a6SEd Tanous         id += "_";
2883c1d019a6SEd Tanous         id += sensorName;
2884ef4c65b7SEd Tanous         member["@odata.id"] = boost::urls::format(
2885ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
2886c1d019a6SEd Tanous 
2887b2ba3072SPatrick Williams         entriesArray.emplace_back(std::move(member));
2888bacb2162SNan Zhou     }
2889bacb2162SNan Zhou 
2890c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
289162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback exit");
2892bacb2162SNan Zhou }
2893e6bd846dSNan Zhou 
2894ac106bf6SEd Tanous inline void handleSensorCollectionGet(
2895ac106bf6SEd Tanous     App& app, const crow::Request& req,
2896ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2897de167a6fSNan Zhou     const std::string& chassisId)
2898de167a6fSNan Zhou {
2899de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2900de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2901de167a6fSNan Zhou     };
2902de167a6fSNan Zhou     query_param::Query delegatedQuery;
2903ac106bf6SEd Tanous     if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
2904de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2905de167a6fSNan Zhou     {
2906de167a6fSNan Zhou         return;
2907de167a6fSNan Zhou     }
2908de167a6fSNan Zhou 
2909de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2910de167a6fSNan Zhou     {
2911de167a6fSNan Zhou         // we perform efficient expand.
2912ac106bf6SEd Tanous         auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2913ac106bf6SEd Tanous             asyncResp, chassisId, sensors::dbus::sensorPaths,
2914de167a6fSNan Zhou             sensors::node::sensors,
2915de167a6fSNan Zhou             /*efficientExpand=*/true);
2916ac106bf6SEd Tanous         getChassisData(sensorsAsyncResp);
2917de167a6fSNan Zhou 
291862598e31SEd Tanous         BMCWEB_LOG_DEBUG(
291962598e31SEd Tanous             "SensorCollection doGet exit via efficient expand handler");
2920de167a6fSNan Zhou         return;
29210bad320cSEd Tanous     }
2922de167a6fSNan Zhou 
2923de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2924de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
2925ac106bf6SEd Tanous     getChassis(asyncResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2926ac106bf6SEd Tanous                std::bind_front(sensors::getChassisCallback, asyncResp,
2927ac106bf6SEd Tanous                                chassisId, sensors::node::sensors));
2928c1d019a6SEd Tanous }
29297f1cc26dSEd Tanous 
2930c1d019a6SEd Tanous inline void
2931c1d019a6SEd Tanous     getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2932c1d019a6SEd Tanous                       const std::string& sensorPath,
2933c1d019a6SEd Tanous                       const ::dbus::utility::MapperGetObject& mapperResponse)
2934c1d019a6SEd Tanous {
2935c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2936c1d019a6SEd Tanous     {
2937c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2938c1d019a6SEd Tanous         return;
2939c1d019a6SEd Tanous     }
2940c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2941c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
294262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
294362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Path {}", sensorPath);
2944c1343bf6SKrzysztof Grobelny 
2945c1343bf6SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2946c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2947c1d019a6SEd Tanous         [asyncResp,
29485e7e2dc5SEd Tanous          sensorPath](const boost::system::error_code& ec,
2949c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2950c1d019a6SEd Tanous             if (ec)
2951c1d019a6SEd Tanous             {
2952c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
2953c1d019a6SEd Tanous                 return;
2954c1d019a6SEd Tanous             }
2955c1d019a6SEd Tanous             sdbusplus::message::object_path path(sensorPath);
2956c1d019a6SEd Tanous             std::string name = path.filename();
2957c1d019a6SEd Tanous             path = path.parent_path();
2958c1d019a6SEd Tanous             std::string type = path.filename();
2959*bd79bce8SPatrick Williams             objectPropertiesToJson(name, type, sensors::node::sensors,
2960*bd79bce8SPatrick Williams                                    valuesDict, asyncResp->res.jsonValue,
2961*bd79bce8SPatrick Williams                                    nullptr);
2962c1343bf6SKrzysztof Grobelny         });
2963de167a6fSNan Zhou }
2964de167a6fSNan Zhou 
2965e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2966c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2967677bb756SEd Tanous                             const std::string& chassisId,
2968c1d019a6SEd Tanous                             const std::string& sensorId)
2969e6bd846dSNan Zhou {
2970c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2971e6bd846dSNan Zhou     {
2972e6bd846dSNan Zhou         return;
2973e6bd846dSNan Zhou     }
2974c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
2975c71d6125SEd Tanous         splitSensorNameAndType(sensorId);
2976c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2977c1d019a6SEd Tanous     {
2978c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2979c1d019a6SEd Tanous         return;
2980c1d019a6SEd Tanous     }
2981c71d6125SEd Tanous 
2982ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2983ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
2984c1d019a6SEd Tanous 
298562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Sensor doGet enter");
2986e6bd846dSNan Zhou 
29872b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2988e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2989c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2990c71d6125SEd Tanous                              '/' + nameType.second;
2991e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2992e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
29932b73119cSGeorge Liu     ::dbus::utility::getDbusObject(
29942b73119cSGeorge Liu         sensorPath, interfaces,
2995aec0ec30SMyung Bae         [asyncResp, sensorId,
29962b73119cSGeorge Liu          sensorPath](const boost::system::error_code& ec,
2997c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
299862598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 enter");
2999aec0ec30SMyung Bae             if (ec == boost::system::errc::io_error)
3000aec0ec30SMyung Bae             {
300162598e31SEd Tanous                 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
3002aec0ec30SMyung Bae                 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
3003aec0ec30SMyung Bae                 return;
3004aec0ec30SMyung Bae             }
3005e6bd846dSNan Zhou             if (ec)
3006e6bd846dSNan Zhou             {
3007c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
300862598e31SEd Tanous                 BMCWEB_LOG_ERROR(
300962598e31SEd Tanous                     "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
3010e6bd846dSNan Zhou                 return;
3011e6bd846dSNan Zhou             }
3012c1d019a6SEd Tanous             getSensorFromDbus(asyncResp, sensorPath, subtree);
301362598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 exit");
30142b73119cSGeorge Liu         });
3015e6bd846dSNan Zhou }
3016e6bd846dSNan Zhou 
3017bacb2162SNan Zhou } // namespace sensors
3018bacb2162SNan Zhou 
30197e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
302095a3ecadSAnthony Wilson {
30217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
3022ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
3023002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3024de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
302595a3ecadSAnthony Wilson }
302695a3ecadSAnthony Wilson 
30277e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
302895a3ecadSAnthony Wilson {
30297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3030ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
3031002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3032e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
303395a3ecadSAnthony Wilson }
303495a3ecadSAnthony Wilson 
303508777fb0SLewanczyk, Dawid } // namespace redfish
3036