xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision cf9e417d3b88eb12f8c6a9d007d6a63c3eeb94f4)
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 
187a1dbc48SGeorge Liu #include "dbus_utility.hpp"
190ec8b83dSEd Tanous #include "generated/enums/sensor.hpp"
200ec8b83dSEd Tanous 
217e860f15SJohn Edward Broadbent #include <app.hpp>
2211ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
231d7c0054SEd Tanous #include <boost/algorithm/string/find.hpp>
241d7c0054SEd Tanous #include <boost/algorithm/string/predicate.hpp>
25c71d6125SEd Tanous #include <boost/algorithm/string/replace.hpp>
2608777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2708777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
281abe55efSEd Tanous #include <dbus_singleton.hpp>
29168e20c1SEd Tanous #include <dbus_utility.hpp>
3045ca1b86SEd Tanous #include <query.hpp>
31ed398213SEd Tanous #include <registries/privilege_registry.hpp>
321e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
3386d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
3486d89ed7SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
35413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
36928fefb9SNan Zhou #include <utils/query_param.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 {
62*cf9e417dSEd 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 
67*cf9e417dSEd 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 
83*cf9e417dSEd 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 
92*cf9e417dSEd Tanous using sensorPair =
93*cf9e417dSEd Tanous     std::pair<std::string_view, std::span<const std::string_view>>;
9402da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
95*cf9e417dSEd Tanous     {{node::power, dbus::powerPaths},
96*cf9e417dSEd Tanous      {node::sensors, dbus::sensorPaths},
97*cf9e417dSEd 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,
207*cf9e417dSEd 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,
217*cf9e417dSEd 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,
229*cf9e417dSEd 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;
292*cf9e417dSEd 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";
35808777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
35908777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
36008777fb0SLewanczyk, Dawid 
36108777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
362002d39b4SEd Tanous     auto respHandler =
363002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
364002d39b4SEd Tanous          sensorNames](const boost::system::error_code ec,
365002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
366413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3671abe55efSEd Tanous         if (ec)
3681abe55efSEd Tanous         {
3698d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
370413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
371413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
37208777fb0SLewanczyk, Dawid             return;
37308777fb0SLewanczyk, Dawid         }
37408777fb0SLewanczyk, Dawid 
37555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
37608777fb0SLewanczyk, Dawid 
37708777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
37808777fb0SLewanczyk, Dawid         // found in the chassis
379fe04d49cSNan Zhou         std::set<std::string> connections;
380413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
38108777fb0SLewanczyk, Dawid 
38249c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
38349c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3841abe55efSEd Tanous         {
38555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
38608777fb0SLewanczyk, Dawid         }
38708777fb0SLewanczyk, Dawid 
38808777fb0SLewanczyk, Dawid         for (const std::pair<
38908777fb0SLewanczyk, Dawid                  std::string,
39008777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3911abe55efSEd Tanous                  object : subtree)
3921abe55efSEd Tanous         {
39349c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3941abe55efSEd Tanous             {
39549c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3961abe55efSEd Tanous                          objData : object.second)
3971abe55efSEd Tanous                 {
39849c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
39908777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
400de629b6eSShawn McCarney                     objectsWithConnection.insert(
401de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
40208777fb0SLewanczyk, Dawid                 }
40308777fb0SLewanczyk, Dawid             }
40408777fb0SLewanczyk, Dawid         }
40555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
406413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
407413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
40808777fb0SLewanczyk, Dawid     };
40908777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
41055c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
41155c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
4121abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
4131abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
414413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
415413961deSRichard Marian Thomaiyar }
416413961deSRichard Marian Thomaiyar 
417413961deSRichard Marian Thomaiyar /**
418413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
419413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
420413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
421413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
422413961deSRichard Marian Thomaiyar  */
423413961deSRichard Marian Thomaiyar template <typename Callback>
424fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
425fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
426413961deSRichard Marian Thomaiyar                     Callback&& callback)
427413961deSRichard Marian Thomaiyar {
428413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
429fe04d49cSNan Zhou         [callback](const std::set<std::string>& connections,
430413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4313174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
43281ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
433413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
43408777fb0SLewanczyk, Dawid }
43508777fb0SLewanczyk, Dawid 
43608777fb0SLewanczyk, Dawid /**
43749c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43849c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43949c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
44049c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
44149c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
44249c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
44349c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
44449c53ac9SJohnathan Mantey  */
44523a21a1cSEd Tanous inline void reduceSensorList(
4467f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
447*cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
44849c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
449fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
45049c53ac9SJohnathan Mantey {
45149c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
45249c53ac9SJohnathan Mantey     {
4537f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
4547f1cc26dSEd Tanous                                    chassisSubNode == sensors::node::thermal
455a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
45649c53ac9SJohnathan Mantey                                        : "Voltages");
45749c53ac9SJohnathan Mantey 
45849c53ac9SJohnathan Mantey         return;
45949c53ac9SJohnathan Mantey     }
46049c53ac9SJohnathan Mantey     if (allSensors->empty())
46149c53ac9SJohnathan Mantey     {
46249c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
46349c53ac9SJohnathan Mantey         return;
46449c53ac9SJohnathan Mantey     }
46549c53ac9SJohnathan Mantey 
4667f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
46749c53ac9SJohnathan Mantey     {
46849c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46949c53ac9SJohnathan Mantey         {
47011ba3979SEd Tanous             if (sensor.starts_with(type))
47149c53ac9SJohnathan Mantey             {
47249c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
47349c53ac9SJohnathan Mantey             }
47449c53ac9SJohnathan Mantey         }
47549c53ac9SJohnathan Mantey     }
47649c53ac9SJohnathan Mantey }
47749c53ac9SJohnathan Mantey 
4787f1cc26dSEd Tanous /*
4797f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
4807f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
4817f1cc26dSEd Tanous  *
4827f1cc26dSEd Tanous  * */
4837f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
4847f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
4857f1cc26dSEd Tanous {
4867f1cc26dSEd Tanous     if (chassisSubNode == sensors::node::power)
4877f1cc26dSEd Tanous     {
4887f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
4897f1cc26dSEd Tanous     }
4907f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::thermal)
4917f1cc26dSEd Tanous     {
4927f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
4937f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
4947f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
4957f1cc26dSEd Tanous     }
4967f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::sensors)
4977f1cc26dSEd Tanous     {
4987f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
4997f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
5007f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
5017f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
5027f1cc26dSEd Tanous     }
5037f1cc26dSEd Tanous 
5047f1cc26dSEd Tanous     if (chassisSubNode != sensors::node::sensors)
5057f1cc26dSEd Tanous     {
5067f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
5077f1cc26dSEd Tanous     }
5087f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
5097f1cc26dSEd Tanous }
5107f1cc26dSEd Tanous 
51149c53ac9SJohnathan Mantey /**
51208777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
513588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
51408777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
51508777fb0SLewanczyk, Dawid  */
51608777fb0SLewanczyk, Dawid template <typename Callback>
5177f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5187f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
519*cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
520*cf9e417dSEd Tanous                 Callback&& callback)
5211abe55efSEd Tanous {
52255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
5237a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
52449c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
525adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
5267a1dbc48SGeorge Liu 
5277a1dbc48SGeorge Liu     // Get the Chassis Collection
5287a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
5297a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
5307f1cc26dSEd Tanous         [callback{std::forward<Callback>(callback)}, asyncResp,
5317f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
5327f1cc26dSEd Tanous          chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
5337a1dbc48SGeorge Liu             const boost::system::error_code& ec,
534002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
53555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5361abe55efSEd Tanous         if (ec)
5371abe55efSEd Tanous         {
53855c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5397f1cc26dSEd Tanous             messages::internalError(asyncResp->res);
54008777fb0SLewanczyk, Dawid             return;
54108777fb0SLewanczyk, Dawid         }
54249c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
54349c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5441abe55efSEd Tanous         {
54528aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
546f8fe53e7SEd Tanous             std::string chassisName = path.filename();
54728aa8de5SGeorge Liu             if (chassisName.empty())
5481abe55efSEd Tanous             {
54949c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
550daf36e2eSEd Tanous                 continue;
551daf36e2eSEd Tanous             }
5527f1cc26dSEd Tanous             if (chassisName == chassisIdStr)
5531abe55efSEd Tanous             {
55449c53ac9SJohnathan Mantey                 chassisPath = &chassis;
55549c53ac9SJohnathan Mantey                 break;
556daf36e2eSEd Tanous             }
55749c53ac9SJohnathan Mantey         }
55849c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5591abe55efSEd Tanous         {
5607f1cc26dSEd Tanous             messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
56149c53ac9SJohnathan Mantey             return;
5621abe55efSEd Tanous         }
5637f1cc26dSEd Tanous         populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
56408777fb0SLewanczyk, Dawid 
5657f1cc26dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
5667f1cc26dSEd Tanous             "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
56795a3ecadSAnthony Wilson 
5688fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5698fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
5701e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
5711e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
5721e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
5737f1cc26dSEd Tanous             [asyncResp, chassisSubNode, sensorTypes,
574f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
575271584abSEd Tanous                 const boost::system::error_code& e,
5761e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
577271584abSEd Tanous             if (e)
57849c53ac9SJohnathan Mantey             {
579271584abSEd Tanous                 if (e.value() != EBADR)
58049c53ac9SJohnathan Mantey                 {
5817f1cc26dSEd Tanous                     messages::internalError(asyncResp->res);
58249c53ac9SJohnathan Mantey                     return;
58349c53ac9SJohnathan Mantey                 }
58449c53ac9SJohnathan Mantey             }
585fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>> culledSensorList =
586fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
5877f1cc26dSEd Tanous             reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
5887f1cc26dSEd Tanous                              &nodeSensorList, culledSensorList);
5897f1cc26dSEd Tanous             BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
59049c53ac9SJohnathan Mantey             callback(culledSensorList);
5911e1e598dSJonathan Doman             });
5927a1dbc48SGeorge Liu         });
59355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
59408777fb0SLewanczyk, Dawid }
59508777fb0SLewanczyk, Dawid 
59608777fb0SLewanczyk, Dawid /**
597adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
598adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
599adc4f0dbSShawn McCarney  * @return State value for inventory item.
60034dd179eSJames Feist  */
60123a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
602adc4f0dbSShawn McCarney {
603adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
604adc4f0dbSShawn McCarney     {
605adc4f0dbSShawn McCarney         return "Absent";
606adc4f0dbSShawn McCarney     }
60734dd179eSJames Feist 
608adc4f0dbSShawn McCarney     return "Enabled";
609adc4f0dbSShawn McCarney }
610adc4f0dbSShawn McCarney 
611adc4f0dbSShawn McCarney /**
612adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
613adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
6141d7c0054SEd Tanous  * @param valuesDict Map of all sensor DBus values.
615adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
616adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
617adc4f0dbSShawn McCarney  * @return Health value for sensor.
618adc4f0dbSShawn McCarney  */
6191d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson,
6201d7c0054SEd Tanous                              const dbus::utility::DBusPropertiesMap& valuesDict,
621adc4f0dbSShawn McCarney                              const InventoryItem* inventoryItem)
62234dd179eSJames Feist {
623adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
624adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
625adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
626adc4f0dbSShawn McCarney     std::string currentHealth;
627adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
628adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
629adc4f0dbSShawn McCarney     {
630adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
631adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
632adc4f0dbSShawn McCarney         {
633adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
634adc4f0dbSShawn McCarney             if (health != nullptr)
635adc4f0dbSShawn McCarney             {
636adc4f0dbSShawn McCarney                 currentHealth = *health;
637adc4f0dbSShawn McCarney             }
638adc4f0dbSShawn McCarney         }
639adc4f0dbSShawn McCarney     }
640adc4f0dbSShawn McCarney 
641adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
642adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
643adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
644adc4f0dbSShawn McCarney     {
645adc4f0dbSShawn McCarney         return "Critical";
646adc4f0dbSShawn McCarney     }
647adc4f0dbSShawn McCarney 
648c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmHigh = nullptr;
649c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmLow = nullptr;
650c1343bf6SKrzysztof Grobelny     const bool* warningAlarmHigh = nullptr;
651c1343bf6SKrzysztof Grobelny     const bool* warningAlarmLow = nullptr;
652711ac7a9SEd Tanous 
653c1343bf6SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
654c1343bf6SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
655c1343bf6SKrzysztof Grobelny         criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
656c1343bf6SKrzysztof Grobelny         "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
657c1343bf6SKrzysztof Grobelny         warningAlarmLow);
658c1343bf6SKrzysztof Grobelny 
659c1343bf6SKrzysztof Grobelny     if (success)
66034dd179eSJames Feist     {
661c1343bf6SKrzysztof Grobelny         // Check if sensor has critical threshold alarm
662c1343bf6SKrzysztof Grobelny         if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
663c1343bf6SKrzysztof Grobelny             (criticalAlarmLow != nullptr && *criticalAlarmLow))
66434dd179eSJames Feist         {
66534dd179eSJames Feist             return "Critical";
66634dd179eSJames Feist         }
66734dd179eSJames Feist     }
66834dd179eSJames Feist 
669adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
670adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
671adc4f0dbSShawn McCarney     {
672adc4f0dbSShawn McCarney         return "Critical";
673adc4f0dbSShawn McCarney     }
674adc4f0dbSShawn McCarney 
675adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that. This
676adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
677adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
678adc4f0dbSShawn McCarney     {
679adc4f0dbSShawn McCarney         return "Warning";
680adc4f0dbSShawn McCarney     }
681adc4f0dbSShawn McCarney 
682c1343bf6SKrzysztof Grobelny     if (success)
683c1343bf6SKrzysztof Grobelny     {
684adc4f0dbSShawn McCarney         // Check if sensor has warning threshold alarm
685c1343bf6SKrzysztof Grobelny         if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
686c1343bf6SKrzysztof Grobelny             (warningAlarmLow != nullptr && *warningAlarmLow))
68734dd179eSJames Feist         {
688ebe4d91eSEd Tanous             return "Warning";
68934dd179eSJames Feist         }
69034dd179eSJames Feist     }
691adc4f0dbSShawn McCarney 
69234dd179eSJames Feist     return "OK";
69334dd179eSJames Feist }
69434dd179eSJames Feist 
69523a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
696d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
697d500549bSAnthony Wilson {
698d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
699d500549bSAnthony Wilson     {
700d500549bSAnthony Wilson         switch (inventoryItem->ledState)
701d500549bSAnthony Wilson         {
702d500549bSAnthony Wilson             case LedState::OFF:
703d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
704d500549bSAnthony Wilson                 break;
705d500549bSAnthony Wilson             case LedState::ON:
706d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
707d500549bSAnthony Wilson                 break;
708d500549bSAnthony Wilson             case LedState::BLINK:
709d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
710d500549bSAnthony Wilson                 break;
71123a21a1cSEd Tanous             case LedState::UNKNOWN:
712d500549bSAnthony Wilson                 break;
713d500549bSAnthony Wilson         }
714d500549bSAnthony Wilson     }
715d500549bSAnthony Wilson }
716d500549bSAnthony Wilson 
71734dd179eSJames Feist /**
71808777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
71908777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
720274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
72108777fb0SLewanczyk, Dawid  * build
7221d7c0054SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
7231d7c0054SEd Tanous  * @param propertiesDict A dictionary of the properties to build the sensor
7241d7c0054SEd Tanous  * from.
7251d7c0054SEd Tanous  * @param sensorJson  The json object to fill
726adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
727adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
72808777fb0SLewanczyk, Dawid  */
7291d7c0054SEd Tanous inline void objectPropertiesToJson(
7301d7c0054SEd Tanous     std::string_view sensorName, std::string_view sensorType,
7311d7c0054SEd Tanous     std::string_view chassisSubNode,
7321d7c0054SEd Tanous     const dbus::utility::DBusPropertiesMap& propertiesDict,
73381ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
7341abe55efSEd Tanous {
7351d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
736adc4f0dbSShawn McCarney     {
737c71d6125SEd Tanous         std::string subNodeEscaped(sensorType);
738c1d019a6SEd Tanous         subNodeEscaped.erase(
739c1d019a6SEd Tanous             std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
740c1d019a6SEd Tanous             subNodeEscaped.end());
741c1d019a6SEd Tanous 
742c1d019a6SEd Tanous         // For sensors in SensorCollection we set Id instead of MemberId,
743c1d019a6SEd Tanous         // including power sensors.
744c1d019a6SEd Tanous         subNodeEscaped += '_';
745c1d019a6SEd Tanous         subNodeEscaped += sensorName;
746c1d019a6SEd Tanous         sensorJson["Id"] = std::move(subNodeEscaped);
747c1d019a6SEd Tanous 
7481d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7491d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7501d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
75195a3ecadSAnthony Wilson     }
75295a3ecadSAnthony Wilson     else if (sensorType != "power")
75395a3ecadSAnthony Wilson     {
75495a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
75595a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
75695a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
75781ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
7581d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7591d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7601d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
761adc4f0dbSShawn McCarney     }
762e742b6ccSEd Tanous 
76381ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
76481ce609eSEd Tanous     sensorJson["Status"]["Health"] =
7651d7c0054SEd Tanous         getHealth(sensorJson, propertiesDict, inventoryItem);
76608777fb0SLewanczyk, Dawid 
76708777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
76808777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
76908777fb0SLewanczyk, Dawid     // that require integers, not floats.
77008777fb0SLewanczyk, Dawid     bool forceToInt = false;
77108777fb0SLewanczyk, Dawid 
7723929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
7731d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
77495a3ecadSAnthony Wilson     {
7752a4ba195SShounak Mitra         sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
776c2bf7f99SWludzik, Jozef 
7770ec8b83dSEd Tanous         sensor::ReadingType readingType = sensors::toReadingType(sensorType);
7780ec8b83dSEd Tanous         if (readingType == sensor::ReadingType::Invalid)
77995a3ecadSAnthony Wilson         {
780c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
781c2bf7f99SWludzik, Jozef                              << sensorType;
78295a3ecadSAnthony Wilson         }
783c2bf7f99SWludzik, Jozef         else
78495a3ecadSAnthony Wilson         {
785c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
78695a3ecadSAnthony Wilson         }
787c2bf7f99SWludzik, Jozef 
7881d7c0054SEd Tanous         std::string_view readingUnits = sensors::toReadingUnits(sensorType);
789c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
790f8ede15eSAdrian Ambrożewicz         {
791c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
792c2bf7f99SWludzik, Jozef                              << sensorType;
793c2bf7f99SWludzik, Jozef         }
794c2bf7f99SWludzik, Jozef         else
795c2bf7f99SWludzik, Jozef         {
796c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
797f8ede15eSAdrian Ambrożewicz         }
79895a3ecadSAnthony Wilson     }
79995a3ecadSAnthony Wilson     else if (sensorType == "temperature")
8001abe55efSEd Tanous     {
8013929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
80281ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
80308777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
80408777fb0SLewanczyk, Dawid         // implementation seems to implement fan
8051abe55efSEd Tanous     }
8061abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
8071abe55efSEd Tanous     {
8083929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
80981ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
81081ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
81181ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
81208777fb0SLewanczyk, Dawid         forceToInt = true;
8131abe55efSEd Tanous     }
8146f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
8156f6d0d32SEd Tanous     {
8163929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
81781ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
81881ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
81981ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
8206f6d0d32SEd Tanous         forceToInt = true;
8216f6d0d32SEd Tanous     }
8221abe55efSEd Tanous     else if (sensorType == "voltage")
8231abe55efSEd Tanous     {
8243929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
82581ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
8261abe55efSEd Tanous     }
8272474adfaSEd Tanous     else if (sensorType == "power")
8282474adfaSEd Tanous     {
8291d7c0054SEd Tanous         if (boost::iequals(sensorName, "total_power"))
830028f7ebcSEddie James         {
83181ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
8327ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
8337ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
83481ce609eSEd Tanous             sensorJson["MemberId"] = "0";
83581ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
8363929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
837028f7ebcSEddie James         }
8381d7c0054SEd Tanous         else if (boost::ifind_first(sensorName, "input").empty())
83949c53ac9SJohnathan Mantey         {
8403929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
84149c53ac9SJohnathan Mantey         }
84249c53ac9SJohnathan Mantey         else
84349c53ac9SJohnathan Mantey         {
8443929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
84549c53ac9SJohnathan Mantey         }
8462474adfaSEd Tanous     }
8471abe55efSEd Tanous     else
8481abe55efSEd Tanous     {
84955c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
85008777fb0SLewanczyk, Dawid         return;
85108777fb0SLewanczyk, Dawid     }
85208777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
8533929aca1SAnthony Wilson     std::vector<
8543929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
8553929aca1SAnthony Wilson         properties;
85608777fb0SLewanczyk, Dawid     properties.reserve(7);
85708777fb0SLewanczyk, Dawid 
85808777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
859de629b6eSShawn McCarney 
8601d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
8613929aca1SAnthony Wilson     {
8623929aca1SAnthony Wilson         properties.emplace_back(
8633929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
8643929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
8653929aca1SAnthony Wilson         properties.emplace_back(
8663929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
8673929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
8683929aca1SAnthony Wilson         properties.emplace_back(
8693929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
8703929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
8713929aca1SAnthony Wilson         properties.emplace_back(
8723929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
8733929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
8743929aca1SAnthony Wilson     }
8753929aca1SAnthony Wilson     else if (sensorType != "power")
876de629b6eSShawn McCarney     {
87708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8783929aca1SAnthony Wilson                                 "WarningHigh",
8793929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
88008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8813929aca1SAnthony Wilson                                 "WarningLow",
8823929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
88308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8843929aca1SAnthony Wilson                                 "CriticalHigh",
8853929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
88608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8873929aca1SAnthony Wilson                                 "CriticalLow",
8883929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
889de629b6eSShawn McCarney     }
89008777fb0SLewanczyk, Dawid 
8912474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
8922474adfaSEd Tanous 
8931d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
89495a3ecadSAnthony Wilson     {
89595a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8963929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
89795a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8983929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
89951c35a8fSGeorge Liu         properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
90051c35a8fSGeorge Liu                                 "Accuracy", "/Accuracy"_json_pointer);
90195a3ecadSAnthony Wilson     }
90295a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9031abe55efSEd Tanous     {
90408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9053929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
90608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9073929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
9081abe55efSEd Tanous     }
909adc4f0dbSShawn McCarney     else if (sensorType != "power")
9101abe55efSEd Tanous     {
91108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9123929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
91308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9143929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
91508777fb0SLewanczyk, Dawid     }
91608777fb0SLewanczyk, Dawid 
9173929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
9183929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
9191abe55efSEd Tanous     {
9201d7c0054SEd Tanous         for (const auto& [valueName, valueVariant] : propertiesDict)
921711ac7a9SEd Tanous         {
922711ac7a9SEd Tanous             if (valueName != std::get<1>(p))
923711ac7a9SEd Tanous             {
924711ac7a9SEd Tanous                 continue;
925711ac7a9SEd Tanous             }
9263929aca1SAnthony Wilson 
9273929aca1SAnthony Wilson             // The property we want to set may be nested json, so use
9283929aca1SAnthony Wilson             // a json_pointer for easy indexing into the json structure.
9293929aca1SAnthony Wilson             const nlohmann::json::json_pointer& key = std::get<2>(p);
9303929aca1SAnthony Wilson 
931abf2add6SEd Tanous             const double* doubleValue = std::get_if<double>(&valueVariant);
93240e4f380SEd Tanous             if (doubleValue == nullptr)
9331abe55efSEd Tanous             {
93440e4f380SEd Tanous                 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
9356f6d0d32SEd Tanous                 continue;
93608777fb0SLewanczyk, Dawid             }
9376f6d0d32SEd Tanous             if (forceToInt)
9386f6d0d32SEd Tanous             {
93940e4f380SEd Tanous                 sensorJson[key] = static_cast<int64_t>(*doubleValue);
9406f6d0d32SEd Tanous             }
9416f6d0d32SEd Tanous             else
9426f6d0d32SEd Tanous             {
94340e4f380SEd Tanous                 sensorJson[key] = *doubleValue;
94408777fb0SLewanczyk, Dawid             }
94508777fb0SLewanczyk, Dawid         }
94608777fb0SLewanczyk, Dawid     }
94708777fb0SLewanczyk, Dawid }
94808777fb0SLewanczyk, Dawid 
9491d7c0054SEd Tanous /**
9501d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
9511d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
9521d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
9531d7c0054SEd Tanous  * build
9541d7c0054SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
9551d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
9561d7c0054SEd Tanous  * interfaces to be built from
9571d7c0054SEd Tanous  * @param sensorJson  The json object to fill
9581d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
9591d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
9601d7c0054SEd Tanous  */
9611d7c0054SEd Tanous inline void objectInterfacesToJson(
9621d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
9631d7c0054SEd Tanous     const std::string& chassisSubNode,
9641d7c0054SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
9651d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
9661d7c0054SEd Tanous {
9671d7c0054SEd Tanous 
9681d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
9691d7c0054SEd Tanous     {
9701d7c0054SEd Tanous         objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
9711d7c0054SEd Tanous                                valuesDict, sensorJson, inventoryItem);
9721d7c0054SEd Tanous     }
973c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
9741d7c0054SEd Tanous }
9751d7c0054SEd Tanous 
976b5a76932SEd Tanous inline void populateFanRedundancy(
977b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
9788bd25ccdSJames Feist {
9798bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
980b9d36b47SEd Tanous         [sensorsAsyncResp](
981b9d36b47SEd Tanous             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         }
11488bd25ccdSJames Feist         },
11498bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
11508bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
11518bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
11528bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
11538bd25ccdSJames Feist         std::array<const char*, 1>{
11548bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
11558bd25ccdSJames Feist }
11568bd25ccdSJames Feist 
1157b5a76932SEd Tanous inline void
115881ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
115949c53ac9SJohnathan Mantey {
11608d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
116149c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
116281ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
116349c53ac9SJohnathan Mantey     {
116449c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
116549c53ac9SJohnathan Mantey     }
116649c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
116749c53ac9SJohnathan Mantey     {
116849c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
116949c53ac9SJohnathan Mantey         if (entry != response.end())
117049c53ac9SJohnathan Mantey         {
117149c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
117202cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
117349c53ac9SJohnathan Mantey                 return c1["Name"] < c2["Name"];
117449c53ac9SJohnathan Mantey             });
117549c53ac9SJohnathan Mantey 
117649c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
117749c53ac9SJohnathan Mantey             size_t count = 0;
117849c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
117949c53ac9SJohnathan Mantey             {
118049c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
118149c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
118249c53ac9SJohnathan Mantey                 {
118349c53ac9SJohnathan Mantey                     continue;
118449c53ac9SJohnathan Mantey                 }
118549c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
118649c53ac9SJohnathan Mantey                 if (value != nullptr)
118749c53ac9SJohnathan Mantey                 {
118849c53ac9SJohnathan Mantey                     *value += std::to_string(count);
118949c53ac9SJohnathan Mantey                     count++;
119081ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
119149c53ac9SJohnathan Mantey                 }
119249c53ac9SJohnathan Mantey             }
119349c53ac9SJohnathan Mantey         }
119449c53ac9SJohnathan Mantey     }
119549c53ac9SJohnathan Mantey }
119649c53ac9SJohnathan Mantey 
119708777fb0SLewanczyk, Dawid /**
1198adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1199adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1200adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1201adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12028fb49dd6SShawn McCarney  */
120323a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1204b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1205adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
12068fb49dd6SShawn McCarney {
1207adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
12088fb49dd6SShawn McCarney     {
1209adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
12108fb49dd6SShawn McCarney         {
1211adc4f0dbSShawn McCarney             return &inventoryItem;
12128fb49dd6SShawn McCarney         }
12138fb49dd6SShawn McCarney     }
12148fb49dd6SShawn McCarney     return nullptr;
12158fb49dd6SShawn McCarney }
12168fb49dd6SShawn McCarney 
12178fb49dd6SShawn McCarney /**
1218adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1219adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1220adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1221adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12228fb49dd6SShawn McCarney  */
122323a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1224b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1225adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1226adc4f0dbSShawn McCarney {
1227adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1228adc4f0dbSShawn McCarney     {
1229adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1230adc4f0dbSShawn McCarney         {
1231adc4f0dbSShawn McCarney             return &inventoryItem;
1232adc4f0dbSShawn McCarney         }
1233adc4f0dbSShawn McCarney     }
1234adc4f0dbSShawn McCarney     return nullptr;
1235adc4f0dbSShawn McCarney }
1236adc4f0dbSShawn McCarney 
1237adc4f0dbSShawn McCarney /**
1238d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1239d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1240d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1241d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1242d500549bSAnthony Wilson  */
1243d500549bSAnthony Wilson inline InventoryItem*
1244d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1245d500549bSAnthony Wilson                             const std::string& ledObjPath)
1246d500549bSAnthony Wilson {
1247d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1248d500549bSAnthony Wilson     {
1249d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1250d500549bSAnthony Wilson         {
1251d500549bSAnthony Wilson             return &inventoryItem;
1252d500549bSAnthony Wilson         }
1253d500549bSAnthony Wilson     }
1254d500549bSAnthony Wilson     return nullptr;
1255d500549bSAnthony Wilson }
1256d500549bSAnthony Wilson 
1257d500549bSAnthony Wilson /**
1258adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1259adc4f0dbSShawn McCarney  *
1260adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1261adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1262adc4f0dbSShawn McCarney  * added to the vector.
1263adc4f0dbSShawn McCarney  *
1264adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1265adc4f0dbSShawn McCarney  * InventoryItem.
1266adc4f0dbSShawn McCarney  *
1267adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1268adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1269adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1270adc4f0dbSShawn McCarney  */
1271b5a76932SEd Tanous inline void addInventoryItem(
1272b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1273b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1274adc4f0dbSShawn McCarney {
1275adc4f0dbSShawn McCarney     // Look for inventory item in vector
1276adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1277adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1278adc4f0dbSShawn McCarney 
1279adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1280adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1281adc4f0dbSShawn McCarney     {
1282adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1283adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1284adc4f0dbSShawn McCarney     }
1285adc4f0dbSShawn McCarney 
1286adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1287adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1288adc4f0dbSShawn McCarney }
1289adc4f0dbSShawn McCarney 
1290adc4f0dbSShawn McCarney /**
1291adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1292adc4f0dbSShawn McCarney  *
1293adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1294adc4f0dbSShawn McCarney  * specified InventoryItem.
1295adc4f0dbSShawn McCarney  *
1296adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1297adc4f0dbSShawn McCarney  * response.
1298adc4f0dbSShawn McCarney  *
1299adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1300adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1301adc4f0dbSShawn McCarney  * for the specified inventory item.
1302adc4f0dbSShawn McCarney  */
130323a21a1cSEd Tanous inline void storeInventoryItemData(
1304adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1305711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
13068fb49dd6SShawn McCarney {
1307adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1308711ac7a9SEd Tanous 
13099eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
13108fb49dd6SShawn McCarney     {
1311711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
13128fb49dd6SShawn McCarney         {
13139eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1314711ac7a9SEd Tanous             {
1315711ac7a9SEd Tanous                 if (name == "Present")
1316711ac7a9SEd Tanous                 {
1317711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1318adc4f0dbSShawn McCarney                     if (value != nullptr)
13198fb49dd6SShawn McCarney                     {
1320adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
13218fb49dd6SShawn McCarney                     }
13228fb49dd6SShawn McCarney                 }
13238fb49dd6SShawn McCarney             }
1324711ac7a9SEd Tanous         }
1325adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1326711ac7a9SEd Tanous 
1327711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
13288fb49dd6SShawn McCarney         {
1329adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
13308fb49dd6SShawn McCarney         }
1331adc4f0dbSShawn McCarney 
1332adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1333711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1334adc4f0dbSShawn McCarney         {
13359eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1336711ac7a9SEd Tanous             {
1337711ac7a9SEd Tanous                 if (name == "Manufacturer")
1338adc4f0dbSShawn McCarney                 {
1339adc4f0dbSShawn McCarney                     const std::string* value =
1340711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1341adc4f0dbSShawn McCarney                     if (value != nullptr)
1342adc4f0dbSShawn McCarney                     {
1343adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1344adc4f0dbSShawn McCarney                     }
1345adc4f0dbSShawn McCarney                 }
1346711ac7a9SEd Tanous                 if (name == "Model")
1347adc4f0dbSShawn McCarney                 {
1348adc4f0dbSShawn McCarney                     const std::string* value =
1349711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1350adc4f0dbSShawn McCarney                     if (value != nullptr)
1351adc4f0dbSShawn McCarney                     {
1352adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1353adc4f0dbSShawn McCarney                     }
1354adc4f0dbSShawn McCarney                 }
1355711ac7a9SEd Tanous                 if (name == "SerialNumber")
1356adc4f0dbSShawn McCarney                 {
1357adc4f0dbSShawn McCarney                     const std::string* value =
1358711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1359adc4f0dbSShawn McCarney                     if (value != nullptr)
1360adc4f0dbSShawn McCarney                     {
1361adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1362adc4f0dbSShawn McCarney                     }
1363adc4f0dbSShawn McCarney                 }
1364711ac7a9SEd Tanous                 if (name == "PartNumber")
1365711ac7a9SEd Tanous                 {
1366711ac7a9SEd Tanous                     const std::string* value =
1367711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1368711ac7a9SEd Tanous                     if (value != nullptr)
1369711ac7a9SEd Tanous                     {
1370711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1371711ac7a9SEd Tanous                     }
1372711ac7a9SEd Tanous                 }
1373711ac7a9SEd Tanous             }
1374adc4f0dbSShawn McCarney         }
1375adc4f0dbSShawn McCarney 
1376711ac7a9SEd Tanous         if (interface ==
1377711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1378adc4f0dbSShawn McCarney         {
13799eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1380adc4f0dbSShawn McCarney             {
1381711ac7a9SEd Tanous                 if (name == "Functional")
1382711ac7a9SEd Tanous                 {
1383711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1384adc4f0dbSShawn McCarney                     if (value != nullptr)
1385adc4f0dbSShawn McCarney                     {
1386adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
13878fb49dd6SShawn McCarney                     }
13888fb49dd6SShawn McCarney                 }
13898fb49dd6SShawn McCarney             }
13908fb49dd6SShawn McCarney         }
1391711ac7a9SEd Tanous     }
1392711ac7a9SEd Tanous }
13938fb49dd6SShawn McCarney 
13948fb49dd6SShawn McCarney /**
1395adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
13968fb49dd6SShawn McCarney  *
1397adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1398adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1399adc4f0dbSShawn McCarney  * inventoryItems vector.
14008fb49dd6SShawn McCarney  *
1401adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1402adc4f0dbSShawn McCarney  * response.
1403adc4f0dbSShawn McCarney  *
1404adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1405adc4f0dbSShawn McCarney  * been obtained.
1406adc4f0dbSShawn McCarney  *
1407adc4f0dbSShawn McCarney  * The callback must have the following signature:
1408adc4f0dbSShawn McCarney  *   @code
1409d500549bSAnthony Wilson  *   callback(void)
1410adc4f0dbSShawn McCarney  *   @endcode
1411adc4f0dbSShawn McCarney  *
1412adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1413adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1414adc4f0dbSShawn McCarney  * last asynchronous function has completed.
14158fb49dd6SShawn McCarney  *
14168fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1417adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1418adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
14198fb49dd6SShawn McCarney  * implements ObjectManager.
1420adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1421adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1422adc4f0dbSShawn McCarney  * in recursive calls to this function.
14238fb49dd6SShawn McCarney  */
1424adc4f0dbSShawn McCarney template <typename Callback>
1425adc4f0dbSShawn McCarney static void getInventoryItemsData(
14268fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1427adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1428d0090733SEd Tanous     std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1429d0090733SEd Tanous     size_t invConnectionsIndex = 0)
14308fb49dd6SShawn McCarney {
1431adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
14328fb49dd6SShawn McCarney 
1433adc4f0dbSShawn McCarney     // If no more connections left, call callback
1434adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
14358fb49dd6SShawn McCarney     {
1436d500549bSAnthony Wilson         callback();
1437adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1438adc4f0dbSShawn McCarney         return;
1439adc4f0dbSShawn McCarney     }
1440adc4f0dbSShawn McCarney 
1441adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1442fe04d49cSNan Zhou     auto it = invConnections->begin();
1443fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1444adc4f0dbSShawn McCarney     if (it != invConnections->end())
1445adc4f0dbSShawn McCarney     {
1446adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1447adc4f0dbSShawn McCarney 
14488fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1449d0090733SEd Tanous         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1450d0090733SEd Tanous                             callback{std::forward<Callback>(callback)},
1451d0090733SEd Tanous                             invConnectionsIndex](
145202cad96eSEd Tanous                                const boost::system::error_code ec,
145302cad96eSEd Tanous                                const dbus::utility::ManagedObjectType& resp) {
1454adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
14558fb49dd6SShawn McCarney             if (ec)
14568fb49dd6SShawn McCarney             {
14578fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1458adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
14598d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
14608fb49dd6SShawn McCarney                 return;
14618fb49dd6SShawn McCarney             }
14628fb49dd6SShawn McCarney 
14638fb49dd6SShawn McCarney             // Loop through returned object paths
14648fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
14658fb49dd6SShawn McCarney             {
14668fb49dd6SShawn McCarney                 const std::string& objPath =
14678fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
14688fb49dd6SShawn McCarney 
1469adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1470adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1471adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1472adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
14738fb49dd6SShawn McCarney                 {
1474adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1475adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
14768fb49dd6SShawn McCarney                 }
14778fb49dd6SShawn McCarney             }
14788fb49dd6SShawn McCarney 
1479adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1480adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1481d0090733SEd Tanous                                   invConnections, std::move(callback),
1482d0090733SEd Tanous                                   invConnectionsIndex + 1);
1483adc4f0dbSShawn McCarney 
1484adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
14858fb49dd6SShawn McCarney         };
14868fb49dd6SShawn McCarney 
14878fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
14888fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
1489d0090733SEd Tanous             std::move(respHandler), invConnection,
1490f8bb0ff3SEd Tanous             "/xyz/openbmc_project/inventory",
14918fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14928fb49dd6SShawn McCarney     }
14938fb49dd6SShawn McCarney 
1494adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
14958fb49dd6SShawn McCarney }
14968fb49dd6SShawn McCarney 
14978fb49dd6SShawn McCarney /**
1498adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
14998fb49dd6SShawn McCarney  *
1500adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1501adc4f0dbSShawn McCarney  * items that are associated with sensors.
15028fb49dd6SShawn McCarney  *
15038fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
15048fb49dd6SShawn McCarney  * been obtained.
15058fb49dd6SShawn McCarney  *
15068fb49dd6SShawn McCarney  * The callback must have the following signature:
15078fb49dd6SShawn McCarney  *   @code
1508fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
15098fb49dd6SShawn McCarney  *   @endcode
15108fb49dd6SShawn McCarney  *
15118fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1512adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
15138fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
15148fb49dd6SShawn McCarney  */
15158fb49dd6SShawn McCarney template <typename Callback>
15168fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1517b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1518b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
15198fb49dd6SShawn McCarney     Callback&& callback)
15208fb49dd6SShawn McCarney {
15218fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
15228fb49dd6SShawn McCarney 
15238fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1524adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
15258fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1526adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1527adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
15288fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
15298fb49dd6SShawn McCarney 
15308fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1531002d39b4SEd Tanous     auto respHandler =
1532002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1533002d39b4SEd Tanous          inventoryItems](
1534b9d36b47SEd Tanous             const boost::system::error_code ec,
1535002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
15368fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
15378fb49dd6SShawn McCarney         if (ec)
15388fb49dd6SShawn McCarney         {
15398d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
15408fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
15418fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
15428fb49dd6SShawn McCarney             return;
15438fb49dd6SShawn McCarney         }
15448fb49dd6SShawn McCarney 
15458fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
1546fe04d49cSNan Zhou         std::shared_ptr<std::set<std::string>> invConnections =
1547fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
15488fb49dd6SShawn McCarney 
15498fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
15508fb49dd6SShawn McCarney         for (const std::pair<
15518fb49dd6SShawn McCarney                  std::string,
15528fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
15538fb49dd6SShawn McCarney                  object : subtree)
15548fb49dd6SShawn McCarney         {
1555adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
15568fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1557adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
15588fb49dd6SShawn McCarney             {
15598fb49dd6SShawn McCarney                 // Store all connections to inventory item
15608fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
15618fb49dd6SShawn McCarney                          objData : object.second)
15628fb49dd6SShawn McCarney                 {
15638fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
15648fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
15658fb49dd6SShawn McCarney                 }
15668fb49dd6SShawn McCarney             }
15678fb49dd6SShawn McCarney         }
1568d500549bSAnthony Wilson 
15698fb49dd6SShawn McCarney         callback(invConnections);
15708fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
15718fb49dd6SShawn McCarney     };
15728fb49dd6SShawn McCarney 
15738fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
15748fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
15758fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
15768fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
15778fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
15788fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
15798fb49dd6SShawn McCarney }
15808fb49dd6SShawn McCarney 
15818fb49dd6SShawn McCarney /**
1582adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
15838fb49dd6SShawn McCarney  *
15848fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1585d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1586d500549bSAnthony Wilson  * their LEDs, if any.
15878fb49dd6SShawn McCarney  *
15888fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
15898fb49dd6SShawn McCarney  * has been obtained.
15908fb49dd6SShawn McCarney  *
15918fb49dd6SShawn McCarney  * The callback must have the following signature:
15928fb49dd6SShawn McCarney  *   @code
1593adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
15948fb49dd6SShawn McCarney  *   @endcode
15958fb49dd6SShawn McCarney  *
15968fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
15978fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
15988fb49dd6SShawn McCarney  * implements ObjectManager.
15998fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
16008fb49dd6SShawn McCarney  */
16018fb49dd6SShawn McCarney template <typename Callback>
1602adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1603b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1604fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
16058fb49dd6SShawn McCarney     Callback&& callback)
16068fb49dd6SShawn McCarney {
1607adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
16088fb49dd6SShawn McCarney 
16098fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
161002cad96eSEd Tanous     auto respHandler =
161102cad96eSEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
16128fb49dd6SShawn McCarney          sensorNames](const boost::system::error_code ec,
161302cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
1614adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
16158fb49dd6SShawn McCarney         if (ec)
16168fb49dd6SShawn McCarney         {
1617adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1618adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
16198d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
16208fb49dd6SShawn McCarney             return;
16218fb49dd6SShawn McCarney         }
16228fb49dd6SShawn McCarney 
1623adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1624adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1625adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1626adc4f0dbSShawn McCarney 
16278fb49dd6SShawn McCarney         // Loop through returned object paths
16288fb49dd6SShawn McCarney         std::string sensorAssocPath;
16298fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
16308fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
16318fb49dd6SShawn McCarney         {
16328fb49dd6SShawn McCarney             const std::string& objPath =
16338fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
16348fb49dd6SShawn McCarney 
16358fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
16368fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
16378fb49dd6SShawn McCarney             {
16388fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
16398fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
16408fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
16418fb49dd6SShawn McCarney                 {
16428fb49dd6SShawn McCarney                     // Get Association interface for object path
1643711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
16448fb49dd6SShawn McCarney                     {
1645711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1646711ac7a9SEd Tanous                         {
1647711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1648711ac7a9SEd Tanous                             {
1649711ac7a9SEd Tanous                                 if (valueName == "endpoints")
16508fb49dd6SShawn McCarney                                 {
16518fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
16528fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1653711ac7a9SEd Tanous                                             &value);
1654711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1655711ac7a9SEd Tanous                                         !endpoints->empty())
16568fb49dd6SShawn McCarney                                     {
1657adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1658adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1659adc4f0dbSShawn McCarney                                             endpoints->front();
1660711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1661711ac7a9SEd Tanous                                                          invItemPath,
1662adc4f0dbSShawn McCarney                                                          sensorName);
16638fb49dd6SShawn McCarney                                     }
16648fb49dd6SShawn McCarney                                 }
16658fb49dd6SShawn McCarney                             }
1666711ac7a9SEd Tanous                         }
1667711ac7a9SEd Tanous                     }
16688fb49dd6SShawn McCarney                     break;
16698fb49dd6SShawn McCarney                 }
16708fb49dd6SShawn McCarney             }
16718fb49dd6SShawn McCarney         }
16728fb49dd6SShawn McCarney 
1673d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1674d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1675d500549bSAnthony Wilson         std::string inventoryAssocPath;
1676d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1677d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1678d500549bSAnthony Wilson         {
1679d500549bSAnthony Wilson             const std::string& objPath =
1680d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1681d500549bSAnthony Wilson 
1682d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1683d500549bSAnthony Wilson             {
1684d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1685d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1686d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1687d500549bSAnthony Wilson                 {
1688711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1689d500549bSAnthony Wilson                     {
1690711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1691711ac7a9SEd Tanous                         {
1692711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1693711ac7a9SEd Tanous                             {
1694711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1695d500549bSAnthony Wilson                                 {
1696d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1697d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1698711ac7a9SEd Tanous                                             &value);
1699711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1700711ac7a9SEd Tanous                                         !endpoints->empty())
1701d500549bSAnthony Wilson                                     {
1702711ac7a9SEd Tanous                                         // Add inventory item to vector
1703d500549bSAnthony Wilson                                         // Store LED path in inventory item
1704711ac7a9SEd Tanous                                         const std::string& ledPath =
1705711ac7a9SEd Tanous                                             endpoints->front();
1706d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1707d500549bSAnthony Wilson                                     }
1708d500549bSAnthony Wilson                                 }
1709d500549bSAnthony Wilson                             }
1710711ac7a9SEd Tanous                         }
1711711ac7a9SEd Tanous                     }
1712711ac7a9SEd Tanous 
1713d500549bSAnthony Wilson                     break;
1714d500549bSAnthony Wilson                 }
1715d500549bSAnthony Wilson             }
1716d500549bSAnthony Wilson         }
1717adc4f0dbSShawn McCarney         callback(inventoryItems);
1718adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
17198fb49dd6SShawn McCarney     };
17208fb49dd6SShawn McCarney 
17218fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
17228fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
1723d0090733SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
17248fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17258fb49dd6SShawn McCarney 
1726adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
17278fb49dd6SShawn McCarney }
17288fb49dd6SShawn McCarney 
17298fb49dd6SShawn McCarney /**
1730d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1731d500549bSAnthony Wilson  *
1732d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1733d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1734d500549bSAnthony Wilson  * inventoryItems vector.
1735d500549bSAnthony Wilson  *
1736d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1737d500549bSAnthony Wilson  * response.
1738d500549bSAnthony Wilson  *
1739d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1740d500549bSAnthony Wilson  * has been obtained.
1741d500549bSAnthony Wilson  *
1742d500549bSAnthony Wilson  * The callback must have the following signature:
1743d500549bSAnthony Wilson  *   @code
174442cbe538SGunnar Mills  *   callback()
1745d500549bSAnthony Wilson  *   @endcode
1746d500549bSAnthony Wilson  *
1747d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1748d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1749d500549bSAnthony Wilson  * last asynchronous function has completed.
1750d500549bSAnthony Wilson  *
1751d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1752d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1753d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1754d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1755d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1756d500549bSAnthony Wilson  * in recursive calls to this function.
1757d500549bSAnthony Wilson  */
1758d500549bSAnthony Wilson template <typename Callback>
1759d500549bSAnthony Wilson void getInventoryLedData(
1760d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1761d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1762fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1763d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1764d500549bSAnthony Wilson {
1765d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1766d500549bSAnthony Wilson 
1767d500549bSAnthony Wilson     // If no more connections left, call callback
1768d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1769d500549bSAnthony Wilson     {
177042cbe538SGunnar Mills         callback();
1771d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1772d500549bSAnthony Wilson         return;
1773d500549bSAnthony Wilson     }
1774d500549bSAnthony Wilson 
1775d500549bSAnthony Wilson     // Get inventory item data from current connection
1776fe04d49cSNan Zhou     auto it = ledConnections->begin();
1777fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1778d500549bSAnthony Wilson     if (it != ledConnections->end())
1779d500549bSAnthony Wilson     {
1780d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1781d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1782d500549bSAnthony Wilson         // Response handler for Get State property
17831e1e598dSJonathan Doman         auto respHandler =
17841e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1785f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
17861e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1787d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1788d500549bSAnthony Wilson             if (ec)
1789d500549bSAnthony Wilson             {
1790d500549bSAnthony Wilson                 BMCWEB_LOG_ERROR
1791d500549bSAnthony Wilson                     << "getInventoryLedData respHandler DBus error " << ec;
17928d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1793d500549bSAnthony Wilson                 return;
1794d500549bSAnthony Wilson             }
1795d500549bSAnthony Wilson 
17961e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Led state: " << state;
1797d500549bSAnthony Wilson             // Find inventory item with this LED object path
1798d500549bSAnthony Wilson             InventoryItem* inventoryItem =
1799d500549bSAnthony Wilson                 findInventoryItemForLed(*inventoryItems, ledPath);
1800d500549bSAnthony Wilson             if (inventoryItem != nullptr)
1801d500549bSAnthony Wilson             {
1802d500549bSAnthony Wilson                 // Store LED state in InventoryItem
180311ba3979SEd Tanous                 if (state.ends_with("On"))
1804d500549bSAnthony Wilson                 {
1805d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::ON;
1806d500549bSAnthony Wilson                 }
180711ba3979SEd Tanous                 else if (state.ends_with("Blink"))
1808d500549bSAnthony Wilson                 {
1809d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::BLINK;
1810d500549bSAnthony Wilson                 }
181111ba3979SEd Tanous                 else if (state.ends_with("Off"))
1812d500549bSAnthony Wilson                 {
1813d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::OFF;
1814d500549bSAnthony Wilson                 }
1815d500549bSAnthony Wilson                 else
1816d500549bSAnthony Wilson                 {
1817d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::UNKNOWN;
1818d500549bSAnthony Wilson                 }
1819d500549bSAnthony Wilson             }
1820d500549bSAnthony Wilson 
1821d500549bSAnthony Wilson             // Recurse to get LED data from next connection
1822d500549bSAnthony Wilson             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1823d500549bSAnthony Wilson                                 ledConnections, std::move(callback),
1824d500549bSAnthony Wilson                                 ledConnectionsIndex + 1);
1825d500549bSAnthony Wilson 
1826d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1827d500549bSAnthony Wilson         };
1828d500549bSAnthony Wilson 
1829d500549bSAnthony Wilson         // Get the State property for the current LED
18301e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
18311e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
18321e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
18331e1e598dSJonathan Doman             std::move(respHandler));
1834d500549bSAnthony Wilson     }
1835d500549bSAnthony Wilson 
1836d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1837d500549bSAnthony Wilson }
1838d500549bSAnthony Wilson 
1839d500549bSAnthony Wilson /**
1840d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1841d500549bSAnthony Wilson  *
1842d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1843d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1844d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1845d500549bSAnthony Wilson  *
1846d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1847d500549bSAnthony Wilson  * response.
1848d500549bSAnthony Wilson  *
1849d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1850d500549bSAnthony Wilson  * been obtained.
1851d500549bSAnthony Wilson  *
1852d500549bSAnthony Wilson  * The callback must have the following signature:
1853d500549bSAnthony Wilson  *   @code
185442cbe538SGunnar Mills  *   callback()
1855d500549bSAnthony Wilson  *   @endcode
1856d500549bSAnthony Wilson  *
1857d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1858d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1859d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1860d500549bSAnthony Wilson  */
1861d500549bSAnthony Wilson template <typename Callback>
1862d500549bSAnthony Wilson void getInventoryLeds(
1863d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1864d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1865d500549bSAnthony Wilson     Callback&& callback)
1866d500549bSAnthony Wilson {
1867d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1868d500549bSAnthony Wilson 
1869d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1870d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
1871d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1872d500549bSAnthony Wilson 
1873d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
1874002d39b4SEd Tanous     auto respHandler =
1875002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1876002d39b4SEd Tanous          inventoryItems](
1877b9d36b47SEd Tanous             const boost::system::error_code ec,
1878002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1879d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1880d500549bSAnthony Wilson         if (ec)
1881d500549bSAnthony Wilson         {
18828d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
1883d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1884d500549bSAnthony Wilson                              << ec;
1885d500549bSAnthony Wilson             return;
1886d500549bSAnthony Wilson         }
1887d500549bSAnthony Wilson 
1888d500549bSAnthony Wilson         // Build map of LED object paths to connections
1889fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1890fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
1891d500549bSAnthony Wilson 
1892d500549bSAnthony Wilson         // Loop through objects from GetSubTree
1893d500549bSAnthony Wilson         for (const std::pair<
1894d500549bSAnthony Wilson                  std::string,
1895d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1896d500549bSAnthony Wilson                  object : subtree)
1897d500549bSAnthony Wilson         {
1898d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
1899d500549bSAnthony Wilson             // items
1900d500549bSAnthony Wilson             const std::string& ledPath = object.first;
1901d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1902d500549bSAnthony Wilson             {
1903d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
1904d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
1905d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
1906d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1907d500549bSAnthony Wilson                                  << connection;
1908d500549bSAnthony Wilson             }
1909d500549bSAnthony Wilson         }
1910d500549bSAnthony Wilson 
1911d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1912d500549bSAnthony Wilson                             std::move(callback));
1913d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1914d500549bSAnthony Wilson     };
1915d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
1916d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
1917d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1918d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
1919d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1920d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1921d500549bSAnthony Wilson }
1922d500549bSAnthony Wilson 
1923d500549bSAnthony Wilson /**
192442cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
192542cbe538SGunnar Mills  *
192642cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
192742cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
192842cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
192942cbe538SGunnar Mills  *
193042cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
193142cbe538SGunnar Mills  * response.
193242cbe538SGunnar Mills  *
193342cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
193442cbe538SGunnar Mills  * when data has been obtained.
193542cbe538SGunnar Mills  *
193642cbe538SGunnar Mills  * The callback must have the following signature:
193742cbe538SGunnar Mills  *   @code
193842cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
193942cbe538SGunnar Mills  *   @endcode
194042cbe538SGunnar Mills  *
194142cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
194242cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
194342cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
194442cbe538SGunnar Mills  *        Supply Attributes
194542cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
194642cbe538SGunnar Mills  */
194742cbe538SGunnar Mills template <typename Callback>
194842cbe538SGunnar Mills void getPowerSupplyAttributesData(
1949b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
195042cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1951fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
195242cbe538SGunnar Mills     Callback&& callback)
195342cbe538SGunnar Mills {
195442cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
195542cbe538SGunnar Mills 
195642cbe538SGunnar Mills     if (psAttributesConnections.empty())
195742cbe538SGunnar Mills     {
195842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
195942cbe538SGunnar Mills         callback(inventoryItems);
196042cbe538SGunnar Mills         return;
196142cbe538SGunnar Mills     }
196242cbe538SGunnar Mills 
196342cbe538SGunnar Mills     // Assuming just one connection (service) for now
1964fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
196542cbe538SGunnar Mills 
196642cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
196742cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
196842cbe538SGunnar Mills 
196942cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
1970002d39b4SEd Tanous     auto respHandler =
1971002d39b4SEd Tanous         [sensorsAsyncResp, inventoryItems,
1972f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
1973002d39b4SEd Tanous             const boost::system::error_code ec, const uint32_t value) {
197442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
197542cbe538SGunnar Mills         if (ec)
197642cbe538SGunnar Mills         {
197742cbe538SGunnar Mills             BMCWEB_LOG_ERROR
197842cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
19798d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
198042cbe538SGunnar Mills             return;
198142cbe538SGunnar Mills         }
198242cbe538SGunnar Mills 
19831e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
198442cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
198542cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
198642cbe538SGunnar Mills         {
198755f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
198842cbe538SGunnar Mills             {
198942cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
19901e1e598dSJonathan Doman                     static_cast<int>(value);
199142cbe538SGunnar Mills             }
199242cbe538SGunnar Mills         }
199342cbe538SGunnar Mills 
199442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
199542cbe538SGunnar Mills         callback(inventoryItems);
199642cbe538SGunnar Mills     };
199742cbe538SGunnar Mills 
199842cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
199942cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
20001e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
20011e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
20021e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
20031e1e598dSJonathan Doman         std::move(respHandler));
200442cbe538SGunnar Mills 
200542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
200642cbe538SGunnar Mills }
200742cbe538SGunnar Mills 
200842cbe538SGunnar Mills /**
200942cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
201042cbe538SGunnar Mills  *
201142cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
201242cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
201342cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
201442cbe538SGunnar Mills  * item.
201542cbe538SGunnar Mills  *
201642cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
201742cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
201842cbe538SGunnar Mills  *
201942cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
202042cbe538SGunnar Mills  * when information has been obtained.
202142cbe538SGunnar Mills  *
202242cbe538SGunnar Mills  * The callback must have the following signature:
202342cbe538SGunnar Mills  *   @code
202442cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
202542cbe538SGunnar Mills  *   @endcode
202642cbe538SGunnar Mills  *
202742cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
202842cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
202942cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
203042cbe538SGunnar Mills  */
203142cbe538SGunnar Mills template <typename Callback>
203242cbe538SGunnar Mills void getPowerSupplyAttributes(
203342cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
203442cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
203542cbe538SGunnar Mills     Callback&& callback)
203642cbe538SGunnar Mills {
203742cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
203842cbe538SGunnar Mills 
203942cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2040a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
204142cbe538SGunnar Mills     {
204242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
204342cbe538SGunnar Mills         callback(inventoryItems);
204442cbe538SGunnar Mills         return;
204542cbe538SGunnar Mills     }
204642cbe538SGunnar Mills 
204742cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
204842cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
204942cbe538SGunnar Mills 
205042cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2051b9d36b47SEd Tanous     auto respHandler =
2052b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2053b9d36b47SEd Tanous          inventoryItems](
2054b9d36b47SEd Tanous             const boost::system::error_code ec,
2055b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
205642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
205742cbe538SGunnar Mills         if (ec)
205842cbe538SGunnar Mills         {
20598d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
206042cbe538SGunnar Mills             BMCWEB_LOG_ERROR
206142cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
206242cbe538SGunnar Mills             return;
206342cbe538SGunnar Mills         }
206426f6976fSEd Tanous         if (subtree.empty())
206542cbe538SGunnar Mills         {
206642cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
206742cbe538SGunnar Mills             callback(inventoryItems);
206842cbe538SGunnar Mills             return;
206942cbe538SGunnar Mills         }
207042cbe538SGunnar Mills 
207142cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
207242cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
207342cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
2074fe04d49cSNan Zhou         std::map<std::string, std::string> psAttributesConnections;
207542cbe538SGunnar Mills 
207642cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
207742cbe538SGunnar Mills         {
207842cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
207942cbe538SGunnar Mills             callback(inventoryItems);
208042cbe538SGunnar Mills             return;
208142cbe538SGunnar Mills         }
208242cbe538SGunnar Mills 
208342cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
208442cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
208542cbe538SGunnar Mills 
208642cbe538SGunnar Mills         if (connection.empty())
208742cbe538SGunnar Mills         {
208842cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
208942cbe538SGunnar Mills             callback(inventoryItems);
209042cbe538SGunnar Mills             return;
209142cbe538SGunnar Mills         }
209242cbe538SGunnar Mills 
209342cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
209442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
209542cbe538SGunnar Mills                          << connection;
209642cbe538SGunnar Mills 
209742cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
209842cbe538SGunnar Mills                                      psAttributesConnections,
209942cbe538SGunnar Mills                                      std::move(callback));
210042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
210142cbe538SGunnar Mills     };
210242cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
210342cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
210442cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
210542cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
210642cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
210742cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
210842cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
210942cbe538SGunnar Mills }
211042cbe538SGunnar Mills 
211142cbe538SGunnar Mills /**
2112adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
21138fb49dd6SShawn McCarney  *
21148fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2115adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
21168fb49dd6SShawn McCarney  *
2117adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2118adc4f0dbSShawn McCarney  * response.
21198fb49dd6SShawn McCarney  *
2120adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2121adc4f0dbSShawn McCarney  * inventory items have been obtained.
2122adc4f0dbSShawn McCarney  *
2123adc4f0dbSShawn McCarney  * The callback must have the following signature:
2124adc4f0dbSShawn McCarney  *   @code
2125adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2126adc4f0dbSShawn McCarney  *   @endcode
21278fb49dd6SShawn McCarney  *
21288fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
21298fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
21308fb49dd6SShawn McCarney  * implements ObjectManager.
2131adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
21328fb49dd6SShawn McCarney  */
2133adc4f0dbSShawn McCarney template <typename Callback>
2134d0090733SEd Tanous static void
2135d0090733SEd Tanous     getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2136fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>> sensorNames,
2137adc4f0dbSShawn McCarney                       Callback&& callback)
21388fb49dd6SShawn McCarney {
2139adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2140adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2141d0090733SEd Tanous         [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
2142adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2143adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
21448fb49dd6SShawn McCarney         auto getInventoryItemsConnectionsCb =
2145d0090733SEd Tanous             [sensorsAsyncResp, inventoryItems,
2146f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
2147fe04d49cSNan Zhou                 std::shared_ptr<std::set<std::string>> invConnections) {
21488fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2149002d39b4SEd Tanous             auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2150d500549bSAnthony Wilson                                             callback{std::move(callback)}]() {
2151d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
215242cbe538SGunnar Mills 
2153002d39b4SEd Tanous                 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2154002d39b4SEd Tanous                                            callback{std::move(callback)}]() {
215542cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
215642cbe538SGunnar Mills                     // Find Power Supply Attributes and get the data
2157002d39b4SEd Tanous                     getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
215842cbe538SGunnar Mills                                              std::move(callback));
215942cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
216042cbe538SGunnar Mills                 };
216142cbe538SGunnar Mills 
2162d500549bSAnthony Wilson                 // Find led connections and get the data
2163d500549bSAnthony Wilson                 getInventoryLeds(sensorsAsyncResp, inventoryItems,
216442cbe538SGunnar Mills                                  std::move(getInventoryLedsCb));
2165d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2166d500549bSAnthony Wilson             };
21678fb49dd6SShawn McCarney 
2168adc4f0dbSShawn McCarney             // Get inventory item data from connections
2169adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2170d0090733SEd Tanous                                   invConnections,
2171d500549bSAnthony Wilson                                   std::move(getInventoryItemsDataCb));
21728fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
21738fb49dd6SShawn McCarney         };
21748fb49dd6SShawn McCarney 
2175adc4f0dbSShawn McCarney         // Get connections that provide inventory item data
2176002d39b4SEd Tanous         getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
21778fb49dd6SShawn McCarney                                      std::move(getInventoryItemsConnectionsCb));
2178adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
21798fb49dd6SShawn McCarney     };
21808fb49dd6SShawn McCarney 
2181adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2182d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
2183adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2184adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2185adc4f0dbSShawn McCarney }
2186adc4f0dbSShawn McCarney 
2187adc4f0dbSShawn McCarney /**
2188adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2189adc4f0dbSShawn McCarney  *
2190adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2191adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2192adc4f0dbSShawn McCarney  * array.
2193adc4f0dbSShawn McCarney  *
2194adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2195adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2196adc4f0dbSShawn McCarney  * object.
2197adc4f0dbSShawn McCarney  *
2198adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2199adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2200adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2201adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2202adc4f0dbSShawn McCarney  */
220323a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2204adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2205adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2206adc4f0dbSShawn McCarney {
2207adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2208adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2209adc4f0dbSShawn McCarney     {
2210adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2211adc4f0dbSShawn McCarney         {
2212adc4f0dbSShawn McCarney             return powerSupply;
2213adc4f0dbSShawn McCarney         }
2214adc4f0dbSShawn McCarney     }
2215adc4f0dbSShawn McCarney 
2216adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2217adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2218adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2219adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2220adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2221adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2222adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2223adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2224adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2225adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2226adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2227d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2228adc4f0dbSShawn McCarney 
222942cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
223042cbe538SGunnar Mills     {
223142cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
223242cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
223342cbe538SGunnar Mills     }
223442cbe538SGunnar Mills 
223542cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2236adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2237adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2238adc4f0dbSShawn McCarney 
2239adc4f0dbSShawn McCarney     return powerSupply;
22408fb49dd6SShawn McCarney }
22418fb49dd6SShawn McCarney 
22428fb49dd6SShawn McCarney /**
2243de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2244de629b6eSShawn McCarney  *
2245de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2246de629b6eSShawn McCarney  *
2247de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2248de629b6eSShawn McCarney  * information has been obtained.
2249de629b6eSShawn McCarney  *
2250adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2251de629b6eSShawn McCarney  *
2252de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2253de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2254de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2255de629b6eSShawn McCarney  *
2256de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2257de629b6eSShawn McCarney  *
2258adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2259adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2260adc4f0dbSShawn McCarney  *
2261de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2262adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2263de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2264de629b6eSShawn McCarney  * implements ObjectManager.
2265adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2266de629b6eSShawn McCarney  */
226723a21a1cSEd Tanous inline void getSensorData(
226881ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2269fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2270fe04d49cSNan Zhou     const std::set<std::string>& connections,
2271b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2272de629b6eSShawn McCarney {
2273de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2274de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2275de629b6eSShawn McCarney     for (const std::string& connection : connections)
2276de629b6eSShawn McCarney     {
2277de629b6eSShawn McCarney         // Response handler to process managed objects
2278002d39b4SEd Tanous         auto getManagedObjectsCb =
2279002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
2280002d39b4SEd Tanous              inventoryItems](const boost::system::error_code ec,
228102cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
2282de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2283de629b6eSShawn McCarney             if (ec)
2284de629b6eSShawn McCarney             {
2285de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
22868d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2287de629b6eSShawn McCarney                 return;
2288de629b6eSShawn McCarney             }
2289de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2290de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2291de629b6eSShawn McCarney             {
2292de629b6eSShawn McCarney                 const std::string& objPath =
2293de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2294de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2295de629b6eSShawn McCarney                                  << objPath;
2296de629b6eSShawn McCarney 
2297de629b6eSShawn McCarney                 std::vector<std::string> split;
2298de629b6eSShawn McCarney                 // Reserve space for
2299de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2300de629b6eSShawn McCarney                 split.reserve(6);
2301de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2302de629b6eSShawn McCarney                 if (split.size() < 6)
2303de629b6eSShawn McCarney                 {
2304de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2305de629b6eSShawn McCarney                                      << objPath;
2306de629b6eSShawn McCarney                     continue;
2307de629b6eSShawn McCarney                 }
2308de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2309de629b6eSShawn McCarney                 // string at the beginning
2310de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2311de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2312de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2313de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
231449c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2315de629b6eSShawn McCarney                 {
2316accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2317de629b6eSShawn McCarney                     continue;
2318de629b6eSShawn McCarney                 }
2319de629b6eSShawn McCarney 
2320adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2321adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2322adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2323adc4f0dbSShawn McCarney 
232495a3ecadSAnthony Wilson                 const std::string& sensorSchema =
232581ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
232695a3ecadSAnthony Wilson 
232795a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
232895a3ecadSAnthony Wilson 
2329928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2330928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
233195a3ecadSAnthony Wilson                 {
2332c1d019a6SEd Tanous                     std::string sensorTypeEscaped(sensorType);
2333c1d019a6SEd Tanous                     sensorTypeEscaped.erase(
2334c1d019a6SEd Tanous                         std::remove(sensorTypeEscaped.begin(),
2335c1d019a6SEd Tanous                                     sensorTypeEscaped.end(), '_'),
2336c1d019a6SEd Tanous                         sensorTypeEscaped.end());
2337c1d019a6SEd Tanous                     std::string sensorId(sensorTypeEscaped);
2338c1d019a6SEd Tanous                     sensorId += "_";
2339c1d019a6SEd Tanous                     sensorId += sensorName;
2340c1d019a6SEd Tanous 
23418d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
2342c1d019a6SEd Tanous                         crow::utility::urlFromPieces(
2343c1d019a6SEd Tanous                             "redfish", "v1", "Chassis",
2344c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
2345c1d019a6SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
23468d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
234795a3ecadSAnthony Wilson                 }
234895a3ecadSAnthony Wilson                 else
234995a3ecadSAnthony Wilson                 {
2350271584abSEd Tanous                     std::string fieldName;
2351928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2352928fefb9SNan Zhou                     {
2353928fefb9SNan Zhou                         fieldName = "Members";
2354928fefb9SNan Zhou                     }
2355928fefb9SNan Zhou                     else if (sensorType == "temperature")
2356de629b6eSShawn McCarney                     {
2357de629b6eSShawn McCarney                         fieldName = "Temperatures";
2358de629b6eSShawn McCarney                     }
2359de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2360de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2361de629b6eSShawn McCarney                     {
2362de629b6eSShawn McCarney                         fieldName = "Fans";
2363de629b6eSShawn McCarney                     }
2364de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2365de629b6eSShawn McCarney                     {
2366de629b6eSShawn McCarney                         fieldName = "Voltages";
2367de629b6eSShawn McCarney                     }
2368de629b6eSShawn McCarney                     else if (sensorType == "power")
2369de629b6eSShawn McCarney                     {
237055f79e6fSEd Tanous                         if (sensorName == "total_power")
2371028f7ebcSEddie James                         {
2372028f7ebcSEddie James                             fieldName = "PowerControl";
2373028f7ebcSEddie James                         }
2374adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2375adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2376028f7ebcSEddie James                         {
2377de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2378de629b6eSShawn McCarney                         }
2379adc4f0dbSShawn McCarney                         else
2380adc4f0dbSShawn McCarney                         {
2381adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2382adc4f0dbSShawn McCarney                             continue;
2383adc4f0dbSShawn McCarney                         }
2384028f7ebcSEddie James                     }
2385de629b6eSShawn McCarney                     else
2386de629b6eSShawn McCarney                     {
2387de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2388de629b6eSShawn McCarney                                          << sensorType;
2389de629b6eSShawn McCarney                         continue;
2390de629b6eSShawn McCarney                     }
2391de629b6eSShawn McCarney 
2392de629b6eSShawn McCarney                     nlohmann::json& tempArray =
23938d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2394adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
239549c53ac9SJohnathan Mantey                     {
2396adc4f0dbSShawn McCarney                         if (tempArray.empty())
23977ab06f49SGunnar Mills                         {
239895a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
239995a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
240095a3ecadSAnthony Wilson                             // naming in power.hpp.
24011476687dSEd Tanous                             nlohmann::json::object_t power;
24021476687dSEd Tanous                             power["@odata.id"] =
2403adc4f0dbSShawn McCarney                                 "/redfish/v1/Chassis/" +
240481ce609eSEd Tanous                                 sensorsAsyncResp->chassisId + "/" +
240581ce609eSEd Tanous                                 sensorsAsyncResp->chassisSubNode + "#/" +
24061476687dSEd Tanous                                 fieldName + "/0";
24071476687dSEd Tanous                             tempArray.push_back(std::move(power));
2408adc4f0dbSShawn McCarney                         }
2409adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2410adc4f0dbSShawn McCarney                     }
2411adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2412adc4f0dbSShawn McCarney                     {
2413adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2414adc4f0dbSShawn McCarney                         {
2415adc4f0dbSShawn McCarney                             sensorJson =
2416adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
241781ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2418adc4f0dbSShawn McCarney                         }
241949c53ac9SJohnathan Mantey                     }
2420928fefb9SNan Zhou                     else if (fieldName == "Members")
2421928fefb9SNan Zhou                     {
2422677bb756SEd Tanous                         std::string sensorTypeEscaped(sensorType);
2423677bb756SEd Tanous                         sensorTypeEscaped.erase(
2424677bb756SEd Tanous                             std::remove(sensorTypeEscaped.begin(),
2425677bb756SEd Tanous                                         sensorTypeEscaped.end(), '_'),
2426677bb756SEd Tanous                             sensorTypeEscaped.end());
2427677bb756SEd Tanous                         std::string sensorId(sensorTypeEscaped);
2428677bb756SEd Tanous                         sensorId += "_";
2429677bb756SEd Tanous                         sensorId += sensorName;
2430677bb756SEd Tanous 
24311476687dSEd Tanous                         nlohmann::json::object_t member;
2432677bb756SEd Tanous                         member["@odata.id"] = crow::utility::urlFromPieces(
2433677bb756SEd Tanous                             "redfish", "v1", "Chassis",
2434677bb756SEd Tanous                             sensorsAsyncResp->chassisId,
2435677bb756SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
24361476687dSEd Tanous                         tempArray.push_back(std::move(member));
2437928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2438928fefb9SNan Zhou                     }
243949c53ac9SJohnathan Mantey                     else
244049c53ac9SJohnathan Mantey                     {
24411476687dSEd Tanous                         nlohmann::json::object_t member;
24421476687dSEd Tanous                         member["@odata.id"] = "/redfish/v1/Chassis/" +
24431476687dSEd Tanous                                               sensorsAsyncResp->chassisId +
24441476687dSEd Tanous                                               "/" +
24451476687dSEd Tanous                                               sensorsAsyncResp->chassisSubNode +
24461476687dSEd Tanous                                               "#/" + fieldName + "/";
24471476687dSEd Tanous                         tempArray.push_back(std::move(member));
2448adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
244949c53ac9SJohnathan Mantey                     }
245095a3ecadSAnthony Wilson                 }
2451de629b6eSShawn McCarney 
2452adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2453adc4f0dbSShawn McCarney                 {
24541d7c0054SEd Tanous                     objectInterfacesToJson(sensorName, sensorType,
24551d7c0054SEd Tanous                                            sensorsAsyncResp->chassisSubNode,
24561d7c0054SEd Tanous                                            objDictEntry.second, *sensorJson,
24571d7c0054SEd Tanous                                            inventoryItem);
24581d7c0054SEd Tanous 
24591d7c0054SEd Tanous                     std::string path = "/xyz/openbmc_project/sensors/";
24601d7c0054SEd Tanous                     path += sensorType;
24611d7c0054SEd Tanous                     path += "/";
24621d7c0054SEd Tanous                     path += sensorName;
2463c1d019a6SEd Tanous                     sensorsAsyncResp->addMetadata(*sensorJson, path);
2464adc4f0dbSShawn McCarney                 }
2465de629b6eSShawn McCarney             }
246681ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
246749c53ac9SJohnathan Mantey             {
246881ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2469928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2470928fefb9SNan Zhou                         sensors::node::sensors &&
2471928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2472928fefb9SNan Zhou                 {
2473928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2474928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2475928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2476928fefb9SNan Zhou                             .size();
2477928fefb9SNan Zhou                 }
2478928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2479928fefb9SNan Zhou                          sensors::node::thermal)
24808bd25ccdSJames Feist                 {
248181ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
24828bd25ccdSJames Feist                 }
248349c53ac9SJohnathan Mantey             }
2484de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2485de629b6eSShawn McCarney         };
2486de629b6eSShawn McCarney 
2487de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2488d0090733SEd Tanous             getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
2489de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
249023a21a1cSEd Tanous     }
2491de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2492de629b6eSShawn McCarney }
2493de629b6eSShawn McCarney 
2494fe04d49cSNan Zhou inline void
2495fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2496fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
24971abe55efSEd Tanous {
2498fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2499fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
250055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2501adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2502d0090733SEd Tanous             [sensorsAsyncResp, sensorNames,
2503d0090733SEd Tanous              connections](const std::shared_ptr<std::vector<InventoryItem>>&
2504adc4f0dbSShawn McCarney                               inventoryItems) {
2505adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
250649c53ac9SJohnathan Mantey             // Get sensor data and store results in JSON
2507002d39b4SEd Tanous             getSensorData(sensorsAsyncResp, sensorNames, connections,
2508d0090733SEd Tanous                           inventoryItems);
2509adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2510adc4f0dbSShawn McCarney         };
2511adc4f0dbSShawn McCarney 
2512adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2513d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2514adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2515adc4f0dbSShawn McCarney 
251655c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb exit";
251708777fb0SLewanczyk, Dawid     };
2518de629b6eSShawn McCarney 
2519de629b6eSShawn McCarney     // Get set of connections that provide sensor values
252081ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
252195a3ecadSAnthony Wilson }
252295a3ecadSAnthony Wilson 
252395a3ecadSAnthony Wilson /**
252495a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
252595a3ecadSAnthony Wilson  *        chassis.
252695a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
252795a3ecadSAnthony Wilson  */
2528b5a76932SEd Tanous inline void
252981ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
253095a3ecadSAnthony Wilson {
253195a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
253295a3ecadSAnthony Wilson     auto getChassisCb =
253381ce609eSEd Tanous         [sensorsAsyncResp](
2534fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
253595a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "getChassisCb enter";
253681ce609eSEd Tanous         processSensorList(sensorsAsyncResp, sensorNames);
253755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassisCb exit";
253808777fb0SLewanczyk, Dawid     };
2539928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2540928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2541928fefb9SNan Zhou     {
25428d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
25438d1b46d7Szhanghch05             nlohmann::json::array();
2544928fefb9SNan Zhou     }
254526f03899SShawn McCarney     // Get set of sensors in chassis
25467f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
25477f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
25487f1cc26dSEd Tanous                std::move(getChassisCb));
254955c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2550271584abSEd Tanous }
255108777fb0SLewanczyk, Dawid 
2552413961deSRichard Marian Thomaiyar /**
255349c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
255449c53ac9SJohnathan Mantey  * the chassis node
255549c53ac9SJohnathan Mantey  *
255649c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
255749c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
255849c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
255949c53ac9SJohnathan Mantey  *                         repeated calls to this function
256049c53ac9SJohnathan Mantey  */
2561fe04d49cSNan Zhou inline bool
2562fe04d49cSNan Zhou     findSensorNameUsingSensorPath(std::string_view sensorName,
256302cad96eSEd Tanous                                   const std::set<std::string>& sensorsList,
2564fe04d49cSNan Zhou                                   std::set<std::string>& sensorsModified)
256549c53ac9SJohnathan Mantey {
2566fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
256749c53ac9SJohnathan Mantey     {
256828aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2569b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
257028aa8de5SGeorge Liu         if (thisSensorName.empty())
257149c53ac9SJohnathan Mantey         {
257249c53ac9SJohnathan Mantey             continue;
257349c53ac9SJohnathan Mantey         }
257449c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
257549c53ac9SJohnathan Mantey         {
257649c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
257749c53ac9SJohnathan Mantey             return true;
257849c53ac9SJohnathan Mantey         }
257949c53ac9SJohnathan Mantey     }
258049c53ac9SJohnathan Mantey     return false;
258149c53ac9SJohnathan Mantey }
258249c53ac9SJohnathan Mantey 
2583c71d6125SEd Tanous inline std::pair<std::string, std::string>
2584c71d6125SEd Tanous     splitSensorNameAndType(std::string_view sensorId)
2585c71d6125SEd Tanous {
2586c71d6125SEd Tanous     size_t index = sensorId.find('_');
2587c71d6125SEd Tanous     if (index == std::string::npos)
2588c71d6125SEd Tanous     {
2589c71d6125SEd Tanous         return std::make_pair<std::string, std::string>("", "");
2590c71d6125SEd Tanous     }
2591c71d6125SEd Tanous     std::string sensorType{sensorId.substr(0, index)};
2592c71d6125SEd Tanous     std::string sensorName{sensorId.substr(index + 1)};
2593c71d6125SEd Tanous     // fan_pwm and fan_tach need special handling
2594c71d6125SEd Tanous     if (sensorType == "fantach" || sensorType == "fanpwm")
2595c71d6125SEd Tanous     {
2596c71d6125SEd Tanous         sensorType.insert(3, 1, '_');
2597c71d6125SEd Tanous     }
2598c71d6125SEd Tanous     return std::make_pair(sensorType, sensorName);
2599c71d6125SEd Tanous }
2600c71d6125SEd Tanous 
260149c53ac9SJohnathan Mantey /**
2602413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2603413961deSRichard Marian Thomaiyar  *
26048d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
26054bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2606413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2607413961deSRichard Marian Thomaiyar  */
260823a21a1cSEd Tanous inline void setSensorsOverride(
2609b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
26104bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2611397fd61fSjayaprakash Mutyala         allCollections)
2612413961deSRichard Marian Thomaiyar {
261370d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
26144bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2615413961deSRichard Marian Thomaiyar 
2616543f4400SEd Tanous     const char* propertyValueName = nullptr;
2617f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2618413961deSRichard Marian Thomaiyar     std::string memberId;
2619543f4400SEd Tanous     double value = 0.0;
2620f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2621f65af9e8SRichard Marian Thomaiyar     {
2622f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2623f65af9e8SRichard Marian Thomaiyar         {
2624f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2625f65af9e8SRichard Marian Thomaiyar         }
2626f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2627f65af9e8SRichard Marian Thomaiyar         {
2628f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2629f65af9e8SRichard Marian Thomaiyar         }
2630f65af9e8SRichard Marian Thomaiyar         else
2631f65af9e8SRichard Marian Thomaiyar         {
2632f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2633f65af9e8SRichard Marian Thomaiyar         }
2634f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2635f65af9e8SRichard Marian Thomaiyar         {
26368d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
26378d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
26388d1b46d7Szhanghch05                                      value))
2639413961deSRichard Marian Thomaiyar             {
2640413961deSRichard Marian Thomaiyar                 return;
2641413961deSRichard Marian Thomaiyar             }
2642f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2643f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2644f65af9e8SRichard Marian Thomaiyar         }
2645f65af9e8SRichard Marian Thomaiyar     }
26464bb3dc34SCarol Wang 
2647002d39b4SEd Tanous     auto getChassisSensorListCb =
2648002d39b4SEd Tanous         [sensorAsyncResp, overrideMap](
2649fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorsList) {
265049c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
265149c53ac9SJohnathan Mantey         // chassis node
2652fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2653fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2654f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2655413961deSRichard Marian Thomaiyar         {
2656f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2657c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
2658c71d6125SEd Tanous                 splitSensorNameAndType(sensor);
2659c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2660c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2661f65af9e8SRichard Marian Thomaiyar             {
2662f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
26638d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2664f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2665413961deSRichard Marian Thomaiyar                 return;
2666413961deSRichard Marian Thomaiyar             }
2667f65af9e8SRichard Marian Thomaiyar         }
2668413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2669002d39b4SEd Tanous         auto getObjectsWithConnectionCb =
2670fe04d49cSNan Zhou             [sensorAsyncResp,
2671fe04d49cSNan Zhou              overrideMap](const std::set<std::string>& /*connections*/,
2672002d39b4SEd Tanous                           const std::set<std::pair<std::string, std::string>>&
2673413961deSRichard Marian Thomaiyar                               objectsWithConnection) {
2674f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2675413961deSRichard Marian Thomaiyar             {
2676413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2677f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2678f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2679f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
26804f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2681a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2682a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2683413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2684413961deSRichard Marian Thomaiyar                                                : "Voltages",
2685f65af9e8SRichard Marian Thomaiyar                                            "Count");
2686f65af9e8SRichard Marian Thomaiyar                 return;
2687f65af9e8SRichard Marian Thomaiyar             }
2688f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2689f65af9e8SRichard Marian Thomaiyar             {
269028aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
269128aa8de5SGeorge Liu                 std::string sensorName = path.filename();
269228aa8de5SGeorge Liu                 if (sensorName.empty())
2693f65af9e8SRichard Marian Thomaiyar                 {
26944f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2695f65af9e8SRichard Marian Thomaiyar                     return;
2696f65af9e8SRichard Marian Thomaiyar                 }
2697f65af9e8SRichard Marian Thomaiyar 
2698f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2699f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2700f65af9e8SRichard Marian Thomaiyar                 {
2701f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2702f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
27034f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2704413961deSRichard Marian Thomaiyar                     return;
2705413961deSRichard Marian Thomaiyar                 }
2706413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2707f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2708413961deSRichard Marian Thomaiyar                     if (ec)
2709413961deSRichard Marian Thomaiyar                     {
27104f277b54SJayaprakash Mutyala                         if (ec.value() ==
27114f277b54SJayaprakash Mutyala                             boost::system::errc::permission_denied)
27124f277b54SJayaprakash Mutyala                         {
27134f277b54SJayaprakash Mutyala                             BMCWEB_LOG_WARNING
27144f277b54SJayaprakash Mutyala                                 << "Manufacturing mode is not Enabled...can't "
27154f277b54SJayaprakash Mutyala                                    "Override the sensor value. ";
27164f277b54SJayaprakash Mutyala 
27174f277b54SJayaprakash Mutyala                             messages::insufficientPrivilege(
27188d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
2719413961deSRichard Marian Thomaiyar                             return;
2720413961deSRichard Marian Thomaiyar                         }
27214f277b54SJayaprakash Mutyala                         BMCWEB_LOG_DEBUG
27224f277b54SJayaprakash Mutyala                             << "setOverrideValueStatus DBUS error: " << ec;
27234f277b54SJayaprakash Mutyala                         messages::internalError(
27244f277b54SJayaprakash Mutyala                             sensorAsyncResp->asyncResp->res);
27254f277b54SJayaprakash Mutyala                     }
2726413961deSRichard Marian Thomaiyar                     },
27274f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
27284f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2729168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2730f65af9e8SRichard Marian Thomaiyar             }
2731413961deSRichard Marian Thomaiyar         };
2732413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2733413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2734413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2735413961deSRichard Marian Thomaiyar     };
2736413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
27377f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
27387f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
27397f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2740413961deSRichard Marian Thomaiyar }
2741413961deSRichard Marian Thomaiyar 
2742a0ec28b6SAdrian Ambrożewicz /**
2743a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2744a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2745a0ec28b6SAdrian Ambrożewicz  *
2746a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2747a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2748a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2749a0ec28b6SAdrian Ambrożewicz  *
2750a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2751a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2752a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2753a0ec28b6SAdrian Ambrożewicz  */
2754021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2755021d32cfSKrzysztof Grobelny                                  const std::string& node,
2756a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2757a0ec28b6SAdrian Ambrożewicz {
275802da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
275902da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
276002da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
276102da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2762a0ec28b6SAdrian Ambrożewicz     {
2763a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2764a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2765a0ec28b6SAdrian Ambrożewicz         return;
2766a0ec28b6SAdrian Ambrożewicz     }
2767d51e072fSKrzysztof Grobelny 
276872374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2769fe04d49cSNan Zhou     auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2770a0ec28b6SAdrian Ambrożewicz                         const boost::beast::http::status status,
2771fe04d49cSNan Zhou                         const std::map<std::string, std::string>& uriToDbus) {
2772fe04d49cSNan Zhou         mapCompleteCb(status, uriToDbus);
2773fe04d49cSNan Zhou     };
2774a0ec28b6SAdrian Ambrożewicz 
2775a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2776d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2777a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2778a0ec28b6SAdrian Ambrożewicz }
2779a0ec28b6SAdrian Ambrożewicz 
2780bacb2162SNan Zhou namespace sensors
2781bacb2162SNan Zhou {
2782928fefb9SNan Zhou 
2783bacb2162SNan Zhou inline void getChassisCallback(
2784c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2785c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2786fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2787bacb2162SNan Zhou {
2788bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
2789bacb2162SNan Zhou 
2790c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2791c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2792bacb2162SNan Zhou     {
2793bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2794bacb2162SNan Zhou 
2795bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2796bacb2162SNan Zhou         std::string sensorName = path.filename();
2797bacb2162SNan Zhou         if (sensorName.empty())
2798bacb2162SNan Zhou         {
2799bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2800c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2801bacb2162SNan Zhou             return;
2802bacb2162SNan Zhou         }
2803c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
2804c1d019a6SEd Tanous         // fan_tach has an underscore in it, so remove it to "normalize" the
2805c1d019a6SEd Tanous         // type in the URI
2806c1d019a6SEd Tanous         type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2807c1d019a6SEd Tanous 
28081476687dSEd Tanous         nlohmann::json::object_t member;
2809c1d019a6SEd Tanous         std::string id = type;
2810c1d019a6SEd Tanous         id += "_";
2811c1d019a6SEd Tanous         id += sensorName;
2812c1d019a6SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
2813c1d019a6SEd Tanous             "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2814c1d019a6SEd Tanous 
28151476687dSEd Tanous         entriesArray.push_back(std::move(member));
2816bacb2162SNan Zhou     }
2817bacb2162SNan Zhou 
2818c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
2819bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2820bacb2162SNan Zhou }
2821e6bd846dSNan Zhou 
2822de167a6fSNan Zhou inline void
2823de167a6fSNan Zhou     handleSensorCollectionGet(App& app, const crow::Request& req,
2824de167a6fSNan Zhou                               const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2825de167a6fSNan Zhou                               const std::string& chassisId)
2826de167a6fSNan Zhou {
2827de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2828de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2829de167a6fSNan Zhou     };
2830de167a6fSNan Zhou     query_param::Query delegatedQuery;
28313ba00073SCarson Labrado     if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
2832de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2833de167a6fSNan Zhou     {
2834de167a6fSNan Zhou         return;
2835de167a6fSNan Zhou     }
2836de167a6fSNan Zhou 
2837de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2838de167a6fSNan Zhou     {
2839de167a6fSNan Zhou         // we perform efficient expand.
2840de167a6fSNan Zhou         auto asyncResp = std::make_shared<SensorsAsyncResp>(
2841de167a6fSNan Zhou             aResp, chassisId, sensors::dbus::sensorPaths,
2842de167a6fSNan Zhou             sensors::node::sensors,
2843de167a6fSNan Zhou             /*efficientExpand=*/true);
2844de167a6fSNan Zhou         getChassisData(asyncResp);
2845de167a6fSNan Zhou 
2846de167a6fSNan Zhou         BMCWEB_LOG_DEBUG
2847de167a6fSNan Zhou             << "SensorCollection doGet exit via efficient expand handler";
2848de167a6fSNan Zhou         return;
28490bad320cSEd Tanous     }
2850de167a6fSNan Zhou 
2851de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2852de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
2853c1d019a6SEd Tanous     getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2854c1d019a6SEd Tanous                std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2855c1d019a6SEd Tanous                                sensors::node::sensors));
2856c1d019a6SEd Tanous }
28577f1cc26dSEd Tanous 
2858c1d019a6SEd Tanous inline void
2859c1d019a6SEd Tanous     getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2860c1d019a6SEd Tanous                       const std::string& sensorPath,
2861c1d019a6SEd Tanous                       const ::dbus::utility::MapperGetObject& mapperResponse)
2862c1d019a6SEd Tanous {
2863c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2864c1d019a6SEd Tanous     {
2865c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2866c1d019a6SEd Tanous         return;
2867c1d019a6SEd Tanous     }
2868c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2869c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
2870c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2871c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Path " << sensorPath;
2872c1343bf6SKrzysztof Grobelny 
2873c1343bf6SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2874c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2875c1d019a6SEd Tanous         [asyncResp,
2876c1d019a6SEd Tanous          sensorPath](const boost::system::error_code ec,
2877c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2878c1d019a6SEd Tanous         if (ec)
2879c1d019a6SEd Tanous         {
2880c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2881c1d019a6SEd Tanous             return;
2882c1d019a6SEd Tanous         }
2883c1d019a6SEd Tanous         sdbusplus::message::object_path path(sensorPath);
2884c1d019a6SEd Tanous         std::string name = path.filename();
2885c1d019a6SEd Tanous         path = path.parent_path();
2886c1d019a6SEd Tanous         std::string type = path.filename();
2887c1d019a6SEd Tanous         objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2888c1d019a6SEd Tanous                                asyncResp->res.jsonValue, nullptr);
2889c1343bf6SKrzysztof Grobelny         });
2890de167a6fSNan Zhou }
2891de167a6fSNan Zhou 
2892e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2893c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2894677bb756SEd Tanous                             const std::string& chassisId,
2895c1d019a6SEd Tanous                             const std::string& sensorId)
2896e6bd846dSNan Zhou {
2897c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2898e6bd846dSNan Zhou     {
2899e6bd846dSNan Zhou         return;
2900e6bd846dSNan Zhou     }
2901c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
2902c71d6125SEd Tanous         splitSensorNameAndType(sensorId);
2903c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2904c1d019a6SEd Tanous     {
2905c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2906c1d019a6SEd Tanous         return;
2907c1d019a6SEd Tanous     }
2908c71d6125SEd Tanous 
2909677bb756SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2910677bb756SEd Tanous         "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
2911c1d019a6SEd Tanous 
2912e6bd846dSNan Zhou     BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2913e6bd846dSNan Zhou 
2914e6bd846dSNan Zhou     const std::array<const char*, 1> interfaces = {
2915e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2916c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2917c71d6125SEd Tanous                              '/' + nameType.second;
2918e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2919e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
2920e6bd846dSNan Zhou     crow::connections::systemBus->async_method_call(
2921c71d6125SEd Tanous         [asyncResp,
2922c71d6125SEd Tanous          sensorPath](const boost::system::error_code ec,
2923c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
2924e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 enter";
2925e6bd846dSNan Zhou         if (ec)
2926e6bd846dSNan Zhou         {
2927c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2928e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2929e6bd846dSNan Zhou                              << "Dbus error " << ec;
2930e6bd846dSNan Zhou             return;
2931e6bd846dSNan Zhou         }
2932c1d019a6SEd Tanous         getSensorFromDbus(asyncResp, sensorPath, subtree);
2933e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 exit";
2934e6bd846dSNan Zhou         },
2935e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper",
2936e6bd846dSNan Zhou         "/xyz/openbmc_project/object_mapper",
2937c1d019a6SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
2938c1d019a6SEd Tanous         interfaces);
2939e6bd846dSNan Zhou }
2940e6bd846dSNan Zhou 
2941bacb2162SNan Zhou } // namespace sensors
2942bacb2162SNan Zhou 
29437e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
294495a3ecadSAnthony Wilson {
29457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2946ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2947002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2948de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
294995a3ecadSAnthony Wilson }
295095a3ecadSAnthony Wilson 
29517e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
295295a3ecadSAnthony Wilson {
29537e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2954ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2955002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2956e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
295795a3ecadSAnthony Wilson }
295895a3ecadSAnthony Wilson 
295908777fb0SLewanczyk, Dawid } // namespace redfish
2960