xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision e99073f55f30e2af2c36d7859513ea9464fa647d)
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"
210ec8b83dSEd Tanous #include "generated/enums/sensor.hpp"
223ccb3adbSEd Tanous #include "query.hpp"
233ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
243ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
253ccb3adbSEd Tanous #include "utils/json_utils.hpp"
263ccb3adbSEd Tanous #include "utils/query_param.hpp"
270ec8b83dSEd Tanous 
2811ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
291d7c0054SEd Tanous #include <boost/algorithm/string/find.hpp>
301d7c0054SEd Tanous #include <boost/algorithm/string/predicate.hpp>
31c71d6125SEd Tanous #include <boost/algorithm/string/replace.hpp>
3208777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
3308777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
34*e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
351e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
3686d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
371214b7e7SGunnar Mills 
387a1dbc48SGeorge Liu #include <array>
391214b7e7SGunnar Mills #include <cmath>
40fe04d49cSNan Zhou #include <iterator>
41fe04d49cSNan Zhou #include <map>
42fe04d49cSNan Zhou #include <set>
437a1dbc48SGeorge Liu #include <string_view>
44b5a76932SEd Tanous #include <utility>
45abf2add6SEd Tanous #include <variant>
4608777fb0SLewanczyk, Dawid 
471abe55efSEd Tanous namespace redfish
481abe55efSEd Tanous {
4908777fb0SLewanczyk, Dawid 
50a0ec28b6SAdrian Ambrożewicz namespace sensors
51a0ec28b6SAdrian Ambrożewicz {
52a0ec28b6SAdrian Ambrożewicz namespace node
53a0ec28b6SAdrian Ambrożewicz {
54a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
55a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
56a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
57a0ec28b6SAdrian Ambrożewicz } // namespace node
58a0ec28b6SAdrian Ambrożewicz 
5902da7c5aSEd Tanous // clang-format off
60a0ec28b6SAdrian Ambrożewicz namespace dbus
61a0ec28b6SAdrian Ambrożewicz {
62cf9e417dSEd Tanous constexpr auto powerPaths = std::to_array<std::string_view>({
6302da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
6402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
6502da7c5aSEd Tanous });
66c2bf7f99SWludzik, Jozef 
67cf9e417dSEd Tanous constexpr auto sensorPaths = std::to_array<std::string_view>({
6802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power",
69a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/current",
707088690cSBasheer Ahmed Muddebihal     "/xyz/openbmc_project/sensors/airflow",
715deabed9SGunnar Mills     "/xyz/openbmc_project/sensors/humidity",
72e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
73e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/voltage",
74e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_tach",
75e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/temperature",
76e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_pwm",
77e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/altitude",
78e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/energy",
79e8204933SGeorge Liu #endif
8002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/utilization"
8102da7c5aSEd Tanous });
8202da7c5aSEd Tanous 
83cf9e417dSEd Tanous constexpr auto thermalPaths = std::to_array<std::string_view>({
8402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
85a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
8602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
8702da7c5aSEd Tanous });
8802da7c5aSEd Tanous 
89c2bf7f99SWludzik, Jozef } // namespace dbus
9002da7c5aSEd Tanous // clang-format on
9102da7c5aSEd Tanous 
92cf9e417dSEd Tanous using sensorPair =
93cf9e417dSEd Tanous     std::pair<std::string_view, std::span<const std::string_view>>;
9402da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
95cf9e417dSEd Tanous     {{node::power, dbus::powerPaths},
96cf9e417dSEd Tanous      {node::sensors, dbus::sensorPaths},
97cf9e417dSEd Tanous      {node::thermal, dbus::thermalPaths}}};
98c2bf7f99SWludzik, Jozef 
990ec8b83dSEd Tanous inline sensor::ReadingType toReadingType(std::string_view sensorType)
100c2bf7f99SWludzik, Jozef {
101c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
102c2bf7f99SWludzik, Jozef     {
1030ec8b83dSEd Tanous         return sensor::ReadingType::Voltage;
104c2bf7f99SWludzik, Jozef     }
105c2bf7f99SWludzik, Jozef     if (sensorType == "power")
106c2bf7f99SWludzik, Jozef     {
1070ec8b83dSEd Tanous         return sensor::ReadingType::Power;
108c2bf7f99SWludzik, Jozef     }
109c2bf7f99SWludzik, Jozef     if (sensorType == "current")
110c2bf7f99SWludzik, Jozef     {
1110ec8b83dSEd Tanous         return sensor::ReadingType::Current;
112c2bf7f99SWludzik, Jozef     }
113c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
114c2bf7f99SWludzik, Jozef     {
1150ec8b83dSEd Tanous         return sensor::ReadingType::Rotational;
116c2bf7f99SWludzik, Jozef     }
117c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
118c2bf7f99SWludzik, Jozef     {
1190ec8b83dSEd Tanous         return sensor::ReadingType::Temperature;
120c2bf7f99SWludzik, Jozef     }
121c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
122c2bf7f99SWludzik, Jozef     {
1230ec8b83dSEd Tanous         return sensor::ReadingType::Percent;
124c2bf7f99SWludzik, Jozef     }
1255deabed9SGunnar Mills     if (sensorType == "humidity")
1265deabed9SGunnar Mills     {
1270ec8b83dSEd Tanous         return sensor::ReadingType::Humidity;
1285deabed9SGunnar Mills     }
129c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
130c2bf7f99SWludzik, Jozef     {
1310ec8b83dSEd Tanous         return sensor::ReadingType::Altitude;
132c2bf7f99SWludzik, Jozef     }
133c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
134c2bf7f99SWludzik, Jozef     {
1350ec8b83dSEd Tanous         return sensor::ReadingType::AirFlow;
136c2bf7f99SWludzik, Jozef     }
137c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
138c2bf7f99SWludzik, Jozef     {
1390ec8b83dSEd Tanous         return sensor::ReadingType::EnergyJoules;
140c2bf7f99SWludzik, Jozef     }
1410ec8b83dSEd Tanous     return sensor::ReadingType::Invalid;
142c2bf7f99SWludzik, Jozef }
143c2bf7f99SWludzik, Jozef 
1441d7c0054SEd Tanous inline std::string_view toReadingUnits(std::string_view sensorType)
145c2bf7f99SWludzik, Jozef {
146c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
147c2bf7f99SWludzik, Jozef     {
148c2bf7f99SWludzik, Jozef         return "V";
149c2bf7f99SWludzik, Jozef     }
150c2bf7f99SWludzik, Jozef     if (sensorType == "power")
151c2bf7f99SWludzik, Jozef     {
152c2bf7f99SWludzik, Jozef         return "W";
153c2bf7f99SWludzik, Jozef     }
154c2bf7f99SWludzik, Jozef     if (sensorType == "current")
155c2bf7f99SWludzik, Jozef     {
156c2bf7f99SWludzik, Jozef         return "A";
157c2bf7f99SWludzik, Jozef     }
158c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
159c2bf7f99SWludzik, Jozef     {
160c2bf7f99SWludzik, Jozef         return "RPM";
161c2bf7f99SWludzik, Jozef     }
162c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
163c2bf7f99SWludzik, Jozef     {
164c2bf7f99SWludzik, Jozef         return "Cel";
165c2bf7f99SWludzik, Jozef     }
1665deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
1675deabed9SGunnar Mills         sensorType == "humidity")
168c2bf7f99SWludzik, Jozef     {
169c2bf7f99SWludzik, Jozef         return "%";
170c2bf7f99SWludzik, Jozef     }
171c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
172c2bf7f99SWludzik, Jozef     {
173c2bf7f99SWludzik, Jozef         return "m";
174c2bf7f99SWludzik, Jozef     }
175c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
176c2bf7f99SWludzik, Jozef     {
177c2bf7f99SWludzik, Jozef         return "cft_i/min";
178c2bf7f99SWludzik, Jozef     }
179c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
180c2bf7f99SWludzik, Jozef     {
181c2bf7f99SWludzik, Jozef         return "J";
182c2bf7f99SWludzik, Jozef     }
183c2bf7f99SWludzik, Jozef     return "";
184a0ec28b6SAdrian Ambrożewicz }
185a0ec28b6SAdrian Ambrożewicz } // namespace sensors
186a0ec28b6SAdrian Ambrożewicz 
18708777fb0SLewanczyk, Dawid /**
188588c3f0dSKowalski, Kamil  * SensorsAsyncResp
18908777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
19008777fb0SLewanczyk, Dawid  */
1911abe55efSEd Tanous class SensorsAsyncResp
1921abe55efSEd Tanous {
19308777fb0SLewanczyk, Dawid   public:
194a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
195a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
196fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
197a0ec28b6SAdrian Ambrożewicz 
198a0ec28b6SAdrian Ambrożewicz     struct SensorData
199a0ec28b6SAdrian Ambrożewicz     {
200a0ec28b6SAdrian Ambrożewicz         const std::string name;
201a0ec28b6SAdrian Ambrożewicz         std::string uri;
202a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
203a0ec28b6SAdrian Ambrożewicz     };
204a0ec28b6SAdrian Ambrożewicz 
2058a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2068d1b46d7Szhanghch05                      const std::string& chassisIdIn,
207cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
20802da7c5aSEd Tanous                      std::string_view subNode) :
2098a592810SEd Tanous         asyncResp(asyncRespIn),
210928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
211928fefb9SNan Zhou         efficientExpand(false)
2121214b7e7SGunnar Mills     {}
21308777fb0SLewanczyk, Dawid 
214a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
2158a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2168d1b46d7Szhanghch05                      const std::string& chassisIdIn,
217cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
21802da7c5aSEd Tanous                      std::string_view subNode,
219a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
2208a592810SEd Tanous         asyncResp(asyncRespIn),
221928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
222928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
223a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
224a0ec28b6SAdrian Ambrożewicz     {}
225a0ec28b6SAdrian Ambrożewicz 
226928fefb9SNan Zhou     // sensor collections expand
2278a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
228928fefb9SNan Zhou                      const std::string& chassisIdIn,
229cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
2308a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
2318a592810SEd Tanous         asyncResp(asyncRespIn),
232928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
2338a592810SEd Tanous         efficientExpand(efficientExpandIn)
234928fefb9SNan Zhou     {}
235928fefb9SNan Zhou 
2361abe55efSEd Tanous     ~SensorsAsyncResp()
2371abe55efSEd Tanous     {
2388d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2398d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2401abe55efSEd Tanous         {
2411abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2421abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2431abe55efSEd Tanous             // proper code
2448d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
24508777fb0SLewanczyk, Dawid         }
246a0ec28b6SAdrian Ambrożewicz 
247a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
248a0ec28b6SAdrian Ambrożewicz         {
249fe04d49cSNan Zhou             std::map<std::string, std::string> map;
2508d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
251a0ec28b6SAdrian Ambrożewicz             {
252a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
253a0ec28b6SAdrian Ambrożewicz                 {
254c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
255a0ec28b6SAdrian Ambrożewicz                 }
256a0ec28b6SAdrian Ambrożewicz             }
2578d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
258a0ec28b6SAdrian Ambrożewicz         }
25908777fb0SLewanczyk, Dawid     }
260588c3f0dSKowalski, Kamil 
261ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
262ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
263ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
264ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
265ecd6a3a2SEd Tanous 
266a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
267c1d019a6SEd Tanous                      const std::string& dbusPath)
268a0ec28b6SAdrian Ambrożewicz     {
269a0ec28b6SAdrian Ambrożewicz         if (metadata)
270a0ec28b6SAdrian Ambrożewicz         {
271c1d019a6SEd Tanous             metadata->emplace_back(SensorData{
272c1d019a6SEd Tanous                 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
273a0ec28b6SAdrian Ambrożewicz         }
274a0ec28b6SAdrian Ambrożewicz     }
275a0ec28b6SAdrian Ambrożewicz 
276a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
277a0ec28b6SAdrian Ambrożewicz     {
278a0ec28b6SAdrian Ambrożewicz         if (metadata)
279a0ec28b6SAdrian Ambrożewicz         {
280a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
281a0ec28b6SAdrian Ambrożewicz             {
282a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
283a0ec28b6SAdrian Ambrożewicz                 {
284a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
285a0ec28b6SAdrian Ambrożewicz                 }
286a0ec28b6SAdrian Ambrożewicz             }
287a0ec28b6SAdrian Ambrożewicz         }
288a0ec28b6SAdrian Ambrożewicz     }
289a0ec28b6SAdrian Ambrożewicz 
2908d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
291a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
292cf9e417dSEd Tanous     const std::span<const std::string_view> types;
293a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
294928fefb9SNan Zhou     const bool efficientExpand;
295a0ec28b6SAdrian Ambrożewicz 
296a0ec28b6SAdrian Ambrożewicz   private:
297a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
298a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
29908777fb0SLewanczyk, Dawid };
30008777fb0SLewanczyk, Dawid 
30108777fb0SLewanczyk, Dawid /**
302d500549bSAnthony Wilson  * Possible states for physical inventory leds
303d500549bSAnthony Wilson  */
304d500549bSAnthony Wilson enum class LedState
305d500549bSAnthony Wilson {
306d500549bSAnthony Wilson     OFF,
307d500549bSAnthony Wilson     ON,
308d500549bSAnthony Wilson     BLINK,
309d500549bSAnthony Wilson     UNKNOWN
310d500549bSAnthony Wilson };
311d500549bSAnthony Wilson 
312d500549bSAnthony Wilson /**
313adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
314adc4f0dbSShawn McCarney  */
315adc4f0dbSShawn McCarney class InventoryItem
316adc4f0dbSShawn McCarney {
317adc4f0dbSShawn McCarney   public:
3184e23a444SEd Tanous     explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
319adc4f0dbSShawn McCarney     {
320adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
32128aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
32228aa8de5SGeorge Liu         name = path.filename();
32328aa8de5SGeorge Liu         if (name.empty())
324adc4f0dbSShawn McCarney         {
32528aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
326adc4f0dbSShawn McCarney         }
327adc4f0dbSShawn McCarney     }
328adc4f0dbSShawn McCarney 
329adc4f0dbSShawn McCarney     std::string objectPath;
330adc4f0dbSShawn McCarney     std::string name;
331e05aec50SEd Tanous     bool isPresent = true;
332e05aec50SEd Tanous     bool isFunctional = true;
333e05aec50SEd Tanous     bool isPowerSupply = false;
334e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
335adc4f0dbSShawn McCarney     std::string manufacturer;
336adc4f0dbSShawn McCarney     std::string model;
337adc4f0dbSShawn McCarney     std::string partNumber;
338adc4f0dbSShawn McCarney     std::string serialNumber;
339adc4f0dbSShawn McCarney     std::set<std::string> sensors;
340d500549bSAnthony Wilson     std::string ledObjectPath;
341e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
342adc4f0dbSShawn McCarney };
343adc4f0dbSShawn McCarney 
344adc4f0dbSShawn McCarney /**
345413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
346588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
34708777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
34808777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
34908777fb0SLewanczyk, Dawid  */
35008777fb0SLewanczyk, Dawid template <typename Callback>
351413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
35281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
353fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
3541abe55efSEd Tanous     Callback&& callback)
3551abe55efSEd Tanous {
356413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
35703b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
358*e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
35908777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
36008777fb0SLewanczyk, Dawid 
361*e99073f5SGeorge Liu     // Make call to ObjectMapper to find all sensors objects
362*e99073f5SGeorge Liu     dbus::utility::getSubTree(
363*e99073f5SGeorge Liu         path, 2, interfaces,
364002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
365*e99073f5SGeorge Liu          sensorNames](const boost::system::error_code& ec,
366002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
367*e99073f5SGeorge Liu         // Response handler for parsing objects subtree
368413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3691abe55efSEd Tanous         if (ec)
3701abe55efSEd Tanous         {
3718d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
372413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
373413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
37408777fb0SLewanczyk, Dawid             return;
37508777fb0SLewanczyk, Dawid         }
37608777fb0SLewanczyk, Dawid 
37755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
37808777fb0SLewanczyk, Dawid 
37908777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
38008777fb0SLewanczyk, Dawid         // found in the chassis
381fe04d49cSNan Zhou         std::set<std::string> connections;
382413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
38308777fb0SLewanczyk, Dawid 
38449c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
38549c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3861abe55efSEd Tanous         {
38755c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
38808777fb0SLewanczyk, Dawid         }
38908777fb0SLewanczyk, Dawid 
39008777fb0SLewanczyk, Dawid         for (const std::pair<
39108777fb0SLewanczyk, Dawid                  std::string,
39208777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3931abe55efSEd Tanous                  object : subtree)
3941abe55efSEd Tanous         {
39549c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3961abe55efSEd Tanous             {
39749c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3981abe55efSEd Tanous                          objData : object.second)
3991abe55efSEd Tanous                 {
40049c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
40108777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
402de629b6eSShawn McCarney                     objectsWithConnection.insert(
403de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
40408777fb0SLewanczyk, Dawid                 }
40508777fb0SLewanczyk, Dawid             }
40608777fb0SLewanczyk, Dawid         }
40755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
408413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
409413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
410*e99073f5SGeorge Liu         });
411413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
412413961deSRichard Marian Thomaiyar }
413413961deSRichard Marian Thomaiyar 
414413961deSRichard Marian Thomaiyar /**
415413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
416413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
417413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
418413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
419413961deSRichard Marian Thomaiyar  */
420413961deSRichard Marian Thomaiyar template <typename Callback>
421fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
422fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
423413961deSRichard Marian Thomaiyar                     Callback&& callback)
424413961deSRichard Marian Thomaiyar {
425413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
426fe04d49cSNan Zhou         [callback](const std::set<std::string>& connections,
427413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4283174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
42981ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
430413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
43108777fb0SLewanczyk, Dawid }
43208777fb0SLewanczyk, Dawid 
43308777fb0SLewanczyk, Dawid /**
43449c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43549c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43649c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
43749c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
43849c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
43949c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
44049c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
44149c53ac9SJohnathan Mantey  */
44223a21a1cSEd Tanous inline void reduceSensorList(
4437f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
444cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
44549c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
446fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
44749c53ac9SJohnathan Mantey {
44849c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
44949c53ac9SJohnathan Mantey     {
4507f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
4517f1cc26dSEd Tanous                                    chassisSubNode == sensors::node::thermal
452a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
45349c53ac9SJohnathan Mantey                                        : "Voltages");
45449c53ac9SJohnathan Mantey 
45549c53ac9SJohnathan Mantey         return;
45649c53ac9SJohnathan Mantey     }
45749c53ac9SJohnathan Mantey     if (allSensors->empty())
45849c53ac9SJohnathan Mantey     {
45949c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
46049c53ac9SJohnathan Mantey         return;
46149c53ac9SJohnathan Mantey     }
46249c53ac9SJohnathan Mantey 
4637f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
46449c53ac9SJohnathan Mantey     {
46549c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46649c53ac9SJohnathan Mantey         {
46711ba3979SEd Tanous             if (sensor.starts_with(type))
46849c53ac9SJohnathan Mantey             {
46949c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
47049c53ac9SJohnathan Mantey             }
47149c53ac9SJohnathan Mantey         }
47249c53ac9SJohnathan Mantey     }
47349c53ac9SJohnathan Mantey }
47449c53ac9SJohnathan Mantey 
4757f1cc26dSEd Tanous /*
4767f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
4777f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
4787f1cc26dSEd Tanous  *
4797f1cc26dSEd Tanous  * */
4807f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
4817f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
4827f1cc26dSEd Tanous {
4837f1cc26dSEd Tanous     if (chassisSubNode == sensors::node::power)
4847f1cc26dSEd Tanous     {
4857f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
4867f1cc26dSEd Tanous     }
4877f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::thermal)
4887f1cc26dSEd Tanous     {
4897f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
4907f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
4917f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
4927f1cc26dSEd Tanous     }
4937f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::sensors)
4947f1cc26dSEd Tanous     {
4957f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
4967f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
4977f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
4987f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
4997f1cc26dSEd Tanous     }
5007f1cc26dSEd Tanous 
5017f1cc26dSEd Tanous     if (chassisSubNode != sensors::node::sensors)
5027f1cc26dSEd Tanous     {
5037f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
5047f1cc26dSEd Tanous     }
5057f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
5067f1cc26dSEd Tanous }
5077f1cc26dSEd Tanous 
50849c53ac9SJohnathan Mantey /**
50908777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
510588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
51108777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
51208777fb0SLewanczyk, Dawid  */
51308777fb0SLewanczyk, Dawid template <typename Callback>
5147f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5157f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
516cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
517cf9e417dSEd Tanous                 Callback&& callback)
5181abe55efSEd Tanous {
51955c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
5207a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
52149c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
522adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
5237a1dbc48SGeorge Liu 
5247a1dbc48SGeorge Liu     // Get the Chassis Collection
5257a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
5267a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
5277f1cc26dSEd Tanous         [callback{std::forward<Callback>(callback)}, asyncResp,
5287f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
5297f1cc26dSEd Tanous          chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
5307a1dbc48SGeorge Liu             const boost::system::error_code& ec,
531002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
53255c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5331abe55efSEd Tanous         if (ec)
5341abe55efSEd Tanous         {
53555c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5367f1cc26dSEd Tanous             messages::internalError(asyncResp->res);
53708777fb0SLewanczyk, Dawid             return;
53808777fb0SLewanczyk, Dawid         }
53949c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
54049c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5411abe55efSEd Tanous         {
54228aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
543f8fe53e7SEd Tanous             std::string chassisName = path.filename();
54428aa8de5SGeorge Liu             if (chassisName.empty())
5451abe55efSEd Tanous             {
54649c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
547daf36e2eSEd Tanous                 continue;
548daf36e2eSEd Tanous             }
5497f1cc26dSEd Tanous             if (chassisName == chassisIdStr)
5501abe55efSEd Tanous             {
55149c53ac9SJohnathan Mantey                 chassisPath = &chassis;
55249c53ac9SJohnathan Mantey                 break;
553daf36e2eSEd Tanous             }
55449c53ac9SJohnathan Mantey         }
55549c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5561abe55efSEd Tanous         {
5577f1cc26dSEd Tanous             messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
55849c53ac9SJohnathan Mantey             return;
5591abe55efSEd Tanous         }
5607f1cc26dSEd Tanous         populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
56108777fb0SLewanczyk, Dawid 
5627f1cc26dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
5637f1cc26dSEd Tanous             "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
56495a3ecadSAnthony Wilson 
5658fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5668fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
5671e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
5681e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
5691e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
5707f1cc26dSEd Tanous             [asyncResp, chassisSubNode, sensorTypes,
571f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
572271584abSEd Tanous                 const boost::system::error_code& e,
5731e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
574271584abSEd Tanous             if (e)
57549c53ac9SJohnathan Mantey             {
576271584abSEd Tanous                 if (e.value() != EBADR)
57749c53ac9SJohnathan Mantey                 {
5787f1cc26dSEd Tanous                     messages::internalError(asyncResp->res);
57949c53ac9SJohnathan Mantey                     return;
58049c53ac9SJohnathan Mantey                 }
58149c53ac9SJohnathan Mantey             }
582fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>> culledSensorList =
583fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
5847f1cc26dSEd Tanous             reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
5857f1cc26dSEd Tanous                              &nodeSensorList, culledSensorList);
5867f1cc26dSEd Tanous             BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
58749c53ac9SJohnathan Mantey             callback(culledSensorList);
5881e1e598dSJonathan Doman             });
5897a1dbc48SGeorge Liu         });
59055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
59108777fb0SLewanczyk, Dawid }
59208777fb0SLewanczyk, Dawid 
59308777fb0SLewanczyk, Dawid /**
594adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
595adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
596adc4f0dbSShawn McCarney  * @return State value for inventory item.
59734dd179eSJames Feist  */
59823a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
599adc4f0dbSShawn McCarney {
600adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
601adc4f0dbSShawn McCarney     {
602adc4f0dbSShawn McCarney         return "Absent";
603adc4f0dbSShawn McCarney     }
60434dd179eSJames Feist 
605adc4f0dbSShawn McCarney     return "Enabled";
606adc4f0dbSShawn McCarney }
607adc4f0dbSShawn McCarney 
608adc4f0dbSShawn McCarney /**
609adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
610adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
6111d7c0054SEd Tanous  * @param valuesDict Map of all sensor DBus values.
612adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
613adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
614adc4f0dbSShawn McCarney  * @return Health value for sensor.
615adc4f0dbSShawn McCarney  */
6161d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson,
6171d7c0054SEd Tanous                              const dbus::utility::DBusPropertiesMap& valuesDict,
618adc4f0dbSShawn McCarney                              const InventoryItem* inventoryItem)
61934dd179eSJames Feist {
620adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
621adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
622adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
623adc4f0dbSShawn McCarney     std::string currentHealth;
624adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
625adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
626adc4f0dbSShawn McCarney     {
627adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
628adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
629adc4f0dbSShawn McCarney         {
630adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
631adc4f0dbSShawn McCarney             if (health != nullptr)
632adc4f0dbSShawn McCarney             {
633adc4f0dbSShawn McCarney                 currentHealth = *health;
634adc4f0dbSShawn McCarney             }
635adc4f0dbSShawn McCarney         }
636adc4f0dbSShawn McCarney     }
637adc4f0dbSShawn McCarney 
638adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
639adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
640adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
641adc4f0dbSShawn McCarney     {
642adc4f0dbSShawn McCarney         return "Critical";
643adc4f0dbSShawn McCarney     }
644adc4f0dbSShawn McCarney 
645c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmHigh = nullptr;
646c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmLow = nullptr;
647c1343bf6SKrzysztof Grobelny     const bool* warningAlarmHigh = nullptr;
648c1343bf6SKrzysztof Grobelny     const bool* warningAlarmLow = nullptr;
649711ac7a9SEd Tanous 
650c1343bf6SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
651c1343bf6SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
652c1343bf6SKrzysztof Grobelny         criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
653c1343bf6SKrzysztof Grobelny         "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
654c1343bf6SKrzysztof Grobelny         warningAlarmLow);
655c1343bf6SKrzysztof Grobelny 
656c1343bf6SKrzysztof Grobelny     if (success)
65734dd179eSJames Feist     {
658c1343bf6SKrzysztof Grobelny         // Check if sensor has critical threshold alarm
659c1343bf6SKrzysztof Grobelny         if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
660c1343bf6SKrzysztof Grobelny             (criticalAlarmLow != nullptr && *criticalAlarmLow))
66134dd179eSJames Feist         {
66234dd179eSJames Feist             return "Critical";
66334dd179eSJames Feist         }
66434dd179eSJames Feist     }
66534dd179eSJames Feist 
666adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
667adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
668adc4f0dbSShawn McCarney     {
669adc4f0dbSShawn McCarney         return "Critical";
670adc4f0dbSShawn McCarney     }
671adc4f0dbSShawn McCarney 
672adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that. This
673adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
674adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
675adc4f0dbSShawn McCarney     {
676adc4f0dbSShawn McCarney         return "Warning";
677adc4f0dbSShawn McCarney     }
678adc4f0dbSShawn McCarney 
679c1343bf6SKrzysztof Grobelny     if (success)
680c1343bf6SKrzysztof Grobelny     {
681adc4f0dbSShawn McCarney         // Check if sensor has warning threshold alarm
682c1343bf6SKrzysztof Grobelny         if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
683c1343bf6SKrzysztof Grobelny             (warningAlarmLow != nullptr && *warningAlarmLow))
68434dd179eSJames Feist         {
685ebe4d91eSEd Tanous             return "Warning";
68634dd179eSJames Feist         }
68734dd179eSJames Feist     }
688adc4f0dbSShawn McCarney 
68934dd179eSJames Feist     return "OK";
69034dd179eSJames Feist }
69134dd179eSJames Feist 
69223a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
693d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
694d500549bSAnthony Wilson {
695d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
696d500549bSAnthony Wilson     {
697d500549bSAnthony Wilson         switch (inventoryItem->ledState)
698d500549bSAnthony Wilson         {
699d500549bSAnthony Wilson             case LedState::OFF:
700d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
701d500549bSAnthony Wilson                 break;
702d500549bSAnthony Wilson             case LedState::ON:
703d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
704d500549bSAnthony Wilson                 break;
705d500549bSAnthony Wilson             case LedState::BLINK:
706d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
707d500549bSAnthony Wilson                 break;
70823a21a1cSEd Tanous             case LedState::UNKNOWN:
709d500549bSAnthony Wilson                 break;
710d500549bSAnthony Wilson         }
711d500549bSAnthony Wilson     }
712d500549bSAnthony Wilson }
713d500549bSAnthony Wilson 
71434dd179eSJames Feist /**
71508777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
71608777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
717274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
71808777fb0SLewanczyk, Dawid  * build
7191d7c0054SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
7201d7c0054SEd Tanous  * @param propertiesDict A dictionary of the properties to build the sensor
7211d7c0054SEd Tanous  * from.
7221d7c0054SEd Tanous  * @param sensorJson  The json object to fill
723adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
724adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
72508777fb0SLewanczyk, Dawid  */
7261d7c0054SEd Tanous inline void objectPropertiesToJson(
7271d7c0054SEd Tanous     std::string_view sensorName, std::string_view sensorType,
7281d7c0054SEd Tanous     std::string_view chassisSubNode,
7291d7c0054SEd Tanous     const dbus::utility::DBusPropertiesMap& propertiesDict,
73081ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
7311abe55efSEd Tanous {
7321d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
733adc4f0dbSShawn McCarney     {
734c71d6125SEd Tanous         std::string subNodeEscaped(sensorType);
735c1d019a6SEd Tanous         subNodeEscaped.erase(
736c1d019a6SEd Tanous             std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
737c1d019a6SEd Tanous             subNodeEscaped.end());
738c1d019a6SEd Tanous 
739c1d019a6SEd Tanous         // For sensors in SensorCollection we set Id instead of MemberId,
740c1d019a6SEd Tanous         // including power sensors.
741c1d019a6SEd Tanous         subNodeEscaped += '_';
742c1d019a6SEd Tanous         subNodeEscaped += sensorName;
743c1d019a6SEd Tanous         sensorJson["Id"] = std::move(subNodeEscaped);
744c1d019a6SEd Tanous 
7451d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7461d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7471d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
74895a3ecadSAnthony Wilson     }
74995a3ecadSAnthony Wilson     else if (sensorType != "power")
75095a3ecadSAnthony Wilson     {
75195a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
75295a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
75395a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
75481ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
7551d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7561d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7571d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
758adc4f0dbSShawn McCarney     }
759e742b6ccSEd Tanous 
76081ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
76181ce609eSEd Tanous     sensorJson["Status"]["Health"] =
7621d7c0054SEd Tanous         getHealth(sensorJson, propertiesDict, inventoryItem);
76308777fb0SLewanczyk, Dawid 
76408777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
76508777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
76608777fb0SLewanczyk, Dawid     // that require integers, not floats.
76708777fb0SLewanczyk, Dawid     bool forceToInt = false;
76808777fb0SLewanczyk, Dawid 
7693929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
7701d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
77195a3ecadSAnthony Wilson     {
7722a4ba195SShounak Mitra         sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
773c2bf7f99SWludzik, Jozef 
7740ec8b83dSEd Tanous         sensor::ReadingType readingType = sensors::toReadingType(sensorType);
7750ec8b83dSEd Tanous         if (readingType == sensor::ReadingType::Invalid)
77695a3ecadSAnthony Wilson         {
777c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
778c2bf7f99SWludzik, Jozef                              << sensorType;
77995a3ecadSAnthony Wilson         }
780c2bf7f99SWludzik, Jozef         else
78195a3ecadSAnthony Wilson         {
782c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
78395a3ecadSAnthony Wilson         }
784c2bf7f99SWludzik, Jozef 
7851d7c0054SEd Tanous         std::string_view readingUnits = sensors::toReadingUnits(sensorType);
786c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
787f8ede15eSAdrian Ambrożewicz         {
788c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
789c2bf7f99SWludzik, Jozef                              << sensorType;
790c2bf7f99SWludzik, Jozef         }
791c2bf7f99SWludzik, Jozef         else
792c2bf7f99SWludzik, Jozef         {
793c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
794f8ede15eSAdrian Ambrożewicz         }
79595a3ecadSAnthony Wilson     }
79695a3ecadSAnthony Wilson     else if (sensorType == "temperature")
7971abe55efSEd Tanous     {
7983929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
79981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
80008777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
80108777fb0SLewanczyk, Dawid         // implementation seems to implement fan
8021abe55efSEd Tanous     }
8031abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
8041abe55efSEd Tanous     {
8053929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
80681ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
80781ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
80881ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
80908777fb0SLewanczyk, Dawid         forceToInt = true;
8101abe55efSEd Tanous     }
8116f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
8126f6d0d32SEd Tanous     {
8133929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
81481ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
81581ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
81681ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
8176f6d0d32SEd Tanous         forceToInt = true;
8186f6d0d32SEd Tanous     }
8191abe55efSEd Tanous     else if (sensorType == "voltage")
8201abe55efSEd Tanous     {
8213929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
82281ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
8231abe55efSEd Tanous     }
8242474adfaSEd Tanous     else if (sensorType == "power")
8252474adfaSEd Tanous     {
8261d7c0054SEd Tanous         if (boost::iequals(sensorName, "total_power"))
827028f7ebcSEddie James         {
82881ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
8297ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
8307ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
83181ce609eSEd Tanous             sensorJson["MemberId"] = "0";
83281ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
8333929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
834028f7ebcSEddie James         }
8351d7c0054SEd Tanous         else if (boost::ifind_first(sensorName, "input").empty())
83649c53ac9SJohnathan Mantey         {
8373929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
83849c53ac9SJohnathan Mantey         }
83949c53ac9SJohnathan Mantey         else
84049c53ac9SJohnathan Mantey         {
8413929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
84249c53ac9SJohnathan Mantey         }
8432474adfaSEd Tanous     }
8441abe55efSEd Tanous     else
8451abe55efSEd Tanous     {
84655c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
84708777fb0SLewanczyk, Dawid         return;
84808777fb0SLewanczyk, Dawid     }
84908777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
8503929aca1SAnthony Wilson     std::vector<
8513929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
8523929aca1SAnthony Wilson         properties;
85308777fb0SLewanczyk, Dawid     properties.reserve(7);
85408777fb0SLewanczyk, Dawid 
85508777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
856de629b6eSShawn McCarney 
8571d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
8583929aca1SAnthony Wilson     {
8593929aca1SAnthony Wilson         properties.emplace_back(
8603929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
8613929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
8623929aca1SAnthony Wilson         properties.emplace_back(
8633929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
8643929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
8653929aca1SAnthony Wilson         properties.emplace_back(
8663929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
8673929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
8683929aca1SAnthony Wilson         properties.emplace_back(
8693929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
8703929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
8713929aca1SAnthony Wilson     }
8723929aca1SAnthony Wilson     else if (sensorType != "power")
873de629b6eSShawn McCarney     {
87408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8753929aca1SAnthony Wilson                                 "WarningHigh",
8763929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
87708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8783929aca1SAnthony Wilson                                 "WarningLow",
8793929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
88008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8813929aca1SAnthony Wilson                                 "CriticalHigh",
8823929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
88308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8843929aca1SAnthony Wilson                                 "CriticalLow",
8853929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
886de629b6eSShawn McCarney     }
88708777fb0SLewanczyk, Dawid 
8882474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
8892474adfaSEd Tanous 
8901d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
89195a3ecadSAnthony Wilson     {
89295a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8933929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
89495a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8953929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
89651c35a8fSGeorge Liu         properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
89751c35a8fSGeorge Liu                                 "Accuracy", "/Accuracy"_json_pointer);
89895a3ecadSAnthony Wilson     }
89995a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9001abe55efSEd Tanous     {
90108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9023929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
90308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9043929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
9051abe55efSEd Tanous     }
906adc4f0dbSShawn McCarney     else if (sensorType != "power")
9071abe55efSEd Tanous     {
90808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9093929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
91008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9113929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
91208777fb0SLewanczyk, Dawid     }
91308777fb0SLewanczyk, Dawid 
9143929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
9153929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
9161abe55efSEd Tanous     {
9171d7c0054SEd Tanous         for (const auto& [valueName, valueVariant] : propertiesDict)
918711ac7a9SEd Tanous         {
919711ac7a9SEd Tanous             if (valueName != std::get<1>(p))
920711ac7a9SEd Tanous             {
921711ac7a9SEd Tanous                 continue;
922711ac7a9SEd Tanous             }
9233929aca1SAnthony Wilson 
9243929aca1SAnthony Wilson             // The property we want to set may be nested json, so use
9253929aca1SAnthony Wilson             // a json_pointer for easy indexing into the json structure.
9263929aca1SAnthony Wilson             const nlohmann::json::json_pointer& key = std::get<2>(p);
9273929aca1SAnthony Wilson 
928abf2add6SEd Tanous             const double* doubleValue = std::get_if<double>(&valueVariant);
92940e4f380SEd Tanous             if (doubleValue == nullptr)
9301abe55efSEd Tanous             {
93140e4f380SEd Tanous                 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
9326f6d0d32SEd Tanous                 continue;
93308777fb0SLewanczyk, Dawid             }
9346f6d0d32SEd Tanous             if (forceToInt)
9356f6d0d32SEd Tanous             {
93640e4f380SEd Tanous                 sensorJson[key] = static_cast<int64_t>(*doubleValue);
9376f6d0d32SEd Tanous             }
9386f6d0d32SEd Tanous             else
9396f6d0d32SEd Tanous             {
94040e4f380SEd Tanous                 sensorJson[key] = *doubleValue;
94108777fb0SLewanczyk, Dawid             }
94208777fb0SLewanczyk, Dawid         }
94308777fb0SLewanczyk, Dawid     }
94408777fb0SLewanczyk, Dawid }
94508777fb0SLewanczyk, Dawid 
9461d7c0054SEd Tanous /**
9471d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
9481d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
9491d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
9501d7c0054SEd Tanous  * build
9511d7c0054SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
9521d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
9531d7c0054SEd Tanous  * interfaces to be built from
9541d7c0054SEd Tanous  * @param sensorJson  The json object to fill
9551d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
9561d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
9571d7c0054SEd Tanous  */
9581d7c0054SEd Tanous inline void objectInterfacesToJson(
9591d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
9601d7c0054SEd Tanous     const std::string& chassisSubNode,
9611d7c0054SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
9621d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
9631d7c0054SEd Tanous {
9641d7c0054SEd Tanous 
9651d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
9661d7c0054SEd Tanous     {
9671d7c0054SEd Tanous         objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
9681d7c0054SEd Tanous                                valuesDict, sensorJson, inventoryItem);
9691d7c0054SEd Tanous     }
970c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
9711d7c0054SEd Tanous }
9721d7c0054SEd Tanous 
973b5a76932SEd Tanous inline void populateFanRedundancy(
974b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
9758bd25ccdSJames Feist {
976*e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
977*e99073f5SGeorge Liu         "xyz.openbmc_project.Control.FanRedundancy"};
978*e99073f5SGeorge Liu     dbus::utility::getSubTree(
979*e99073f5SGeorge Liu         "/xyz/openbmc_project/control", 2, interfaces,
980b9d36b47SEd Tanous         [sensorsAsyncResp](
981*e99073f5SGeorge Liu             const boost::system::error_code& ec,
982b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
9838bd25ccdSJames Feist         if (ec)
9848bd25ccdSJames Feist         {
9858bd25ccdSJames Feist             return; // don't have to have this interface
9868bd25ccdSJames Feist         }
987002d39b4SEd Tanous         for (const std::pair<
988002d39b4SEd Tanous                  std::string,
989002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
990e278c18fSEd Tanous                  pathPair : resp)
9918bd25ccdSJames Feist         {
992e278c18fSEd Tanous             const std::string& path = pathPair.first;
993002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
994002d39b4SEd Tanous                 objDict = pathPair.second;
9958bd25ccdSJames Feist             if (objDict.empty())
9968bd25ccdSJames Feist             {
9978bd25ccdSJames Feist                 continue; // this should be impossible
9988bd25ccdSJames Feist             }
9998bd25ccdSJames Feist 
10008bd25ccdSJames Feist             const std::string& owner = objDict.begin()->first;
10011e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::vector<std::string>>(
10021e1e598dSJonathan Doman                 *crow::connections::systemBus,
10031e1e598dSJonathan Doman                 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
10041e1e598dSJonathan Doman                 "xyz.openbmc_project.Association", "endpoints",
1005002d39b4SEd Tanous                 [path, owner,
1006002d39b4SEd Tanous                  sensorsAsyncResp](const boost::system::error_code e,
10071e1e598dSJonathan Doman                                    const std::vector<std::string>& endpoints) {
1008271584abSEd Tanous                 if (e)
10098bd25ccdSJames Feist                 {
10108bd25ccdSJames Feist                     return; // if they don't have an association we
10118bd25ccdSJames Feist                             // can't tell what chassis is
10128bd25ccdSJames Feist                 }
1013002d39b4SEd Tanous                 auto found =
1014002d39b4SEd Tanous                     std::find_if(endpoints.begin(), endpoints.end(),
10158bd25ccdSJames Feist                                  [sensorsAsyncResp](const std::string& entry) {
1016002d39b4SEd Tanous                     return entry.find(sensorsAsyncResp->chassisId) !=
10178bd25ccdSJames Feist                            std::string::npos;
10188bd25ccdSJames Feist                     });
10198bd25ccdSJames Feist 
10201e1e598dSJonathan Doman                 if (found == endpoints.end())
10218bd25ccdSJames Feist                 {
10228bd25ccdSJames Feist                     return;
10238bd25ccdSJames Feist                 }
102486d89ed7SKrzysztof Grobelny                 sdbusplus::asio::getAllProperties(
102586d89ed7SKrzysztof Grobelny                     *crow::connections::systemBus, owner, path,
102686d89ed7SKrzysztof Grobelny                     "xyz.openbmc_project.Control.FanRedundancy",
10278bd25ccdSJames Feist                     [path, sensorsAsyncResp](
1028271584abSEd Tanous                         const boost::system::error_code& err,
102986d89ed7SKrzysztof Grobelny                         const dbus::utility::DBusPropertiesMap& ret) {
1030271584abSEd Tanous                     if (err)
10318bd25ccdSJames Feist                     {
10328bd25ccdSJames Feist                         return; // don't have to have this
10338bd25ccdSJames Feist                                 // interface
10348bd25ccdSJames Feist                     }
10358bd25ccdSJames Feist 
103686d89ed7SKrzysztof Grobelny                     const uint8_t* allowedFailures = nullptr;
103786d89ed7SKrzysztof Grobelny                     const std::vector<std::string>* collection = nullptr;
103886d89ed7SKrzysztof Grobelny                     const std::string* status = nullptr;
103986d89ed7SKrzysztof Grobelny 
104086d89ed7SKrzysztof Grobelny                     const bool success = sdbusplus::unpackPropertiesNoThrow(
104186d89ed7SKrzysztof Grobelny                         dbus_utils::UnpackErrorPrinter(), ret,
104286d89ed7SKrzysztof Grobelny                         "AllowedFailures", allowedFailures, "Collection",
104386d89ed7SKrzysztof Grobelny                         collection, "Status", status);
104486d89ed7SKrzysztof Grobelny 
104586d89ed7SKrzysztof Grobelny                     if (!success)
104686d89ed7SKrzysztof Grobelny                     {
104786d89ed7SKrzysztof Grobelny                         messages::internalError(
104886d89ed7SKrzysztof Grobelny                             sensorsAsyncResp->asyncResp->res);
104986d89ed7SKrzysztof Grobelny                         return;
105086d89ed7SKrzysztof Grobelny                     }
105186d89ed7SKrzysztof Grobelny 
105286d89ed7SKrzysztof Grobelny                     if (allowedFailures == nullptr || collection == nullptr ||
105386d89ed7SKrzysztof Grobelny                         status == nullptr)
10548bd25ccdSJames Feist                     {
1055002d39b4SEd Tanous                         BMCWEB_LOG_ERROR << "Invalid redundancy interface";
10568bd25ccdSJames Feist                         messages::internalError(
10578d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
10588bd25ccdSJames Feist                         return;
10598bd25ccdSJames Feist                     }
10608bd25ccdSJames Feist 
1061002d39b4SEd Tanous                     sdbusplus::message::object_path objectPath(path);
106228aa8de5SGeorge Liu                     std::string name = objectPath.filename();
106328aa8de5SGeorge Liu                     if (name.empty())
10648bd25ccdSJames Feist                     {
10658bd25ccdSJames Feist                         // this should be impossible
10668bd25ccdSJames Feist                         messages::internalError(
10678d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
10688bd25ccdSJames Feist                         return;
10698bd25ccdSJames Feist                     }
1070002d39b4SEd Tanous                     std::replace(name.begin(), name.end(), '_', ' ');
10718bd25ccdSJames Feist 
10728bd25ccdSJames Feist                     std::string health;
10738bd25ccdSJames Feist 
107411ba3979SEd Tanous                     if (status->ends_with("Full"))
10758bd25ccdSJames Feist                     {
10768bd25ccdSJames Feist                         health = "OK";
10778bd25ccdSJames Feist                     }
107811ba3979SEd Tanous                     else if (status->ends_with("Degraded"))
10798bd25ccdSJames Feist                     {
10808bd25ccdSJames Feist                         health = "Warning";
10818bd25ccdSJames Feist                     }
10828bd25ccdSJames Feist                     else
10838bd25ccdSJames Feist                     {
10848bd25ccdSJames Feist                         health = "Critical";
10858bd25ccdSJames Feist                     }
10861476687dSEd Tanous                     nlohmann::json::array_t redfishCollection;
10878bd25ccdSJames Feist                     const auto& fanRedfish =
1088002d39b4SEd Tanous                         sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
10898bd25ccdSJames Feist                     for (const std::string& item : *collection)
10908bd25ccdSJames Feist                     {
10918a592810SEd Tanous                         sdbusplus::message::object_path itemPath(item);
10928a592810SEd Tanous                         std::string itemName = itemPath.filename();
109328aa8de5SGeorge Liu                         if (itemName.empty())
109428aa8de5SGeorge Liu                         {
109528aa8de5SGeorge Liu                             continue;
109628aa8de5SGeorge Liu                         }
10978bd25ccdSJames Feist                         /*
10988bd25ccdSJames Feist                         todo(ed): merge patch that fixes the names
10998bd25ccdSJames Feist                         std::replace(itemName.begin(),
11008bd25ccdSJames Feist                                      itemName.end(), '_', ' ');*/
1101002d39b4SEd Tanous                         auto schemaItem =
1102002d39b4SEd Tanous                             std::find_if(fanRedfish.begin(), fanRedfish.end(),
11038bd25ccdSJames Feist                                          [itemName](const nlohmann::json& fan) {
11048bd25ccdSJames Feist                             return fan["MemberId"] == itemName;
11058bd25ccdSJames Feist                             });
11068bd25ccdSJames Feist                         if (schemaItem != fanRedfish.end())
11078bd25ccdSJames Feist                         {
11088a592810SEd Tanous                             nlohmann::json::object_t collectionId;
11098a592810SEd Tanous                             collectionId["@odata.id"] =
11101476687dSEd Tanous                                 (*schemaItem)["@odata.id"];
11111476687dSEd Tanous                             redfishCollection.emplace_back(
11128a592810SEd Tanous                                 std::move(collectionId));
11138bd25ccdSJames Feist                         }
11148bd25ccdSJames Feist                         else
11158bd25ccdSJames Feist                         {
1116002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "failed to find fan in schema";
11178bd25ccdSJames Feist                             messages::internalError(
11188d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
11198bd25ccdSJames Feist                             return;
11208bd25ccdSJames Feist                         }
11218bd25ccdSJames Feist                     }
11228bd25ccdSJames Feist 
11233e9e72ebSKuiying Wang                     size_t minNumNeeded =
112426f6976fSEd Tanous                         collection->empty()
112526f6976fSEd Tanous                             ? 0
112626f6976fSEd Tanous                             : collection->size() - *allowedFailures;
1127002d39b4SEd Tanous                     nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
11288bd25ccdSJames Feist                                                 .jsonValue["Redundancy"];
11291476687dSEd Tanous 
11301476687dSEd Tanous                     nlohmann::json::object_t redundancy;
11311476687dSEd Tanous                     redundancy["@odata.id"] =
1132002d39b4SEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1133002d39b4SEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode +
1134002d39b4SEd Tanous                         "#/Redundancy/" + std::to_string(jResp.size());
1135002d39b4SEd Tanous                     redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
11361476687dSEd Tanous                     redundancy["MinNumNeeded"] = minNumNeeded;
11371476687dSEd Tanous                     redundancy["MemberId"] = name;
11381476687dSEd Tanous                     redundancy["Mode"] = "N+m";
11391476687dSEd Tanous                     redundancy["Name"] = name;
11401476687dSEd Tanous                     redundancy["RedundancySet"] = redfishCollection;
11411476687dSEd Tanous                     redundancy["Status"]["Health"] = health;
11421476687dSEd Tanous                     redundancy["Status"]["State"] = "Enabled";
11431476687dSEd Tanous 
11441476687dSEd Tanous                     jResp.push_back(std::move(redundancy));
114586d89ed7SKrzysztof Grobelny                     });
11461e1e598dSJonathan Doman                 });
11478bd25ccdSJames Feist         }
1148*e99073f5SGeorge Liu         });
11498bd25ccdSJames Feist }
11508bd25ccdSJames Feist 
1151b5a76932SEd Tanous inline void
115281ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
115349c53ac9SJohnathan Mantey {
11548d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
115549c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
115681ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
115749c53ac9SJohnathan Mantey     {
115849c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
115949c53ac9SJohnathan Mantey     }
116049c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
116149c53ac9SJohnathan Mantey     {
116249c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
116349c53ac9SJohnathan Mantey         if (entry != response.end())
116449c53ac9SJohnathan Mantey         {
116549c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
116602cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
116749c53ac9SJohnathan Mantey                 return c1["Name"] < c2["Name"];
116849c53ac9SJohnathan Mantey             });
116949c53ac9SJohnathan Mantey 
117049c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
117149c53ac9SJohnathan Mantey             size_t count = 0;
117249c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
117349c53ac9SJohnathan Mantey             {
117449c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
117549c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
117649c53ac9SJohnathan Mantey                 {
117749c53ac9SJohnathan Mantey                     continue;
117849c53ac9SJohnathan Mantey                 }
117949c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
118049c53ac9SJohnathan Mantey                 if (value != nullptr)
118149c53ac9SJohnathan Mantey                 {
118249c53ac9SJohnathan Mantey                     *value += std::to_string(count);
118349c53ac9SJohnathan Mantey                     count++;
118481ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
118549c53ac9SJohnathan Mantey                 }
118649c53ac9SJohnathan Mantey             }
118749c53ac9SJohnathan Mantey         }
118849c53ac9SJohnathan Mantey     }
118949c53ac9SJohnathan Mantey }
119049c53ac9SJohnathan Mantey 
119108777fb0SLewanczyk, Dawid /**
1192adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1193adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1194adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1195adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
11968fb49dd6SShawn McCarney  */
119723a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1198b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1199adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
12008fb49dd6SShawn McCarney {
1201adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
12028fb49dd6SShawn McCarney     {
1203adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
12048fb49dd6SShawn McCarney         {
1205adc4f0dbSShawn McCarney             return &inventoryItem;
12068fb49dd6SShawn McCarney         }
12078fb49dd6SShawn McCarney     }
12088fb49dd6SShawn McCarney     return nullptr;
12098fb49dd6SShawn McCarney }
12108fb49dd6SShawn McCarney 
12118fb49dd6SShawn McCarney /**
1212adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1213adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1214adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1215adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12168fb49dd6SShawn McCarney  */
121723a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1218b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1219adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1220adc4f0dbSShawn McCarney {
1221adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1222adc4f0dbSShawn McCarney     {
1223adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1224adc4f0dbSShawn McCarney         {
1225adc4f0dbSShawn McCarney             return &inventoryItem;
1226adc4f0dbSShawn McCarney         }
1227adc4f0dbSShawn McCarney     }
1228adc4f0dbSShawn McCarney     return nullptr;
1229adc4f0dbSShawn McCarney }
1230adc4f0dbSShawn McCarney 
1231adc4f0dbSShawn McCarney /**
1232d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1233d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1234d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1235d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1236d500549bSAnthony Wilson  */
1237d500549bSAnthony Wilson inline InventoryItem*
1238d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1239d500549bSAnthony Wilson                             const std::string& ledObjPath)
1240d500549bSAnthony Wilson {
1241d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1242d500549bSAnthony Wilson     {
1243d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1244d500549bSAnthony Wilson         {
1245d500549bSAnthony Wilson             return &inventoryItem;
1246d500549bSAnthony Wilson         }
1247d500549bSAnthony Wilson     }
1248d500549bSAnthony Wilson     return nullptr;
1249d500549bSAnthony Wilson }
1250d500549bSAnthony Wilson 
1251d500549bSAnthony Wilson /**
1252adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1253adc4f0dbSShawn McCarney  *
1254adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1255adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1256adc4f0dbSShawn McCarney  * added to the vector.
1257adc4f0dbSShawn McCarney  *
1258adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1259adc4f0dbSShawn McCarney  * InventoryItem.
1260adc4f0dbSShawn McCarney  *
1261adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1262adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1263adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1264adc4f0dbSShawn McCarney  */
1265b5a76932SEd Tanous inline void addInventoryItem(
1266b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1267b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1268adc4f0dbSShawn McCarney {
1269adc4f0dbSShawn McCarney     // Look for inventory item in vector
1270adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1271adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1272adc4f0dbSShawn McCarney 
1273adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1274adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1275adc4f0dbSShawn McCarney     {
1276adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1277adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1278adc4f0dbSShawn McCarney     }
1279adc4f0dbSShawn McCarney 
1280adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1281adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1282adc4f0dbSShawn McCarney }
1283adc4f0dbSShawn McCarney 
1284adc4f0dbSShawn McCarney /**
1285adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1286adc4f0dbSShawn McCarney  *
1287adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1288adc4f0dbSShawn McCarney  * specified InventoryItem.
1289adc4f0dbSShawn McCarney  *
1290adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1291adc4f0dbSShawn McCarney  * response.
1292adc4f0dbSShawn McCarney  *
1293adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1294adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1295adc4f0dbSShawn McCarney  * for the specified inventory item.
1296adc4f0dbSShawn McCarney  */
129723a21a1cSEd Tanous inline void storeInventoryItemData(
1298adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1299711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
13008fb49dd6SShawn McCarney {
1301adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1302711ac7a9SEd Tanous 
13039eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
13048fb49dd6SShawn McCarney     {
1305711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
13068fb49dd6SShawn McCarney         {
13079eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1308711ac7a9SEd Tanous             {
1309711ac7a9SEd Tanous                 if (name == "Present")
1310711ac7a9SEd Tanous                 {
1311711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1312adc4f0dbSShawn McCarney                     if (value != nullptr)
13138fb49dd6SShawn McCarney                     {
1314adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
13158fb49dd6SShawn McCarney                     }
13168fb49dd6SShawn McCarney                 }
13178fb49dd6SShawn McCarney             }
1318711ac7a9SEd Tanous         }
1319adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1320711ac7a9SEd Tanous 
1321711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
13228fb49dd6SShawn McCarney         {
1323adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
13248fb49dd6SShawn McCarney         }
1325adc4f0dbSShawn McCarney 
1326adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1327711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1328adc4f0dbSShawn McCarney         {
13299eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1330711ac7a9SEd Tanous             {
1331711ac7a9SEd Tanous                 if (name == "Manufacturer")
1332adc4f0dbSShawn McCarney                 {
1333adc4f0dbSShawn McCarney                     const std::string* value =
1334711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1335adc4f0dbSShawn McCarney                     if (value != nullptr)
1336adc4f0dbSShawn McCarney                     {
1337adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1338adc4f0dbSShawn McCarney                     }
1339adc4f0dbSShawn McCarney                 }
1340711ac7a9SEd Tanous                 if (name == "Model")
1341adc4f0dbSShawn McCarney                 {
1342adc4f0dbSShawn McCarney                     const std::string* value =
1343711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1344adc4f0dbSShawn McCarney                     if (value != nullptr)
1345adc4f0dbSShawn McCarney                     {
1346adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1347adc4f0dbSShawn McCarney                     }
1348adc4f0dbSShawn McCarney                 }
1349711ac7a9SEd Tanous                 if (name == "SerialNumber")
1350adc4f0dbSShawn McCarney                 {
1351adc4f0dbSShawn McCarney                     const std::string* value =
1352711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1353adc4f0dbSShawn McCarney                     if (value != nullptr)
1354adc4f0dbSShawn McCarney                     {
1355adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1356adc4f0dbSShawn McCarney                     }
1357adc4f0dbSShawn McCarney                 }
1358711ac7a9SEd Tanous                 if (name == "PartNumber")
1359711ac7a9SEd Tanous                 {
1360711ac7a9SEd Tanous                     const std::string* value =
1361711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1362711ac7a9SEd Tanous                     if (value != nullptr)
1363711ac7a9SEd Tanous                     {
1364711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1365711ac7a9SEd Tanous                     }
1366711ac7a9SEd Tanous                 }
1367711ac7a9SEd Tanous             }
1368adc4f0dbSShawn McCarney         }
1369adc4f0dbSShawn McCarney 
1370711ac7a9SEd Tanous         if (interface ==
1371711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1372adc4f0dbSShawn McCarney         {
13739eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1374adc4f0dbSShawn McCarney             {
1375711ac7a9SEd Tanous                 if (name == "Functional")
1376711ac7a9SEd Tanous                 {
1377711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1378adc4f0dbSShawn McCarney                     if (value != nullptr)
1379adc4f0dbSShawn McCarney                     {
1380adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
13818fb49dd6SShawn McCarney                     }
13828fb49dd6SShawn McCarney                 }
13838fb49dd6SShawn McCarney             }
13848fb49dd6SShawn McCarney         }
1385711ac7a9SEd Tanous     }
1386711ac7a9SEd Tanous }
13878fb49dd6SShawn McCarney 
13888fb49dd6SShawn McCarney /**
1389adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
13908fb49dd6SShawn McCarney  *
1391adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1392adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1393adc4f0dbSShawn McCarney  * inventoryItems vector.
13948fb49dd6SShawn McCarney  *
1395adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1396adc4f0dbSShawn McCarney  * response.
1397adc4f0dbSShawn McCarney  *
1398adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1399adc4f0dbSShawn McCarney  * been obtained.
1400adc4f0dbSShawn McCarney  *
1401adc4f0dbSShawn McCarney  * The callback must have the following signature:
1402adc4f0dbSShawn McCarney  *   @code
1403d500549bSAnthony Wilson  *   callback(void)
1404adc4f0dbSShawn McCarney  *   @endcode
1405adc4f0dbSShawn McCarney  *
1406adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1407adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1408adc4f0dbSShawn McCarney  * last asynchronous function has completed.
14098fb49dd6SShawn McCarney  *
14108fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1411adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1412adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
14138fb49dd6SShawn McCarney  * implements ObjectManager.
1414adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1415adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1416adc4f0dbSShawn McCarney  * in recursive calls to this function.
14178fb49dd6SShawn McCarney  */
1418adc4f0dbSShawn McCarney template <typename Callback>
1419adc4f0dbSShawn McCarney static void getInventoryItemsData(
14208fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1421adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1422d0090733SEd Tanous     std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1423d0090733SEd Tanous     size_t invConnectionsIndex = 0)
14248fb49dd6SShawn McCarney {
1425adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
14268fb49dd6SShawn McCarney 
1427adc4f0dbSShawn McCarney     // If no more connections left, call callback
1428adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
14298fb49dd6SShawn McCarney     {
1430d500549bSAnthony Wilson         callback();
1431adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1432adc4f0dbSShawn McCarney         return;
1433adc4f0dbSShawn McCarney     }
1434adc4f0dbSShawn McCarney 
1435adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1436fe04d49cSNan Zhou     auto it = invConnections->begin();
1437fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1438adc4f0dbSShawn McCarney     if (it != invConnections->end())
1439adc4f0dbSShawn McCarney     {
1440adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1441adc4f0dbSShawn McCarney 
14428fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1443d0090733SEd Tanous         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1444d0090733SEd Tanous                             callback{std::forward<Callback>(callback)},
1445d0090733SEd Tanous                             invConnectionsIndex](
144602cad96eSEd Tanous                                const boost::system::error_code ec,
144702cad96eSEd Tanous                                const dbus::utility::ManagedObjectType& resp) {
1448adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
14498fb49dd6SShawn McCarney             if (ec)
14508fb49dd6SShawn McCarney             {
14518fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1452adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
14538d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
14548fb49dd6SShawn McCarney                 return;
14558fb49dd6SShawn McCarney             }
14568fb49dd6SShawn McCarney 
14578fb49dd6SShawn McCarney             // Loop through returned object paths
14588fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
14598fb49dd6SShawn McCarney             {
14608fb49dd6SShawn McCarney                 const std::string& objPath =
14618fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
14628fb49dd6SShawn McCarney 
1463adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1464adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1465adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1466adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
14678fb49dd6SShawn McCarney                 {
1468adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1469adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
14708fb49dd6SShawn McCarney                 }
14718fb49dd6SShawn McCarney             }
14728fb49dd6SShawn McCarney 
1473adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1474adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1475d0090733SEd Tanous                                   invConnections, std::move(callback),
1476d0090733SEd Tanous                                   invConnectionsIndex + 1);
1477adc4f0dbSShawn McCarney 
1478adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
14798fb49dd6SShawn McCarney         };
14808fb49dd6SShawn McCarney 
14818fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
14828fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
1483d0090733SEd Tanous             std::move(respHandler), invConnection,
1484f8bb0ff3SEd Tanous             "/xyz/openbmc_project/inventory",
14858fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14868fb49dd6SShawn McCarney     }
14878fb49dd6SShawn McCarney 
1488adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
14898fb49dd6SShawn McCarney }
14908fb49dd6SShawn McCarney 
14918fb49dd6SShawn McCarney /**
1492adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
14938fb49dd6SShawn McCarney  *
1494adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1495adc4f0dbSShawn McCarney  * items that are associated with sensors.
14968fb49dd6SShawn McCarney  *
14978fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
14988fb49dd6SShawn McCarney  * been obtained.
14998fb49dd6SShawn McCarney  *
15008fb49dd6SShawn McCarney  * The callback must have the following signature:
15018fb49dd6SShawn McCarney  *   @code
1502fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
15038fb49dd6SShawn McCarney  *   @endcode
15048fb49dd6SShawn McCarney  *
15058fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1506adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
15078fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
15088fb49dd6SShawn McCarney  */
15098fb49dd6SShawn McCarney template <typename Callback>
15108fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1511b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1512b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
15138fb49dd6SShawn McCarney     Callback&& callback)
15148fb49dd6SShawn McCarney {
15158fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
15168fb49dd6SShawn McCarney 
15178fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1518*e99073f5SGeorge Liu     constexpr std::array<std::string_view, 4> interfaces = {
15198fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1520adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1521adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
15228fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
15238fb49dd6SShawn McCarney 
1524*e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1525*e99073f5SGeorge Liu     dbus::utility::getSubTree(
1526*e99073f5SGeorge Liu         path, 0, interfaces,
1527002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1528002d39b4SEd Tanous          inventoryItems](
1529*e99073f5SGeorge Liu             const boost::system::error_code& ec,
1530002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1531*e99073f5SGeorge Liu         // Response handler for parsing output from GetSubTree
15328fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
15338fb49dd6SShawn McCarney         if (ec)
15348fb49dd6SShawn McCarney         {
15358d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
15368fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
15378fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
15388fb49dd6SShawn McCarney             return;
15398fb49dd6SShawn McCarney         }
15408fb49dd6SShawn McCarney 
15418fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
1542fe04d49cSNan Zhou         std::shared_ptr<std::set<std::string>> invConnections =
1543fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
15448fb49dd6SShawn McCarney 
15458fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
15468fb49dd6SShawn McCarney         for (const std::pair<
15478fb49dd6SShawn McCarney                  std::string,
15488fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
15498fb49dd6SShawn McCarney                  object : subtree)
15508fb49dd6SShawn McCarney         {
1551adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
15528fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1553adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
15548fb49dd6SShawn McCarney             {
15558fb49dd6SShawn McCarney                 // Store all connections to inventory item
15568fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
15578fb49dd6SShawn McCarney                          objData : object.second)
15588fb49dd6SShawn McCarney                 {
15598fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
15608fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
15618fb49dd6SShawn McCarney                 }
15628fb49dd6SShawn McCarney             }
15638fb49dd6SShawn McCarney         }
1564d500549bSAnthony Wilson 
15658fb49dd6SShawn McCarney         callback(invConnections);
15668fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1567*e99073f5SGeorge Liu         });
15688fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
15698fb49dd6SShawn McCarney }
15708fb49dd6SShawn McCarney 
15718fb49dd6SShawn McCarney /**
1572adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
15738fb49dd6SShawn McCarney  *
15748fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1575d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1576d500549bSAnthony Wilson  * their LEDs, if any.
15778fb49dd6SShawn McCarney  *
15788fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
15798fb49dd6SShawn McCarney  * has been obtained.
15808fb49dd6SShawn McCarney  *
15818fb49dd6SShawn McCarney  * The callback must have the following signature:
15828fb49dd6SShawn McCarney  *   @code
1583adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
15848fb49dd6SShawn McCarney  *   @endcode
15858fb49dd6SShawn McCarney  *
15868fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
15878fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
15888fb49dd6SShawn McCarney  * implements ObjectManager.
15898fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
15908fb49dd6SShawn McCarney  */
15918fb49dd6SShawn McCarney template <typename Callback>
1592adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1593b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1594fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
15958fb49dd6SShawn McCarney     Callback&& callback)
15968fb49dd6SShawn McCarney {
1597adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
15988fb49dd6SShawn McCarney 
15998fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
160002cad96eSEd Tanous     auto respHandler =
160102cad96eSEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
16028fb49dd6SShawn McCarney          sensorNames](const boost::system::error_code ec,
160302cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
1604adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
16058fb49dd6SShawn McCarney         if (ec)
16068fb49dd6SShawn McCarney         {
1607adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1608adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
16098d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
16108fb49dd6SShawn McCarney             return;
16118fb49dd6SShawn McCarney         }
16128fb49dd6SShawn McCarney 
1613adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1614adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1615adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1616adc4f0dbSShawn McCarney 
16178fb49dd6SShawn McCarney         // Loop through returned object paths
16188fb49dd6SShawn McCarney         std::string sensorAssocPath;
16198fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
16208fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
16218fb49dd6SShawn McCarney         {
16228fb49dd6SShawn McCarney             const std::string& objPath =
16238fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
16248fb49dd6SShawn McCarney 
16258fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
16268fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
16278fb49dd6SShawn McCarney             {
16288fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
16298fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
16308fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
16318fb49dd6SShawn McCarney                 {
16328fb49dd6SShawn McCarney                     // Get Association interface for object path
1633711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
16348fb49dd6SShawn McCarney                     {
1635711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1636711ac7a9SEd Tanous                         {
1637711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1638711ac7a9SEd Tanous                             {
1639711ac7a9SEd Tanous                                 if (valueName == "endpoints")
16408fb49dd6SShawn McCarney                                 {
16418fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
16428fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1643711ac7a9SEd Tanous                                             &value);
1644711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1645711ac7a9SEd Tanous                                         !endpoints->empty())
16468fb49dd6SShawn McCarney                                     {
1647adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1648adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1649adc4f0dbSShawn McCarney                                             endpoints->front();
1650711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1651711ac7a9SEd Tanous                                                          invItemPath,
1652adc4f0dbSShawn McCarney                                                          sensorName);
16538fb49dd6SShawn McCarney                                     }
16548fb49dd6SShawn McCarney                                 }
16558fb49dd6SShawn McCarney                             }
1656711ac7a9SEd Tanous                         }
1657711ac7a9SEd Tanous                     }
16588fb49dd6SShawn McCarney                     break;
16598fb49dd6SShawn McCarney                 }
16608fb49dd6SShawn McCarney             }
16618fb49dd6SShawn McCarney         }
16628fb49dd6SShawn McCarney 
1663d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1664d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1665d500549bSAnthony Wilson         std::string inventoryAssocPath;
1666d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1667d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1668d500549bSAnthony Wilson         {
1669d500549bSAnthony Wilson             const std::string& objPath =
1670d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1671d500549bSAnthony Wilson 
1672d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1673d500549bSAnthony Wilson             {
1674d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1675d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1676d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1677d500549bSAnthony Wilson                 {
1678711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1679d500549bSAnthony Wilson                     {
1680711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1681711ac7a9SEd Tanous                         {
1682711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1683711ac7a9SEd Tanous                             {
1684711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1685d500549bSAnthony Wilson                                 {
1686d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1687d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1688711ac7a9SEd Tanous                                             &value);
1689711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1690711ac7a9SEd Tanous                                         !endpoints->empty())
1691d500549bSAnthony Wilson                                     {
1692711ac7a9SEd Tanous                                         // Add inventory item to vector
1693d500549bSAnthony Wilson                                         // Store LED path in inventory item
1694711ac7a9SEd Tanous                                         const std::string& ledPath =
1695711ac7a9SEd Tanous                                             endpoints->front();
1696d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1697d500549bSAnthony Wilson                                     }
1698d500549bSAnthony Wilson                                 }
1699d500549bSAnthony Wilson                             }
1700711ac7a9SEd Tanous                         }
1701711ac7a9SEd Tanous                     }
1702711ac7a9SEd Tanous 
1703d500549bSAnthony Wilson                     break;
1704d500549bSAnthony Wilson                 }
1705d500549bSAnthony Wilson             }
1706d500549bSAnthony Wilson         }
1707adc4f0dbSShawn McCarney         callback(inventoryItems);
1708adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
17098fb49dd6SShawn McCarney     };
17108fb49dd6SShawn McCarney 
17118fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
17128fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
1713d0090733SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
17148fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17158fb49dd6SShawn McCarney 
1716adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
17178fb49dd6SShawn McCarney }
17188fb49dd6SShawn McCarney 
17198fb49dd6SShawn McCarney /**
1720d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1721d500549bSAnthony Wilson  *
1722d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1723d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1724d500549bSAnthony Wilson  * inventoryItems vector.
1725d500549bSAnthony Wilson  *
1726d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1727d500549bSAnthony Wilson  * response.
1728d500549bSAnthony Wilson  *
1729d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1730d500549bSAnthony Wilson  * has been obtained.
1731d500549bSAnthony Wilson  *
1732d500549bSAnthony Wilson  * The callback must have the following signature:
1733d500549bSAnthony Wilson  *   @code
173442cbe538SGunnar Mills  *   callback()
1735d500549bSAnthony Wilson  *   @endcode
1736d500549bSAnthony Wilson  *
1737d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1738d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1739d500549bSAnthony Wilson  * last asynchronous function has completed.
1740d500549bSAnthony Wilson  *
1741d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1742d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1743d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1744d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1745d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1746d500549bSAnthony Wilson  * in recursive calls to this function.
1747d500549bSAnthony Wilson  */
1748d500549bSAnthony Wilson template <typename Callback>
1749d500549bSAnthony Wilson void getInventoryLedData(
1750d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1751d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1752fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1753d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1754d500549bSAnthony Wilson {
1755d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1756d500549bSAnthony Wilson 
1757d500549bSAnthony Wilson     // If no more connections left, call callback
1758d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1759d500549bSAnthony Wilson     {
176042cbe538SGunnar Mills         callback();
1761d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1762d500549bSAnthony Wilson         return;
1763d500549bSAnthony Wilson     }
1764d500549bSAnthony Wilson 
1765d500549bSAnthony Wilson     // Get inventory item data from current connection
1766fe04d49cSNan Zhou     auto it = ledConnections->begin();
1767fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1768d500549bSAnthony Wilson     if (it != ledConnections->end())
1769d500549bSAnthony Wilson     {
1770d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1771d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1772d500549bSAnthony Wilson         // Response handler for Get State property
17731e1e598dSJonathan Doman         auto respHandler =
17741e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1775f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
17761e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1777d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1778d500549bSAnthony Wilson             if (ec)
1779d500549bSAnthony Wilson             {
1780d500549bSAnthony Wilson                 BMCWEB_LOG_ERROR
1781d500549bSAnthony Wilson                     << "getInventoryLedData respHandler DBus error " << ec;
17828d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1783d500549bSAnthony Wilson                 return;
1784d500549bSAnthony Wilson             }
1785d500549bSAnthony Wilson 
17861e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Led state: " << state;
1787d500549bSAnthony Wilson             // Find inventory item with this LED object path
1788d500549bSAnthony Wilson             InventoryItem* inventoryItem =
1789d500549bSAnthony Wilson                 findInventoryItemForLed(*inventoryItems, ledPath);
1790d500549bSAnthony Wilson             if (inventoryItem != nullptr)
1791d500549bSAnthony Wilson             {
1792d500549bSAnthony Wilson                 // Store LED state in InventoryItem
179311ba3979SEd Tanous                 if (state.ends_with("On"))
1794d500549bSAnthony Wilson                 {
1795d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::ON;
1796d500549bSAnthony Wilson                 }
179711ba3979SEd Tanous                 else if (state.ends_with("Blink"))
1798d500549bSAnthony Wilson                 {
1799d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::BLINK;
1800d500549bSAnthony Wilson                 }
180111ba3979SEd Tanous                 else if (state.ends_with("Off"))
1802d500549bSAnthony Wilson                 {
1803d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::OFF;
1804d500549bSAnthony Wilson                 }
1805d500549bSAnthony Wilson                 else
1806d500549bSAnthony Wilson                 {
1807d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::UNKNOWN;
1808d500549bSAnthony Wilson                 }
1809d500549bSAnthony Wilson             }
1810d500549bSAnthony Wilson 
1811d500549bSAnthony Wilson             // Recurse to get LED data from next connection
1812d500549bSAnthony Wilson             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1813d500549bSAnthony Wilson                                 ledConnections, std::move(callback),
1814d500549bSAnthony Wilson                                 ledConnectionsIndex + 1);
1815d500549bSAnthony Wilson 
1816d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1817d500549bSAnthony Wilson         };
1818d500549bSAnthony Wilson 
1819d500549bSAnthony Wilson         // Get the State property for the current LED
18201e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
18211e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
18221e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
18231e1e598dSJonathan Doman             std::move(respHandler));
1824d500549bSAnthony Wilson     }
1825d500549bSAnthony Wilson 
1826d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1827d500549bSAnthony Wilson }
1828d500549bSAnthony Wilson 
1829d500549bSAnthony Wilson /**
1830d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1831d500549bSAnthony Wilson  *
1832d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1833d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1834d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1835d500549bSAnthony Wilson  *
1836d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1837d500549bSAnthony Wilson  * response.
1838d500549bSAnthony Wilson  *
1839d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1840d500549bSAnthony Wilson  * been obtained.
1841d500549bSAnthony Wilson  *
1842d500549bSAnthony Wilson  * The callback must have the following signature:
1843d500549bSAnthony Wilson  *   @code
184442cbe538SGunnar Mills  *   callback()
1845d500549bSAnthony Wilson  *   @endcode
1846d500549bSAnthony Wilson  *
1847d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1848d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1849d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1850d500549bSAnthony Wilson  */
1851d500549bSAnthony Wilson template <typename Callback>
1852d500549bSAnthony Wilson void getInventoryLeds(
1853d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1854d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1855d500549bSAnthony Wilson     Callback&& callback)
1856d500549bSAnthony Wilson {
1857d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1858d500549bSAnthony Wilson 
1859d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1860*e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1861d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1862d500549bSAnthony Wilson 
1863*e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1864*e99073f5SGeorge Liu     dbus::utility::getSubTree(
1865*e99073f5SGeorge Liu         path, 0, interfaces,
1866002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1867002d39b4SEd Tanous          inventoryItems](
1868*e99073f5SGeorge Liu             const boost::system::error_code& ec,
1869002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1870*e99073f5SGeorge Liu         // Response handler for parsing output from GetSubTree
1871d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1872d500549bSAnthony Wilson         if (ec)
1873d500549bSAnthony Wilson         {
18748d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
1875d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1876d500549bSAnthony Wilson                              << ec;
1877d500549bSAnthony Wilson             return;
1878d500549bSAnthony Wilson         }
1879d500549bSAnthony Wilson 
1880d500549bSAnthony Wilson         // Build map of LED object paths to connections
1881fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1882fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
1883d500549bSAnthony Wilson 
1884d500549bSAnthony Wilson         // Loop through objects from GetSubTree
1885d500549bSAnthony Wilson         for (const std::pair<
1886d500549bSAnthony Wilson                  std::string,
1887d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1888d500549bSAnthony Wilson                  object : subtree)
1889d500549bSAnthony Wilson         {
1890d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
1891d500549bSAnthony Wilson             // items
1892d500549bSAnthony Wilson             const std::string& ledPath = object.first;
1893d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1894d500549bSAnthony Wilson             {
1895d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
1896d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
1897d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
1898d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1899d500549bSAnthony Wilson                                  << connection;
1900d500549bSAnthony Wilson             }
1901d500549bSAnthony Wilson         }
1902d500549bSAnthony Wilson 
1903d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1904d500549bSAnthony Wilson                             std::move(callback));
1905d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1906*e99073f5SGeorge Liu         });
1907d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1908d500549bSAnthony Wilson }
1909d500549bSAnthony Wilson 
1910d500549bSAnthony Wilson /**
191142cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
191242cbe538SGunnar Mills  *
191342cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
191442cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
191542cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
191642cbe538SGunnar Mills  *
191742cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
191842cbe538SGunnar Mills  * response.
191942cbe538SGunnar Mills  *
192042cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
192142cbe538SGunnar Mills  * when data has been obtained.
192242cbe538SGunnar Mills  *
192342cbe538SGunnar Mills  * The callback must have the following signature:
192442cbe538SGunnar Mills  *   @code
192542cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
192642cbe538SGunnar Mills  *   @endcode
192742cbe538SGunnar Mills  *
192842cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
192942cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
193042cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
193142cbe538SGunnar Mills  *        Supply Attributes
193242cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
193342cbe538SGunnar Mills  */
193442cbe538SGunnar Mills template <typename Callback>
193542cbe538SGunnar Mills void getPowerSupplyAttributesData(
1936b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
193742cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1938fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
193942cbe538SGunnar Mills     Callback&& callback)
194042cbe538SGunnar Mills {
194142cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
194242cbe538SGunnar Mills 
194342cbe538SGunnar Mills     if (psAttributesConnections.empty())
194442cbe538SGunnar Mills     {
194542cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
194642cbe538SGunnar Mills         callback(inventoryItems);
194742cbe538SGunnar Mills         return;
194842cbe538SGunnar Mills     }
194942cbe538SGunnar Mills 
195042cbe538SGunnar Mills     // Assuming just one connection (service) for now
1951fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
195242cbe538SGunnar Mills 
195342cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
195442cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
195542cbe538SGunnar Mills 
195642cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
1957002d39b4SEd Tanous     auto respHandler =
1958002d39b4SEd Tanous         [sensorsAsyncResp, inventoryItems,
1959f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
1960002d39b4SEd Tanous             const boost::system::error_code ec, const uint32_t value) {
196142cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
196242cbe538SGunnar Mills         if (ec)
196342cbe538SGunnar Mills         {
196442cbe538SGunnar Mills             BMCWEB_LOG_ERROR
196542cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
19668d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
196742cbe538SGunnar Mills             return;
196842cbe538SGunnar Mills         }
196942cbe538SGunnar Mills 
19701e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
197142cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
197242cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
197342cbe538SGunnar Mills         {
197455f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
197542cbe538SGunnar Mills             {
197642cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
19771e1e598dSJonathan Doman                     static_cast<int>(value);
197842cbe538SGunnar Mills             }
197942cbe538SGunnar Mills         }
198042cbe538SGunnar Mills 
198142cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
198242cbe538SGunnar Mills         callback(inventoryItems);
198342cbe538SGunnar Mills     };
198442cbe538SGunnar Mills 
198542cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
198642cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
19871e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
19881e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
19891e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
19901e1e598dSJonathan Doman         std::move(respHandler));
199142cbe538SGunnar Mills 
199242cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
199342cbe538SGunnar Mills }
199442cbe538SGunnar Mills 
199542cbe538SGunnar Mills /**
199642cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
199742cbe538SGunnar Mills  *
199842cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
199942cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
200042cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
200142cbe538SGunnar Mills  * item.
200242cbe538SGunnar Mills  *
200342cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
200442cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
200542cbe538SGunnar Mills  *
200642cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
200742cbe538SGunnar Mills  * when information has been obtained.
200842cbe538SGunnar Mills  *
200942cbe538SGunnar Mills  * The callback must have the following signature:
201042cbe538SGunnar Mills  *   @code
201142cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
201242cbe538SGunnar Mills  *   @endcode
201342cbe538SGunnar Mills  *
201442cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
201542cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
201642cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
201742cbe538SGunnar Mills  */
201842cbe538SGunnar Mills template <typename Callback>
201942cbe538SGunnar Mills void getPowerSupplyAttributes(
202042cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
202142cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
202242cbe538SGunnar Mills     Callback&& callback)
202342cbe538SGunnar Mills {
202442cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
202542cbe538SGunnar Mills 
202642cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2027a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
202842cbe538SGunnar Mills     {
202942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
203042cbe538SGunnar Mills         callback(inventoryItems);
203142cbe538SGunnar Mills         return;
203242cbe538SGunnar Mills     }
203342cbe538SGunnar Mills 
2034*e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
203542cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
203642cbe538SGunnar Mills 
2037*e99073f5SGeorge Liu     // Make call to ObjectMapper to find the PowerSupplyAttributes service
2038*e99073f5SGeorge Liu     dbus::utility::getSubTree(
2039*e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, interfaces,
2040b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2041b9d36b47SEd Tanous          inventoryItems](
2042*e99073f5SGeorge Liu             const boost::system::error_code& ec,
2043b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
2044*e99073f5SGeorge Liu         // Response handler for parsing output from GetSubTree
204542cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
204642cbe538SGunnar Mills         if (ec)
204742cbe538SGunnar Mills         {
20488d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
204942cbe538SGunnar Mills             BMCWEB_LOG_ERROR
205042cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
205142cbe538SGunnar Mills             return;
205242cbe538SGunnar Mills         }
205326f6976fSEd Tanous         if (subtree.empty())
205442cbe538SGunnar Mills         {
205542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
205642cbe538SGunnar Mills             callback(inventoryItems);
205742cbe538SGunnar Mills             return;
205842cbe538SGunnar Mills         }
205942cbe538SGunnar Mills 
206042cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
206142cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
206242cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
2063fe04d49cSNan Zhou         std::map<std::string, std::string> psAttributesConnections;
206442cbe538SGunnar Mills 
206542cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
206642cbe538SGunnar Mills         {
206742cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
206842cbe538SGunnar Mills             callback(inventoryItems);
206942cbe538SGunnar Mills             return;
207042cbe538SGunnar Mills         }
207142cbe538SGunnar Mills 
207242cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
207342cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
207442cbe538SGunnar Mills 
207542cbe538SGunnar Mills         if (connection.empty())
207642cbe538SGunnar Mills         {
207742cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
207842cbe538SGunnar Mills             callback(inventoryItems);
207942cbe538SGunnar Mills             return;
208042cbe538SGunnar Mills         }
208142cbe538SGunnar Mills 
208242cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
208342cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
208442cbe538SGunnar Mills                          << connection;
208542cbe538SGunnar Mills 
208642cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
208742cbe538SGunnar Mills                                      psAttributesConnections,
208842cbe538SGunnar Mills                                      std::move(callback));
208942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2090*e99073f5SGeorge Liu         });
209142cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
209242cbe538SGunnar Mills }
209342cbe538SGunnar Mills 
209442cbe538SGunnar Mills /**
2095adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
20968fb49dd6SShawn McCarney  *
20978fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2098adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
20998fb49dd6SShawn McCarney  *
2100adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2101adc4f0dbSShawn McCarney  * response.
21028fb49dd6SShawn McCarney  *
2103adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2104adc4f0dbSShawn McCarney  * inventory items have been obtained.
2105adc4f0dbSShawn McCarney  *
2106adc4f0dbSShawn McCarney  * The callback must have the following signature:
2107adc4f0dbSShawn McCarney  *   @code
2108adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2109adc4f0dbSShawn McCarney  *   @endcode
21108fb49dd6SShawn McCarney  *
21118fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
21128fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
21138fb49dd6SShawn McCarney  * implements ObjectManager.
2114adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
21158fb49dd6SShawn McCarney  */
2116adc4f0dbSShawn McCarney template <typename Callback>
2117d0090733SEd Tanous static void
2118d0090733SEd Tanous     getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2119fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>> sensorNames,
2120adc4f0dbSShawn McCarney                       Callback&& callback)
21218fb49dd6SShawn McCarney {
2122adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2123adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2124d0090733SEd Tanous         [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
2125adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2126adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
21278fb49dd6SShawn McCarney         auto getInventoryItemsConnectionsCb =
2128d0090733SEd Tanous             [sensorsAsyncResp, inventoryItems,
2129f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
2130fe04d49cSNan Zhou                 std::shared_ptr<std::set<std::string>> invConnections) {
21318fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2132002d39b4SEd Tanous             auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2133d500549bSAnthony Wilson                                             callback{std::move(callback)}]() {
2134d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
213542cbe538SGunnar Mills 
2136002d39b4SEd Tanous                 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2137002d39b4SEd Tanous                                            callback{std::move(callback)}]() {
213842cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
213942cbe538SGunnar Mills                     // Find Power Supply Attributes and get the data
2140002d39b4SEd Tanous                     getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
214142cbe538SGunnar Mills                                              std::move(callback));
214242cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
214342cbe538SGunnar Mills                 };
214442cbe538SGunnar Mills 
2145d500549bSAnthony Wilson                 // Find led connections and get the data
2146d500549bSAnthony Wilson                 getInventoryLeds(sensorsAsyncResp, inventoryItems,
214742cbe538SGunnar Mills                                  std::move(getInventoryLedsCb));
2148d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2149d500549bSAnthony Wilson             };
21508fb49dd6SShawn McCarney 
2151adc4f0dbSShawn McCarney             // Get inventory item data from connections
2152adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2153d0090733SEd Tanous                                   invConnections,
2154d500549bSAnthony Wilson                                   std::move(getInventoryItemsDataCb));
21558fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
21568fb49dd6SShawn McCarney         };
21578fb49dd6SShawn McCarney 
2158adc4f0dbSShawn McCarney         // Get connections that provide inventory item data
2159002d39b4SEd Tanous         getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
21608fb49dd6SShawn McCarney                                      std::move(getInventoryItemsConnectionsCb));
2161adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
21628fb49dd6SShawn McCarney     };
21638fb49dd6SShawn McCarney 
2164adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2165d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
2166adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2167adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2168adc4f0dbSShawn McCarney }
2169adc4f0dbSShawn McCarney 
2170adc4f0dbSShawn McCarney /**
2171adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2172adc4f0dbSShawn McCarney  *
2173adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2174adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2175adc4f0dbSShawn McCarney  * array.
2176adc4f0dbSShawn McCarney  *
2177adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2178adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2179adc4f0dbSShawn McCarney  * object.
2180adc4f0dbSShawn McCarney  *
2181adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2182adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2183adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2184adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2185adc4f0dbSShawn McCarney  */
218623a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2187adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2188adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2189adc4f0dbSShawn McCarney {
2190adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2191adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2192adc4f0dbSShawn McCarney     {
2193adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2194adc4f0dbSShawn McCarney         {
2195adc4f0dbSShawn McCarney             return powerSupply;
2196adc4f0dbSShawn McCarney         }
2197adc4f0dbSShawn McCarney     }
2198adc4f0dbSShawn McCarney 
2199adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2200adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2201adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2202adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2203adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2204adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2205adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2206adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2207adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2208adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2209adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2210d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2211adc4f0dbSShawn McCarney 
221242cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
221342cbe538SGunnar Mills     {
221442cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
221542cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
221642cbe538SGunnar Mills     }
221742cbe538SGunnar Mills 
221842cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2219adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2220adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2221adc4f0dbSShawn McCarney 
2222adc4f0dbSShawn McCarney     return powerSupply;
22238fb49dd6SShawn McCarney }
22248fb49dd6SShawn McCarney 
22258fb49dd6SShawn McCarney /**
2226de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2227de629b6eSShawn McCarney  *
2228de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2229de629b6eSShawn McCarney  *
2230de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2231de629b6eSShawn McCarney  * information has been obtained.
2232de629b6eSShawn McCarney  *
2233adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2234de629b6eSShawn McCarney  *
2235de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2236de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2237de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2238de629b6eSShawn McCarney  *
2239de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2240de629b6eSShawn McCarney  *
2241adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2242adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2243adc4f0dbSShawn McCarney  *
2244de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2245adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2246de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2247de629b6eSShawn McCarney  * implements ObjectManager.
2248adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2249de629b6eSShawn McCarney  */
225023a21a1cSEd Tanous inline void getSensorData(
225181ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2252fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2253fe04d49cSNan Zhou     const std::set<std::string>& connections,
2254b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2255de629b6eSShawn McCarney {
2256de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2257de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2258de629b6eSShawn McCarney     for (const std::string& connection : connections)
2259de629b6eSShawn McCarney     {
2260de629b6eSShawn McCarney         // Response handler to process managed objects
2261002d39b4SEd Tanous         auto getManagedObjectsCb =
2262002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
2263002d39b4SEd Tanous              inventoryItems](const boost::system::error_code ec,
226402cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
2265de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2266de629b6eSShawn McCarney             if (ec)
2267de629b6eSShawn McCarney             {
2268de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
22698d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2270de629b6eSShawn McCarney                 return;
2271de629b6eSShawn McCarney             }
2272de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2273de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2274de629b6eSShawn McCarney             {
2275de629b6eSShawn McCarney                 const std::string& objPath =
2276de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2277de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2278de629b6eSShawn McCarney                                  << objPath;
2279de629b6eSShawn McCarney 
2280de629b6eSShawn McCarney                 std::vector<std::string> split;
2281de629b6eSShawn McCarney                 // Reserve space for
2282de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2283de629b6eSShawn McCarney                 split.reserve(6);
2284de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2285de629b6eSShawn McCarney                 if (split.size() < 6)
2286de629b6eSShawn McCarney                 {
2287de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2288de629b6eSShawn McCarney                                      << objPath;
2289de629b6eSShawn McCarney                     continue;
2290de629b6eSShawn McCarney                 }
2291de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2292de629b6eSShawn McCarney                 // string at the beginning
2293de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2294de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2295de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2296de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
229749c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2298de629b6eSShawn McCarney                 {
2299accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2300de629b6eSShawn McCarney                     continue;
2301de629b6eSShawn McCarney                 }
2302de629b6eSShawn McCarney 
2303adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2304adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2305adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2306adc4f0dbSShawn McCarney 
230795a3ecadSAnthony Wilson                 const std::string& sensorSchema =
230881ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
230995a3ecadSAnthony Wilson 
231095a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
231195a3ecadSAnthony Wilson 
2312928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2313928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
231495a3ecadSAnthony Wilson                 {
2315c1d019a6SEd Tanous                     std::string sensorTypeEscaped(sensorType);
2316c1d019a6SEd Tanous                     sensorTypeEscaped.erase(
2317c1d019a6SEd Tanous                         std::remove(sensorTypeEscaped.begin(),
2318c1d019a6SEd Tanous                                     sensorTypeEscaped.end(), '_'),
2319c1d019a6SEd Tanous                         sensorTypeEscaped.end());
2320c1d019a6SEd Tanous                     std::string sensorId(sensorTypeEscaped);
2321c1d019a6SEd Tanous                     sensorId += "_";
2322c1d019a6SEd Tanous                     sensorId += sensorName;
2323c1d019a6SEd Tanous 
23248d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
2325c1d019a6SEd Tanous                         crow::utility::urlFromPieces(
2326c1d019a6SEd Tanous                             "redfish", "v1", "Chassis",
2327c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
2328c1d019a6SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
23298d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
233095a3ecadSAnthony Wilson                 }
233195a3ecadSAnthony Wilson                 else
233295a3ecadSAnthony Wilson                 {
2333271584abSEd Tanous                     std::string fieldName;
2334928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2335928fefb9SNan Zhou                     {
2336928fefb9SNan Zhou                         fieldName = "Members";
2337928fefb9SNan Zhou                     }
2338928fefb9SNan Zhou                     else if (sensorType == "temperature")
2339de629b6eSShawn McCarney                     {
2340de629b6eSShawn McCarney                         fieldName = "Temperatures";
2341de629b6eSShawn McCarney                     }
2342de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2343de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2344de629b6eSShawn McCarney                     {
2345de629b6eSShawn McCarney                         fieldName = "Fans";
2346de629b6eSShawn McCarney                     }
2347de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2348de629b6eSShawn McCarney                     {
2349de629b6eSShawn McCarney                         fieldName = "Voltages";
2350de629b6eSShawn McCarney                     }
2351de629b6eSShawn McCarney                     else if (sensorType == "power")
2352de629b6eSShawn McCarney                     {
235355f79e6fSEd Tanous                         if (sensorName == "total_power")
2354028f7ebcSEddie James                         {
2355028f7ebcSEddie James                             fieldName = "PowerControl";
2356028f7ebcSEddie James                         }
2357adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2358adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2359028f7ebcSEddie James                         {
2360de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2361de629b6eSShawn McCarney                         }
2362adc4f0dbSShawn McCarney                         else
2363adc4f0dbSShawn McCarney                         {
2364adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2365adc4f0dbSShawn McCarney                             continue;
2366adc4f0dbSShawn McCarney                         }
2367028f7ebcSEddie James                     }
2368de629b6eSShawn McCarney                     else
2369de629b6eSShawn McCarney                     {
2370de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2371de629b6eSShawn McCarney                                          << sensorType;
2372de629b6eSShawn McCarney                         continue;
2373de629b6eSShawn McCarney                     }
2374de629b6eSShawn McCarney 
2375de629b6eSShawn McCarney                     nlohmann::json& tempArray =
23768d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2377adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
237849c53ac9SJohnathan Mantey                     {
2379adc4f0dbSShawn McCarney                         if (tempArray.empty())
23807ab06f49SGunnar Mills                         {
238195a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
238295a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
238395a3ecadSAnthony Wilson                             // naming in power.hpp.
23841476687dSEd Tanous                             nlohmann::json::object_t power;
23851476687dSEd Tanous                             power["@odata.id"] =
2386adc4f0dbSShawn McCarney                                 "/redfish/v1/Chassis/" +
238781ce609eSEd Tanous                                 sensorsAsyncResp->chassisId + "/" +
238881ce609eSEd Tanous                                 sensorsAsyncResp->chassisSubNode + "#/" +
23891476687dSEd Tanous                                 fieldName + "/0";
23901476687dSEd Tanous                             tempArray.push_back(std::move(power));
2391adc4f0dbSShawn McCarney                         }
2392adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2393adc4f0dbSShawn McCarney                     }
2394adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2395adc4f0dbSShawn McCarney                     {
2396adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2397adc4f0dbSShawn McCarney                         {
2398adc4f0dbSShawn McCarney                             sensorJson =
2399adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
240081ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2401adc4f0dbSShawn McCarney                         }
240249c53ac9SJohnathan Mantey                     }
2403928fefb9SNan Zhou                     else if (fieldName == "Members")
2404928fefb9SNan Zhou                     {
2405677bb756SEd Tanous                         std::string sensorTypeEscaped(sensorType);
2406677bb756SEd Tanous                         sensorTypeEscaped.erase(
2407677bb756SEd Tanous                             std::remove(sensorTypeEscaped.begin(),
2408677bb756SEd Tanous                                         sensorTypeEscaped.end(), '_'),
2409677bb756SEd Tanous                             sensorTypeEscaped.end());
2410677bb756SEd Tanous                         std::string sensorId(sensorTypeEscaped);
2411677bb756SEd Tanous                         sensorId += "_";
2412677bb756SEd Tanous                         sensorId += sensorName;
2413677bb756SEd Tanous 
24141476687dSEd Tanous                         nlohmann::json::object_t member;
2415677bb756SEd Tanous                         member["@odata.id"] = crow::utility::urlFromPieces(
2416677bb756SEd Tanous                             "redfish", "v1", "Chassis",
2417677bb756SEd Tanous                             sensorsAsyncResp->chassisId,
2418677bb756SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
24191476687dSEd Tanous                         tempArray.push_back(std::move(member));
2420928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2421928fefb9SNan Zhou                     }
242249c53ac9SJohnathan Mantey                     else
242349c53ac9SJohnathan Mantey                     {
24241476687dSEd Tanous                         nlohmann::json::object_t member;
24251476687dSEd Tanous                         member["@odata.id"] = "/redfish/v1/Chassis/" +
24261476687dSEd Tanous                                               sensorsAsyncResp->chassisId +
24271476687dSEd Tanous                                               "/" +
24281476687dSEd Tanous                                               sensorsAsyncResp->chassisSubNode +
24291476687dSEd Tanous                                               "#/" + fieldName + "/";
24301476687dSEd Tanous                         tempArray.push_back(std::move(member));
2431adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
243249c53ac9SJohnathan Mantey                     }
243395a3ecadSAnthony Wilson                 }
2434de629b6eSShawn McCarney 
2435adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2436adc4f0dbSShawn McCarney                 {
24371d7c0054SEd Tanous                     objectInterfacesToJson(sensorName, sensorType,
24381d7c0054SEd Tanous                                            sensorsAsyncResp->chassisSubNode,
24391d7c0054SEd Tanous                                            objDictEntry.second, *sensorJson,
24401d7c0054SEd Tanous                                            inventoryItem);
24411d7c0054SEd Tanous 
24421d7c0054SEd Tanous                     std::string path = "/xyz/openbmc_project/sensors/";
24431d7c0054SEd Tanous                     path += sensorType;
24441d7c0054SEd Tanous                     path += "/";
24451d7c0054SEd Tanous                     path += sensorName;
2446c1d019a6SEd Tanous                     sensorsAsyncResp->addMetadata(*sensorJson, path);
2447adc4f0dbSShawn McCarney                 }
2448de629b6eSShawn McCarney             }
244981ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
245049c53ac9SJohnathan Mantey             {
245181ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2452928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2453928fefb9SNan Zhou                         sensors::node::sensors &&
2454928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2455928fefb9SNan Zhou                 {
2456928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2457928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2458928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2459928fefb9SNan Zhou                             .size();
2460928fefb9SNan Zhou                 }
2461928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2462928fefb9SNan Zhou                          sensors::node::thermal)
24638bd25ccdSJames Feist                 {
246481ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
24658bd25ccdSJames Feist                 }
246649c53ac9SJohnathan Mantey             }
2467de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2468de629b6eSShawn McCarney         };
2469de629b6eSShawn McCarney 
2470de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2471d0090733SEd Tanous             getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
2472de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
247323a21a1cSEd Tanous     }
2474de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2475de629b6eSShawn McCarney }
2476de629b6eSShawn McCarney 
2477fe04d49cSNan Zhou inline void
2478fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2479fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
24801abe55efSEd Tanous {
2481fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2482fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
248355c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2484adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2485d0090733SEd Tanous             [sensorsAsyncResp, sensorNames,
2486d0090733SEd Tanous              connections](const std::shared_ptr<std::vector<InventoryItem>>&
2487adc4f0dbSShawn McCarney                               inventoryItems) {
2488adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
248949c53ac9SJohnathan Mantey             // Get sensor data and store results in JSON
2490002d39b4SEd Tanous             getSensorData(sensorsAsyncResp, sensorNames, connections,
2491d0090733SEd Tanous                           inventoryItems);
2492adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2493adc4f0dbSShawn McCarney         };
2494adc4f0dbSShawn McCarney 
2495adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2496d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2497adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2498adc4f0dbSShawn McCarney 
249955c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb exit";
250008777fb0SLewanczyk, Dawid     };
2501de629b6eSShawn McCarney 
2502de629b6eSShawn McCarney     // Get set of connections that provide sensor values
250381ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
250495a3ecadSAnthony Wilson }
250595a3ecadSAnthony Wilson 
250695a3ecadSAnthony Wilson /**
250795a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
250895a3ecadSAnthony Wilson  *        chassis.
250995a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
251095a3ecadSAnthony Wilson  */
2511b5a76932SEd Tanous inline void
251281ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
251395a3ecadSAnthony Wilson {
251495a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
251595a3ecadSAnthony Wilson     auto getChassisCb =
251681ce609eSEd Tanous         [sensorsAsyncResp](
2517fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
251895a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "getChassisCb enter";
251981ce609eSEd Tanous         processSensorList(sensorsAsyncResp, sensorNames);
252055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassisCb exit";
252108777fb0SLewanczyk, Dawid     };
2522928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2523928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2524928fefb9SNan Zhou     {
25258d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
25268d1b46d7Szhanghch05             nlohmann::json::array();
2527928fefb9SNan Zhou     }
252826f03899SShawn McCarney     // Get set of sensors in chassis
25297f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
25307f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
25317f1cc26dSEd Tanous                std::move(getChassisCb));
253255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2533271584abSEd Tanous }
253408777fb0SLewanczyk, Dawid 
2535413961deSRichard Marian Thomaiyar /**
253649c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
253749c53ac9SJohnathan Mantey  * the chassis node
253849c53ac9SJohnathan Mantey  *
253949c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
254049c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
254149c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
254249c53ac9SJohnathan Mantey  *                         repeated calls to this function
254349c53ac9SJohnathan Mantey  */
2544fe04d49cSNan Zhou inline bool
2545fe04d49cSNan Zhou     findSensorNameUsingSensorPath(std::string_view sensorName,
254602cad96eSEd Tanous                                   const std::set<std::string>& sensorsList,
2547fe04d49cSNan Zhou                                   std::set<std::string>& sensorsModified)
254849c53ac9SJohnathan Mantey {
2549fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
255049c53ac9SJohnathan Mantey     {
255128aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2552b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
255328aa8de5SGeorge Liu         if (thisSensorName.empty())
255449c53ac9SJohnathan Mantey         {
255549c53ac9SJohnathan Mantey             continue;
255649c53ac9SJohnathan Mantey         }
255749c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
255849c53ac9SJohnathan Mantey         {
255949c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
256049c53ac9SJohnathan Mantey             return true;
256149c53ac9SJohnathan Mantey         }
256249c53ac9SJohnathan Mantey     }
256349c53ac9SJohnathan Mantey     return false;
256449c53ac9SJohnathan Mantey }
256549c53ac9SJohnathan Mantey 
2566c71d6125SEd Tanous inline std::pair<std::string, std::string>
2567c71d6125SEd Tanous     splitSensorNameAndType(std::string_view sensorId)
2568c71d6125SEd Tanous {
2569c71d6125SEd Tanous     size_t index = sensorId.find('_');
2570c71d6125SEd Tanous     if (index == std::string::npos)
2571c71d6125SEd Tanous     {
2572c71d6125SEd Tanous         return std::make_pair<std::string, std::string>("", "");
2573c71d6125SEd Tanous     }
2574c71d6125SEd Tanous     std::string sensorType{sensorId.substr(0, index)};
2575c71d6125SEd Tanous     std::string sensorName{sensorId.substr(index + 1)};
2576c71d6125SEd Tanous     // fan_pwm and fan_tach need special handling
2577c71d6125SEd Tanous     if (sensorType == "fantach" || sensorType == "fanpwm")
2578c71d6125SEd Tanous     {
2579c71d6125SEd Tanous         sensorType.insert(3, 1, '_');
2580c71d6125SEd Tanous     }
2581c71d6125SEd Tanous     return std::make_pair(sensorType, sensorName);
2582c71d6125SEd Tanous }
2583c71d6125SEd Tanous 
258449c53ac9SJohnathan Mantey /**
2585413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2586413961deSRichard Marian Thomaiyar  *
25878d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
25884bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2589413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2590413961deSRichard Marian Thomaiyar  */
259123a21a1cSEd Tanous inline void setSensorsOverride(
2592b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
25934bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2594397fd61fSjayaprakash Mutyala         allCollections)
2595413961deSRichard Marian Thomaiyar {
259670d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
25974bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2598413961deSRichard Marian Thomaiyar 
2599543f4400SEd Tanous     const char* propertyValueName = nullptr;
2600f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2601413961deSRichard Marian Thomaiyar     std::string memberId;
2602543f4400SEd Tanous     double value = 0.0;
2603f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2604f65af9e8SRichard Marian Thomaiyar     {
2605f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2606f65af9e8SRichard Marian Thomaiyar         {
2607f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2608f65af9e8SRichard Marian Thomaiyar         }
2609f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2610f65af9e8SRichard Marian Thomaiyar         {
2611f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2612f65af9e8SRichard Marian Thomaiyar         }
2613f65af9e8SRichard Marian Thomaiyar         else
2614f65af9e8SRichard Marian Thomaiyar         {
2615f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2616f65af9e8SRichard Marian Thomaiyar         }
2617f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2618f65af9e8SRichard Marian Thomaiyar         {
26198d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
26208d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
26218d1b46d7Szhanghch05                                      value))
2622413961deSRichard Marian Thomaiyar             {
2623413961deSRichard Marian Thomaiyar                 return;
2624413961deSRichard Marian Thomaiyar             }
2625f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2626f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2627f65af9e8SRichard Marian Thomaiyar         }
2628f65af9e8SRichard Marian Thomaiyar     }
26294bb3dc34SCarol Wang 
2630002d39b4SEd Tanous     auto getChassisSensorListCb =
2631002d39b4SEd Tanous         [sensorAsyncResp, overrideMap](
2632fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorsList) {
263349c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
263449c53ac9SJohnathan Mantey         // chassis node
2635fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2636fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2637f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2638413961deSRichard Marian Thomaiyar         {
2639f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2640c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
2641c71d6125SEd Tanous                 splitSensorNameAndType(sensor);
2642c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2643c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2644f65af9e8SRichard Marian Thomaiyar             {
2645f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
26468d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2647f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2648413961deSRichard Marian Thomaiyar                 return;
2649413961deSRichard Marian Thomaiyar             }
2650f65af9e8SRichard Marian Thomaiyar         }
2651413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2652002d39b4SEd Tanous         auto getObjectsWithConnectionCb =
2653fe04d49cSNan Zhou             [sensorAsyncResp,
2654fe04d49cSNan Zhou              overrideMap](const std::set<std::string>& /*connections*/,
2655002d39b4SEd Tanous                           const std::set<std::pair<std::string, std::string>>&
2656413961deSRichard Marian Thomaiyar                               objectsWithConnection) {
2657f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2658413961deSRichard Marian Thomaiyar             {
2659413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2660f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2661f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2662f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
26634f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2664a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2665a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2666413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2667413961deSRichard Marian Thomaiyar                                                : "Voltages",
2668f65af9e8SRichard Marian Thomaiyar                                            "Count");
2669f65af9e8SRichard Marian Thomaiyar                 return;
2670f65af9e8SRichard Marian Thomaiyar             }
2671f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2672f65af9e8SRichard Marian Thomaiyar             {
267328aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
267428aa8de5SGeorge Liu                 std::string sensorName = path.filename();
267528aa8de5SGeorge Liu                 if (sensorName.empty())
2676f65af9e8SRichard Marian Thomaiyar                 {
26774f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2678f65af9e8SRichard Marian Thomaiyar                     return;
2679f65af9e8SRichard Marian Thomaiyar                 }
2680f65af9e8SRichard Marian Thomaiyar 
2681f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2682f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2683f65af9e8SRichard Marian Thomaiyar                 {
2684f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2685f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
26864f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2687413961deSRichard Marian Thomaiyar                     return;
2688413961deSRichard Marian Thomaiyar                 }
2689413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2690f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2691413961deSRichard Marian Thomaiyar                     if (ec)
2692413961deSRichard Marian Thomaiyar                     {
26934f277b54SJayaprakash Mutyala                         if (ec.value() ==
26944f277b54SJayaprakash Mutyala                             boost::system::errc::permission_denied)
26954f277b54SJayaprakash Mutyala                         {
26964f277b54SJayaprakash Mutyala                             BMCWEB_LOG_WARNING
26974f277b54SJayaprakash Mutyala                                 << "Manufacturing mode is not Enabled...can't "
26984f277b54SJayaprakash Mutyala                                    "Override the sensor value. ";
26994f277b54SJayaprakash Mutyala 
27004f277b54SJayaprakash Mutyala                             messages::insufficientPrivilege(
27018d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
2702413961deSRichard Marian Thomaiyar                             return;
2703413961deSRichard Marian Thomaiyar                         }
27044f277b54SJayaprakash Mutyala                         BMCWEB_LOG_DEBUG
27054f277b54SJayaprakash Mutyala                             << "setOverrideValueStatus DBUS error: " << ec;
27064f277b54SJayaprakash Mutyala                         messages::internalError(
27074f277b54SJayaprakash Mutyala                             sensorAsyncResp->asyncResp->res);
27084f277b54SJayaprakash Mutyala                     }
2709413961deSRichard Marian Thomaiyar                     },
27104f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
27114f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2712168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2713f65af9e8SRichard Marian Thomaiyar             }
2714413961deSRichard Marian Thomaiyar         };
2715413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2716413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2717413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2718413961deSRichard Marian Thomaiyar     };
2719413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
27207f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
27217f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
27227f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2723413961deSRichard Marian Thomaiyar }
2724413961deSRichard Marian Thomaiyar 
2725a0ec28b6SAdrian Ambrożewicz /**
2726a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2727a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2728a0ec28b6SAdrian Ambrożewicz  *
2729a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2730a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2731a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2732a0ec28b6SAdrian Ambrożewicz  *
2733a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2734a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2735a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2736a0ec28b6SAdrian Ambrożewicz  */
2737021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2738021d32cfSKrzysztof Grobelny                                  const std::string& node,
2739a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2740a0ec28b6SAdrian Ambrożewicz {
274102da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
274202da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
274302da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
274402da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2745a0ec28b6SAdrian Ambrożewicz     {
2746a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2747a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2748a0ec28b6SAdrian Ambrożewicz         return;
2749a0ec28b6SAdrian Ambrożewicz     }
2750d51e072fSKrzysztof Grobelny 
275172374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2752fe04d49cSNan Zhou     auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2753a0ec28b6SAdrian Ambrożewicz                         const boost::beast::http::status status,
2754fe04d49cSNan Zhou                         const std::map<std::string, std::string>& uriToDbus) {
2755fe04d49cSNan Zhou         mapCompleteCb(status, uriToDbus);
2756fe04d49cSNan Zhou     };
2757a0ec28b6SAdrian Ambrożewicz 
2758a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2759d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2760a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2761a0ec28b6SAdrian Ambrożewicz }
2762a0ec28b6SAdrian Ambrożewicz 
2763bacb2162SNan Zhou namespace sensors
2764bacb2162SNan Zhou {
2765928fefb9SNan Zhou 
2766bacb2162SNan Zhou inline void getChassisCallback(
2767c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2768c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2769fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2770bacb2162SNan Zhou {
2771bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
2772bacb2162SNan Zhou 
2773c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2774c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2775bacb2162SNan Zhou     {
2776bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2777bacb2162SNan Zhou 
2778bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2779bacb2162SNan Zhou         std::string sensorName = path.filename();
2780bacb2162SNan Zhou         if (sensorName.empty())
2781bacb2162SNan Zhou         {
2782bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2783c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2784bacb2162SNan Zhou             return;
2785bacb2162SNan Zhou         }
2786c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
2787c1d019a6SEd Tanous         // fan_tach has an underscore in it, so remove it to "normalize" the
2788c1d019a6SEd Tanous         // type in the URI
2789c1d019a6SEd Tanous         type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2790c1d019a6SEd Tanous 
27911476687dSEd Tanous         nlohmann::json::object_t member;
2792c1d019a6SEd Tanous         std::string id = type;
2793c1d019a6SEd Tanous         id += "_";
2794c1d019a6SEd Tanous         id += sensorName;
2795c1d019a6SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
2796c1d019a6SEd Tanous             "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2797c1d019a6SEd Tanous 
27981476687dSEd Tanous         entriesArray.push_back(std::move(member));
2799bacb2162SNan Zhou     }
2800bacb2162SNan Zhou 
2801c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
2802bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2803bacb2162SNan Zhou }
2804e6bd846dSNan Zhou 
2805de167a6fSNan Zhou inline void
2806de167a6fSNan Zhou     handleSensorCollectionGet(App& app, const crow::Request& req,
2807de167a6fSNan Zhou                               const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2808de167a6fSNan Zhou                               const std::string& chassisId)
2809de167a6fSNan Zhou {
2810de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2811de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2812de167a6fSNan Zhou     };
2813de167a6fSNan Zhou     query_param::Query delegatedQuery;
28143ba00073SCarson Labrado     if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
2815de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2816de167a6fSNan Zhou     {
2817de167a6fSNan Zhou         return;
2818de167a6fSNan Zhou     }
2819de167a6fSNan Zhou 
2820de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2821de167a6fSNan Zhou     {
2822de167a6fSNan Zhou         // we perform efficient expand.
2823de167a6fSNan Zhou         auto asyncResp = std::make_shared<SensorsAsyncResp>(
2824de167a6fSNan Zhou             aResp, chassisId, sensors::dbus::sensorPaths,
2825de167a6fSNan Zhou             sensors::node::sensors,
2826de167a6fSNan Zhou             /*efficientExpand=*/true);
2827de167a6fSNan Zhou         getChassisData(asyncResp);
2828de167a6fSNan Zhou 
2829de167a6fSNan Zhou         BMCWEB_LOG_DEBUG
2830de167a6fSNan Zhou             << "SensorCollection doGet exit via efficient expand handler";
2831de167a6fSNan Zhou         return;
28320bad320cSEd Tanous     }
2833de167a6fSNan Zhou 
2834de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2835de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
2836c1d019a6SEd Tanous     getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2837c1d019a6SEd Tanous                std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2838c1d019a6SEd Tanous                                sensors::node::sensors));
2839c1d019a6SEd Tanous }
28407f1cc26dSEd Tanous 
2841c1d019a6SEd Tanous inline void
2842c1d019a6SEd Tanous     getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2843c1d019a6SEd Tanous                       const std::string& sensorPath,
2844c1d019a6SEd Tanous                       const ::dbus::utility::MapperGetObject& mapperResponse)
2845c1d019a6SEd Tanous {
2846c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2847c1d019a6SEd Tanous     {
2848c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2849c1d019a6SEd Tanous         return;
2850c1d019a6SEd Tanous     }
2851c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2852c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
2853c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2854c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Path " << sensorPath;
2855c1343bf6SKrzysztof Grobelny 
2856c1343bf6SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2857c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2858c1d019a6SEd Tanous         [asyncResp,
2859c1d019a6SEd Tanous          sensorPath](const boost::system::error_code ec,
2860c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2861c1d019a6SEd Tanous         if (ec)
2862c1d019a6SEd Tanous         {
2863c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2864c1d019a6SEd Tanous             return;
2865c1d019a6SEd Tanous         }
2866c1d019a6SEd Tanous         sdbusplus::message::object_path path(sensorPath);
2867c1d019a6SEd Tanous         std::string name = path.filename();
2868c1d019a6SEd Tanous         path = path.parent_path();
2869c1d019a6SEd Tanous         std::string type = path.filename();
2870c1d019a6SEd Tanous         objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2871c1d019a6SEd Tanous                                asyncResp->res.jsonValue, nullptr);
2872c1343bf6SKrzysztof Grobelny         });
2873de167a6fSNan Zhou }
2874de167a6fSNan Zhou 
2875e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2876c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2877677bb756SEd Tanous                             const std::string& chassisId,
2878c1d019a6SEd Tanous                             const std::string& sensorId)
2879e6bd846dSNan Zhou {
2880c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2881e6bd846dSNan Zhou     {
2882e6bd846dSNan Zhou         return;
2883e6bd846dSNan Zhou     }
2884c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
2885c71d6125SEd Tanous         splitSensorNameAndType(sensorId);
2886c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2887c1d019a6SEd Tanous     {
2888c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2889c1d019a6SEd Tanous         return;
2890c1d019a6SEd Tanous     }
2891c71d6125SEd Tanous 
2892677bb756SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2893677bb756SEd Tanous         "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
2894c1d019a6SEd Tanous 
2895e6bd846dSNan Zhou     BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2896e6bd846dSNan Zhou 
28972b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2898e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2899c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2900c71d6125SEd Tanous                              '/' + nameType.second;
2901e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2902e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
29032b73119cSGeorge Liu     ::dbus::utility::getDbusObject(
29042b73119cSGeorge Liu         sensorPath, interfaces,
2905c71d6125SEd Tanous         [asyncResp,
29062b73119cSGeorge Liu          sensorPath](const boost::system::error_code& ec,
2907c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
2908e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 enter";
2909e6bd846dSNan Zhou         if (ec)
2910e6bd846dSNan Zhou         {
2911c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2912e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2913e6bd846dSNan Zhou                              << "Dbus error " << ec;
2914e6bd846dSNan Zhou             return;
2915e6bd846dSNan Zhou         }
2916c1d019a6SEd Tanous         getSensorFromDbus(asyncResp, sensorPath, subtree);
2917e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 exit";
29182b73119cSGeorge Liu         });
2919e6bd846dSNan Zhou }
2920e6bd846dSNan Zhou 
2921bacb2162SNan Zhou } // namespace sensors
2922bacb2162SNan Zhou 
29237e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
292495a3ecadSAnthony Wilson {
29257e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2926ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2927002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2928de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
292995a3ecadSAnthony Wilson }
293095a3ecadSAnthony Wilson 
29317e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
293295a3ecadSAnthony Wilson {
29337e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2934ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2935002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2936e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
293795a3ecadSAnthony Wilson }
293895a3ecadSAnthony Wilson 
293908777fb0SLewanczyk, Dawid } // namespace redfish
2940