xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision d00907336f7497df6073a668fa8b398e6a12bf5b)
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 
180ec8b83dSEd Tanous #include "generated/enums/sensor.hpp"
190ec8b83dSEd Tanous 
207e860f15SJohn Edward Broadbent #include <app.hpp>
2111ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
221d7c0054SEd Tanous #include <boost/algorithm/string/find.hpp>
231d7c0054SEd Tanous #include <boost/algorithm/string/predicate.hpp>
2408777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2508777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
261abe55efSEd Tanous #include <dbus_singleton.hpp>
27168e20c1SEd Tanous #include <dbus_utility.hpp>
2845ca1b86SEd Tanous #include <query.hpp>
29ed398213SEd Tanous #include <registries/privilege_registry.hpp>
301e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
3186d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
3286d89ed7SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
33413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
34928fefb9SNan Zhou #include <utils/query_param.hpp>
351214b7e7SGunnar Mills 
361214b7e7SGunnar Mills #include <cmath>
37fe04d49cSNan Zhou #include <iterator>
38fe04d49cSNan Zhou #include <map>
39fe04d49cSNan Zhou #include <set>
40b5a76932SEd Tanous #include <utility>
41abf2add6SEd Tanous #include <variant>
4208777fb0SLewanczyk, Dawid 
431abe55efSEd Tanous namespace redfish
441abe55efSEd Tanous {
4508777fb0SLewanczyk, Dawid 
46a0ec28b6SAdrian Ambrożewicz namespace sensors
47a0ec28b6SAdrian Ambrożewicz {
48a0ec28b6SAdrian Ambrożewicz namespace node
49a0ec28b6SAdrian Ambrożewicz {
50a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
51a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
52a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
53a0ec28b6SAdrian Ambrożewicz } // namespace node
54a0ec28b6SAdrian Ambrożewicz 
5502da7c5aSEd Tanous // clang-format off
56a0ec28b6SAdrian Ambrożewicz namespace dbus
57a0ec28b6SAdrian Ambrożewicz {
584ee8e211SEd Tanous static auto powerPaths = std::to_array<std::string_view>({
5902da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
6002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
6102da7c5aSEd Tanous });
62c2bf7f99SWludzik, Jozef 
634ee8e211SEd Tanous static auto sensorPaths = std::to_array<std::string_view>({
6402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power",
65a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/current",
667088690cSBasheer Ahmed Muddebihal     "/xyz/openbmc_project/sensors/airflow",
675deabed9SGunnar Mills     "/xyz/openbmc_project/sensors/humidity",
68e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
69e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/voltage",
70e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_tach",
71e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/temperature",
72e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_pwm",
73e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/altitude",
74e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/energy",
75e8204933SGeorge Liu #endif
7602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/utilization"
7702da7c5aSEd Tanous });
7802da7c5aSEd Tanous 
794ee8e211SEd Tanous static auto thermalPaths = std::to_array<std::string_view>({
8002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
81a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
8202da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
8302da7c5aSEd Tanous });
8402da7c5aSEd Tanous 
85c2bf7f99SWludzik, Jozef } // namespace dbus
8602da7c5aSEd Tanous // clang-format on
8702da7c5aSEd Tanous 
8802da7c5aSEd Tanous using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
8902da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
9002da7c5aSEd Tanous     {{node::power, std::span<std::string_view>(dbus::powerPaths)},
9102da7c5aSEd Tanous      {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
9202da7c5aSEd Tanous      {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
93c2bf7f99SWludzik, Jozef 
940ec8b83dSEd Tanous inline sensor::ReadingType toReadingType(std::string_view sensorType)
95c2bf7f99SWludzik, Jozef {
96c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
97c2bf7f99SWludzik, Jozef     {
980ec8b83dSEd Tanous         return sensor::ReadingType::Voltage;
99c2bf7f99SWludzik, Jozef     }
100c2bf7f99SWludzik, Jozef     if (sensorType == "power")
101c2bf7f99SWludzik, Jozef     {
1020ec8b83dSEd Tanous         return sensor::ReadingType::Power;
103c2bf7f99SWludzik, Jozef     }
104c2bf7f99SWludzik, Jozef     if (sensorType == "current")
105c2bf7f99SWludzik, Jozef     {
1060ec8b83dSEd Tanous         return sensor::ReadingType::Current;
107c2bf7f99SWludzik, Jozef     }
108c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
109c2bf7f99SWludzik, Jozef     {
1100ec8b83dSEd Tanous         return sensor::ReadingType::Rotational;
111c2bf7f99SWludzik, Jozef     }
112c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
113c2bf7f99SWludzik, Jozef     {
1140ec8b83dSEd Tanous         return sensor::ReadingType::Temperature;
115c2bf7f99SWludzik, Jozef     }
116c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
117c2bf7f99SWludzik, Jozef     {
1180ec8b83dSEd Tanous         return sensor::ReadingType::Percent;
119c2bf7f99SWludzik, Jozef     }
1205deabed9SGunnar Mills     if (sensorType == "humidity")
1215deabed9SGunnar Mills     {
1220ec8b83dSEd Tanous         return sensor::ReadingType::Humidity;
1235deabed9SGunnar Mills     }
124c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
125c2bf7f99SWludzik, Jozef     {
1260ec8b83dSEd Tanous         return sensor::ReadingType::Altitude;
127c2bf7f99SWludzik, Jozef     }
128c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
129c2bf7f99SWludzik, Jozef     {
1300ec8b83dSEd Tanous         return sensor::ReadingType::AirFlow;
131c2bf7f99SWludzik, Jozef     }
132c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
133c2bf7f99SWludzik, Jozef     {
1340ec8b83dSEd Tanous         return sensor::ReadingType::EnergyJoules;
135c2bf7f99SWludzik, Jozef     }
1360ec8b83dSEd Tanous     return sensor::ReadingType::Invalid;
137c2bf7f99SWludzik, Jozef }
138c2bf7f99SWludzik, Jozef 
1391d7c0054SEd Tanous inline std::string_view toReadingUnits(std::string_view sensorType)
140c2bf7f99SWludzik, Jozef {
141c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
142c2bf7f99SWludzik, Jozef     {
143c2bf7f99SWludzik, Jozef         return "V";
144c2bf7f99SWludzik, Jozef     }
145c2bf7f99SWludzik, Jozef     if (sensorType == "power")
146c2bf7f99SWludzik, Jozef     {
147c2bf7f99SWludzik, Jozef         return "W";
148c2bf7f99SWludzik, Jozef     }
149c2bf7f99SWludzik, Jozef     if (sensorType == "current")
150c2bf7f99SWludzik, Jozef     {
151c2bf7f99SWludzik, Jozef         return "A";
152c2bf7f99SWludzik, Jozef     }
153c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
154c2bf7f99SWludzik, Jozef     {
155c2bf7f99SWludzik, Jozef         return "RPM";
156c2bf7f99SWludzik, Jozef     }
157c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
158c2bf7f99SWludzik, Jozef     {
159c2bf7f99SWludzik, Jozef         return "Cel";
160c2bf7f99SWludzik, Jozef     }
1615deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
1625deabed9SGunnar Mills         sensorType == "humidity")
163c2bf7f99SWludzik, Jozef     {
164c2bf7f99SWludzik, Jozef         return "%";
165c2bf7f99SWludzik, Jozef     }
166c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
167c2bf7f99SWludzik, Jozef     {
168c2bf7f99SWludzik, Jozef         return "m";
169c2bf7f99SWludzik, Jozef     }
170c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
171c2bf7f99SWludzik, Jozef     {
172c2bf7f99SWludzik, Jozef         return "cft_i/min";
173c2bf7f99SWludzik, Jozef     }
174c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
175c2bf7f99SWludzik, Jozef     {
176c2bf7f99SWludzik, Jozef         return "J";
177c2bf7f99SWludzik, Jozef     }
178c2bf7f99SWludzik, Jozef     return "";
179a0ec28b6SAdrian Ambrożewicz }
180a0ec28b6SAdrian Ambrożewicz } // namespace sensors
181a0ec28b6SAdrian Ambrożewicz 
18208777fb0SLewanczyk, Dawid /**
183588c3f0dSKowalski, Kamil  * SensorsAsyncResp
18408777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
18508777fb0SLewanczyk, Dawid  */
1861abe55efSEd Tanous class SensorsAsyncResp
1871abe55efSEd Tanous {
18808777fb0SLewanczyk, Dawid   public:
189a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
190a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
191fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
192a0ec28b6SAdrian Ambrożewicz 
193a0ec28b6SAdrian Ambrożewicz     struct SensorData
194a0ec28b6SAdrian Ambrożewicz     {
195a0ec28b6SAdrian Ambrożewicz         const std::string name;
196a0ec28b6SAdrian Ambrożewicz         std::string uri;
197a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
198a0ec28b6SAdrian Ambrożewicz     };
199a0ec28b6SAdrian Ambrożewicz 
2008a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2018d1b46d7Szhanghch05                      const std::string& chassisIdIn,
20202da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
20302da7c5aSEd Tanous                      std::string_view subNode) :
2048a592810SEd Tanous         asyncResp(asyncRespIn),
205928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
206928fefb9SNan Zhou         efficientExpand(false)
2071214b7e7SGunnar Mills     {}
20808777fb0SLewanczyk, Dawid 
209a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
2108a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2118d1b46d7Szhanghch05                      const std::string& chassisIdIn,
21202da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
21302da7c5aSEd Tanous                      std::string_view subNode,
214a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
2158a592810SEd Tanous         asyncResp(asyncRespIn),
216928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
217928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
218a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
219a0ec28b6SAdrian Ambrożewicz     {}
220a0ec28b6SAdrian Ambrożewicz 
221928fefb9SNan Zhou     // sensor collections expand
2228a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
223928fefb9SNan Zhou                      const std::string& chassisIdIn,
22402da7c5aSEd Tanous                      const std::span<std::string_view> typesIn,
2258a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
2268a592810SEd Tanous         asyncResp(asyncRespIn),
227928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
2288a592810SEd Tanous         efficientExpand(efficientExpandIn)
229928fefb9SNan Zhou     {}
230928fefb9SNan Zhou 
2311abe55efSEd Tanous     ~SensorsAsyncResp()
2321abe55efSEd Tanous     {
2338d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2348d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2351abe55efSEd Tanous         {
2361abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2371abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2381abe55efSEd Tanous             // proper code
2398d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
24008777fb0SLewanczyk, Dawid         }
241a0ec28b6SAdrian Ambrożewicz 
242a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
243a0ec28b6SAdrian Ambrożewicz         {
244fe04d49cSNan Zhou             std::map<std::string, std::string> map;
2458d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
246a0ec28b6SAdrian Ambrożewicz             {
247a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
248a0ec28b6SAdrian Ambrożewicz                 {
249c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
250a0ec28b6SAdrian Ambrożewicz                 }
251a0ec28b6SAdrian Ambrożewicz             }
2528d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
253a0ec28b6SAdrian Ambrożewicz         }
25408777fb0SLewanczyk, Dawid     }
255588c3f0dSKowalski, Kamil 
256ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
257ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
258ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
259ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
260ecd6a3a2SEd Tanous 
261a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
262c1d019a6SEd Tanous                      const std::string& dbusPath)
263a0ec28b6SAdrian Ambrożewicz     {
264a0ec28b6SAdrian Ambrożewicz         if (metadata)
265a0ec28b6SAdrian Ambrożewicz         {
266c1d019a6SEd Tanous             metadata->emplace_back(SensorData{
267c1d019a6SEd Tanous                 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
268a0ec28b6SAdrian Ambrożewicz         }
269a0ec28b6SAdrian Ambrożewicz     }
270a0ec28b6SAdrian Ambrożewicz 
271a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
272a0ec28b6SAdrian Ambrożewicz     {
273a0ec28b6SAdrian Ambrożewicz         if (metadata)
274a0ec28b6SAdrian Ambrożewicz         {
275a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
276a0ec28b6SAdrian Ambrożewicz             {
277a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
278a0ec28b6SAdrian Ambrożewicz                 {
279a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
280a0ec28b6SAdrian Ambrożewicz                 }
281a0ec28b6SAdrian Ambrożewicz             }
282a0ec28b6SAdrian Ambrożewicz         }
283a0ec28b6SAdrian Ambrożewicz     }
284a0ec28b6SAdrian Ambrożewicz 
2858d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
286a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
28702da7c5aSEd Tanous     const std::span<std::string_view> types;
288a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
289928fefb9SNan Zhou     const bool efficientExpand;
290a0ec28b6SAdrian Ambrożewicz 
291a0ec28b6SAdrian Ambrożewicz   private:
292a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
293a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
29408777fb0SLewanczyk, Dawid };
29508777fb0SLewanczyk, Dawid 
29608777fb0SLewanczyk, Dawid /**
297d500549bSAnthony Wilson  * Possible states for physical inventory leds
298d500549bSAnthony Wilson  */
299d500549bSAnthony Wilson enum class LedState
300d500549bSAnthony Wilson {
301d500549bSAnthony Wilson     OFF,
302d500549bSAnthony Wilson     ON,
303d500549bSAnthony Wilson     BLINK,
304d500549bSAnthony Wilson     UNKNOWN
305d500549bSAnthony Wilson };
306d500549bSAnthony Wilson 
307d500549bSAnthony Wilson /**
308adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
309adc4f0dbSShawn McCarney  */
310adc4f0dbSShawn McCarney class InventoryItem
311adc4f0dbSShawn McCarney {
312adc4f0dbSShawn McCarney   public:
3134e23a444SEd Tanous     explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
314adc4f0dbSShawn McCarney     {
315adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
31628aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
31728aa8de5SGeorge Liu         name = path.filename();
31828aa8de5SGeorge Liu         if (name.empty())
319adc4f0dbSShawn McCarney         {
32028aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
321adc4f0dbSShawn McCarney         }
322adc4f0dbSShawn McCarney     }
323adc4f0dbSShawn McCarney 
324adc4f0dbSShawn McCarney     std::string objectPath;
325adc4f0dbSShawn McCarney     std::string name;
326e05aec50SEd Tanous     bool isPresent = true;
327e05aec50SEd Tanous     bool isFunctional = true;
328e05aec50SEd Tanous     bool isPowerSupply = false;
329e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
330adc4f0dbSShawn McCarney     std::string manufacturer;
331adc4f0dbSShawn McCarney     std::string model;
332adc4f0dbSShawn McCarney     std::string partNumber;
333adc4f0dbSShawn McCarney     std::string serialNumber;
334adc4f0dbSShawn McCarney     std::set<std::string> sensors;
335d500549bSAnthony Wilson     std::string ledObjectPath;
336e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
337adc4f0dbSShawn McCarney };
338adc4f0dbSShawn McCarney 
339adc4f0dbSShawn McCarney /**
340413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
341588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
34208777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
34308777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
34408777fb0SLewanczyk, Dawid  */
34508777fb0SLewanczyk, Dawid template <typename Callback>
346413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
34781ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
348fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
3491abe55efSEd Tanous     Callback&& callback)
3501abe55efSEd Tanous {
351413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
35203b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
35308777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
35408777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
35508777fb0SLewanczyk, Dawid 
35608777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
357002d39b4SEd Tanous     auto respHandler =
358002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
359002d39b4SEd Tanous          sensorNames](const boost::system::error_code ec,
360002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
361413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3621abe55efSEd Tanous         if (ec)
3631abe55efSEd Tanous         {
3648d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
365413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
366413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
36708777fb0SLewanczyk, Dawid             return;
36808777fb0SLewanczyk, Dawid         }
36908777fb0SLewanczyk, Dawid 
37055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
37108777fb0SLewanczyk, Dawid 
37208777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
37308777fb0SLewanczyk, Dawid         // found in the chassis
374fe04d49cSNan Zhou         std::set<std::string> connections;
375413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
37608777fb0SLewanczyk, Dawid 
37749c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
37849c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3791abe55efSEd Tanous         {
38055c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
38108777fb0SLewanczyk, Dawid         }
38208777fb0SLewanczyk, Dawid 
38308777fb0SLewanczyk, Dawid         for (const std::pair<
38408777fb0SLewanczyk, Dawid                  std::string,
38508777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3861abe55efSEd Tanous                  object : subtree)
3871abe55efSEd Tanous         {
38849c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3891abe55efSEd Tanous             {
39049c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3911abe55efSEd Tanous                          objData : object.second)
3921abe55efSEd Tanous                 {
39349c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
39408777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
395de629b6eSShawn McCarney                     objectsWithConnection.insert(
396de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
39708777fb0SLewanczyk, Dawid                 }
39808777fb0SLewanczyk, Dawid             }
39908777fb0SLewanczyk, Dawid         }
40055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
401413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
402413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
40308777fb0SLewanczyk, Dawid     };
40408777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
40555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
40655c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
4071abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
4081abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
409413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
410413961deSRichard Marian Thomaiyar }
411413961deSRichard Marian Thomaiyar 
412413961deSRichard Marian Thomaiyar /**
413413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
414413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
415413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
416413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
417413961deSRichard Marian Thomaiyar  */
418413961deSRichard Marian Thomaiyar template <typename Callback>
419fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
420fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
421413961deSRichard Marian Thomaiyar                     Callback&& callback)
422413961deSRichard Marian Thomaiyar {
423413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
424fe04d49cSNan Zhou         [callback](const std::set<std::string>& connections,
425413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4263174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
42781ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
428413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
42908777fb0SLewanczyk, Dawid }
43008777fb0SLewanczyk, Dawid 
43108777fb0SLewanczyk, Dawid /**
43249c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43349c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43449c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
43549c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
43649c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
43749c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
43849c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
43949c53ac9SJohnathan Mantey  */
44023a21a1cSEd Tanous inline void reduceSensorList(
4417f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
4427f1cc26dSEd Tanous     std::span<std::string_view> sensorTypes,
44349c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
444fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
44549c53ac9SJohnathan Mantey {
44649c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
44749c53ac9SJohnathan Mantey     {
4487f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
4497f1cc26dSEd Tanous                                    chassisSubNode == sensors::node::thermal
450a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
45149c53ac9SJohnathan Mantey                                        : "Voltages");
45249c53ac9SJohnathan Mantey 
45349c53ac9SJohnathan Mantey         return;
45449c53ac9SJohnathan Mantey     }
45549c53ac9SJohnathan Mantey     if (allSensors->empty())
45649c53ac9SJohnathan Mantey     {
45749c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
45849c53ac9SJohnathan Mantey         return;
45949c53ac9SJohnathan Mantey     }
46049c53ac9SJohnathan Mantey 
4617f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
46249c53ac9SJohnathan Mantey     {
46349c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46449c53ac9SJohnathan Mantey         {
46511ba3979SEd Tanous             if (sensor.starts_with(type))
46649c53ac9SJohnathan Mantey             {
46749c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
46849c53ac9SJohnathan Mantey             }
46949c53ac9SJohnathan Mantey         }
47049c53ac9SJohnathan Mantey     }
47149c53ac9SJohnathan Mantey }
47249c53ac9SJohnathan Mantey 
4737f1cc26dSEd Tanous /*
4747f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
4757f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
4767f1cc26dSEd Tanous  *
4777f1cc26dSEd Tanous  * */
4787f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
4797f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
4807f1cc26dSEd Tanous {
4817f1cc26dSEd Tanous     if (chassisSubNode == sensors::node::power)
4827f1cc26dSEd Tanous     {
4837f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
4847f1cc26dSEd Tanous     }
4857f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::thermal)
4867f1cc26dSEd Tanous     {
4877f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
4887f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
4897f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
4907f1cc26dSEd Tanous     }
4917f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::sensors)
4927f1cc26dSEd Tanous     {
4937f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
4947f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
4957f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
4967f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
4977f1cc26dSEd Tanous     }
4987f1cc26dSEd Tanous 
4997f1cc26dSEd Tanous     if (chassisSubNode != sensors::node::sensors)
5007f1cc26dSEd Tanous     {
5017f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
5027f1cc26dSEd Tanous     }
5037f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
5047f1cc26dSEd Tanous }
5057f1cc26dSEd Tanous 
50649c53ac9SJohnathan Mantey /**
50708777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
508588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
50908777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
51008777fb0SLewanczyk, Dawid  */
51108777fb0SLewanczyk, Dawid template <typename Callback>
5127f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5137f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
5147f1cc26dSEd Tanous                 std::span<std::string_view> sensorTypes, Callback&& callback)
5151abe55efSEd Tanous {
51655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
517adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
51849c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
519adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
520002d39b4SEd Tanous     auto respHandler =
5217f1cc26dSEd Tanous         [callback{std::forward<Callback>(callback)}, asyncResp,
5227f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
5237f1cc26dSEd Tanous          chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
52449c53ac9SJohnathan Mantey             const boost::system::error_code ec,
525002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
52655c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5271abe55efSEd Tanous         if (ec)
5281abe55efSEd Tanous         {
52955c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5307f1cc26dSEd Tanous             messages::internalError(asyncResp->res);
53108777fb0SLewanczyk, Dawid             return;
53208777fb0SLewanczyk, Dawid         }
53349c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
53449c53ac9SJohnathan Mantey         std::string chassisName;
53549c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5361abe55efSEd Tanous         {
53728aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
53828aa8de5SGeorge Liu             chassisName = path.filename();
53928aa8de5SGeorge Liu             if (chassisName.empty())
5401abe55efSEd Tanous             {
54149c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
542daf36e2eSEd Tanous                 continue;
543daf36e2eSEd Tanous             }
5447f1cc26dSEd Tanous             if (chassisName == chassisIdStr)
5451abe55efSEd Tanous             {
54649c53ac9SJohnathan Mantey                 chassisPath = &chassis;
54749c53ac9SJohnathan Mantey                 break;
548daf36e2eSEd Tanous             }
54949c53ac9SJohnathan Mantey         }
55049c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5511abe55efSEd Tanous         {
5527f1cc26dSEd Tanous             messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
55349c53ac9SJohnathan Mantey             return;
5541abe55efSEd Tanous         }
5557f1cc26dSEd Tanous         populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
55608777fb0SLewanczyk, Dawid 
5577f1cc26dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
5587f1cc26dSEd Tanous             "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
55995a3ecadSAnthony Wilson 
5608fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5618fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
5621e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
5631e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
5641e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
5657f1cc26dSEd Tanous             [asyncResp, chassisSubNode, sensorTypes,
566f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
567271584abSEd Tanous                 const boost::system::error_code& e,
5681e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
569271584abSEd Tanous             if (e)
57049c53ac9SJohnathan Mantey             {
571271584abSEd Tanous                 if (e.value() != EBADR)
57249c53ac9SJohnathan Mantey                 {
5737f1cc26dSEd Tanous                     messages::internalError(asyncResp->res);
57449c53ac9SJohnathan Mantey                     return;
57549c53ac9SJohnathan Mantey                 }
57649c53ac9SJohnathan Mantey             }
577fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>> culledSensorList =
578fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
5797f1cc26dSEd Tanous             reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
5807f1cc26dSEd Tanous                              &nodeSensorList, culledSensorList);
5817f1cc26dSEd Tanous             BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
58249c53ac9SJohnathan Mantey             callback(culledSensorList);
5831e1e598dSJonathan Doman             });
58449c53ac9SJohnathan Mantey     };
58549c53ac9SJohnathan Mantey 
58649c53ac9SJohnathan Mantey     // Get the Chassis Collection
58749c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
58849c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
58949c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
59049c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
591271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
59255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
59308777fb0SLewanczyk, Dawid }
59408777fb0SLewanczyk, Dawid 
59508777fb0SLewanczyk, Dawid /**
596adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
597adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
598adc4f0dbSShawn McCarney  * @return State value for inventory item.
59934dd179eSJames Feist  */
60023a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
601adc4f0dbSShawn McCarney {
602adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
603adc4f0dbSShawn McCarney     {
604adc4f0dbSShawn McCarney         return "Absent";
605adc4f0dbSShawn McCarney     }
60634dd179eSJames Feist 
607adc4f0dbSShawn McCarney     return "Enabled";
608adc4f0dbSShawn McCarney }
609adc4f0dbSShawn McCarney 
610adc4f0dbSShawn McCarney /**
611adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
612adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
6131d7c0054SEd Tanous  * @param valuesDict Map of all sensor DBus values.
614adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
615adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
616adc4f0dbSShawn McCarney  * @return Health value for sensor.
617adc4f0dbSShawn McCarney  */
6181d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson,
6191d7c0054SEd Tanous                              const dbus::utility::DBusPropertiesMap& valuesDict,
620adc4f0dbSShawn McCarney                              const InventoryItem* inventoryItem)
62134dd179eSJames Feist {
622adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
623adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
624adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
625adc4f0dbSShawn McCarney     std::string currentHealth;
626adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
627adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
628adc4f0dbSShawn McCarney     {
629adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
630adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
631adc4f0dbSShawn McCarney         {
632adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
633adc4f0dbSShawn McCarney             if (health != nullptr)
634adc4f0dbSShawn McCarney             {
635adc4f0dbSShawn McCarney                 currentHealth = *health;
636adc4f0dbSShawn McCarney             }
637adc4f0dbSShawn McCarney         }
638adc4f0dbSShawn McCarney     }
639adc4f0dbSShawn McCarney 
640adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
641adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
642adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
643adc4f0dbSShawn McCarney     {
644adc4f0dbSShawn McCarney         return "Critical";
645adc4f0dbSShawn McCarney     }
646adc4f0dbSShawn McCarney 
647c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmHigh = nullptr;
648c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmLow = nullptr;
649c1343bf6SKrzysztof Grobelny     const bool* warningAlarmHigh = nullptr;
650c1343bf6SKrzysztof Grobelny     const bool* warningAlarmLow = nullptr;
651711ac7a9SEd Tanous 
652c1343bf6SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
653c1343bf6SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
654c1343bf6SKrzysztof Grobelny         criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
655c1343bf6SKrzysztof Grobelny         "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
656c1343bf6SKrzysztof Grobelny         warningAlarmLow);
657c1343bf6SKrzysztof Grobelny 
658c1343bf6SKrzysztof Grobelny     if (success)
65934dd179eSJames Feist     {
660c1343bf6SKrzysztof Grobelny         // Check if sensor has critical threshold alarm
661c1343bf6SKrzysztof Grobelny         if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
662c1343bf6SKrzysztof Grobelny             (criticalAlarmLow != nullptr && *criticalAlarmLow))
66334dd179eSJames Feist         {
66434dd179eSJames Feist             return "Critical";
66534dd179eSJames Feist         }
66634dd179eSJames Feist     }
66734dd179eSJames Feist 
668adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
669adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
670adc4f0dbSShawn McCarney     {
671adc4f0dbSShawn McCarney         return "Critical";
672adc4f0dbSShawn McCarney     }
673adc4f0dbSShawn McCarney 
674adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that. This
675adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
676adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
677adc4f0dbSShawn McCarney     {
678adc4f0dbSShawn McCarney         return "Warning";
679adc4f0dbSShawn McCarney     }
680adc4f0dbSShawn McCarney 
681c1343bf6SKrzysztof Grobelny     if (success)
682c1343bf6SKrzysztof Grobelny     {
683adc4f0dbSShawn McCarney         // Check if sensor has warning threshold alarm
684c1343bf6SKrzysztof Grobelny         if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
685c1343bf6SKrzysztof Grobelny             (warningAlarmLow != nullptr && *warningAlarmLow))
68634dd179eSJames Feist         {
687ebe4d91eSEd Tanous             return "Warning";
68834dd179eSJames Feist         }
68934dd179eSJames Feist     }
690adc4f0dbSShawn McCarney 
69134dd179eSJames Feist     return "OK";
69234dd179eSJames Feist }
69334dd179eSJames Feist 
69423a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
695d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
696d500549bSAnthony Wilson {
697d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
698d500549bSAnthony Wilson     {
699d500549bSAnthony Wilson         switch (inventoryItem->ledState)
700d500549bSAnthony Wilson         {
701d500549bSAnthony Wilson             case LedState::OFF:
702d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
703d500549bSAnthony Wilson                 break;
704d500549bSAnthony Wilson             case LedState::ON:
705d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
706d500549bSAnthony Wilson                 break;
707d500549bSAnthony Wilson             case LedState::BLINK:
708d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
709d500549bSAnthony Wilson                 break;
71023a21a1cSEd Tanous             case LedState::UNKNOWN:
711d500549bSAnthony Wilson                 break;
712d500549bSAnthony Wilson         }
713d500549bSAnthony Wilson     }
714d500549bSAnthony Wilson }
715d500549bSAnthony Wilson 
71634dd179eSJames Feist /**
71708777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
71808777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
719274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
72008777fb0SLewanczyk, Dawid  * build
7211d7c0054SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
7221d7c0054SEd Tanous  * @param propertiesDict A dictionary of the properties to build the sensor
7231d7c0054SEd Tanous  * from.
7241d7c0054SEd Tanous  * @param sensorJson  The json object to fill
725adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
726adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
72708777fb0SLewanczyk, Dawid  */
7281d7c0054SEd Tanous inline void objectPropertiesToJson(
7291d7c0054SEd Tanous     std::string_view sensorName, std::string_view sensorType,
7301d7c0054SEd Tanous     std::string_view chassisSubNode,
7311d7c0054SEd Tanous     const dbus::utility::DBusPropertiesMap& propertiesDict,
73281ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
7331abe55efSEd Tanous {
7341d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
735adc4f0dbSShawn McCarney     {
736c1d019a6SEd Tanous         std::string subNodeEscaped(chassisSubNode);
737c1d019a6SEd Tanous         subNodeEscaped.erase(
738c1d019a6SEd Tanous             std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
739c1d019a6SEd Tanous             subNodeEscaped.end());
740c1d019a6SEd Tanous 
741c1d019a6SEd Tanous         // For sensors in SensorCollection we set Id instead of MemberId,
742c1d019a6SEd Tanous         // including power sensors.
743c1d019a6SEd Tanous         subNodeEscaped += '_';
744c1d019a6SEd Tanous         subNodeEscaped += sensorName;
745c1d019a6SEd Tanous         sensorJson["Id"] = std::move(subNodeEscaped);
746c1d019a6SEd Tanous 
7471d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7481d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7491d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
75095a3ecadSAnthony Wilson     }
75195a3ecadSAnthony Wilson     else if (sensorType != "power")
75295a3ecadSAnthony Wilson     {
75395a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
75495a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
75595a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
75681ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
7571d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7581d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7591d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
760adc4f0dbSShawn McCarney     }
761e742b6ccSEd Tanous 
76281ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
76381ce609eSEd Tanous     sensorJson["Status"]["Health"] =
7641d7c0054SEd Tanous         getHealth(sensorJson, propertiesDict, inventoryItem);
76508777fb0SLewanczyk, Dawid 
76608777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
76708777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
76808777fb0SLewanczyk, Dawid     // that require integers, not floats.
76908777fb0SLewanczyk, Dawid     bool forceToInt = false;
77008777fb0SLewanczyk, Dawid 
7713929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
7721d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
77395a3ecadSAnthony Wilson     {
7742a4ba195SShounak Mitra         sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
775c2bf7f99SWludzik, Jozef 
7760ec8b83dSEd Tanous         sensor::ReadingType readingType = sensors::toReadingType(sensorType);
7770ec8b83dSEd Tanous         if (readingType == sensor::ReadingType::Invalid)
77895a3ecadSAnthony Wilson         {
779c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
780c2bf7f99SWludzik, Jozef                              << sensorType;
78195a3ecadSAnthony Wilson         }
782c2bf7f99SWludzik, Jozef         else
78395a3ecadSAnthony Wilson         {
784c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
78595a3ecadSAnthony Wilson         }
786c2bf7f99SWludzik, Jozef 
7871d7c0054SEd Tanous         std::string_view readingUnits = sensors::toReadingUnits(sensorType);
788c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
789f8ede15eSAdrian Ambrożewicz         {
790c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
791c2bf7f99SWludzik, Jozef                              << sensorType;
792c2bf7f99SWludzik, Jozef         }
793c2bf7f99SWludzik, Jozef         else
794c2bf7f99SWludzik, Jozef         {
795c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
796f8ede15eSAdrian Ambrożewicz         }
79795a3ecadSAnthony Wilson     }
79895a3ecadSAnthony Wilson     else if (sensorType == "temperature")
7991abe55efSEd Tanous     {
8003929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
80181ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
80208777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
80308777fb0SLewanczyk, Dawid         // implementation seems to implement fan
8041abe55efSEd Tanous     }
8051abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
8061abe55efSEd Tanous     {
8073929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
80881ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
80981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
81081ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
81108777fb0SLewanczyk, Dawid         forceToInt = true;
8121abe55efSEd Tanous     }
8136f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
8146f6d0d32SEd Tanous     {
8153929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
81681ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
81781ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
81881ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
8196f6d0d32SEd Tanous         forceToInt = true;
8206f6d0d32SEd Tanous     }
8211abe55efSEd Tanous     else if (sensorType == "voltage")
8221abe55efSEd Tanous     {
8233929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
82481ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
8251abe55efSEd Tanous     }
8262474adfaSEd Tanous     else if (sensorType == "power")
8272474adfaSEd Tanous     {
8281d7c0054SEd Tanous         if (boost::iequals(sensorName, "total_power"))
829028f7ebcSEddie James         {
83081ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
8317ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
8327ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
83381ce609eSEd Tanous             sensorJson["MemberId"] = "0";
83481ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
8353929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
836028f7ebcSEddie James         }
8371d7c0054SEd Tanous         else if (boost::ifind_first(sensorName, "input").empty())
83849c53ac9SJohnathan Mantey         {
8393929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
84049c53ac9SJohnathan Mantey         }
84149c53ac9SJohnathan Mantey         else
84249c53ac9SJohnathan Mantey         {
8433929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
84449c53ac9SJohnathan Mantey         }
8452474adfaSEd Tanous     }
8461abe55efSEd Tanous     else
8471abe55efSEd Tanous     {
84855c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
84908777fb0SLewanczyk, Dawid         return;
85008777fb0SLewanczyk, Dawid     }
85108777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
8523929aca1SAnthony Wilson     std::vector<
8533929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
8543929aca1SAnthony Wilson         properties;
85508777fb0SLewanczyk, Dawid     properties.reserve(7);
85608777fb0SLewanczyk, Dawid 
85708777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
858de629b6eSShawn McCarney 
8591d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
8603929aca1SAnthony Wilson     {
8613929aca1SAnthony Wilson         properties.emplace_back(
8623929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
8633929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
8643929aca1SAnthony Wilson         properties.emplace_back(
8653929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
8663929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
8673929aca1SAnthony Wilson         properties.emplace_back(
8683929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
8693929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
8703929aca1SAnthony Wilson         properties.emplace_back(
8713929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
8723929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
8733929aca1SAnthony Wilson     }
8743929aca1SAnthony Wilson     else if (sensorType != "power")
875de629b6eSShawn McCarney     {
87608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8773929aca1SAnthony Wilson                                 "WarningHigh",
8783929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
87908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8803929aca1SAnthony Wilson                                 "WarningLow",
8813929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
88208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8833929aca1SAnthony Wilson                                 "CriticalHigh",
8843929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
88508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8863929aca1SAnthony Wilson                                 "CriticalLow",
8873929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
888de629b6eSShawn McCarney     }
88908777fb0SLewanczyk, Dawid 
8902474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
8912474adfaSEd Tanous 
8921d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
89395a3ecadSAnthony Wilson     {
89495a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8953929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
89695a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8973929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
89895a3ecadSAnthony Wilson     }
89995a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9001abe55efSEd Tanous     {
90108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9023929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
90308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9043929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
9051abe55efSEd Tanous     }
906adc4f0dbSShawn McCarney     else if (sensorType != "power")
9071abe55efSEd Tanous     {
90808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9093929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
91008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9113929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
91208777fb0SLewanczyk, Dawid     }
91308777fb0SLewanczyk, Dawid 
9143929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
9153929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
9161abe55efSEd Tanous     {
9171d7c0054SEd Tanous         for (const auto& [valueName, valueVariant] : propertiesDict)
918711ac7a9SEd Tanous         {
919711ac7a9SEd Tanous             if (valueName != std::get<1>(p))
920711ac7a9SEd Tanous             {
921711ac7a9SEd Tanous                 continue;
922711ac7a9SEd Tanous             }
9233929aca1SAnthony Wilson 
9243929aca1SAnthony Wilson             // The property we want to set may be nested json, so use
9253929aca1SAnthony Wilson             // a json_pointer for easy indexing into the json structure.
9263929aca1SAnthony Wilson             const nlohmann::json::json_pointer& key = std::get<2>(p);
9273929aca1SAnthony Wilson 
928abf2add6SEd Tanous             const double* doubleValue = std::get_if<double>(&valueVariant);
92940e4f380SEd Tanous             if (doubleValue == nullptr)
9301abe55efSEd Tanous             {
93140e4f380SEd Tanous                 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
9326f6d0d32SEd Tanous                 continue;
93308777fb0SLewanczyk, Dawid             }
9346f6d0d32SEd Tanous             if (forceToInt)
9356f6d0d32SEd Tanous             {
93640e4f380SEd Tanous                 sensorJson[key] = static_cast<int64_t>(*doubleValue);
9376f6d0d32SEd Tanous             }
9386f6d0d32SEd Tanous             else
9396f6d0d32SEd Tanous             {
94040e4f380SEd Tanous                 sensorJson[key] = *doubleValue;
94108777fb0SLewanczyk, Dawid             }
94208777fb0SLewanczyk, Dawid         }
94308777fb0SLewanczyk, Dawid     }
94408777fb0SLewanczyk, Dawid }
94508777fb0SLewanczyk, Dawid 
9461d7c0054SEd Tanous /**
9471d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
9481d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
9491d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
9501d7c0054SEd Tanous  * build
9511d7c0054SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
9521d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
9531d7c0054SEd Tanous  * interfaces to be built from
9541d7c0054SEd Tanous  * @param sensorJson  The json object to fill
9551d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
9561d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
9571d7c0054SEd Tanous  */
9581d7c0054SEd Tanous inline void objectInterfacesToJson(
9591d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
9601d7c0054SEd Tanous     const std::string& chassisSubNode,
9611d7c0054SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
9621d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
9631d7c0054SEd Tanous {
9641d7c0054SEd Tanous 
9651d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
9661d7c0054SEd Tanous     {
9671d7c0054SEd Tanous         objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
9681d7c0054SEd Tanous                                valuesDict, sensorJson, inventoryItem);
9691d7c0054SEd Tanous     }
970c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
9711d7c0054SEd Tanous }
9721d7c0054SEd Tanous 
973b5a76932SEd Tanous inline void populateFanRedundancy(
974b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
9758bd25ccdSJames Feist {
9768bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
977b9d36b47SEd Tanous         [sensorsAsyncResp](
978b9d36b47SEd Tanous             const boost::system::error_code ec,
979b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
9808bd25ccdSJames Feist         if (ec)
9818bd25ccdSJames Feist         {
9828bd25ccdSJames Feist             return; // don't have to have this interface
9838bd25ccdSJames Feist         }
984002d39b4SEd Tanous         for (const std::pair<
985002d39b4SEd Tanous                  std::string,
986002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
987e278c18fSEd Tanous                  pathPair : resp)
9888bd25ccdSJames Feist         {
989e278c18fSEd Tanous             const std::string& path = pathPair.first;
990002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
991002d39b4SEd Tanous                 objDict = pathPair.second;
9928bd25ccdSJames Feist             if (objDict.empty())
9938bd25ccdSJames Feist             {
9948bd25ccdSJames Feist                 continue; // this should be impossible
9958bd25ccdSJames Feist             }
9968bd25ccdSJames Feist 
9978bd25ccdSJames Feist             const std::string& owner = objDict.begin()->first;
9981e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::vector<std::string>>(
9991e1e598dSJonathan Doman                 *crow::connections::systemBus,
10001e1e598dSJonathan Doman                 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
10011e1e598dSJonathan Doman                 "xyz.openbmc_project.Association", "endpoints",
1002002d39b4SEd Tanous                 [path, owner,
1003002d39b4SEd Tanous                  sensorsAsyncResp](const boost::system::error_code e,
10041e1e598dSJonathan Doman                                    const std::vector<std::string>& endpoints) {
1005271584abSEd Tanous                 if (e)
10068bd25ccdSJames Feist                 {
10078bd25ccdSJames Feist                     return; // if they don't have an association we
10088bd25ccdSJames Feist                             // can't tell what chassis is
10098bd25ccdSJames Feist                 }
1010002d39b4SEd Tanous                 auto found =
1011002d39b4SEd Tanous                     std::find_if(endpoints.begin(), endpoints.end(),
10128bd25ccdSJames Feist                                  [sensorsAsyncResp](const std::string& entry) {
1013002d39b4SEd Tanous                     return entry.find(sensorsAsyncResp->chassisId) !=
10148bd25ccdSJames Feist                            std::string::npos;
10158bd25ccdSJames Feist                     });
10168bd25ccdSJames Feist 
10171e1e598dSJonathan Doman                 if (found == endpoints.end())
10188bd25ccdSJames Feist                 {
10198bd25ccdSJames Feist                     return;
10208bd25ccdSJames Feist                 }
102186d89ed7SKrzysztof Grobelny                 sdbusplus::asio::getAllProperties(
102286d89ed7SKrzysztof Grobelny                     *crow::connections::systemBus, owner, path,
102386d89ed7SKrzysztof Grobelny                     "xyz.openbmc_project.Control.FanRedundancy",
10248bd25ccdSJames Feist                     [path, sensorsAsyncResp](
1025271584abSEd Tanous                         const boost::system::error_code& err,
102686d89ed7SKrzysztof Grobelny                         const dbus::utility::DBusPropertiesMap& ret) {
1027271584abSEd Tanous                     if (err)
10288bd25ccdSJames Feist                     {
10298bd25ccdSJames Feist                         return; // don't have to have this
10308bd25ccdSJames Feist                                 // interface
10318bd25ccdSJames Feist                     }
10328bd25ccdSJames Feist 
103386d89ed7SKrzysztof Grobelny                     const uint8_t* allowedFailures = nullptr;
103486d89ed7SKrzysztof Grobelny                     const std::vector<std::string>* collection = nullptr;
103586d89ed7SKrzysztof Grobelny                     const std::string* status = nullptr;
103686d89ed7SKrzysztof Grobelny 
103786d89ed7SKrzysztof Grobelny                     const bool success = sdbusplus::unpackPropertiesNoThrow(
103886d89ed7SKrzysztof Grobelny                         dbus_utils::UnpackErrorPrinter(), ret,
103986d89ed7SKrzysztof Grobelny                         "AllowedFailures", allowedFailures, "Collection",
104086d89ed7SKrzysztof Grobelny                         collection, "Status", status);
104186d89ed7SKrzysztof Grobelny 
104286d89ed7SKrzysztof Grobelny                     if (!success)
104386d89ed7SKrzysztof Grobelny                     {
104486d89ed7SKrzysztof Grobelny                         messages::internalError(
104586d89ed7SKrzysztof Grobelny                             sensorsAsyncResp->asyncResp->res);
104686d89ed7SKrzysztof Grobelny                         return;
104786d89ed7SKrzysztof Grobelny                     }
104886d89ed7SKrzysztof Grobelny 
104986d89ed7SKrzysztof Grobelny                     if (allowedFailures == nullptr || collection == nullptr ||
105086d89ed7SKrzysztof Grobelny                         status == nullptr)
10518bd25ccdSJames Feist                     {
1052002d39b4SEd Tanous                         BMCWEB_LOG_ERROR << "Invalid redundancy interface";
10538bd25ccdSJames Feist                         messages::internalError(
10548d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
10558bd25ccdSJames Feist                         return;
10568bd25ccdSJames Feist                     }
10578bd25ccdSJames Feist 
1058002d39b4SEd Tanous                     sdbusplus::message::object_path objectPath(path);
105928aa8de5SGeorge Liu                     std::string name = objectPath.filename();
106028aa8de5SGeorge Liu                     if (name.empty())
10618bd25ccdSJames Feist                     {
10628bd25ccdSJames Feist                         // this should be impossible
10638bd25ccdSJames Feist                         messages::internalError(
10648d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
10658bd25ccdSJames Feist                         return;
10668bd25ccdSJames Feist                     }
1067002d39b4SEd Tanous                     std::replace(name.begin(), name.end(), '_', ' ');
10688bd25ccdSJames Feist 
10698bd25ccdSJames Feist                     std::string health;
10708bd25ccdSJames Feist 
107111ba3979SEd Tanous                     if (status->ends_with("Full"))
10728bd25ccdSJames Feist                     {
10738bd25ccdSJames Feist                         health = "OK";
10748bd25ccdSJames Feist                     }
107511ba3979SEd Tanous                     else if (status->ends_with("Degraded"))
10768bd25ccdSJames Feist                     {
10778bd25ccdSJames Feist                         health = "Warning";
10788bd25ccdSJames Feist                     }
10798bd25ccdSJames Feist                     else
10808bd25ccdSJames Feist                     {
10818bd25ccdSJames Feist                         health = "Critical";
10828bd25ccdSJames Feist                     }
10831476687dSEd Tanous                     nlohmann::json::array_t redfishCollection;
10848bd25ccdSJames Feist                     const auto& fanRedfish =
1085002d39b4SEd Tanous                         sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
10868bd25ccdSJames Feist                     for (const std::string& item : *collection)
10878bd25ccdSJames Feist                     {
10888a592810SEd Tanous                         sdbusplus::message::object_path itemPath(item);
10898a592810SEd Tanous                         std::string itemName = itemPath.filename();
109028aa8de5SGeorge Liu                         if (itemName.empty())
109128aa8de5SGeorge Liu                         {
109228aa8de5SGeorge Liu                             continue;
109328aa8de5SGeorge Liu                         }
10948bd25ccdSJames Feist                         /*
10958bd25ccdSJames Feist                         todo(ed): merge patch that fixes the names
10968bd25ccdSJames Feist                         std::replace(itemName.begin(),
10978bd25ccdSJames Feist                                      itemName.end(), '_', ' ');*/
1098002d39b4SEd Tanous                         auto schemaItem =
1099002d39b4SEd Tanous                             std::find_if(fanRedfish.begin(), fanRedfish.end(),
11008bd25ccdSJames Feist                                          [itemName](const nlohmann::json& fan) {
11018bd25ccdSJames Feist                             return fan["MemberId"] == itemName;
11028bd25ccdSJames Feist                             });
11038bd25ccdSJames Feist                         if (schemaItem != fanRedfish.end())
11048bd25ccdSJames Feist                         {
11058a592810SEd Tanous                             nlohmann::json::object_t collectionId;
11068a592810SEd Tanous                             collectionId["@odata.id"] =
11071476687dSEd Tanous                                 (*schemaItem)["@odata.id"];
11081476687dSEd Tanous                             redfishCollection.emplace_back(
11098a592810SEd Tanous                                 std::move(collectionId));
11108bd25ccdSJames Feist                         }
11118bd25ccdSJames Feist                         else
11128bd25ccdSJames Feist                         {
1113002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "failed to find fan in schema";
11148bd25ccdSJames Feist                             messages::internalError(
11158d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
11168bd25ccdSJames Feist                             return;
11178bd25ccdSJames Feist                         }
11188bd25ccdSJames Feist                     }
11198bd25ccdSJames Feist 
11203e9e72ebSKuiying Wang                     size_t minNumNeeded =
112126f6976fSEd Tanous                         collection->empty()
112226f6976fSEd Tanous                             ? 0
112326f6976fSEd Tanous                             : collection->size() - *allowedFailures;
1124002d39b4SEd Tanous                     nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
11258bd25ccdSJames Feist                                                 .jsonValue["Redundancy"];
11261476687dSEd Tanous 
11271476687dSEd Tanous                     nlohmann::json::object_t redundancy;
11281476687dSEd Tanous                     redundancy["@odata.id"] =
1129002d39b4SEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1130002d39b4SEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode +
1131002d39b4SEd Tanous                         "#/Redundancy/" + std::to_string(jResp.size());
1132002d39b4SEd Tanous                     redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
11331476687dSEd Tanous                     redundancy["MinNumNeeded"] = minNumNeeded;
11341476687dSEd Tanous                     redundancy["MemberId"] = name;
11351476687dSEd Tanous                     redundancy["Mode"] = "N+m";
11361476687dSEd Tanous                     redundancy["Name"] = name;
11371476687dSEd Tanous                     redundancy["RedundancySet"] = redfishCollection;
11381476687dSEd Tanous                     redundancy["Status"]["Health"] = health;
11391476687dSEd Tanous                     redundancy["Status"]["State"] = "Enabled";
11401476687dSEd Tanous 
11411476687dSEd Tanous                     jResp.push_back(std::move(redundancy));
114286d89ed7SKrzysztof Grobelny                     });
11431e1e598dSJonathan Doman                 });
11448bd25ccdSJames Feist         }
11458bd25ccdSJames Feist         },
11468bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
11478bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
11488bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
11498bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
11508bd25ccdSJames Feist         std::array<const char*, 1>{
11518bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
11528bd25ccdSJames Feist }
11538bd25ccdSJames Feist 
1154b5a76932SEd Tanous inline void
115581ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
115649c53ac9SJohnathan Mantey {
11578d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
115849c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
115981ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
116049c53ac9SJohnathan Mantey     {
116149c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
116249c53ac9SJohnathan Mantey     }
116349c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
116449c53ac9SJohnathan Mantey     {
116549c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
116649c53ac9SJohnathan Mantey         if (entry != response.end())
116749c53ac9SJohnathan Mantey         {
116849c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
116902cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
117049c53ac9SJohnathan Mantey                 return c1["Name"] < c2["Name"];
117149c53ac9SJohnathan Mantey             });
117249c53ac9SJohnathan Mantey 
117349c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
117449c53ac9SJohnathan Mantey             size_t count = 0;
117549c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
117649c53ac9SJohnathan Mantey             {
117749c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
117849c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
117949c53ac9SJohnathan Mantey                 {
118049c53ac9SJohnathan Mantey                     continue;
118149c53ac9SJohnathan Mantey                 }
118249c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
118349c53ac9SJohnathan Mantey                 if (value != nullptr)
118449c53ac9SJohnathan Mantey                 {
118549c53ac9SJohnathan Mantey                     *value += std::to_string(count);
118649c53ac9SJohnathan Mantey                     count++;
118781ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
118849c53ac9SJohnathan Mantey                 }
118949c53ac9SJohnathan Mantey             }
119049c53ac9SJohnathan Mantey         }
119149c53ac9SJohnathan Mantey     }
119249c53ac9SJohnathan Mantey }
119349c53ac9SJohnathan Mantey 
119408777fb0SLewanczyk, Dawid /**
1195adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1196adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1197adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1198adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
11998fb49dd6SShawn McCarney  */
120023a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1201b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1202adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
12038fb49dd6SShawn McCarney {
1204adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
12058fb49dd6SShawn McCarney     {
1206adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
12078fb49dd6SShawn McCarney         {
1208adc4f0dbSShawn McCarney             return &inventoryItem;
12098fb49dd6SShawn McCarney         }
12108fb49dd6SShawn McCarney     }
12118fb49dd6SShawn McCarney     return nullptr;
12128fb49dd6SShawn McCarney }
12138fb49dd6SShawn McCarney 
12148fb49dd6SShawn McCarney /**
1215adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1216adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1217adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1218adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12198fb49dd6SShawn McCarney  */
122023a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1221b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1222adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1223adc4f0dbSShawn McCarney {
1224adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1225adc4f0dbSShawn McCarney     {
1226adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1227adc4f0dbSShawn McCarney         {
1228adc4f0dbSShawn McCarney             return &inventoryItem;
1229adc4f0dbSShawn McCarney         }
1230adc4f0dbSShawn McCarney     }
1231adc4f0dbSShawn McCarney     return nullptr;
1232adc4f0dbSShawn McCarney }
1233adc4f0dbSShawn McCarney 
1234adc4f0dbSShawn McCarney /**
1235d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1236d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1237d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1238d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1239d500549bSAnthony Wilson  */
1240d500549bSAnthony Wilson inline InventoryItem*
1241d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1242d500549bSAnthony Wilson                             const std::string& ledObjPath)
1243d500549bSAnthony Wilson {
1244d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1245d500549bSAnthony Wilson     {
1246d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1247d500549bSAnthony Wilson         {
1248d500549bSAnthony Wilson             return &inventoryItem;
1249d500549bSAnthony Wilson         }
1250d500549bSAnthony Wilson     }
1251d500549bSAnthony Wilson     return nullptr;
1252d500549bSAnthony Wilson }
1253d500549bSAnthony Wilson 
1254d500549bSAnthony Wilson /**
1255adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1256adc4f0dbSShawn McCarney  *
1257adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1258adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1259adc4f0dbSShawn McCarney  * added to the vector.
1260adc4f0dbSShawn McCarney  *
1261adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1262adc4f0dbSShawn McCarney  * InventoryItem.
1263adc4f0dbSShawn McCarney  *
1264adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1265adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1266adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1267adc4f0dbSShawn McCarney  */
1268b5a76932SEd Tanous inline void addInventoryItem(
1269b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1270b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1271adc4f0dbSShawn McCarney {
1272adc4f0dbSShawn McCarney     // Look for inventory item in vector
1273adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1274adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1275adc4f0dbSShawn McCarney 
1276adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1277adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1278adc4f0dbSShawn McCarney     {
1279adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1280adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1281adc4f0dbSShawn McCarney     }
1282adc4f0dbSShawn McCarney 
1283adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1284adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1285adc4f0dbSShawn McCarney }
1286adc4f0dbSShawn McCarney 
1287adc4f0dbSShawn McCarney /**
1288adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1289adc4f0dbSShawn McCarney  *
1290adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1291adc4f0dbSShawn McCarney  * specified InventoryItem.
1292adc4f0dbSShawn McCarney  *
1293adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1294adc4f0dbSShawn McCarney  * response.
1295adc4f0dbSShawn McCarney  *
1296adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1297adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1298adc4f0dbSShawn McCarney  * for the specified inventory item.
1299adc4f0dbSShawn McCarney  */
130023a21a1cSEd Tanous inline void storeInventoryItemData(
1301adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1302711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
13038fb49dd6SShawn McCarney {
1304adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1305711ac7a9SEd Tanous 
13069eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
13078fb49dd6SShawn McCarney     {
1308711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
13098fb49dd6SShawn McCarney         {
13109eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1311711ac7a9SEd Tanous             {
1312711ac7a9SEd Tanous                 if (name == "Present")
1313711ac7a9SEd Tanous                 {
1314711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1315adc4f0dbSShawn McCarney                     if (value != nullptr)
13168fb49dd6SShawn McCarney                     {
1317adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
13188fb49dd6SShawn McCarney                     }
13198fb49dd6SShawn McCarney                 }
13208fb49dd6SShawn McCarney             }
1321711ac7a9SEd Tanous         }
1322adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1323711ac7a9SEd Tanous 
1324711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
13258fb49dd6SShawn McCarney         {
1326adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
13278fb49dd6SShawn McCarney         }
1328adc4f0dbSShawn McCarney 
1329adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1330711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1331adc4f0dbSShawn McCarney         {
13329eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1333711ac7a9SEd Tanous             {
1334711ac7a9SEd Tanous                 if (name == "Manufacturer")
1335adc4f0dbSShawn McCarney                 {
1336adc4f0dbSShawn McCarney                     const std::string* value =
1337711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1338adc4f0dbSShawn McCarney                     if (value != nullptr)
1339adc4f0dbSShawn McCarney                     {
1340adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1341adc4f0dbSShawn McCarney                     }
1342adc4f0dbSShawn McCarney                 }
1343711ac7a9SEd Tanous                 if (name == "Model")
1344adc4f0dbSShawn McCarney                 {
1345adc4f0dbSShawn McCarney                     const std::string* value =
1346711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1347adc4f0dbSShawn McCarney                     if (value != nullptr)
1348adc4f0dbSShawn McCarney                     {
1349adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1350adc4f0dbSShawn McCarney                     }
1351adc4f0dbSShawn McCarney                 }
1352711ac7a9SEd Tanous                 if (name == "SerialNumber")
1353adc4f0dbSShawn McCarney                 {
1354adc4f0dbSShawn McCarney                     const std::string* value =
1355711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1356adc4f0dbSShawn McCarney                     if (value != nullptr)
1357adc4f0dbSShawn McCarney                     {
1358adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1359adc4f0dbSShawn McCarney                     }
1360adc4f0dbSShawn McCarney                 }
1361711ac7a9SEd Tanous                 if (name == "PartNumber")
1362711ac7a9SEd Tanous                 {
1363711ac7a9SEd Tanous                     const std::string* value =
1364711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1365711ac7a9SEd Tanous                     if (value != nullptr)
1366711ac7a9SEd Tanous                     {
1367711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1368711ac7a9SEd Tanous                     }
1369711ac7a9SEd Tanous                 }
1370711ac7a9SEd Tanous             }
1371adc4f0dbSShawn McCarney         }
1372adc4f0dbSShawn McCarney 
1373711ac7a9SEd Tanous         if (interface ==
1374711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1375adc4f0dbSShawn McCarney         {
13769eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1377adc4f0dbSShawn McCarney             {
1378711ac7a9SEd Tanous                 if (name == "Functional")
1379711ac7a9SEd Tanous                 {
1380711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1381adc4f0dbSShawn McCarney                     if (value != nullptr)
1382adc4f0dbSShawn McCarney                     {
1383adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
13848fb49dd6SShawn McCarney                     }
13858fb49dd6SShawn McCarney                 }
13868fb49dd6SShawn McCarney             }
13878fb49dd6SShawn McCarney         }
1388711ac7a9SEd Tanous     }
1389711ac7a9SEd Tanous }
13908fb49dd6SShawn McCarney 
13918fb49dd6SShawn McCarney /**
1392adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
13938fb49dd6SShawn McCarney  *
1394adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1395adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1396adc4f0dbSShawn McCarney  * inventoryItems vector.
13978fb49dd6SShawn McCarney  *
1398adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1399adc4f0dbSShawn McCarney  * response.
1400adc4f0dbSShawn McCarney  *
1401adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1402adc4f0dbSShawn McCarney  * been obtained.
1403adc4f0dbSShawn McCarney  *
1404adc4f0dbSShawn McCarney  * The callback must have the following signature:
1405adc4f0dbSShawn McCarney  *   @code
1406d500549bSAnthony Wilson  *   callback(void)
1407adc4f0dbSShawn McCarney  *   @endcode
1408adc4f0dbSShawn McCarney  *
1409adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1410adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1411adc4f0dbSShawn McCarney  * last asynchronous function has completed.
14128fb49dd6SShawn McCarney  *
14138fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1414adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1415adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
14168fb49dd6SShawn McCarney  * implements ObjectManager.
1417adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1418adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1419adc4f0dbSShawn McCarney  * in recursive calls to this function.
14208fb49dd6SShawn McCarney  */
1421adc4f0dbSShawn McCarney template <typename Callback>
1422adc4f0dbSShawn McCarney static void getInventoryItemsData(
14238fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1424adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1425*d0090733SEd Tanous     std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1426*d0090733SEd Tanous     size_t invConnectionsIndex = 0)
14278fb49dd6SShawn McCarney {
1428adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
14298fb49dd6SShawn McCarney 
1430adc4f0dbSShawn McCarney     // If no more connections left, call callback
1431adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
14328fb49dd6SShawn McCarney     {
1433d500549bSAnthony Wilson         callback();
1434adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1435adc4f0dbSShawn McCarney         return;
1436adc4f0dbSShawn McCarney     }
1437adc4f0dbSShawn McCarney 
1438adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1439fe04d49cSNan Zhou     auto it = invConnections->begin();
1440fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1441adc4f0dbSShawn McCarney     if (it != invConnections->end())
1442adc4f0dbSShawn McCarney     {
1443adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1444adc4f0dbSShawn McCarney 
14458fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1446*d0090733SEd Tanous         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1447*d0090733SEd Tanous                             callback{std::forward<Callback>(callback)},
1448*d0090733SEd Tanous                             invConnectionsIndex](
144902cad96eSEd Tanous                                const boost::system::error_code ec,
145002cad96eSEd Tanous                                const dbus::utility::ManagedObjectType& resp) {
1451adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
14528fb49dd6SShawn McCarney             if (ec)
14538fb49dd6SShawn McCarney             {
14548fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1455adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
14568d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
14578fb49dd6SShawn McCarney                 return;
14588fb49dd6SShawn McCarney             }
14598fb49dd6SShawn McCarney 
14608fb49dd6SShawn McCarney             // Loop through returned object paths
14618fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
14628fb49dd6SShawn McCarney             {
14638fb49dd6SShawn McCarney                 const std::string& objPath =
14648fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
14658fb49dd6SShawn McCarney 
1466adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1467adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1468adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1469adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
14708fb49dd6SShawn McCarney                 {
1471adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1472adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
14738fb49dd6SShawn McCarney                 }
14748fb49dd6SShawn McCarney             }
14758fb49dd6SShawn McCarney 
1476adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1477adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1478*d0090733SEd Tanous                                   invConnections, std::move(callback),
1479*d0090733SEd Tanous                                   invConnectionsIndex + 1);
1480adc4f0dbSShawn McCarney 
1481adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
14828fb49dd6SShawn McCarney         };
14838fb49dd6SShawn McCarney 
14848fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
14858fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
1486*d0090733SEd Tanous             std::move(respHandler), invConnection,
1487*d0090733SEd Tanous             "/xyz/openbmc_project/sensors",
14888fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14898fb49dd6SShawn McCarney     }
14908fb49dd6SShawn McCarney 
1491adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
14928fb49dd6SShawn McCarney }
14938fb49dd6SShawn McCarney 
14948fb49dd6SShawn McCarney /**
1495adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
14968fb49dd6SShawn McCarney  *
1497adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1498adc4f0dbSShawn McCarney  * items that are associated with sensors.
14998fb49dd6SShawn McCarney  *
15008fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
15018fb49dd6SShawn McCarney  * been obtained.
15028fb49dd6SShawn McCarney  *
15038fb49dd6SShawn McCarney  * The callback must have the following signature:
15048fb49dd6SShawn McCarney  *   @code
1505fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
15068fb49dd6SShawn McCarney  *   @endcode
15078fb49dd6SShawn McCarney  *
15088fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1509adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
15108fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
15118fb49dd6SShawn McCarney  */
15128fb49dd6SShawn McCarney template <typename Callback>
15138fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1514b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1515b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
15168fb49dd6SShawn McCarney     Callback&& callback)
15178fb49dd6SShawn McCarney {
15188fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
15198fb49dd6SShawn McCarney 
15208fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1521adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
15228fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1523adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1524adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
15258fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
15268fb49dd6SShawn McCarney 
15278fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1528002d39b4SEd Tanous     auto respHandler =
1529002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1530002d39b4SEd Tanous          inventoryItems](
1531b9d36b47SEd Tanous             const boost::system::error_code ec,
1532002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
15338fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
15348fb49dd6SShawn McCarney         if (ec)
15358fb49dd6SShawn McCarney         {
15368d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
15378fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
15388fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
15398fb49dd6SShawn McCarney             return;
15408fb49dd6SShawn McCarney         }
15418fb49dd6SShawn McCarney 
15428fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
1543fe04d49cSNan Zhou         std::shared_ptr<std::set<std::string>> invConnections =
1544fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
15458fb49dd6SShawn McCarney 
15468fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
15478fb49dd6SShawn McCarney         for (const std::pair<
15488fb49dd6SShawn McCarney                  std::string,
15498fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
15508fb49dd6SShawn McCarney                  object : subtree)
15518fb49dd6SShawn McCarney         {
1552adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
15538fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1554adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
15558fb49dd6SShawn McCarney             {
15568fb49dd6SShawn McCarney                 // Store all connections to inventory item
15578fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
15588fb49dd6SShawn McCarney                          objData : object.second)
15598fb49dd6SShawn McCarney                 {
15608fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
15618fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
15628fb49dd6SShawn McCarney                 }
15638fb49dd6SShawn McCarney             }
15648fb49dd6SShawn McCarney         }
1565d500549bSAnthony Wilson 
15668fb49dd6SShawn McCarney         callback(invConnections);
15678fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
15688fb49dd6SShawn McCarney     };
15698fb49dd6SShawn McCarney 
15708fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
15718fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
15728fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
15738fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
15748fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
15758fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
15768fb49dd6SShawn McCarney }
15778fb49dd6SShawn McCarney 
15788fb49dd6SShawn McCarney /**
1579adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
15808fb49dd6SShawn McCarney  *
15818fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1582d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1583d500549bSAnthony Wilson  * their LEDs, if any.
15848fb49dd6SShawn McCarney  *
15858fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
15868fb49dd6SShawn McCarney  * has been obtained.
15878fb49dd6SShawn McCarney  *
15888fb49dd6SShawn McCarney  * The callback must have the following signature:
15898fb49dd6SShawn McCarney  *   @code
1590adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
15918fb49dd6SShawn McCarney  *   @endcode
15928fb49dd6SShawn McCarney  *
15938fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
15948fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
15958fb49dd6SShawn McCarney  * implements ObjectManager.
15968fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
15978fb49dd6SShawn McCarney  */
15988fb49dd6SShawn McCarney template <typename Callback>
1599adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1600b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1601fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
16028fb49dd6SShawn McCarney     Callback&& callback)
16038fb49dd6SShawn McCarney {
1604adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
16058fb49dd6SShawn McCarney 
16068fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
160702cad96eSEd Tanous     auto respHandler =
160802cad96eSEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
16098fb49dd6SShawn McCarney          sensorNames](const boost::system::error_code ec,
161002cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
1611adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
16128fb49dd6SShawn McCarney         if (ec)
16138fb49dd6SShawn McCarney         {
1614adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1615adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
16168d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
16178fb49dd6SShawn McCarney             return;
16188fb49dd6SShawn McCarney         }
16198fb49dd6SShawn McCarney 
1620adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1621adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1622adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1623adc4f0dbSShawn McCarney 
16248fb49dd6SShawn McCarney         // Loop through returned object paths
16258fb49dd6SShawn McCarney         std::string sensorAssocPath;
16268fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
16278fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
16288fb49dd6SShawn McCarney         {
16298fb49dd6SShawn McCarney             const std::string& objPath =
16308fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
16318fb49dd6SShawn McCarney 
16328fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
16338fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
16348fb49dd6SShawn McCarney             {
16358fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
16368fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
16378fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
16388fb49dd6SShawn McCarney                 {
16398fb49dd6SShawn McCarney                     // Get Association interface for object path
1640711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
16418fb49dd6SShawn McCarney                     {
1642711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1643711ac7a9SEd Tanous                         {
1644711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1645711ac7a9SEd Tanous                             {
1646711ac7a9SEd Tanous                                 if (valueName == "endpoints")
16478fb49dd6SShawn McCarney                                 {
16488fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
16498fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1650711ac7a9SEd Tanous                                             &value);
1651711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1652711ac7a9SEd Tanous                                         !endpoints->empty())
16538fb49dd6SShawn McCarney                                     {
1654adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1655adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1656adc4f0dbSShawn McCarney                                             endpoints->front();
1657711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1658711ac7a9SEd Tanous                                                          invItemPath,
1659adc4f0dbSShawn McCarney                                                          sensorName);
16608fb49dd6SShawn McCarney                                     }
16618fb49dd6SShawn McCarney                                 }
16628fb49dd6SShawn McCarney                             }
1663711ac7a9SEd Tanous                         }
1664711ac7a9SEd Tanous                     }
16658fb49dd6SShawn McCarney                     break;
16668fb49dd6SShawn McCarney                 }
16678fb49dd6SShawn McCarney             }
16688fb49dd6SShawn McCarney         }
16698fb49dd6SShawn McCarney 
1670d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1671d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1672d500549bSAnthony Wilson         std::string inventoryAssocPath;
1673d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1674d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1675d500549bSAnthony Wilson         {
1676d500549bSAnthony Wilson             const std::string& objPath =
1677d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1678d500549bSAnthony Wilson 
1679d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1680d500549bSAnthony Wilson             {
1681d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1682d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1683d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1684d500549bSAnthony Wilson                 {
1685711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1686d500549bSAnthony Wilson                     {
1687711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1688711ac7a9SEd Tanous                         {
1689711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1690711ac7a9SEd Tanous                             {
1691711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1692d500549bSAnthony Wilson                                 {
1693d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1694d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1695711ac7a9SEd Tanous                                             &value);
1696711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1697711ac7a9SEd Tanous                                         !endpoints->empty())
1698d500549bSAnthony Wilson                                     {
1699711ac7a9SEd Tanous                                         // Add inventory item to vector
1700d500549bSAnthony Wilson                                         // Store LED path in inventory item
1701711ac7a9SEd Tanous                                         const std::string& ledPath =
1702711ac7a9SEd Tanous                                             endpoints->front();
1703d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1704d500549bSAnthony Wilson                                     }
1705d500549bSAnthony Wilson                                 }
1706d500549bSAnthony Wilson                             }
1707711ac7a9SEd Tanous                         }
1708711ac7a9SEd Tanous                     }
1709711ac7a9SEd Tanous 
1710d500549bSAnthony Wilson                     break;
1711d500549bSAnthony Wilson                 }
1712d500549bSAnthony Wilson             }
1713d500549bSAnthony Wilson         }
1714adc4f0dbSShawn McCarney         callback(inventoryItems);
1715adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
17168fb49dd6SShawn McCarney     };
17178fb49dd6SShawn McCarney 
17188fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
17198fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
1720*d0090733SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
17218fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17228fb49dd6SShawn McCarney 
1723adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
17248fb49dd6SShawn McCarney }
17258fb49dd6SShawn McCarney 
17268fb49dd6SShawn McCarney /**
1727d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1728d500549bSAnthony Wilson  *
1729d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1730d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1731d500549bSAnthony Wilson  * inventoryItems vector.
1732d500549bSAnthony Wilson  *
1733d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1734d500549bSAnthony Wilson  * response.
1735d500549bSAnthony Wilson  *
1736d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1737d500549bSAnthony Wilson  * has been obtained.
1738d500549bSAnthony Wilson  *
1739d500549bSAnthony Wilson  * The callback must have the following signature:
1740d500549bSAnthony Wilson  *   @code
174142cbe538SGunnar Mills  *   callback()
1742d500549bSAnthony Wilson  *   @endcode
1743d500549bSAnthony Wilson  *
1744d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1745d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1746d500549bSAnthony Wilson  * last asynchronous function has completed.
1747d500549bSAnthony Wilson  *
1748d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1749d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1750d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1751d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1752d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1753d500549bSAnthony Wilson  * in recursive calls to this function.
1754d500549bSAnthony Wilson  */
1755d500549bSAnthony Wilson template <typename Callback>
1756d500549bSAnthony Wilson void getInventoryLedData(
1757d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1758d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1759fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1760d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1761d500549bSAnthony Wilson {
1762d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1763d500549bSAnthony Wilson 
1764d500549bSAnthony Wilson     // If no more connections left, call callback
1765d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1766d500549bSAnthony Wilson     {
176742cbe538SGunnar Mills         callback();
1768d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1769d500549bSAnthony Wilson         return;
1770d500549bSAnthony Wilson     }
1771d500549bSAnthony Wilson 
1772d500549bSAnthony Wilson     // Get inventory item data from current connection
1773fe04d49cSNan Zhou     auto it = ledConnections->begin();
1774fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1775d500549bSAnthony Wilson     if (it != ledConnections->end())
1776d500549bSAnthony Wilson     {
1777d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1778d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1779d500549bSAnthony Wilson         // Response handler for Get State property
17801e1e598dSJonathan Doman         auto respHandler =
17811e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1782f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
17831e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1784d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1785d500549bSAnthony Wilson             if (ec)
1786d500549bSAnthony Wilson             {
1787d500549bSAnthony Wilson                 BMCWEB_LOG_ERROR
1788d500549bSAnthony Wilson                     << "getInventoryLedData respHandler DBus error " << ec;
17898d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1790d500549bSAnthony Wilson                 return;
1791d500549bSAnthony Wilson             }
1792d500549bSAnthony Wilson 
17931e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Led state: " << state;
1794d500549bSAnthony Wilson             // Find inventory item with this LED object path
1795d500549bSAnthony Wilson             InventoryItem* inventoryItem =
1796d500549bSAnthony Wilson                 findInventoryItemForLed(*inventoryItems, ledPath);
1797d500549bSAnthony Wilson             if (inventoryItem != nullptr)
1798d500549bSAnthony Wilson             {
1799d500549bSAnthony Wilson                 // Store LED state in InventoryItem
180011ba3979SEd Tanous                 if (state.ends_with("On"))
1801d500549bSAnthony Wilson                 {
1802d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::ON;
1803d500549bSAnthony Wilson                 }
180411ba3979SEd Tanous                 else if (state.ends_with("Blink"))
1805d500549bSAnthony Wilson                 {
1806d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::BLINK;
1807d500549bSAnthony Wilson                 }
180811ba3979SEd Tanous                 else if (state.ends_with("Off"))
1809d500549bSAnthony Wilson                 {
1810d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::OFF;
1811d500549bSAnthony Wilson                 }
1812d500549bSAnthony Wilson                 else
1813d500549bSAnthony Wilson                 {
1814d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::UNKNOWN;
1815d500549bSAnthony Wilson                 }
1816d500549bSAnthony Wilson             }
1817d500549bSAnthony Wilson 
1818d500549bSAnthony Wilson             // Recurse to get LED data from next connection
1819d500549bSAnthony Wilson             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1820d500549bSAnthony Wilson                                 ledConnections, std::move(callback),
1821d500549bSAnthony Wilson                                 ledConnectionsIndex + 1);
1822d500549bSAnthony Wilson 
1823d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1824d500549bSAnthony Wilson         };
1825d500549bSAnthony Wilson 
1826d500549bSAnthony Wilson         // Get the State property for the current LED
18271e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
18281e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
18291e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
18301e1e598dSJonathan Doman             std::move(respHandler));
1831d500549bSAnthony Wilson     }
1832d500549bSAnthony Wilson 
1833d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1834d500549bSAnthony Wilson }
1835d500549bSAnthony Wilson 
1836d500549bSAnthony Wilson /**
1837d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1838d500549bSAnthony Wilson  *
1839d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1840d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1841d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1842d500549bSAnthony Wilson  *
1843d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1844d500549bSAnthony Wilson  * response.
1845d500549bSAnthony Wilson  *
1846d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1847d500549bSAnthony Wilson  * been obtained.
1848d500549bSAnthony Wilson  *
1849d500549bSAnthony Wilson  * The callback must have the following signature:
1850d500549bSAnthony Wilson  *   @code
185142cbe538SGunnar Mills  *   callback()
1852d500549bSAnthony Wilson  *   @endcode
1853d500549bSAnthony Wilson  *
1854d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1855d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1856d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1857d500549bSAnthony Wilson  */
1858d500549bSAnthony Wilson template <typename Callback>
1859d500549bSAnthony Wilson void getInventoryLeds(
1860d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1861d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1862d500549bSAnthony Wilson     Callback&& callback)
1863d500549bSAnthony Wilson {
1864d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1865d500549bSAnthony Wilson 
1866d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1867d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
1868d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1869d500549bSAnthony Wilson 
1870d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
1871002d39b4SEd Tanous     auto respHandler =
1872002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1873002d39b4SEd Tanous          inventoryItems](
1874b9d36b47SEd Tanous             const boost::system::error_code ec,
1875002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1876d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1877d500549bSAnthony Wilson         if (ec)
1878d500549bSAnthony Wilson         {
18798d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
1880d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1881d500549bSAnthony Wilson                              << ec;
1882d500549bSAnthony Wilson             return;
1883d500549bSAnthony Wilson         }
1884d500549bSAnthony Wilson 
1885d500549bSAnthony Wilson         // Build map of LED object paths to connections
1886fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1887fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
1888d500549bSAnthony Wilson 
1889d500549bSAnthony Wilson         // Loop through objects from GetSubTree
1890d500549bSAnthony Wilson         for (const std::pair<
1891d500549bSAnthony Wilson                  std::string,
1892d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1893d500549bSAnthony Wilson                  object : subtree)
1894d500549bSAnthony Wilson         {
1895d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
1896d500549bSAnthony Wilson             // items
1897d500549bSAnthony Wilson             const std::string& ledPath = object.first;
1898d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1899d500549bSAnthony Wilson             {
1900d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
1901d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
1902d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
1903d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1904d500549bSAnthony Wilson                                  << connection;
1905d500549bSAnthony Wilson             }
1906d500549bSAnthony Wilson         }
1907d500549bSAnthony Wilson 
1908d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1909d500549bSAnthony Wilson                             std::move(callback));
1910d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1911d500549bSAnthony Wilson     };
1912d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
1913d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
1914d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1915d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
1916d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1917d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1918d500549bSAnthony Wilson }
1919d500549bSAnthony Wilson 
1920d500549bSAnthony Wilson /**
192142cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
192242cbe538SGunnar Mills  *
192342cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
192442cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
192542cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
192642cbe538SGunnar Mills  *
192742cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
192842cbe538SGunnar Mills  * response.
192942cbe538SGunnar Mills  *
193042cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
193142cbe538SGunnar Mills  * when data has been obtained.
193242cbe538SGunnar Mills  *
193342cbe538SGunnar Mills  * The callback must have the following signature:
193442cbe538SGunnar Mills  *   @code
193542cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
193642cbe538SGunnar Mills  *   @endcode
193742cbe538SGunnar Mills  *
193842cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
193942cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
194042cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
194142cbe538SGunnar Mills  *        Supply Attributes
194242cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
194342cbe538SGunnar Mills  */
194442cbe538SGunnar Mills template <typename Callback>
194542cbe538SGunnar Mills void getPowerSupplyAttributesData(
1946b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
194742cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1948fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
194942cbe538SGunnar Mills     Callback&& callback)
195042cbe538SGunnar Mills {
195142cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
195242cbe538SGunnar Mills 
195342cbe538SGunnar Mills     if (psAttributesConnections.empty())
195442cbe538SGunnar Mills     {
195542cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
195642cbe538SGunnar Mills         callback(inventoryItems);
195742cbe538SGunnar Mills         return;
195842cbe538SGunnar Mills     }
195942cbe538SGunnar Mills 
196042cbe538SGunnar Mills     // Assuming just one connection (service) for now
1961fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
196242cbe538SGunnar Mills 
196342cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
196442cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
196542cbe538SGunnar Mills 
196642cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
1967002d39b4SEd Tanous     auto respHandler =
1968002d39b4SEd Tanous         [sensorsAsyncResp, inventoryItems,
1969f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
1970002d39b4SEd Tanous             const boost::system::error_code ec, const uint32_t value) {
197142cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
197242cbe538SGunnar Mills         if (ec)
197342cbe538SGunnar Mills         {
197442cbe538SGunnar Mills             BMCWEB_LOG_ERROR
197542cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
19768d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
197742cbe538SGunnar Mills             return;
197842cbe538SGunnar Mills         }
197942cbe538SGunnar Mills 
19801e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
198142cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
198242cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
198342cbe538SGunnar Mills         {
198455f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
198542cbe538SGunnar Mills             {
198642cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
19871e1e598dSJonathan Doman                     static_cast<int>(value);
198842cbe538SGunnar Mills             }
198942cbe538SGunnar Mills         }
199042cbe538SGunnar Mills 
199142cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
199242cbe538SGunnar Mills         callback(inventoryItems);
199342cbe538SGunnar Mills     };
199442cbe538SGunnar Mills 
199542cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
199642cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
19971e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
19981e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
19991e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
20001e1e598dSJonathan Doman         std::move(respHandler));
200142cbe538SGunnar Mills 
200242cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
200342cbe538SGunnar Mills }
200442cbe538SGunnar Mills 
200542cbe538SGunnar Mills /**
200642cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
200742cbe538SGunnar Mills  *
200842cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
200942cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
201042cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
201142cbe538SGunnar Mills  * item.
201242cbe538SGunnar Mills  *
201342cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
201442cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
201542cbe538SGunnar Mills  *
201642cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
201742cbe538SGunnar Mills  * when information has been obtained.
201842cbe538SGunnar Mills  *
201942cbe538SGunnar Mills  * The callback must have the following signature:
202042cbe538SGunnar Mills  *   @code
202142cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
202242cbe538SGunnar Mills  *   @endcode
202342cbe538SGunnar Mills  *
202442cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
202542cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
202642cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
202742cbe538SGunnar Mills  */
202842cbe538SGunnar Mills template <typename Callback>
202942cbe538SGunnar Mills void getPowerSupplyAttributes(
203042cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
203142cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
203242cbe538SGunnar Mills     Callback&& callback)
203342cbe538SGunnar Mills {
203442cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
203542cbe538SGunnar Mills 
203642cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2037a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
203842cbe538SGunnar Mills     {
203942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
204042cbe538SGunnar Mills         callback(inventoryItems);
204142cbe538SGunnar Mills         return;
204242cbe538SGunnar Mills     }
204342cbe538SGunnar Mills 
204442cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
204542cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
204642cbe538SGunnar Mills 
204742cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2048b9d36b47SEd Tanous     auto respHandler =
2049b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2050b9d36b47SEd Tanous          inventoryItems](
2051b9d36b47SEd Tanous             const boost::system::error_code ec,
2052b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
205342cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
205442cbe538SGunnar Mills         if (ec)
205542cbe538SGunnar Mills         {
20568d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
205742cbe538SGunnar Mills             BMCWEB_LOG_ERROR
205842cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
205942cbe538SGunnar Mills             return;
206042cbe538SGunnar Mills         }
206126f6976fSEd Tanous         if (subtree.empty())
206242cbe538SGunnar Mills         {
206342cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
206442cbe538SGunnar Mills             callback(inventoryItems);
206542cbe538SGunnar Mills             return;
206642cbe538SGunnar Mills         }
206742cbe538SGunnar Mills 
206842cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
206942cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
207042cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
2071fe04d49cSNan Zhou         std::map<std::string, std::string> psAttributesConnections;
207242cbe538SGunnar Mills 
207342cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
207442cbe538SGunnar Mills         {
207542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
207642cbe538SGunnar Mills             callback(inventoryItems);
207742cbe538SGunnar Mills             return;
207842cbe538SGunnar Mills         }
207942cbe538SGunnar Mills 
208042cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
208142cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
208242cbe538SGunnar Mills 
208342cbe538SGunnar Mills         if (connection.empty())
208442cbe538SGunnar Mills         {
208542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
208642cbe538SGunnar Mills             callback(inventoryItems);
208742cbe538SGunnar Mills             return;
208842cbe538SGunnar Mills         }
208942cbe538SGunnar Mills 
209042cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
209142cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
209242cbe538SGunnar Mills                          << connection;
209342cbe538SGunnar Mills 
209442cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
209542cbe538SGunnar Mills                                      psAttributesConnections,
209642cbe538SGunnar Mills                                      std::move(callback));
209742cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
209842cbe538SGunnar Mills     };
209942cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
210042cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
210142cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
210242cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
210342cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
210442cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
210542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
210642cbe538SGunnar Mills }
210742cbe538SGunnar Mills 
210842cbe538SGunnar Mills /**
2109adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
21108fb49dd6SShawn McCarney  *
21118fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2112adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
21138fb49dd6SShawn McCarney  *
2114adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2115adc4f0dbSShawn McCarney  * response.
21168fb49dd6SShawn McCarney  *
2117adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2118adc4f0dbSShawn McCarney  * inventory items have been obtained.
2119adc4f0dbSShawn McCarney  *
2120adc4f0dbSShawn McCarney  * The callback must have the following signature:
2121adc4f0dbSShawn McCarney  *   @code
2122adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2123adc4f0dbSShawn McCarney  *   @endcode
21248fb49dd6SShawn McCarney  *
21258fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
21268fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
21278fb49dd6SShawn McCarney  * implements ObjectManager.
2128adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
21298fb49dd6SShawn McCarney  */
2130adc4f0dbSShawn McCarney template <typename Callback>
2131*d0090733SEd Tanous static void
2132*d0090733SEd Tanous     getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2133fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>> sensorNames,
2134adc4f0dbSShawn McCarney                       Callback&& callback)
21358fb49dd6SShawn McCarney {
2136adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2137adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2138*d0090733SEd Tanous         [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
2139adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2140adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
21418fb49dd6SShawn McCarney         auto getInventoryItemsConnectionsCb =
2142*d0090733SEd Tanous             [sensorsAsyncResp, inventoryItems,
2143f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
2144fe04d49cSNan Zhou                 std::shared_ptr<std::set<std::string>> invConnections) {
21458fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2146002d39b4SEd Tanous             auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2147d500549bSAnthony Wilson                                             callback{std::move(callback)}]() {
2148d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
214942cbe538SGunnar Mills 
2150002d39b4SEd Tanous                 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2151002d39b4SEd Tanous                                            callback{std::move(callback)}]() {
215242cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
215342cbe538SGunnar Mills                     // Find Power Supply Attributes and get the data
2154002d39b4SEd Tanous                     getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
215542cbe538SGunnar Mills                                              std::move(callback));
215642cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
215742cbe538SGunnar Mills                 };
215842cbe538SGunnar Mills 
2159d500549bSAnthony Wilson                 // Find led connections and get the data
2160d500549bSAnthony Wilson                 getInventoryLeds(sensorsAsyncResp, inventoryItems,
216142cbe538SGunnar Mills                                  std::move(getInventoryLedsCb));
2162d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2163d500549bSAnthony Wilson             };
21648fb49dd6SShawn McCarney 
2165adc4f0dbSShawn McCarney             // Get inventory item data from connections
2166adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2167*d0090733SEd Tanous                                   invConnections,
2168d500549bSAnthony Wilson                                   std::move(getInventoryItemsDataCb));
21698fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
21708fb49dd6SShawn McCarney         };
21718fb49dd6SShawn McCarney 
2172adc4f0dbSShawn McCarney         // Get connections that provide inventory item data
2173002d39b4SEd Tanous         getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
21748fb49dd6SShawn McCarney                                      std::move(getInventoryItemsConnectionsCb));
2175adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
21768fb49dd6SShawn McCarney     };
21778fb49dd6SShawn McCarney 
2178adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2179*d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
2180adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2181adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2182adc4f0dbSShawn McCarney }
2183adc4f0dbSShawn McCarney 
2184adc4f0dbSShawn McCarney /**
2185adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2186adc4f0dbSShawn McCarney  *
2187adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2188adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2189adc4f0dbSShawn McCarney  * array.
2190adc4f0dbSShawn McCarney  *
2191adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2192adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2193adc4f0dbSShawn McCarney  * object.
2194adc4f0dbSShawn McCarney  *
2195adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2196adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2197adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2198adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2199adc4f0dbSShawn McCarney  */
220023a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2201adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2202adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2203adc4f0dbSShawn McCarney {
2204adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2205adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2206adc4f0dbSShawn McCarney     {
2207adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2208adc4f0dbSShawn McCarney         {
2209adc4f0dbSShawn McCarney             return powerSupply;
2210adc4f0dbSShawn McCarney         }
2211adc4f0dbSShawn McCarney     }
2212adc4f0dbSShawn McCarney 
2213adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2214adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2215adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2216adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2217adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2218adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2219adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2220adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2221adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2222adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2223adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2224d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2225adc4f0dbSShawn McCarney 
222642cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
222742cbe538SGunnar Mills     {
222842cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
222942cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
223042cbe538SGunnar Mills     }
223142cbe538SGunnar Mills 
223242cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2233adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2234adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2235adc4f0dbSShawn McCarney 
2236adc4f0dbSShawn McCarney     return powerSupply;
22378fb49dd6SShawn McCarney }
22388fb49dd6SShawn McCarney 
22398fb49dd6SShawn McCarney /**
2240de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2241de629b6eSShawn McCarney  *
2242de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2243de629b6eSShawn McCarney  *
2244de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2245de629b6eSShawn McCarney  * information has been obtained.
2246de629b6eSShawn McCarney  *
2247adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2248de629b6eSShawn McCarney  *
2249de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2250de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2251de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2252de629b6eSShawn McCarney  *
2253de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2254de629b6eSShawn McCarney  *
2255adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2256adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2257adc4f0dbSShawn McCarney  *
2258de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2259adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2260de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2261de629b6eSShawn McCarney  * implements ObjectManager.
2262adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2263de629b6eSShawn McCarney  */
226423a21a1cSEd Tanous inline void getSensorData(
226581ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2266fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2267fe04d49cSNan Zhou     const std::set<std::string>& connections,
2268b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2269de629b6eSShawn McCarney {
2270de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2271de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2272de629b6eSShawn McCarney     for (const std::string& connection : connections)
2273de629b6eSShawn McCarney     {
2274de629b6eSShawn McCarney         // Response handler to process managed objects
2275002d39b4SEd Tanous         auto getManagedObjectsCb =
2276002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
2277002d39b4SEd Tanous              inventoryItems](const boost::system::error_code ec,
227802cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
2279de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2280de629b6eSShawn McCarney             if (ec)
2281de629b6eSShawn McCarney             {
2282de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
22838d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2284de629b6eSShawn McCarney                 return;
2285de629b6eSShawn McCarney             }
2286de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2287de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2288de629b6eSShawn McCarney             {
2289de629b6eSShawn McCarney                 const std::string& objPath =
2290de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2291de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2292de629b6eSShawn McCarney                                  << objPath;
2293de629b6eSShawn McCarney 
2294de629b6eSShawn McCarney                 std::vector<std::string> split;
2295de629b6eSShawn McCarney                 // Reserve space for
2296de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2297de629b6eSShawn McCarney                 split.reserve(6);
2298de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2299de629b6eSShawn McCarney                 if (split.size() < 6)
2300de629b6eSShawn McCarney                 {
2301de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2302de629b6eSShawn McCarney                                      << objPath;
2303de629b6eSShawn McCarney                     continue;
2304de629b6eSShawn McCarney                 }
2305de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2306de629b6eSShawn McCarney                 // string at the beginning
2307de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2308de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2309de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2310de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
231149c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2312de629b6eSShawn McCarney                 {
2313accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2314de629b6eSShawn McCarney                     continue;
2315de629b6eSShawn McCarney                 }
2316de629b6eSShawn McCarney 
2317adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2318adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2319adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2320adc4f0dbSShawn McCarney 
232195a3ecadSAnthony Wilson                 const std::string& sensorSchema =
232281ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
232395a3ecadSAnthony Wilson 
232495a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
232595a3ecadSAnthony Wilson 
2326928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2327928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
232895a3ecadSAnthony Wilson                 {
2329c1d019a6SEd Tanous                     std::string sensorTypeEscaped(sensorType);
2330c1d019a6SEd Tanous                     sensorTypeEscaped.erase(
2331c1d019a6SEd Tanous                         std::remove(sensorTypeEscaped.begin(),
2332c1d019a6SEd Tanous                                     sensorTypeEscaped.end(), '_'),
2333c1d019a6SEd Tanous                         sensorTypeEscaped.end());
2334c1d019a6SEd Tanous                     std::string sensorId(sensorTypeEscaped);
2335c1d019a6SEd Tanous                     sensorId += "_";
2336c1d019a6SEd Tanous                     sensorId += sensorName;
2337c1d019a6SEd Tanous 
23388d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
2339c1d019a6SEd Tanous                         crow::utility::urlFromPieces(
2340c1d019a6SEd Tanous                             "redfish", "v1", "Chassis",
2341c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
2342c1d019a6SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
23438d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
234495a3ecadSAnthony Wilson                 }
234595a3ecadSAnthony Wilson                 else
234695a3ecadSAnthony Wilson                 {
2347271584abSEd Tanous                     std::string fieldName;
2348928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2349928fefb9SNan Zhou                     {
2350928fefb9SNan Zhou                         fieldName = "Members";
2351928fefb9SNan Zhou                     }
2352928fefb9SNan Zhou                     else if (sensorType == "temperature")
2353de629b6eSShawn McCarney                     {
2354de629b6eSShawn McCarney                         fieldName = "Temperatures";
2355de629b6eSShawn McCarney                     }
2356de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2357de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2358de629b6eSShawn McCarney                     {
2359de629b6eSShawn McCarney                         fieldName = "Fans";
2360de629b6eSShawn McCarney                     }
2361de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2362de629b6eSShawn McCarney                     {
2363de629b6eSShawn McCarney                         fieldName = "Voltages";
2364de629b6eSShawn McCarney                     }
2365de629b6eSShawn McCarney                     else if (sensorType == "power")
2366de629b6eSShawn McCarney                     {
236755f79e6fSEd Tanous                         if (sensorName == "total_power")
2368028f7ebcSEddie James                         {
2369028f7ebcSEddie James                             fieldName = "PowerControl";
2370028f7ebcSEddie James                         }
2371adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2372adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2373028f7ebcSEddie James                         {
2374de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2375de629b6eSShawn McCarney                         }
2376adc4f0dbSShawn McCarney                         else
2377adc4f0dbSShawn McCarney                         {
2378adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2379adc4f0dbSShawn McCarney                             continue;
2380adc4f0dbSShawn McCarney                         }
2381028f7ebcSEddie James                     }
2382de629b6eSShawn McCarney                     else
2383de629b6eSShawn McCarney                     {
2384de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2385de629b6eSShawn McCarney                                          << sensorType;
2386de629b6eSShawn McCarney                         continue;
2387de629b6eSShawn McCarney                     }
2388de629b6eSShawn McCarney 
2389de629b6eSShawn McCarney                     nlohmann::json& tempArray =
23908d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2391adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
239249c53ac9SJohnathan Mantey                     {
2393adc4f0dbSShawn McCarney                         if (tempArray.empty())
23947ab06f49SGunnar Mills                         {
239595a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
239695a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
239795a3ecadSAnthony Wilson                             // naming in power.hpp.
23981476687dSEd Tanous                             nlohmann::json::object_t power;
23991476687dSEd Tanous                             power["@odata.id"] =
2400adc4f0dbSShawn McCarney                                 "/redfish/v1/Chassis/" +
240181ce609eSEd Tanous                                 sensorsAsyncResp->chassisId + "/" +
240281ce609eSEd Tanous                                 sensorsAsyncResp->chassisSubNode + "#/" +
24031476687dSEd Tanous                                 fieldName + "/0";
24041476687dSEd Tanous                             tempArray.push_back(std::move(power));
2405adc4f0dbSShawn McCarney                         }
2406adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2407adc4f0dbSShawn McCarney                     }
2408adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2409adc4f0dbSShawn McCarney                     {
2410adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2411adc4f0dbSShawn McCarney                         {
2412adc4f0dbSShawn McCarney                             sensorJson =
2413adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
241481ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2415adc4f0dbSShawn McCarney                         }
241649c53ac9SJohnathan Mantey                     }
2417928fefb9SNan Zhou                     else if (fieldName == "Members")
2418928fefb9SNan Zhou                     {
2419677bb756SEd Tanous                         std::string sensorTypeEscaped(sensorType);
2420677bb756SEd Tanous                         sensorTypeEscaped.erase(
2421677bb756SEd Tanous                             std::remove(sensorTypeEscaped.begin(),
2422677bb756SEd Tanous                                         sensorTypeEscaped.end(), '_'),
2423677bb756SEd Tanous                             sensorTypeEscaped.end());
2424677bb756SEd Tanous                         std::string sensorId(sensorTypeEscaped);
2425677bb756SEd Tanous                         sensorId += "_";
2426677bb756SEd Tanous                         sensorId += sensorName;
2427677bb756SEd Tanous 
24281476687dSEd Tanous                         nlohmann::json::object_t member;
2429677bb756SEd Tanous                         member["@odata.id"] = crow::utility::urlFromPieces(
2430677bb756SEd Tanous                             "redfish", "v1", "Chassis",
2431677bb756SEd Tanous                             sensorsAsyncResp->chassisId,
2432677bb756SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
24331476687dSEd Tanous                         tempArray.push_back(std::move(member));
2434928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2435928fefb9SNan Zhou                     }
243649c53ac9SJohnathan Mantey                     else
243749c53ac9SJohnathan Mantey                     {
24381476687dSEd Tanous                         nlohmann::json::object_t member;
24391476687dSEd Tanous                         member["@odata.id"] = "/redfish/v1/Chassis/" +
24401476687dSEd Tanous                                               sensorsAsyncResp->chassisId +
24411476687dSEd Tanous                                               "/" +
24421476687dSEd Tanous                                               sensorsAsyncResp->chassisSubNode +
24431476687dSEd Tanous                                               "#/" + fieldName + "/";
24441476687dSEd Tanous                         tempArray.push_back(std::move(member));
2445adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
244649c53ac9SJohnathan Mantey                     }
244795a3ecadSAnthony Wilson                 }
2448de629b6eSShawn McCarney 
2449adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2450adc4f0dbSShawn McCarney                 {
24511d7c0054SEd Tanous                     objectInterfacesToJson(sensorName, sensorType,
24521d7c0054SEd Tanous                                            sensorsAsyncResp->chassisSubNode,
24531d7c0054SEd Tanous                                            objDictEntry.second, *sensorJson,
24541d7c0054SEd Tanous                                            inventoryItem);
24551d7c0054SEd Tanous 
24561d7c0054SEd Tanous                     std::string path = "/xyz/openbmc_project/sensors/";
24571d7c0054SEd Tanous                     path += sensorType;
24581d7c0054SEd Tanous                     path += "/";
24591d7c0054SEd Tanous                     path += sensorName;
2460c1d019a6SEd Tanous                     sensorsAsyncResp->addMetadata(*sensorJson, path);
2461adc4f0dbSShawn McCarney                 }
2462de629b6eSShawn McCarney             }
246381ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
246449c53ac9SJohnathan Mantey             {
246581ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2466928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2467928fefb9SNan Zhou                         sensors::node::sensors &&
2468928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2469928fefb9SNan Zhou                 {
2470928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2471928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2472928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2473928fefb9SNan Zhou                             .size();
2474928fefb9SNan Zhou                 }
2475928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2476928fefb9SNan Zhou                          sensors::node::thermal)
24778bd25ccdSJames Feist                 {
247881ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
24798bd25ccdSJames Feist                 }
248049c53ac9SJohnathan Mantey             }
2481de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2482de629b6eSShawn McCarney         };
2483de629b6eSShawn McCarney 
2484de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2485*d0090733SEd Tanous             getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
2486de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
248723a21a1cSEd Tanous     }
2488de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2489de629b6eSShawn McCarney }
2490de629b6eSShawn McCarney 
2491fe04d49cSNan Zhou inline void
2492fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2493fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
24941abe55efSEd Tanous {
2495fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2496fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
249755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2498adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2499*d0090733SEd Tanous             [sensorsAsyncResp, sensorNames,
2500*d0090733SEd Tanous              connections](const std::shared_ptr<std::vector<InventoryItem>>&
2501adc4f0dbSShawn McCarney                               inventoryItems) {
2502adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
250349c53ac9SJohnathan Mantey             // Get sensor data and store results in JSON
2504002d39b4SEd Tanous             getSensorData(sensorsAsyncResp, sensorNames, connections,
2505*d0090733SEd Tanous                           inventoryItems);
2506adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2507adc4f0dbSShawn McCarney         };
2508adc4f0dbSShawn McCarney 
2509adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2510*d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2511adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2512adc4f0dbSShawn McCarney 
251355c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb exit";
251408777fb0SLewanczyk, Dawid     };
2515de629b6eSShawn McCarney 
2516de629b6eSShawn McCarney     // Get set of connections that provide sensor values
251781ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
251895a3ecadSAnthony Wilson }
251995a3ecadSAnthony Wilson 
252095a3ecadSAnthony Wilson /**
252195a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
252295a3ecadSAnthony Wilson  *        chassis.
252395a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
252495a3ecadSAnthony Wilson  */
2525b5a76932SEd Tanous inline void
252681ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
252795a3ecadSAnthony Wilson {
252895a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
252995a3ecadSAnthony Wilson     auto getChassisCb =
253081ce609eSEd Tanous         [sensorsAsyncResp](
2531fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
253295a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "getChassisCb enter";
253381ce609eSEd Tanous         processSensorList(sensorsAsyncResp, sensorNames);
253455c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassisCb exit";
253508777fb0SLewanczyk, Dawid     };
2536928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2537928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2538928fefb9SNan Zhou     {
25398d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
25408d1b46d7Szhanghch05             nlohmann::json::array();
2541928fefb9SNan Zhou     }
254226f03899SShawn McCarney     // Get set of sensors in chassis
25437f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
25447f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
25457f1cc26dSEd Tanous                std::move(getChassisCb));
254655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2547271584abSEd Tanous }
254808777fb0SLewanczyk, Dawid 
2549413961deSRichard Marian Thomaiyar /**
255049c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
255149c53ac9SJohnathan Mantey  * the chassis node
255249c53ac9SJohnathan Mantey  *
255349c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
255449c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
255549c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
255649c53ac9SJohnathan Mantey  *                         repeated calls to this function
255749c53ac9SJohnathan Mantey  */
2558fe04d49cSNan Zhou inline bool
2559fe04d49cSNan Zhou     findSensorNameUsingSensorPath(std::string_view sensorName,
256002cad96eSEd Tanous                                   const std::set<std::string>& sensorsList,
2561fe04d49cSNan Zhou                                   std::set<std::string>& sensorsModified)
256249c53ac9SJohnathan Mantey {
2563fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
256449c53ac9SJohnathan Mantey     {
256528aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2566b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
256728aa8de5SGeorge Liu         if (thisSensorName.empty())
256849c53ac9SJohnathan Mantey         {
256949c53ac9SJohnathan Mantey             continue;
257049c53ac9SJohnathan Mantey         }
257149c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
257249c53ac9SJohnathan Mantey         {
257349c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
257449c53ac9SJohnathan Mantey             return true;
257549c53ac9SJohnathan Mantey         }
257649c53ac9SJohnathan Mantey     }
257749c53ac9SJohnathan Mantey     return false;
257849c53ac9SJohnathan Mantey }
257949c53ac9SJohnathan Mantey 
258049c53ac9SJohnathan Mantey /**
2581413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2582413961deSRichard Marian Thomaiyar  *
25838d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
25844bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2585413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2586413961deSRichard Marian Thomaiyar  */
258723a21a1cSEd Tanous inline void setSensorsOverride(
2588b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
25894bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2590397fd61fSjayaprakash Mutyala         allCollections)
2591413961deSRichard Marian Thomaiyar {
259270d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
25934bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2594413961deSRichard Marian Thomaiyar 
2595543f4400SEd Tanous     const char* propertyValueName = nullptr;
2596f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2597413961deSRichard Marian Thomaiyar     std::string memberId;
2598543f4400SEd Tanous     double value = 0.0;
2599f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2600f65af9e8SRichard Marian Thomaiyar     {
2601f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2602f65af9e8SRichard Marian Thomaiyar         {
2603f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2604f65af9e8SRichard Marian Thomaiyar         }
2605f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2606f65af9e8SRichard Marian Thomaiyar         {
2607f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2608f65af9e8SRichard Marian Thomaiyar         }
2609f65af9e8SRichard Marian Thomaiyar         else
2610f65af9e8SRichard Marian Thomaiyar         {
2611f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2612f65af9e8SRichard Marian Thomaiyar         }
2613f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2614f65af9e8SRichard Marian Thomaiyar         {
26158d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
26168d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
26178d1b46d7Szhanghch05                                      value))
2618413961deSRichard Marian Thomaiyar             {
2619413961deSRichard Marian Thomaiyar                 return;
2620413961deSRichard Marian Thomaiyar             }
2621f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2622f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2623f65af9e8SRichard Marian Thomaiyar         }
2624f65af9e8SRichard Marian Thomaiyar     }
26254bb3dc34SCarol Wang 
2626002d39b4SEd Tanous     auto getChassisSensorListCb =
2627002d39b4SEd Tanous         [sensorAsyncResp, overrideMap](
2628fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorsList) {
262949c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
263049c53ac9SJohnathan Mantey         // chassis node
2631fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2632fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2633f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2634413961deSRichard Marian Thomaiyar         {
2635f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
263649c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
263749c53ac9SJohnathan Mantey                                                *sensorNames))
2638f65af9e8SRichard Marian Thomaiyar             {
2639f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
26408d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2641f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2642413961deSRichard Marian Thomaiyar                 return;
2643413961deSRichard Marian Thomaiyar             }
2644f65af9e8SRichard Marian Thomaiyar         }
2645413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2646002d39b4SEd Tanous         auto getObjectsWithConnectionCb =
2647fe04d49cSNan Zhou             [sensorAsyncResp,
2648fe04d49cSNan Zhou              overrideMap](const std::set<std::string>& /*connections*/,
2649002d39b4SEd Tanous                           const std::set<std::pair<std::string, std::string>>&
2650413961deSRichard Marian Thomaiyar                               objectsWithConnection) {
2651f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2652413961deSRichard Marian Thomaiyar             {
2653413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2654f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2655f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2656f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
26574f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2658a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2659a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2660413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2661413961deSRichard Marian Thomaiyar                                                : "Voltages",
2662f65af9e8SRichard Marian Thomaiyar                                            "Count");
2663f65af9e8SRichard Marian Thomaiyar                 return;
2664f65af9e8SRichard Marian Thomaiyar             }
2665f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2666f65af9e8SRichard Marian Thomaiyar             {
266728aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
266828aa8de5SGeorge Liu                 std::string sensorName = path.filename();
266928aa8de5SGeorge Liu                 if (sensorName.empty())
2670f65af9e8SRichard Marian Thomaiyar                 {
26714f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2672f65af9e8SRichard Marian Thomaiyar                     return;
2673f65af9e8SRichard Marian Thomaiyar                 }
2674f65af9e8SRichard Marian Thomaiyar 
2675f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2676f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2677f65af9e8SRichard Marian Thomaiyar                 {
2678f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2679f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
26804f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2681413961deSRichard Marian Thomaiyar                     return;
2682413961deSRichard Marian Thomaiyar                 }
2683413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2684f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2685413961deSRichard Marian Thomaiyar                     if (ec)
2686413961deSRichard Marian Thomaiyar                     {
26874f277b54SJayaprakash Mutyala                         if (ec.value() ==
26884f277b54SJayaprakash Mutyala                             boost::system::errc::permission_denied)
26894f277b54SJayaprakash Mutyala                         {
26904f277b54SJayaprakash Mutyala                             BMCWEB_LOG_WARNING
26914f277b54SJayaprakash Mutyala                                 << "Manufacturing mode is not Enabled...can't "
26924f277b54SJayaprakash Mutyala                                    "Override the sensor value. ";
26934f277b54SJayaprakash Mutyala 
26944f277b54SJayaprakash Mutyala                             messages::insufficientPrivilege(
26958d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
2696413961deSRichard Marian Thomaiyar                             return;
2697413961deSRichard Marian Thomaiyar                         }
26984f277b54SJayaprakash Mutyala                         BMCWEB_LOG_DEBUG
26994f277b54SJayaprakash Mutyala                             << "setOverrideValueStatus DBUS error: " << ec;
27004f277b54SJayaprakash Mutyala                         messages::internalError(
27014f277b54SJayaprakash Mutyala                             sensorAsyncResp->asyncResp->res);
27024f277b54SJayaprakash Mutyala                     }
2703413961deSRichard Marian Thomaiyar                     },
27044f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
27054f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2706168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2707f65af9e8SRichard Marian Thomaiyar             }
2708413961deSRichard Marian Thomaiyar         };
2709413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2710413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2711413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2712413961deSRichard Marian Thomaiyar     };
2713413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
27147f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
27157f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
27167f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2717413961deSRichard Marian Thomaiyar }
2718413961deSRichard Marian Thomaiyar 
2719a0ec28b6SAdrian Ambrożewicz /**
2720a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2721a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2722a0ec28b6SAdrian Ambrożewicz  *
2723a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2724a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2725a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2726a0ec28b6SAdrian Ambrożewicz  *
2727a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2728a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2729a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2730a0ec28b6SAdrian Ambrożewicz  */
2731021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2732021d32cfSKrzysztof Grobelny                                  const std::string& node,
2733a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2734a0ec28b6SAdrian Ambrożewicz {
273502da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
273602da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
273702da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
273802da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2739a0ec28b6SAdrian Ambrożewicz     {
2740a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2741a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2742a0ec28b6SAdrian Ambrożewicz         return;
2743a0ec28b6SAdrian Ambrożewicz     }
2744d51e072fSKrzysztof Grobelny 
274572374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2746fe04d49cSNan Zhou     auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2747a0ec28b6SAdrian Ambrożewicz                         const boost::beast::http::status status,
2748fe04d49cSNan Zhou                         const std::map<std::string, std::string>& uriToDbus) {
2749fe04d49cSNan Zhou         mapCompleteCb(status, uriToDbus);
2750fe04d49cSNan Zhou     };
2751a0ec28b6SAdrian Ambrożewicz 
2752a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2753d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2754a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2755a0ec28b6SAdrian Ambrożewicz }
2756a0ec28b6SAdrian Ambrożewicz 
2757bacb2162SNan Zhou namespace sensors
2758bacb2162SNan Zhou {
2759928fefb9SNan Zhou 
2760bacb2162SNan Zhou inline void getChassisCallback(
2761c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2762c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2763fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2764bacb2162SNan Zhou {
2765bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
2766bacb2162SNan Zhou 
2767c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2768c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2769bacb2162SNan Zhou     {
2770bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2771bacb2162SNan Zhou 
2772bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2773bacb2162SNan Zhou         std::string sensorName = path.filename();
2774bacb2162SNan Zhou         if (sensorName.empty())
2775bacb2162SNan Zhou         {
2776bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2777c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2778bacb2162SNan Zhou             return;
2779bacb2162SNan Zhou         }
2780c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
2781c1d019a6SEd Tanous         // fan_tach has an underscore in it, so remove it to "normalize" the
2782c1d019a6SEd Tanous         // type in the URI
2783c1d019a6SEd Tanous         type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2784c1d019a6SEd Tanous 
27851476687dSEd Tanous         nlohmann::json::object_t member;
2786c1d019a6SEd Tanous         std::string id = type;
2787c1d019a6SEd Tanous         id += "_";
2788c1d019a6SEd Tanous         id += sensorName;
2789c1d019a6SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
2790c1d019a6SEd Tanous             "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2791c1d019a6SEd Tanous 
27921476687dSEd Tanous         entriesArray.push_back(std::move(member));
2793bacb2162SNan Zhou     }
2794bacb2162SNan Zhou 
2795c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
2796bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2797bacb2162SNan Zhou }
2798e6bd846dSNan Zhou 
2799de167a6fSNan Zhou inline void
2800de167a6fSNan Zhou     handleSensorCollectionGet(App& app, const crow::Request& req,
2801de167a6fSNan Zhou                               const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2802de167a6fSNan Zhou                               const std::string& chassisId)
2803de167a6fSNan Zhou {
2804de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2805de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2806de167a6fSNan Zhou     };
2807de167a6fSNan Zhou     query_param::Query delegatedQuery;
28083ba00073SCarson Labrado     if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
2809de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2810de167a6fSNan Zhou     {
2811de167a6fSNan Zhou         return;
2812de167a6fSNan Zhou     }
2813de167a6fSNan Zhou 
2814de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2815de167a6fSNan Zhou     {
2816de167a6fSNan Zhou         // we perform efficient expand.
2817de167a6fSNan Zhou         auto asyncResp = std::make_shared<SensorsAsyncResp>(
2818de167a6fSNan Zhou             aResp, chassisId, sensors::dbus::sensorPaths,
2819de167a6fSNan Zhou             sensors::node::sensors,
2820de167a6fSNan Zhou             /*efficientExpand=*/true);
2821de167a6fSNan Zhou         getChassisData(asyncResp);
2822de167a6fSNan Zhou 
2823de167a6fSNan Zhou         BMCWEB_LOG_DEBUG
2824de167a6fSNan Zhou             << "SensorCollection doGet exit via efficient expand handler";
2825de167a6fSNan Zhou         return;
28260bad320cSEd Tanous     }
2827de167a6fSNan Zhou 
2828de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2829de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
2830c1d019a6SEd Tanous     getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2831c1d019a6SEd Tanous                std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2832c1d019a6SEd Tanous                                sensors::node::sensors));
2833c1d019a6SEd Tanous }
28347f1cc26dSEd Tanous 
2835c1d019a6SEd Tanous inline void
2836c1d019a6SEd Tanous     getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2837c1d019a6SEd Tanous                       const std::string& sensorPath,
2838c1d019a6SEd Tanous                       const ::dbus::utility::MapperGetObject& mapperResponse)
2839c1d019a6SEd Tanous {
2840c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2841c1d019a6SEd Tanous     {
2842c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2843c1d019a6SEd Tanous         return;
2844c1d019a6SEd Tanous     }
2845c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2846c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
2847c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2848c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Path " << sensorPath;
2849c1343bf6SKrzysztof Grobelny 
2850c1343bf6SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2851c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2852c1d019a6SEd Tanous         [asyncResp,
2853c1d019a6SEd Tanous          sensorPath](const boost::system::error_code ec,
2854c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2855c1d019a6SEd Tanous         if (ec)
2856c1d019a6SEd Tanous         {
2857c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2858c1d019a6SEd Tanous             return;
2859c1d019a6SEd Tanous         }
2860c1d019a6SEd Tanous         sdbusplus::message::object_path path(sensorPath);
2861c1d019a6SEd Tanous         std::string name = path.filename();
2862c1d019a6SEd Tanous         path = path.parent_path();
2863c1d019a6SEd Tanous         std::string type = path.filename();
2864c1d019a6SEd Tanous         objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2865c1d019a6SEd Tanous                                asyncResp->res.jsonValue, nullptr);
2866c1343bf6SKrzysztof Grobelny         });
2867de167a6fSNan Zhou }
2868de167a6fSNan Zhou 
2869e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2870c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2871677bb756SEd Tanous                             const std::string& chassisId,
2872c1d019a6SEd Tanous                             const std::string& sensorId)
2873e6bd846dSNan Zhou {
2874c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2875e6bd846dSNan Zhou     {
2876e6bd846dSNan Zhou         return;
2877e6bd846dSNan Zhou     }
2878c1d019a6SEd Tanous     size_t index = sensorId.find('_');
2879c1d019a6SEd Tanous     if (index == std::string::npos)
2880c1d019a6SEd Tanous     {
2881c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2882c1d019a6SEd Tanous         return;
2883c1d019a6SEd Tanous     }
2884677bb756SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2885677bb756SEd Tanous         "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
2886c1d019a6SEd Tanous     std::string sensorType = sensorId.substr(0, index);
2887c1d019a6SEd Tanous     std::string sensorName = sensorId.substr(index + 1);
2888c1d019a6SEd Tanous     // fan_pwm and fan_tach need special handling
2889c1d019a6SEd Tanous     if (sensorType == "fantach" || sensorType == "fanpwm")
2890c1d019a6SEd Tanous     {
2891c1d019a6SEd Tanous         sensorType.insert(3, 1, '_');
2892c1d019a6SEd Tanous     }
2893c1d019a6SEd Tanous 
2894e6bd846dSNan Zhou     BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2895e6bd846dSNan Zhou 
2896e6bd846dSNan Zhou     const std::array<const char*, 1> interfaces = {
2897e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2898c1d019a6SEd Tanous     std::string sensorPath =
2899c1d019a6SEd Tanous         "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName;
2900e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2901e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
2902e6bd846dSNan Zhou     crow::connections::systemBus->async_method_call(
2903c1d019a6SEd Tanous         [asyncResp, sensorPath,
2904e6bd846dSNan Zhou          sensorName](const boost::system::error_code ec,
2905c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
2906e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 enter";
2907e6bd846dSNan Zhou         if (ec)
2908e6bd846dSNan Zhou         {
2909c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2910e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2911e6bd846dSNan Zhou                              << "Dbus error " << ec;
2912e6bd846dSNan Zhou             return;
2913e6bd846dSNan Zhou         }
2914c1d019a6SEd Tanous         getSensorFromDbus(asyncResp, sensorPath, subtree);
2915e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 exit";
2916e6bd846dSNan Zhou         },
2917e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper",
2918e6bd846dSNan Zhou         "/xyz/openbmc_project/object_mapper",
2919c1d019a6SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
2920c1d019a6SEd Tanous         interfaces);
2921e6bd846dSNan Zhou }
2922e6bd846dSNan Zhou 
2923bacb2162SNan Zhou } // namespace sensors
2924bacb2162SNan Zhou 
29257e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
292695a3ecadSAnthony Wilson {
29277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2928ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2929002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2930de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
293195a3ecadSAnthony Wilson }
293295a3ecadSAnthony Wilson 
29337e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
293495a3ecadSAnthony Wilson {
29357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2936ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2937002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2938e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
293995a3ecadSAnthony Wilson }
294095a3ecadSAnthony Wilson 
294108777fb0SLewanczyk, Dawid } // namespace redfish
2942