xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 7a1dbc4803bf78bfc0c574e6676b3c5def4cdae3)
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 
18*7a1dbc48SGeorge Liu #include "dbus_utility.hpp"
190ec8b83dSEd Tanous #include "generated/enums/sensor.hpp"
200ec8b83dSEd Tanous 
217e860f15SJohn Edward Broadbent #include <app.hpp>
2211ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
231d7c0054SEd Tanous #include <boost/algorithm/string/find.hpp>
241d7c0054SEd Tanous #include <boost/algorithm/string/predicate.hpp>
25c71d6125SEd Tanous #include <boost/algorithm/string/replace.hpp>
2608777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2708777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
281abe55efSEd Tanous #include <dbus_singleton.hpp>
29168e20c1SEd Tanous #include <dbus_utility.hpp>
3045ca1b86SEd Tanous #include <query.hpp>
31ed398213SEd Tanous #include <registries/privilege_registry.hpp>
321e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
3386d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
3486d89ed7SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
35413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
36928fefb9SNan Zhou #include <utils/query_param.hpp>
371214b7e7SGunnar Mills 
38*7a1dbc48SGeorge Liu #include <array>
391214b7e7SGunnar Mills #include <cmath>
40fe04d49cSNan Zhou #include <iterator>
41fe04d49cSNan Zhou #include <map>
42fe04d49cSNan Zhou #include <set>
43*7a1dbc48SGeorge Liu #include <string_view>
44b5a76932SEd Tanous #include <utility>
45abf2add6SEd Tanous #include <variant>
4608777fb0SLewanczyk, Dawid 
471abe55efSEd Tanous namespace redfish
481abe55efSEd Tanous {
4908777fb0SLewanczyk, Dawid 
50a0ec28b6SAdrian Ambrożewicz namespace sensors
51a0ec28b6SAdrian Ambrożewicz {
52a0ec28b6SAdrian Ambrożewicz namespace node
53a0ec28b6SAdrian Ambrożewicz {
54a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
55a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
56a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
57a0ec28b6SAdrian Ambrożewicz } // namespace node
58a0ec28b6SAdrian Ambrożewicz 
5902da7c5aSEd Tanous // clang-format off
60a0ec28b6SAdrian Ambrożewicz namespace dbus
61a0ec28b6SAdrian Ambrożewicz {
624ee8e211SEd Tanous static auto powerPaths = std::to_array<std::string_view>({
6302da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
6402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
6502da7c5aSEd Tanous });
66c2bf7f99SWludzik, Jozef 
674ee8e211SEd Tanous static auto sensorPaths = std::to_array<std::string_view>({
6802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power",
69a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/current",
707088690cSBasheer Ahmed Muddebihal     "/xyz/openbmc_project/sensors/airflow",
715deabed9SGunnar Mills     "/xyz/openbmc_project/sensors/humidity",
72e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
73e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/voltage",
74e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_tach",
75e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/temperature",
76e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_pwm",
77e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/altitude",
78e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/energy",
79e8204933SGeorge Liu #endif
8002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/utilization"
8102da7c5aSEd Tanous });
8202da7c5aSEd Tanous 
834ee8e211SEd Tanous static auto thermalPaths = std::to_array<std::string_view>({
8402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
85a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
8602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
8702da7c5aSEd Tanous });
8802da7c5aSEd Tanous 
89c2bf7f99SWludzik, Jozef } // namespace dbus
9002da7c5aSEd Tanous // clang-format on
9102da7c5aSEd Tanous 
9202da7c5aSEd Tanous using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
9302da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
9402da7c5aSEd Tanous     {{node::power, std::span<std::string_view>(dbus::powerPaths)},
9502da7c5aSEd Tanous      {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
9602da7c5aSEd Tanous      {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
97c2bf7f99SWludzik, Jozef 
980ec8b83dSEd Tanous inline sensor::ReadingType toReadingType(std::string_view sensorType)
99c2bf7f99SWludzik, Jozef {
100c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
101c2bf7f99SWludzik, Jozef     {
1020ec8b83dSEd Tanous         return sensor::ReadingType::Voltage;
103c2bf7f99SWludzik, Jozef     }
104c2bf7f99SWludzik, Jozef     if (sensorType == "power")
105c2bf7f99SWludzik, Jozef     {
1060ec8b83dSEd Tanous         return sensor::ReadingType::Power;
107c2bf7f99SWludzik, Jozef     }
108c2bf7f99SWludzik, Jozef     if (sensorType == "current")
109c2bf7f99SWludzik, Jozef     {
1100ec8b83dSEd Tanous         return sensor::ReadingType::Current;
111c2bf7f99SWludzik, Jozef     }
112c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
113c2bf7f99SWludzik, Jozef     {
1140ec8b83dSEd Tanous         return sensor::ReadingType::Rotational;
115c2bf7f99SWludzik, Jozef     }
116c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
117c2bf7f99SWludzik, Jozef     {
1180ec8b83dSEd Tanous         return sensor::ReadingType::Temperature;
119c2bf7f99SWludzik, Jozef     }
120c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
121c2bf7f99SWludzik, Jozef     {
1220ec8b83dSEd Tanous         return sensor::ReadingType::Percent;
123c2bf7f99SWludzik, Jozef     }
1245deabed9SGunnar Mills     if (sensorType == "humidity")
1255deabed9SGunnar Mills     {
1260ec8b83dSEd Tanous         return sensor::ReadingType::Humidity;
1275deabed9SGunnar Mills     }
128c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
129c2bf7f99SWludzik, Jozef     {
1300ec8b83dSEd Tanous         return sensor::ReadingType::Altitude;
131c2bf7f99SWludzik, Jozef     }
132c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
133c2bf7f99SWludzik, Jozef     {
1340ec8b83dSEd Tanous         return sensor::ReadingType::AirFlow;
135c2bf7f99SWludzik, Jozef     }
136c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
137c2bf7f99SWludzik, Jozef     {
1380ec8b83dSEd Tanous         return sensor::ReadingType::EnergyJoules;
139c2bf7f99SWludzik, Jozef     }
1400ec8b83dSEd Tanous     return sensor::ReadingType::Invalid;
141c2bf7f99SWludzik, Jozef }
142c2bf7f99SWludzik, Jozef 
1431d7c0054SEd Tanous inline std::string_view toReadingUnits(std::string_view sensorType)
144c2bf7f99SWludzik, Jozef {
145c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
146c2bf7f99SWludzik, Jozef     {
147c2bf7f99SWludzik, Jozef         return "V";
148c2bf7f99SWludzik, Jozef     }
149c2bf7f99SWludzik, Jozef     if (sensorType == "power")
150c2bf7f99SWludzik, Jozef     {
151c2bf7f99SWludzik, Jozef         return "W";
152c2bf7f99SWludzik, Jozef     }
153c2bf7f99SWludzik, Jozef     if (sensorType == "current")
154c2bf7f99SWludzik, Jozef     {
155c2bf7f99SWludzik, Jozef         return "A";
156c2bf7f99SWludzik, Jozef     }
157c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
158c2bf7f99SWludzik, Jozef     {
159c2bf7f99SWludzik, Jozef         return "RPM";
160c2bf7f99SWludzik, Jozef     }
161c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
162c2bf7f99SWludzik, Jozef     {
163c2bf7f99SWludzik, Jozef         return "Cel";
164c2bf7f99SWludzik, Jozef     }
1655deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
1665deabed9SGunnar Mills         sensorType == "humidity")
167c2bf7f99SWludzik, Jozef     {
168c2bf7f99SWludzik, Jozef         return "%";
169c2bf7f99SWludzik, Jozef     }
170c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
171c2bf7f99SWludzik, Jozef     {
172c2bf7f99SWludzik, Jozef         return "m";
173c2bf7f99SWludzik, Jozef     }
174c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
175c2bf7f99SWludzik, Jozef     {
176c2bf7f99SWludzik, Jozef         return "cft_i/min";
177c2bf7f99SWludzik, Jozef     }
178c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
179c2bf7f99SWludzik, Jozef     {
180c2bf7f99SWludzik, Jozef         return "J";
181c2bf7f99SWludzik, Jozef     }
182c2bf7f99SWludzik, Jozef     return "";
183a0ec28b6SAdrian Ambrożewicz }
184a0ec28b6SAdrian Ambrożewicz } // namespace sensors
185a0ec28b6SAdrian Ambrożewicz 
18608777fb0SLewanczyk, Dawid /**
187588c3f0dSKowalski, Kamil  * SensorsAsyncResp
18808777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
18908777fb0SLewanczyk, Dawid  */
1901abe55efSEd Tanous class SensorsAsyncResp
1911abe55efSEd Tanous {
19208777fb0SLewanczyk, Dawid   public:
193a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
194a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
195fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
196a0ec28b6SAdrian Ambrożewicz 
197a0ec28b6SAdrian Ambrożewicz     struct SensorData
198a0ec28b6SAdrian Ambrożewicz     {
199a0ec28b6SAdrian Ambrożewicz         const std::string name;
200a0ec28b6SAdrian Ambrożewicz         std::string uri;
201a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
202a0ec28b6SAdrian Ambrożewicz     };
203a0ec28b6SAdrian Ambrożewicz 
2048a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2058d1b46d7Szhanghch05                      const std::string& chassisIdIn,
20602da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
20702da7c5aSEd Tanous                      std::string_view subNode) :
2088a592810SEd Tanous         asyncResp(asyncRespIn),
209928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
210928fefb9SNan Zhou         efficientExpand(false)
2111214b7e7SGunnar Mills     {}
21208777fb0SLewanczyk, Dawid 
213a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
2148a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2158d1b46d7Szhanghch05                      const std::string& chassisIdIn,
21602da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
21702da7c5aSEd Tanous                      std::string_view subNode,
218a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
2198a592810SEd Tanous         asyncResp(asyncRespIn),
220928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
221928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
222a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
223a0ec28b6SAdrian Ambrożewicz     {}
224a0ec28b6SAdrian Ambrożewicz 
225928fefb9SNan Zhou     // sensor collections expand
2268a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
227928fefb9SNan Zhou                      const std::string& chassisIdIn,
22802da7c5aSEd Tanous                      const std::span<std::string_view> typesIn,
2298a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
2308a592810SEd Tanous         asyncResp(asyncRespIn),
231928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
2328a592810SEd Tanous         efficientExpand(efficientExpandIn)
233928fefb9SNan Zhou     {}
234928fefb9SNan Zhou 
2351abe55efSEd Tanous     ~SensorsAsyncResp()
2361abe55efSEd Tanous     {
2378d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2388d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2391abe55efSEd Tanous         {
2401abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2411abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2421abe55efSEd Tanous             // proper code
2438d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
24408777fb0SLewanczyk, Dawid         }
245a0ec28b6SAdrian Ambrożewicz 
246a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
247a0ec28b6SAdrian Ambrożewicz         {
248fe04d49cSNan Zhou             std::map<std::string, std::string> map;
2498d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
250a0ec28b6SAdrian Ambrożewicz             {
251a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
252a0ec28b6SAdrian Ambrożewicz                 {
253c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
254a0ec28b6SAdrian Ambrożewicz                 }
255a0ec28b6SAdrian Ambrożewicz             }
2568d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
257a0ec28b6SAdrian Ambrożewicz         }
25808777fb0SLewanczyk, Dawid     }
259588c3f0dSKowalski, Kamil 
260ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
261ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
262ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
263ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
264ecd6a3a2SEd Tanous 
265a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
266c1d019a6SEd Tanous                      const std::string& dbusPath)
267a0ec28b6SAdrian Ambrożewicz     {
268a0ec28b6SAdrian Ambrożewicz         if (metadata)
269a0ec28b6SAdrian Ambrożewicz         {
270c1d019a6SEd Tanous             metadata->emplace_back(SensorData{
271c1d019a6SEd Tanous                 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
272a0ec28b6SAdrian Ambrożewicz         }
273a0ec28b6SAdrian Ambrożewicz     }
274a0ec28b6SAdrian Ambrożewicz 
275a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
276a0ec28b6SAdrian Ambrożewicz     {
277a0ec28b6SAdrian Ambrożewicz         if (metadata)
278a0ec28b6SAdrian Ambrożewicz         {
279a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
280a0ec28b6SAdrian Ambrożewicz             {
281a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
282a0ec28b6SAdrian Ambrożewicz                 {
283a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
284a0ec28b6SAdrian Ambrożewicz                 }
285a0ec28b6SAdrian Ambrożewicz             }
286a0ec28b6SAdrian Ambrożewicz         }
287a0ec28b6SAdrian Ambrożewicz     }
288a0ec28b6SAdrian Ambrożewicz 
2898d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
290a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
29102da7c5aSEd Tanous     const std::span<std::string_view> types;
292a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
293928fefb9SNan Zhou     const bool efficientExpand;
294a0ec28b6SAdrian Ambrożewicz 
295a0ec28b6SAdrian Ambrożewicz   private:
296a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
297a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
29808777fb0SLewanczyk, Dawid };
29908777fb0SLewanczyk, Dawid 
30008777fb0SLewanczyk, Dawid /**
301d500549bSAnthony Wilson  * Possible states for physical inventory leds
302d500549bSAnthony Wilson  */
303d500549bSAnthony Wilson enum class LedState
304d500549bSAnthony Wilson {
305d500549bSAnthony Wilson     OFF,
306d500549bSAnthony Wilson     ON,
307d500549bSAnthony Wilson     BLINK,
308d500549bSAnthony Wilson     UNKNOWN
309d500549bSAnthony Wilson };
310d500549bSAnthony Wilson 
311d500549bSAnthony Wilson /**
312adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
313adc4f0dbSShawn McCarney  */
314adc4f0dbSShawn McCarney class InventoryItem
315adc4f0dbSShawn McCarney {
316adc4f0dbSShawn McCarney   public:
3174e23a444SEd Tanous     explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
318adc4f0dbSShawn McCarney     {
319adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
32028aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
32128aa8de5SGeorge Liu         name = path.filename();
32228aa8de5SGeorge Liu         if (name.empty())
323adc4f0dbSShawn McCarney         {
32428aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
325adc4f0dbSShawn McCarney         }
326adc4f0dbSShawn McCarney     }
327adc4f0dbSShawn McCarney 
328adc4f0dbSShawn McCarney     std::string objectPath;
329adc4f0dbSShawn McCarney     std::string name;
330e05aec50SEd Tanous     bool isPresent = true;
331e05aec50SEd Tanous     bool isFunctional = true;
332e05aec50SEd Tanous     bool isPowerSupply = false;
333e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
334adc4f0dbSShawn McCarney     std::string manufacturer;
335adc4f0dbSShawn McCarney     std::string model;
336adc4f0dbSShawn McCarney     std::string partNumber;
337adc4f0dbSShawn McCarney     std::string serialNumber;
338adc4f0dbSShawn McCarney     std::set<std::string> sensors;
339d500549bSAnthony Wilson     std::string ledObjectPath;
340e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
341adc4f0dbSShawn McCarney };
342adc4f0dbSShawn McCarney 
343adc4f0dbSShawn McCarney /**
344413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
345588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
34608777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
34708777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
34808777fb0SLewanczyk, Dawid  */
34908777fb0SLewanczyk, Dawid template <typename Callback>
350413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
35181ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
352fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
3531abe55efSEd Tanous     Callback&& callback)
3541abe55efSEd Tanous {
355413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
35603b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
35708777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
35808777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
35908777fb0SLewanczyk, Dawid 
36008777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
361002d39b4SEd Tanous     auto respHandler =
362002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
363002d39b4SEd Tanous          sensorNames](const boost::system::error_code ec,
364002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
365413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3661abe55efSEd Tanous         if (ec)
3671abe55efSEd Tanous         {
3688d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
369413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
370413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
37108777fb0SLewanczyk, Dawid             return;
37208777fb0SLewanczyk, Dawid         }
37308777fb0SLewanczyk, Dawid 
37455c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
37508777fb0SLewanczyk, Dawid 
37608777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
37708777fb0SLewanczyk, Dawid         // found in the chassis
378fe04d49cSNan Zhou         std::set<std::string> connections;
379413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
38008777fb0SLewanczyk, Dawid 
38149c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
38249c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3831abe55efSEd Tanous         {
38455c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
38508777fb0SLewanczyk, Dawid         }
38608777fb0SLewanczyk, Dawid 
38708777fb0SLewanczyk, Dawid         for (const std::pair<
38808777fb0SLewanczyk, Dawid                  std::string,
38908777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3901abe55efSEd Tanous                  object : subtree)
3911abe55efSEd Tanous         {
39249c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3931abe55efSEd Tanous             {
39449c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3951abe55efSEd Tanous                          objData : object.second)
3961abe55efSEd Tanous                 {
39749c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
39808777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
399de629b6eSShawn McCarney                     objectsWithConnection.insert(
400de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
40108777fb0SLewanczyk, Dawid                 }
40208777fb0SLewanczyk, Dawid             }
40308777fb0SLewanczyk, Dawid         }
40455c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
405413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
406413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
40708777fb0SLewanczyk, Dawid     };
40808777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
40955c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
41055c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
4111abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
4121abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
413413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
414413961deSRichard Marian Thomaiyar }
415413961deSRichard Marian Thomaiyar 
416413961deSRichard Marian Thomaiyar /**
417413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
418413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
419413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
420413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
421413961deSRichard Marian Thomaiyar  */
422413961deSRichard Marian Thomaiyar template <typename Callback>
423fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
424fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
425413961deSRichard Marian Thomaiyar                     Callback&& callback)
426413961deSRichard Marian Thomaiyar {
427413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
428fe04d49cSNan Zhou         [callback](const std::set<std::string>& connections,
429413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4303174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
43181ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
432413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
43308777fb0SLewanczyk, Dawid }
43408777fb0SLewanczyk, Dawid 
43508777fb0SLewanczyk, Dawid /**
43649c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43749c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43849c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
43949c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
44049c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
44149c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
44249c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
44349c53ac9SJohnathan Mantey  */
44423a21a1cSEd Tanous inline void reduceSensorList(
4457f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
4467f1cc26dSEd Tanous     std::span<std::string_view> sensorTypes,
44749c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
448fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
44949c53ac9SJohnathan Mantey {
45049c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
45149c53ac9SJohnathan Mantey     {
4527f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
4537f1cc26dSEd Tanous                                    chassisSubNode == sensors::node::thermal
454a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
45549c53ac9SJohnathan Mantey                                        : "Voltages");
45649c53ac9SJohnathan Mantey 
45749c53ac9SJohnathan Mantey         return;
45849c53ac9SJohnathan Mantey     }
45949c53ac9SJohnathan Mantey     if (allSensors->empty())
46049c53ac9SJohnathan Mantey     {
46149c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
46249c53ac9SJohnathan Mantey         return;
46349c53ac9SJohnathan Mantey     }
46449c53ac9SJohnathan Mantey 
4657f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
46649c53ac9SJohnathan Mantey     {
46749c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46849c53ac9SJohnathan Mantey         {
46911ba3979SEd Tanous             if (sensor.starts_with(type))
47049c53ac9SJohnathan Mantey             {
47149c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
47249c53ac9SJohnathan Mantey             }
47349c53ac9SJohnathan Mantey         }
47449c53ac9SJohnathan Mantey     }
47549c53ac9SJohnathan Mantey }
47649c53ac9SJohnathan Mantey 
4777f1cc26dSEd Tanous /*
4787f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
4797f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
4807f1cc26dSEd Tanous  *
4817f1cc26dSEd Tanous  * */
4827f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
4837f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
4847f1cc26dSEd Tanous {
4857f1cc26dSEd Tanous     if (chassisSubNode == sensors::node::power)
4867f1cc26dSEd Tanous     {
4877f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
4887f1cc26dSEd Tanous     }
4897f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::thermal)
4907f1cc26dSEd Tanous     {
4917f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
4927f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
4937f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
4947f1cc26dSEd Tanous     }
4957f1cc26dSEd Tanous     else if (chassisSubNode == sensors::node::sensors)
4967f1cc26dSEd Tanous     {
4977f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
4987f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
4997f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
5007f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
5017f1cc26dSEd Tanous     }
5027f1cc26dSEd Tanous 
5037f1cc26dSEd Tanous     if (chassisSubNode != sensors::node::sensors)
5047f1cc26dSEd Tanous     {
5057f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
5067f1cc26dSEd Tanous     }
5077f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
5087f1cc26dSEd Tanous }
5097f1cc26dSEd Tanous 
51049c53ac9SJohnathan Mantey /**
51108777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
512588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
51308777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
51408777fb0SLewanczyk, Dawid  */
51508777fb0SLewanczyk, Dawid template <typename Callback>
5167f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5177f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
5187f1cc26dSEd Tanous                 std::span<std::string_view> sensorTypes, Callback&& callback)
5191abe55efSEd Tanous {
52055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
521*7a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
52249c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
523adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
524*7a1dbc48SGeorge Liu 
525*7a1dbc48SGeorge Liu     // Get the Chassis Collection
526*7a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
527*7a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
5287f1cc26dSEd Tanous         [callback{std::forward<Callback>(callback)}, asyncResp,
5297f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
5307f1cc26dSEd Tanous          chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
531*7a1dbc48SGeorge Liu             const boost::system::error_code& ec,
532002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
53355c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5341abe55efSEd Tanous         if (ec)
5351abe55efSEd Tanous         {
53655c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5377f1cc26dSEd Tanous             messages::internalError(asyncResp->res);
53808777fb0SLewanczyk, Dawid             return;
53908777fb0SLewanczyk, Dawid         }
54049c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
54149c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5421abe55efSEd Tanous         {
54328aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
544f8fe53e7SEd Tanous             std::string chassisName = path.filename();
54528aa8de5SGeorge Liu             if (chassisName.empty())
5461abe55efSEd Tanous             {
54749c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
548daf36e2eSEd Tanous                 continue;
549daf36e2eSEd Tanous             }
5507f1cc26dSEd Tanous             if (chassisName == chassisIdStr)
5511abe55efSEd Tanous             {
55249c53ac9SJohnathan Mantey                 chassisPath = &chassis;
55349c53ac9SJohnathan Mantey                 break;
554daf36e2eSEd Tanous             }
55549c53ac9SJohnathan Mantey         }
55649c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5571abe55efSEd Tanous         {
5587f1cc26dSEd Tanous             messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
55949c53ac9SJohnathan Mantey             return;
5601abe55efSEd Tanous         }
5617f1cc26dSEd Tanous         populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
56208777fb0SLewanczyk, Dawid 
5637f1cc26dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
5647f1cc26dSEd Tanous             "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
56595a3ecadSAnthony Wilson 
5668fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5678fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
5681e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
5691e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
5701e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
5717f1cc26dSEd Tanous             [asyncResp, chassisSubNode, sensorTypes,
572f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
573271584abSEd Tanous                 const boost::system::error_code& e,
5741e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
575271584abSEd Tanous             if (e)
57649c53ac9SJohnathan Mantey             {
577271584abSEd Tanous                 if (e.value() != EBADR)
57849c53ac9SJohnathan Mantey                 {
5797f1cc26dSEd Tanous                     messages::internalError(asyncResp->res);
58049c53ac9SJohnathan Mantey                     return;
58149c53ac9SJohnathan Mantey                 }
58249c53ac9SJohnathan Mantey             }
583fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>> culledSensorList =
584fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
5857f1cc26dSEd Tanous             reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
5867f1cc26dSEd Tanous                              &nodeSensorList, culledSensorList);
5877f1cc26dSEd Tanous             BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
58849c53ac9SJohnathan Mantey             callback(culledSensorList);
5891e1e598dSJonathan Doman             });
590*7a1dbc48SGeorge Liu         });
59155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
59208777fb0SLewanczyk, Dawid }
59308777fb0SLewanczyk, Dawid 
59408777fb0SLewanczyk, Dawid /**
595adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
596adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
597adc4f0dbSShawn McCarney  * @return State value for inventory item.
59834dd179eSJames Feist  */
59923a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
600adc4f0dbSShawn McCarney {
601adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
602adc4f0dbSShawn McCarney     {
603adc4f0dbSShawn McCarney         return "Absent";
604adc4f0dbSShawn McCarney     }
60534dd179eSJames Feist 
606adc4f0dbSShawn McCarney     return "Enabled";
607adc4f0dbSShawn McCarney }
608adc4f0dbSShawn McCarney 
609adc4f0dbSShawn McCarney /**
610adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
611adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
6121d7c0054SEd Tanous  * @param valuesDict Map of all sensor DBus values.
613adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
614adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
615adc4f0dbSShawn McCarney  * @return Health value for sensor.
616adc4f0dbSShawn McCarney  */
6171d7c0054SEd Tanous inline std::string getHealth(nlohmann::json& sensorJson,
6181d7c0054SEd Tanous                              const dbus::utility::DBusPropertiesMap& valuesDict,
619adc4f0dbSShawn McCarney                              const InventoryItem* inventoryItem)
62034dd179eSJames Feist {
621adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
622adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
623adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
624adc4f0dbSShawn McCarney     std::string currentHealth;
625adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
626adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
627adc4f0dbSShawn McCarney     {
628adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
629adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
630adc4f0dbSShawn McCarney         {
631adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
632adc4f0dbSShawn McCarney             if (health != nullptr)
633adc4f0dbSShawn McCarney             {
634adc4f0dbSShawn McCarney                 currentHealth = *health;
635adc4f0dbSShawn McCarney             }
636adc4f0dbSShawn McCarney         }
637adc4f0dbSShawn McCarney     }
638adc4f0dbSShawn McCarney 
639adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
640adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
641adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
642adc4f0dbSShawn McCarney     {
643adc4f0dbSShawn McCarney         return "Critical";
644adc4f0dbSShawn McCarney     }
645adc4f0dbSShawn McCarney 
646c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmHigh = nullptr;
647c1343bf6SKrzysztof Grobelny     const bool* criticalAlarmLow = nullptr;
648c1343bf6SKrzysztof Grobelny     const bool* warningAlarmHigh = nullptr;
649c1343bf6SKrzysztof Grobelny     const bool* warningAlarmLow = nullptr;
650711ac7a9SEd Tanous 
651c1343bf6SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
652c1343bf6SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
653c1343bf6SKrzysztof Grobelny         criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
654c1343bf6SKrzysztof Grobelny         "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
655c1343bf6SKrzysztof Grobelny         warningAlarmLow);
656c1343bf6SKrzysztof Grobelny 
657c1343bf6SKrzysztof Grobelny     if (success)
65834dd179eSJames Feist     {
659c1343bf6SKrzysztof Grobelny         // Check if sensor has critical threshold alarm
660c1343bf6SKrzysztof Grobelny         if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
661c1343bf6SKrzysztof Grobelny             (criticalAlarmLow != nullptr && *criticalAlarmLow))
66234dd179eSJames Feist         {
66334dd179eSJames Feist             return "Critical";
66434dd179eSJames Feist         }
66534dd179eSJames Feist     }
66634dd179eSJames Feist 
667adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
668adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
669adc4f0dbSShawn McCarney     {
670adc4f0dbSShawn McCarney         return "Critical";
671adc4f0dbSShawn McCarney     }
672adc4f0dbSShawn McCarney 
673adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that. This
674adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
675adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
676adc4f0dbSShawn McCarney     {
677adc4f0dbSShawn McCarney         return "Warning";
678adc4f0dbSShawn McCarney     }
679adc4f0dbSShawn McCarney 
680c1343bf6SKrzysztof Grobelny     if (success)
681c1343bf6SKrzysztof Grobelny     {
682adc4f0dbSShawn McCarney         // Check if sensor has warning threshold alarm
683c1343bf6SKrzysztof Grobelny         if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
684c1343bf6SKrzysztof Grobelny             (warningAlarmLow != nullptr && *warningAlarmLow))
68534dd179eSJames Feist         {
686ebe4d91eSEd Tanous             return "Warning";
68734dd179eSJames Feist         }
68834dd179eSJames Feist     }
689adc4f0dbSShawn McCarney 
69034dd179eSJames Feist     return "OK";
69134dd179eSJames Feist }
69234dd179eSJames Feist 
69323a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
694d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
695d500549bSAnthony Wilson {
696d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
697d500549bSAnthony Wilson     {
698d500549bSAnthony Wilson         switch (inventoryItem->ledState)
699d500549bSAnthony Wilson         {
700d500549bSAnthony Wilson             case LedState::OFF:
701d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
702d500549bSAnthony Wilson                 break;
703d500549bSAnthony Wilson             case LedState::ON:
704d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
705d500549bSAnthony Wilson                 break;
706d500549bSAnthony Wilson             case LedState::BLINK:
707d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
708d500549bSAnthony Wilson                 break;
70923a21a1cSEd Tanous             case LedState::UNKNOWN:
710d500549bSAnthony Wilson                 break;
711d500549bSAnthony Wilson         }
712d500549bSAnthony Wilson     }
713d500549bSAnthony Wilson }
714d500549bSAnthony Wilson 
71534dd179eSJames Feist /**
71608777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
71708777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
718274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
71908777fb0SLewanczyk, Dawid  * build
7201d7c0054SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
7211d7c0054SEd Tanous  * @param propertiesDict A dictionary of the properties to build the sensor
7221d7c0054SEd Tanous  * from.
7231d7c0054SEd Tanous  * @param sensorJson  The json object to fill
724adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
725adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
72608777fb0SLewanczyk, Dawid  */
7271d7c0054SEd Tanous inline void objectPropertiesToJson(
7281d7c0054SEd Tanous     std::string_view sensorName, std::string_view sensorType,
7291d7c0054SEd Tanous     std::string_view chassisSubNode,
7301d7c0054SEd Tanous     const dbus::utility::DBusPropertiesMap& propertiesDict,
73181ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
7321abe55efSEd Tanous {
7331d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
734adc4f0dbSShawn McCarney     {
735c71d6125SEd Tanous         std::string subNodeEscaped(sensorType);
736c1d019a6SEd Tanous         subNodeEscaped.erase(
737c1d019a6SEd Tanous             std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
738c1d019a6SEd Tanous             subNodeEscaped.end());
739c1d019a6SEd Tanous 
740c1d019a6SEd Tanous         // For sensors in SensorCollection we set Id instead of MemberId,
741c1d019a6SEd Tanous         // including power sensors.
742c1d019a6SEd Tanous         subNodeEscaped += '_';
743c1d019a6SEd Tanous         subNodeEscaped += sensorName;
744c1d019a6SEd Tanous         sensorJson["Id"] = std::move(subNodeEscaped);
745c1d019a6SEd Tanous 
7461d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7471d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7481d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
74995a3ecadSAnthony Wilson     }
75095a3ecadSAnthony Wilson     else if (sensorType != "power")
75195a3ecadSAnthony Wilson     {
75295a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
75395a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
75495a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
75581ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
7561d7c0054SEd Tanous         std::string sensorNameEs(sensorName);
7571d7c0054SEd Tanous         std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
7581d7c0054SEd Tanous         sensorJson["Name"] = std::move(sensorNameEs);
759adc4f0dbSShawn McCarney     }
760e742b6ccSEd Tanous 
76181ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
76281ce609eSEd Tanous     sensorJson["Status"]["Health"] =
7631d7c0054SEd Tanous         getHealth(sensorJson, propertiesDict, inventoryItem);
76408777fb0SLewanczyk, Dawid 
76508777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
76608777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
76708777fb0SLewanczyk, Dawid     // that require integers, not floats.
76808777fb0SLewanczyk, Dawid     bool forceToInt = false;
76908777fb0SLewanczyk, Dawid 
7703929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
7711d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
77295a3ecadSAnthony Wilson     {
7732a4ba195SShounak Mitra         sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
774c2bf7f99SWludzik, Jozef 
7750ec8b83dSEd Tanous         sensor::ReadingType readingType = sensors::toReadingType(sensorType);
7760ec8b83dSEd Tanous         if (readingType == sensor::ReadingType::Invalid)
77795a3ecadSAnthony Wilson         {
778c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
779c2bf7f99SWludzik, Jozef                              << sensorType;
78095a3ecadSAnthony Wilson         }
781c2bf7f99SWludzik, Jozef         else
78295a3ecadSAnthony Wilson         {
783c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
78495a3ecadSAnthony Wilson         }
785c2bf7f99SWludzik, Jozef 
7861d7c0054SEd Tanous         std::string_view readingUnits = sensors::toReadingUnits(sensorType);
787c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
788f8ede15eSAdrian Ambrożewicz         {
789c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
790c2bf7f99SWludzik, Jozef                              << sensorType;
791c2bf7f99SWludzik, Jozef         }
792c2bf7f99SWludzik, Jozef         else
793c2bf7f99SWludzik, Jozef         {
794c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
795f8ede15eSAdrian Ambrożewicz         }
79695a3ecadSAnthony Wilson     }
79795a3ecadSAnthony Wilson     else if (sensorType == "temperature")
7981abe55efSEd Tanous     {
7993929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
80081ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
80108777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
80208777fb0SLewanczyk, Dawid         // implementation seems to implement fan
8031abe55efSEd Tanous     }
8041abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
8051abe55efSEd Tanous     {
8063929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
80781ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
80881ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
80981ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
81008777fb0SLewanczyk, Dawid         forceToInt = true;
8111abe55efSEd Tanous     }
8126f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
8136f6d0d32SEd Tanous     {
8143929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
81581ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
81681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
81781ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
8186f6d0d32SEd Tanous         forceToInt = true;
8196f6d0d32SEd Tanous     }
8201abe55efSEd Tanous     else if (sensorType == "voltage")
8211abe55efSEd Tanous     {
8223929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
82381ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
8241abe55efSEd Tanous     }
8252474adfaSEd Tanous     else if (sensorType == "power")
8262474adfaSEd Tanous     {
8271d7c0054SEd Tanous         if (boost::iequals(sensorName, "total_power"))
828028f7ebcSEddie James         {
82981ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
8307ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
8317ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
83281ce609eSEd Tanous             sensorJson["MemberId"] = "0";
83381ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
8343929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
835028f7ebcSEddie James         }
8361d7c0054SEd Tanous         else if (boost::ifind_first(sensorName, "input").empty())
83749c53ac9SJohnathan Mantey         {
8383929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
83949c53ac9SJohnathan Mantey         }
84049c53ac9SJohnathan Mantey         else
84149c53ac9SJohnathan Mantey         {
8423929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
84349c53ac9SJohnathan Mantey         }
8442474adfaSEd Tanous     }
8451abe55efSEd Tanous     else
8461abe55efSEd Tanous     {
84755c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
84808777fb0SLewanczyk, Dawid         return;
84908777fb0SLewanczyk, Dawid     }
85008777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
8513929aca1SAnthony Wilson     std::vector<
8523929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
8533929aca1SAnthony Wilson         properties;
85408777fb0SLewanczyk, Dawid     properties.reserve(7);
85508777fb0SLewanczyk, Dawid 
85608777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
857de629b6eSShawn McCarney 
8581d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
8593929aca1SAnthony Wilson     {
8603929aca1SAnthony Wilson         properties.emplace_back(
8613929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
8623929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
8633929aca1SAnthony Wilson         properties.emplace_back(
8643929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
8653929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
8663929aca1SAnthony Wilson         properties.emplace_back(
8673929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
8683929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
8693929aca1SAnthony Wilson         properties.emplace_back(
8703929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
8713929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
8723929aca1SAnthony Wilson     }
8733929aca1SAnthony Wilson     else if (sensorType != "power")
874de629b6eSShawn McCarney     {
87508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8763929aca1SAnthony Wilson                                 "WarningHigh",
8773929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
87808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8793929aca1SAnthony Wilson                                 "WarningLow",
8803929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
88108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8823929aca1SAnthony Wilson                                 "CriticalHigh",
8833929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
88408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8853929aca1SAnthony Wilson                                 "CriticalLow",
8863929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
887de629b6eSShawn McCarney     }
88808777fb0SLewanczyk, Dawid 
8892474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
8902474adfaSEd Tanous 
8911d7c0054SEd Tanous     if (chassisSubNode == sensors::node::sensors)
89295a3ecadSAnthony Wilson     {
89395a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8943929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
89595a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8963929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
89751c35a8fSGeorge Liu         properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
89851c35a8fSGeorge Liu                                 "Accuracy", "/Accuracy"_json_pointer);
89995a3ecadSAnthony Wilson     }
90095a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9011abe55efSEd Tanous     {
90208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9033929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
90408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9053929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
9061abe55efSEd Tanous     }
907adc4f0dbSShawn McCarney     else if (sensorType != "power")
9081abe55efSEd Tanous     {
90908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9103929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
91108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9123929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
91308777fb0SLewanczyk, Dawid     }
91408777fb0SLewanczyk, Dawid 
9153929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
9163929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
9171abe55efSEd Tanous     {
9181d7c0054SEd Tanous         for (const auto& [valueName, valueVariant] : propertiesDict)
919711ac7a9SEd Tanous         {
920711ac7a9SEd Tanous             if (valueName != std::get<1>(p))
921711ac7a9SEd Tanous             {
922711ac7a9SEd Tanous                 continue;
923711ac7a9SEd Tanous             }
9243929aca1SAnthony Wilson 
9253929aca1SAnthony Wilson             // The property we want to set may be nested json, so use
9263929aca1SAnthony Wilson             // a json_pointer for easy indexing into the json structure.
9273929aca1SAnthony Wilson             const nlohmann::json::json_pointer& key = std::get<2>(p);
9283929aca1SAnthony Wilson 
929abf2add6SEd Tanous             const double* doubleValue = std::get_if<double>(&valueVariant);
93040e4f380SEd Tanous             if (doubleValue == nullptr)
9311abe55efSEd Tanous             {
93240e4f380SEd Tanous                 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
9336f6d0d32SEd Tanous                 continue;
93408777fb0SLewanczyk, Dawid             }
9356f6d0d32SEd Tanous             if (forceToInt)
9366f6d0d32SEd Tanous             {
93740e4f380SEd Tanous                 sensorJson[key] = static_cast<int64_t>(*doubleValue);
9386f6d0d32SEd Tanous             }
9396f6d0d32SEd Tanous             else
9406f6d0d32SEd Tanous             {
94140e4f380SEd Tanous                 sensorJson[key] = *doubleValue;
94208777fb0SLewanczyk, Dawid             }
94308777fb0SLewanczyk, Dawid         }
94408777fb0SLewanczyk, Dawid     }
94508777fb0SLewanczyk, Dawid }
94608777fb0SLewanczyk, Dawid 
9471d7c0054SEd Tanous /**
9481d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
9491d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
9501d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
9511d7c0054SEd Tanous  * build
9521d7c0054SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
9531d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
9541d7c0054SEd Tanous  * interfaces to be built from
9551d7c0054SEd Tanous  * @param sensorJson  The json object to fill
9561d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
9571d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
9581d7c0054SEd Tanous  */
9591d7c0054SEd Tanous inline void objectInterfacesToJson(
9601d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
9611d7c0054SEd Tanous     const std::string& chassisSubNode,
9621d7c0054SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
9631d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
9641d7c0054SEd Tanous {
9651d7c0054SEd Tanous 
9661d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
9671d7c0054SEd Tanous     {
9681d7c0054SEd Tanous         objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
9691d7c0054SEd Tanous                                valuesDict, sensorJson, inventoryItem);
9701d7c0054SEd Tanous     }
971c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
9721d7c0054SEd Tanous }
9731d7c0054SEd Tanous 
974b5a76932SEd Tanous inline void populateFanRedundancy(
975b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
9768bd25ccdSJames Feist {
9778bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
978b9d36b47SEd Tanous         [sensorsAsyncResp](
979b9d36b47SEd Tanous             const boost::system::error_code ec,
980b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
9818bd25ccdSJames Feist         if (ec)
9828bd25ccdSJames Feist         {
9838bd25ccdSJames Feist             return; // don't have to have this interface
9848bd25ccdSJames Feist         }
985002d39b4SEd Tanous         for (const std::pair<
986002d39b4SEd Tanous                  std::string,
987002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
988e278c18fSEd Tanous                  pathPair : resp)
9898bd25ccdSJames Feist         {
990e278c18fSEd Tanous             const std::string& path = pathPair.first;
991002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
992002d39b4SEd Tanous                 objDict = pathPair.second;
9938bd25ccdSJames Feist             if (objDict.empty())
9948bd25ccdSJames Feist             {
9958bd25ccdSJames Feist                 continue; // this should be impossible
9968bd25ccdSJames Feist             }
9978bd25ccdSJames Feist 
9988bd25ccdSJames Feist             const std::string& owner = objDict.begin()->first;
9991e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::vector<std::string>>(
10001e1e598dSJonathan Doman                 *crow::connections::systemBus,
10011e1e598dSJonathan Doman                 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
10021e1e598dSJonathan Doman                 "xyz.openbmc_project.Association", "endpoints",
1003002d39b4SEd Tanous                 [path, owner,
1004002d39b4SEd Tanous                  sensorsAsyncResp](const boost::system::error_code e,
10051e1e598dSJonathan Doman                                    const std::vector<std::string>& endpoints) {
1006271584abSEd Tanous                 if (e)
10078bd25ccdSJames Feist                 {
10088bd25ccdSJames Feist                     return; // if they don't have an association we
10098bd25ccdSJames Feist                             // can't tell what chassis is
10108bd25ccdSJames Feist                 }
1011002d39b4SEd Tanous                 auto found =
1012002d39b4SEd Tanous                     std::find_if(endpoints.begin(), endpoints.end(),
10138bd25ccdSJames Feist                                  [sensorsAsyncResp](const std::string& entry) {
1014002d39b4SEd Tanous                     return entry.find(sensorsAsyncResp->chassisId) !=
10158bd25ccdSJames Feist                            std::string::npos;
10168bd25ccdSJames Feist                     });
10178bd25ccdSJames Feist 
10181e1e598dSJonathan Doman                 if (found == endpoints.end())
10198bd25ccdSJames Feist                 {
10208bd25ccdSJames Feist                     return;
10218bd25ccdSJames Feist                 }
102286d89ed7SKrzysztof Grobelny                 sdbusplus::asio::getAllProperties(
102386d89ed7SKrzysztof Grobelny                     *crow::connections::systemBus, owner, path,
102486d89ed7SKrzysztof Grobelny                     "xyz.openbmc_project.Control.FanRedundancy",
10258bd25ccdSJames Feist                     [path, sensorsAsyncResp](
1026271584abSEd Tanous                         const boost::system::error_code& err,
102786d89ed7SKrzysztof Grobelny                         const dbus::utility::DBusPropertiesMap& ret) {
1028271584abSEd Tanous                     if (err)
10298bd25ccdSJames Feist                     {
10308bd25ccdSJames Feist                         return; // don't have to have this
10318bd25ccdSJames Feist                                 // interface
10328bd25ccdSJames Feist                     }
10338bd25ccdSJames Feist 
103486d89ed7SKrzysztof Grobelny                     const uint8_t* allowedFailures = nullptr;
103586d89ed7SKrzysztof Grobelny                     const std::vector<std::string>* collection = nullptr;
103686d89ed7SKrzysztof Grobelny                     const std::string* status = nullptr;
103786d89ed7SKrzysztof Grobelny 
103886d89ed7SKrzysztof Grobelny                     const bool success = sdbusplus::unpackPropertiesNoThrow(
103986d89ed7SKrzysztof Grobelny                         dbus_utils::UnpackErrorPrinter(), ret,
104086d89ed7SKrzysztof Grobelny                         "AllowedFailures", allowedFailures, "Collection",
104186d89ed7SKrzysztof Grobelny                         collection, "Status", status);
104286d89ed7SKrzysztof Grobelny 
104386d89ed7SKrzysztof Grobelny                     if (!success)
104486d89ed7SKrzysztof Grobelny                     {
104586d89ed7SKrzysztof Grobelny                         messages::internalError(
104686d89ed7SKrzysztof Grobelny                             sensorsAsyncResp->asyncResp->res);
104786d89ed7SKrzysztof Grobelny                         return;
104886d89ed7SKrzysztof Grobelny                     }
104986d89ed7SKrzysztof Grobelny 
105086d89ed7SKrzysztof Grobelny                     if (allowedFailures == nullptr || collection == nullptr ||
105186d89ed7SKrzysztof Grobelny                         status == nullptr)
10528bd25ccdSJames Feist                     {
1053002d39b4SEd Tanous                         BMCWEB_LOG_ERROR << "Invalid redundancy interface";
10548bd25ccdSJames Feist                         messages::internalError(
10558d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
10568bd25ccdSJames Feist                         return;
10578bd25ccdSJames Feist                     }
10588bd25ccdSJames Feist 
1059002d39b4SEd Tanous                     sdbusplus::message::object_path objectPath(path);
106028aa8de5SGeorge Liu                     std::string name = objectPath.filename();
106128aa8de5SGeorge Liu                     if (name.empty())
10628bd25ccdSJames Feist                     {
10638bd25ccdSJames Feist                         // this should be impossible
10648bd25ccdSJames Feist                         messages::internalError(
10658d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
10668bd25ccdSJames Feist                         return;
10678bd25ccdSJames Feist                     }
1068002d39b4SEd Tanous                     std::replace(name.begin(), name.end(), '_', ' ');
10698bd25ccdSJames Feist 
10708bd25ccdSJames Feist                     std::string health;
10718bd25ccdSJames Feist 
107211ba3979SEd Tanous                     if (status->ends_with("Full"))
10738bd25ccdSJames Feist                     {
10748bd25ccdSJames Feist                         health = "OK";
10758bd25ccdSJames Feist                     }
107611ba3979SEd Tanous                     else if (status->ends_with("Degraded"))
10778bd25ccdSJames Feist                     {
10788bd25ccdSJames Feist                         health = "Warning";
10798bd25ccdSJames Feist                     }
10808bd25ccdSJames Feist                     else
10818bd25ccdSJames Feist                     {
10828bd25ccdSJames Feist                         health = "Critical";
10838bd25ccdSJames Feist                     }
10841476687dSEd Tanous                     nlohmann::json::array_t redfishCollection;
10858bd25ccdSJames Feist                     const auto& fanRedfish =
1086002d39b4SEd Tanous                         sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
10878bd25ccdSJames Feist                     for (const std::string& item : *collection)
10888bd25ccdSJames Feist                     {
10898a592810SEd Tanous                         sdbusplus::message::object_path itemPath(item);
10908a592810SEd Tanous                         std::string itemName = itemPath.filename();
109128aa8de5SGeorge Liu                         if (itemName.empty())
109228aa8de5SGeorge Liu                         {
109328aa8de5SGeorge Liu                             continue;
109428aa8de5SGeorge Liu                         }
10958bd25ccdSJames Feist                         /*
10968bd25ccdSJames Feist                         todo(ed): merge patch that fixes the names
10978bd25ccdSJames Feist                         std::replace(itemName.begin(),
10988bd25ccdSJames Feist                                      itemName.end(), '_', ' ');*/
1099002d39b4SEd Tanous                         auto schemaItem =
1100002d39b4SEd Tanous                             std::find_if(fanRedfish.begin(), fanRedfish.end(),
11018bd25ccdSJames Feist                                          [itemName](const nlohmann::json& fan) {
11028bd25ccdSJames Feist                             return fan["MemberId"] == itemName;
11038bd25ccdSJames Feist                             });
11048bd25ccdSJames Feist                         if (schemaItem != fanRedfish.end())
11058bd25ccdSJames Feist                         {
11068a592810SEd Tanous                             nlohmann::json::object_t collectionId;
11078a592810SEd Tanous                             collectionId["@odata.id"] =
11081476687dSEd Tanous                                 (*schemaItem)["@odata.id"];
11091476687dSEd Tanous                             redfishCollection.emplace_back(
11108a592810SEd Tanous                                 std::move(collectionId));
11118bd25ccdSJames Feist                         }
11128bd25ccdSJames Feist                         else
11138bd25ccdSJames Feist                         {
1114002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "failed to find fan in schema";
11158bd25ccdSJames Feist                             messages::internalError(
11168d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
11178bd25ccdSJames Feist                             return;
11188bd25ccdSJames Feist                         }
11198bd25ccdSJames Feist                     }
11208bd25ccdSJames Feist 
11213e9e72ebSKuiying Wang                     size_t minNumNeeded =
112226f6976fSEd Tanous                         collection->empty()
112326f6976fSEd Tanous                             ? 0
112426f6976fSEd Tanous                             : collection->size() - *allowedFailures;
1125002d39b4SEd Tanous                     nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
11268bd25ccdSJames Feist                                                 .jsonValue["Redundancy"];
11271476687dSEd Tanous 
11281476687dSEd Tanous                     nlohmann::json::object_t redundancy;
11291476687dSEd Tanous                     redundancy["@odata.id"] =
1130002d39b4SEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1131002d39b4SEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode +
1132002d39b4SEd Tanous                         "#/Redundancy/" + std::to_string(jResp.size());
1133002d39b4SEd Tanous                     redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
11341476687dSEd Tanous                     redundancy["MinNumNeeded"] = minNumNeeded;
11351476687dSEd Tanous                     redundancy["MemberId"] = name;
11361476687dSEd Tanous                     redundancy["Mode"] = "N+m";
11371476687dSEd Tanous                     redundancy["Name"] = name;
11381476687dSEd Tanous                     redundancy["RedundancySet"] = redfishCollection;
11391476687dSEd Tanous                     redundancy["Status"]["Health"] = health;
11401476687dSEd Tanous                     redundancy["Status"]["State"] = "Enabled";
11411476687dSEd Tanous 
11421476687dSEd Tanous                     jResp.push_back(std::move(redundancy));
114386d89ed7SKrzysztof Grobelny                     });
11441e1e598dSJonathan Doman                 });
11458bd25ccdSJames Feist         }
11468bd25ccdSJames Feist         },
11478bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
11488bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
11498bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
11508bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
11518bd25ccdSJames Feist         std::array<const char*, 1>{
11528bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
11538bd25ccdSJames Feist }
11548bd25ccdSJames Feist 
1155b5a76932SEd Tanous inline void
115681ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
115749c53ac9SJohnathan Mantey {
11588d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
115949c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
116081ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
116149c53ac9SJohnathan Mantey     {
116249c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
116349c53ac9SJohnathan Mantey     }
116449c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
116549c53ac9SJohnathan Mantey     {
116649c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
116749c53ac9SJohnathan Mantey         if (entry != response.end())
116849c53ac9SJohnathan Mantey         {
116949c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
117002cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
117149c53ac9SJohnathan Mantey                 return c1["Name"] < c2["Name"];
117249c53ac9SJohnathan Mantey             });
117349c53ac9SJohnathan Mantey 
117449c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
117549c53ac9SJohnathan Mantey             size_t count = 0;
117649c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
117749c53ac9SJohnathan Mantey             {
117849c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
117949c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
118049c53ac9SJohnathan Mantey                 {
118149c53ac9SJohnathan Mantey                     continue;
118249c53ac9SJohnathan Mantey                 }
118349c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
118449c53ac9SJohnathan Mantey                 if (value != nullptr)
118549c53ac9SJohnathan Mantey                 {
118649c53ac9SJohnathan Mantey                     *value += std::to_string(count);
118749c53ac9SJohnathan Mantey                     count++;
118881ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
118949c53ac9SJohnathan Mantey                 }
119049c53ac9SJohnathan Mantey             }
119149c53ac9SJohnathan Mantey         }
119249c53ac9SJohnathan Mantey     }
119349c53ac9SJohnathan Mantey }
119449c53ac9SJohnathan Mantey 
119508777fb0SLewanczyk, Dawid /**
1196adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1197adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1198adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1199adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12008fb49dd6SShawn McCarney  */
120123a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1202b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1203adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
12048fb49dd6SShawn McCarney {
1205adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
12068fb49dd6SShawn McCarney     {
1207adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
12088fb49dd6SShawn McCarney         {
1209adc4f0dbSShawn McCarney             return &inventoryItem;
12108fb49dd6SShawn McCarney         }
12118fb49dd6SShawn McCarney     }
12128fb49dd6SShawn McCarney     return nullptr;
12138fb49dd6SShawn McCarney }
12148fb49dd6SShawn McCarney 
12158fb49dd6SShawn McCarney /**
1216adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1217adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1218adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1219adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12208fb49dd6SShawn McCarney  */
122123a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1222b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1223adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1224adc4f0dbSShawn McCarney {
1225adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1226adc4f0dbSShawn McCarney     {
1227adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1228adc4f0dbSShawn McCarney         {
1229adc4f0dbSShawn McCarney             return &inventoryItem;
1230adc4f0dbSShawn McCarney         }
1231adc4f0dbSShawn McCarney     }
1232adc4f0dbSShawn McCarney     return nullptr;
1233adc4f0dbSShawn McCarney }
1234adc4f0dbSShawn McCarney 
1235adc4f0dbSShawn McCarney /**
1236d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1237d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1238d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1239d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1240d500549bSAnthony Wilson  */
1241d500549bSAnthony Wilson inline InventoryItem*
1242d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1243d500549bSAnthony Wilson                             const std::string& ledObjPath)
1244d500549bSAnthony Wilson {
1245d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1246d500549bSAnthony Wilson     {
1247d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1248d500549bSAnthony Wilson         {
1249d500549bSAnthony Wilson             return &inventoryItem;
1250d500549bSAnthony Wilson         }
1251d500549bSAnthony Wilson     }
1252d500549bSAnthony Wilson     return nullptr;
1253d500549bSAnthony Wilson }
1254d500549bSAnthony Wilson 
1255d500549bSAnthony Wilson /**
1256adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1257adc4f0dbSShawn McCarney  *
1258adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1259adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1260adc4f0dbSShawn McCarney  * added to the vector.
1261adc4f0dbSShawn McCarney  *
1262adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1263adc4f0dbSShawn McCarney  * InventoryItem.
1264adc4f0dbSShawn McCarney  *
1265adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1266adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1267adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1268adc4f0dbSShawn McCarney  */
1269b5a76932SEd Tanous inline void addInventoryItem(
1270b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1271b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1272adc4f0dbSShawn McCarney {
1273adc4f0dbSShawn McCarney     // Look for inventory item in vector
1274adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1275adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1276adc4f0dbSShawn McCarney 
1277adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1278adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1279adc4f0dbSShawn McCarney     {
1280adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1281adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1282adc4f0dbSShawn McCarney     }
1283adc4f0dbSShawn McCarney 
1284adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1285adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1286adc4f0dbSShawn McCarney }
1287adc4f0dbSShawn McCarney 
1288adc4f0dbSShawn McCarney /**
1289adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1290adc4f0dbSShawn McCarney  *
1291adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1292adc4f0dbSShawn McCarney  * specified InventoryItem.
1293adc4f0dbSShawn McCarney  *
1294adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1295adc4f0dbSShawn McCarney  * response.
1296adc4f0dbSShawn McCarney  *
1297adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1298adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1299adc4f0dbSShawn McCarney  * for the specified inventory item.
1300adc4f0dbSShawn McCarney  */
130123a21a1cSEd Tanous inline void storeInventoryItemData(
1302adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1303711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
13048fb49dd6SShawn McCarney {
1305adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1306711ac7a9SEd Tanous 
13079eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
13088fb49dd6SShawn McCarney     {
1309711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
13108fb49dd6SShawn McCarney         {
13119eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1312711ac7a9SEd Tanous             {
1313711ac7a9SEd Tanous                 if (name == "Present")
1314711ac7a9SEd Tanous                 {
1315711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1316adc4f0dbSShawn McCarney                     if (value != nullptr)
13178fb49dd6SShawn McCarney                     {
1318adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
13198fb49dd6SShawn McCarney                     }
13208fb49dd6SShawn McCarney                 }
13218fb49dd6SShawn McCarney             }
1322711ac7a9SEd Tanous         }
1323adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1324711ac7a9SEd Tanous 
1325711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
13268fb49dd6SShawn McCarney         {
1327adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
13288fb49dd6SShawn McCarney         }
1329adc4f0dbSShawn McCarney 
1330adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1331711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1332adc4f0dbSShawn McCarney         {
13339eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1334711ac7a9SEd Tanous             {
1335711ac7a9SEd Tanous                 if (name == "Manufacturer")
1336adc4f0dbSShawn McCarney                 {
1337adc4f0dbSShawn McCarney                     const std::string* value =
1338711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1339adc4f0dbSShawn McCarney                     if (value != nullptr)
1340adc4f0dbSShawn McCarney                     {
1341adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1342adc4f0dbSShawn McCarney                     }
1343adc4f0dbSShawn McCarney                 }
1344711ac7a9SEd Tanous                 if (name == "Model")
1345adc4f0dbSShawn McCarney                 {
1346adc4f0dbSShawn McCarney                     const std::string* value =
1347711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1348adc4f0dbSShawn McCarney                     if (value != nullptr)
1349adc4f0dbSShawn McCarney                     {
1350adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1351adc4f0dbSShawn McCarney                     }
1352adc4f0dbSShawn McCarney                 }
1353711ac7a9SEd Tanous                 if (name == "SerialNumber")
1354adc4f0dbSShawn McCarney                 {
1355adc4f0dbSShawn McCarney                     const std::string* value =
1356711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1357adc4f0dbSShawn McCarney                     if (value != nullptr)
1358adc4f0dbSShawn McCarney                     {
1359adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1360adc4f0dbSShawn McCarney                     }
1361adc4f0dbSShawn McCarney                 }
1362711ac7a9SEd Tanous                 if (name == "PartNumber")
1363711ac7a9SEd Tanous                 {
1364711ac7a9SEd Tanous                     const std::string* value =
1365711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1366711ac7a9SEd Tanous                     if (value != nullptr)
1367711ac7a9SEd Tanous                     {
1368711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1369711ac7a9SEd Tanous                     }
1370711ac7a9SEd Tanous                 }
1371711ac7a9SEd Tanous             }
1372adc4f0dbSShawn McCarney         }
1373adc4f0dbSShawn McCarney 
1374711ac7a9SEd Tanous         if (interface ==
1375711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1376adc4f0dbSShawn McCarney         {
13779eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1378adc4f0dbSShawn McCarney             {
1379711ac7a9SEd Tanous                 if (name == "Functional")
1380711ac7a9SEd Tanous                 {
1381711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1382adc4f0dbSShawn McCarney                     if (value != nullptr)
1383adc4f0dbSShawn McCarney                     {
1384adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
13858fb49dd6SShawn McCarney                     }
13868fb49dd6SShawn McCarney                 }
13878fb49dd6SShawn McCarney             }
13888fb49dd6SShawn McCarney         }
1389711ac7a9SEd Tanous     }
1390711ac7a9SEd Tanous }
13918fb49dd6SShawn McCarney 
13928fb49dd6SShawn McCarney /**
1393adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
13948fb49dd6SShawn McCarney  *
1395adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1396adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1397adc4f0dbSShawn McCarney  * inventoryItems vector.
13988fb49dd6SShawn McCarney  *
1399adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1400adc4f0dbSShawn McCarney  * response.
1401adc4f0dbSShawn McCarney  *
1402adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1403adc4f0dbSShawn McCarney  * been obtained.
1404adc4f0dbSShawn McCarney  *
1405adc4f0dbSShawn McCarney  * The callback must have the following signature:
1406adc4f0dbSShawn McCarney  *   @code
1407d500549bSAnthony Wilson  *   callback(void)
1408adc4f0dbSShawn McCarney  *   @endcode
1409adc4f0dbSShawn McCarney  *
1410adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1411adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1412adc4f0dbSShawn McCarney  * last asynchronous function has completed.
14138fb49dd6SShawn McCarney  *
14148fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1415adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1416adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
14178fb49dd6SShawn McCarney  * implements ObjectManager.
1418adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1419adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1420adc4f0dbSShawn McCarney  * in recursive calls to this function.
14218fb49dd6SShawn McCarney  */
1422adc4f0dbSShawn McCarney template <typename Callback>
1423adc4f0dbSShawn McCarney static void getInventoryItemsData(
14248fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1425adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1426d0090733SEd Tanous     std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1427d0090733SEd Tanous     size_t invConnectionsIndex = 0)
14288fb49dd6SShawn McCarney {
1429adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
14308fb49dd6SShawn McCarney 
1431adc4f0dbSShawn McCarney     // If no more connections left, call callback
1432adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
14338fb49dd6SShawn McCarney     {
1434d500549bSAnthony Wilson         callback();
1435adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1436adc4f0dbSShawn McCarney         return;
1437adc4f0dbSShawn McCarney     }
1438adc4f0dbSShawn McCarney 
1439adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1440fe04d49cSNan Zhou     auto it = invConnections->begin();
1441fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1442adc4f0dbSShawn McCarney     if (it != invConnections->end())
1443adc4f0dbSShawn McCarney     {
1444adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1445adc4f0dbSShawn McCarney 
14468fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1447d0090733SEd Tanous         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1448d0090733SEd Tanous                             callback{std::forward<Callback>(callback)},
1449d0090733SEd Tanous                             invConnectionsIndex](
145002cad96eSEd Tanous                                const boost::system::error_code ec,
145102cad96eSEd Tanous                                const dbus::utility::ManagedObjectType& resp) {
1452adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
14538fb49dd6SShawn McCarney             if (ec)
14548fb49dd6SShawn McCarney             {
14558fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1456adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
14578d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
14588fb49dd6SShawn McCarney                 return;
14598fb49dd6SShawn McCarney             }
14608fb49dd6SShawn McCarney 
14618fb49dd6SShawn McCarney             // Loop through returned object paths
14628fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
14638fb49dd6SShawn McCarney             {
14648fb49dd6SShawn McCarney                 const std::string& objPath =
14658fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
14668fb49dd6SShawn McCarney 
1467adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1468adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1469adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1470adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
14718fb49dd6SShawn McCarney                 {
1472adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1473adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
14748fb49dd6SShawn McCarney                 }
14758fb49dd6SShawn McCarney             }
14768fb49dd6SShawn McCarney 
1477adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1478adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1479d0090733SEd Tanous                                   invConnections, std::move(callback),
1480d0090733SEd Tanous                                   invConnectionsIndex + 1);
1481adc4f0dbSShawn McCarney 
1482adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
14838fb49dd6SShawn McCarney         };
14848fb49dd6SShawn McCarney 
14858fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
14868fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
1487d0090733SEd Tanous             std::move(respHandler), invConnection,
1488f8bb0ff3SEd Tanous             "/xyz/openbmc_project/inventory",
14898fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14908fb49dd6SShawn McCarney     }
14918fb49dd6SShawn McCarney 
1492adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
14938fb49dd6SShawn McCarney }
14948fb49dd6SShawn McCarney 
14958fb49dd6SShawn McCarney /**
1496adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
14978fb49dd6SShawn McCarney  *
1498adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1499adc4f0dbSShawn McCarney  * items that are associated with sensors.
15008fb49dd6SShawn McCarney  *
15018fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
15028fb49dd6SShawn McCarney  * been obtained.
15038fb49dd6SShawn McCarney  *
15048fb49dd6SShawn McCarney  * The callback must have the following signature:
15058fb49dd6SShawn McCarney  *   @code
1506fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
15078fb49dd6SShawn McCarney  *   @endcode
15088fb49dd6SShawn McCarney  *
15098fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1510adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
15118fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
15128fb49dd6SShawn McCarney  */
15138fb49dd6SShawn McCarney template <typename Callback>
15148fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1515b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1516b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
15178fb49dd6SShawn McCarney     Callback&& callback)
15188fb49dd6SShawn McCarney {
15198fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
15208fb49dd6SShawn McCarney 
15218fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1522adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
15238fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1524adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1525adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
15268fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
15278fb49dd6SShawn McCarney 
15288fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1529002d39b4SEd Tanous     auto respHandler =
1530002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1531002d39b4SEd Tanous          inventoryItems](
1532b9d36b47SEd Tanous             const boost::system::error_code ec,
1533002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
15348fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
15358fb49dd6SShawn McCarney         if (ec)
15368fb49dd6SShawn McCarney         {
15378d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
15388fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
15398fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
15408fb49dd6SShawn McCarney             return;
15418fb49dd6SShawn McCarney         }
15428fb49dd6SShawn McCarney 
15438fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
1544fe04d49cSNan Zhou         std::shared_ptr<std::set<std::string>> invConnections =
1545fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
15468fb49dd6SShawn McCarney 
15478fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
15488fb49dd6SShawn McCarney         for (const std::pair<
15498fb49dd6SShawn McCarney                  std::string,
15508fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
15518fb49dd6SShawn McCarney                  object : subtree)
15528fb49dd6SShawn McCarney         {
1553adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
15548fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1555adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
15568fb49dd6SShawn McCarney             {
15578fb49dd6SShawn McCarney                 // Store all connections to inventory item
15588fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
15598fb49dd6SShawn McCarney                          objData : object.second)
15608fb49dd6SShawn McCarney                 {
15618fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
15628fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
15638fb49dd6SShawn McCarney                 }
15648fb49dd6SShawn McCarney             }
15658fb49dd6SShawn McCarney         }
1566d500549bSAnthony Wilson 
15678fb49dd6SShawn McCarney         callback(invConnections);
15688fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
15698fb49dd6SShawn McCarney     };
15708fb49dd6SShawn McCarney 
15718fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
15728fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
15738fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
15748fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
15758fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
15768fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
15778fb49dd6SShawn McCarney }
15788fb49dd6SShawn McCarney 
15798fb49dd6SShawn McCarney /**
1580adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
15818fb49dd6SShawn McCarney  *
15828fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1583d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1584d500549bSAnthony Wilson  * their LEDs, if any.
15858fb49dd6SShawn McCarney  *
15868fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
15878fb49dd6SShawn McCarney  * has been obtained.
15888fb49dd6SShawn McCarney  *
15898fb49dd6SShawn McCarney  * The callback must have the following signature:
15908fb49dd6SShawn McCarney  *   @code
1591adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
15928fb49dd6SShawn McCarney  *   @endcode
15938fb49dd6SShawn McCarney  *
15948fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
15958fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
15968fb49dd6SShawn McCarney  * implements ObjectManager.
15978fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
15988fb49dd6SShawn McCarney  */
15998fb49dd6SShawn McCarney template <typename Callback>
1600adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1601b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1602fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
16038fb49dd6SShawn McCarney     Callback&& callback)
16048fb49dd6SShawn McCarney {
1605adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
16068fb49dd6SShawn McCarney 
16078fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
160802cad96eSEd Tanous     auto respHandler =
160902cad96eSEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
16108fb49dd6SShawn McCarney          sensorNames](const boost::system::error_code ec,
161102cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
1612adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
16138fb49dd6SShawn McCarney         if (ec)
16148fb49dd6SShawn McCarney         {
1615adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1616adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
16178d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
16188fb49dd6SShawn McCarney             return;
16198fb49dd6SShawn McCarney         }
16208fb49dd6SShawn McCarney 
1621adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1622adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1623adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1624adc4f0dbSShawn McCarney 
16258fb49dd6SShawn McCarney         // Loop through returned object paths
16268fb49dd6SShawn McCarney         std::string sensorAssocPath;
16278fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
16288fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
16298fb49dd6SShawn McCarney         {
16308fb49dd6SShawn McCarney             const std::string& objPath =
16318fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
16328fb49dd6SShawn McCarney 
16338fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
16348fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
16358fb49dd6SShawn McCarney             {
16368fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
16378fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
16388fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
16398fb49dd6SShawn McCarney                 {
16408fb49dd6SShawn McCarney                     // Get Association interface for object path
1641711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
16428fb49dd6SShawn McCarney                     {
1643711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1644711ac7a9SEd Tanous                         {
1645711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1646711ac7a9SEd Tanous                             {
1647711ac7a9SEd Tanous                                 if (valueName == "endpoints")
16488fb49dd6SShawn McCarney                                 {
16498fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
16508fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1651711ac7a9SEd Tanous                                             &value);
1652711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1653711ac7a9SEd Tanous                                         !endpoints->empty())
16548fb49dd6SShawn McCarney                                     {
1655adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1656adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1657adc4f0dbSShawn McCarney                                             endpoints->front();
1658711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1659711ac7a9SEd Tanous                                                          invItemPath,
1660adc4f0dbSShawn McCarney                                                          sensorName);
16618fb49dd6SShawn McCarney                                     }
16628fb49dd6SShawn McCarney                                 }
16638fb49dd6SShawn McCarney                             }
1664711ac7a9SEd Tanous                         }
1665711ac7a9SEd Tanous                     }
16668fb49dd6SShawn McCarney                     break;
16678fb49dd6SShawn McCarney                 }
16688fb49dd6SShawn McCarney             }
16698fb49dd6SShawn McCarney         }
16708fb49dd6SShawn McCarney 
1671d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1672d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1673d500549bSAnthony Wilson         std::string inventoryAssocPath;
1674d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1675d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1676d500549bSAnthony Wilson         {
1677d500549bSAnthony Wilson             const std::string& objPath =
1678d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1679d500549bSAnthony Wilson 
1680d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1681d500549bSAnthony Wilson             {
1682d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1683d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1684d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1685d500549bSAnthony Wilson                 {
1686711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1687d500549bSAnthony Wilson                     {
1688711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1689711ac7a9SEd Tanous                         {
1690711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1691711ac7a9SEd Tanous                             {
1692711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1693d500549bSAnthony Wilson                                 {
1694d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1695d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1696711ac7a9SEd Tanous                                             &value);
1697711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1698711ac7a9SEd Tanous                                         !endpoints->empty())
1699d500549bSAnthony Wilson                                     {
1700711ac7a9SEd Tanous                                         // Add inventory item to vector
1701d500549bSAnthony Wilson                                         // Store LED path in inventory item
1702711ac7a9SEd Tanous                                         const std::string& ledPath =
1703711ac7a9SEd Tanous                                             endpoints->front();
1704d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1705d500549bSAnthony Wilson                                     }
1706d500549bSAnthony Wilson                                 }
1707d500549bSAnthony Wilson                             }
1708711ac7a9SEd Tanous                         }
1709711ac7a9SEd Tanous                     }
1710711ac7a9SEd Tanous 
1711d500549bSAnthony Wilson                     break;
1712d500549bSAnthony Wilson                 }
1713d500549bSAnthony Wilson             }
1714d500549bSAnthony Wilson         }
1715adc4f0dbSShawn McCarney         callback(inventoryItems);
1716adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
17178fb49dd6SShawn McCarney     };
17188fb49dd6SShawn McCarney 
17198fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
17208fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
1721d0090733SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
17228fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17238fb49dd6SShawn McCarney 
1724adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
17258fb49dd6SShawn McCarney }
17268fb49dd6SShawn McCarney 
17278fb49dd6SShawn McCarney /**
1728d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1729d500549bSAnthony Wilson  *
1730d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1731d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1732d500549bSAnthony Wilson  * inventoryItems vector.
1733d500549bSAnthony Wilson  *
1734d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1735d500549bSAnthony Wilson  * response.
1736d500549bSAnthony Wilson  *
1737d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1738d500549bSAnthony Wilson  * has been obtained.
1739d500549bSAnthony Wilson  *
1740d500549bSAnthony Wilson  * The callback must have the following signature:
1741d500549bSAnthony Wilson  *   @code
174242cbe538SGunnar Mills  *   callback()
1743d500549bSAnthony Wilson  *   @endcode
1744d500549bSAnthony Wilson  *
1745d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1746d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1747d500549bSAnthony Wilson  * last asynchronous function has completed.
1748d500549bSAnthony Wilson  *
1749d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1750d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1751d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1752d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1753d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1754d500549bSAnthony Wilson  * in recursive calls to this function.
1755d500549bSAnthony Wilson  */
1756d500549bSAnthony Wilson template <typename Callback>
1757d500549bSAnthony Wilson void getInventoryLedData(
1758d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1759d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1760fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1761d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1762d500549bSAnthony Wilson {
1763d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1764d500549bSAnthony Wilson 
1765d500549bSAnthony Wilson     // If no more connections left, call callback
1766d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1767d500549bSAnthony Wilson     {
176842cbe538SGunnar Mills         callback();
1769d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1770d500549bSAnthony Wilson         return;
1771d500549bSAnthony Wilson     }
1772d500549bSAnthony Wilson 
1773d500549bSAnthony Wilson     // Get inventory item data from current connection
1774fe04d49cSNan Zhou     auto it = ledConnections->begin();
1775fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1776d500549bSAnthony Wilson     if (it != ledConnections->end())
1777d500549bSAnthony Wilson     {
1778d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1779d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1780d500549bSAnthony Wilson         // Response handler for Get State property
17811e1e598dSJonathan Doman         auto respHandler =
17821e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1783f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
17841e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1785d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1786d500549bSAnthony Wilson             if (ec)
1787d500549bSAnthony Wilson             {
1788d500549bSAnthony Wilson                 BMCWEB_LOG_ERROR
1789d500549bSAnthony Wilson                     << "getInventoryLedData respHandler DBus error " << ec;
17908d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1791d500549bSAnthony Wilson                 return;
1792d500549bSAnthony Wilson             }
1793d500549bSAnthony Wilson 
17941e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Led state: " << state;
1795d500549bSAnthony Wilson             // Find inventory item with this LED object path
1796d500549bSAnthony Wilson             InventoryItem* inventoryItem =
1797d500549bSAnthony Wilson                 findInventoryItemForLed(*inventoryItems, ledPath);
1798d500549bSAnthony Wilson             if (inventoryItem != nullptr)
1799d500549bSAnthony Wilson             {
1800d500549bSAnthony Wilson                 // Store LED state in InventoryItem
180111ba3979SEd Tanous                 if (state.ends_with("On"))
1802d500549bSAnthony Wilson                 {
1803d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::ON;
1804d500549bSAnthony Wilson                 }
180511ba3979SEd Tanous                 else if (state.ends_with("Blink"))
1806d500549bSAnthony Wilson                 {
1807d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::BLINK;
1808d500549bSAnthony Wilson                 }
180911ba3979SEd Tanous                 else if (state.ends_with("Off"))
1810d500549bSAnthony Wilson                 {
1811d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::OFF;
1812d500549bSAnthony Wilson                 }
1813d500549bSAnthony Wilson                 else
1814d500549bSAnthony Wilson                 {
1815d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::UNKNOWN;
1816d500549bSAnthony Wilson                 }
1817d500549bSAnthony Wilson             }
1818d500549bSAnthony Wilson 
1819d500549bSAnthony Wilson             // Recurse to get LED data from next connection
1820d500549bSAnthony Wilson             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1821d500549bSAnthony Wilson                                 ledConnections, std::move(callback),
1822d500549bSAnthony Wilson                                 ledConnectionsIndex + 1);
1823d500549bSAnthony Wilson 
1824d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1825d500549bSAnthony Wilson         };
1826d500549bSAnthony Wilson 
1827d500549bSAnthony Wilson         // Get the State property for the current LED
18281e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
18291e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
18301e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
18311e1e598dSJonathan Doman             std::move(respHandler));
1832d500549bSAnthony Wilson     }
1833d500549bSAnthony Wilson 
1834d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1835d500549bSAnthony Wilson }
1836d500549bSAnthony Wilson 
1837d500549bSAnthony Wilson /**
1838d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1839d500549bSAnthony Wilson  *
1840d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1841d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1842d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1843d500549bSAnthony Wilson  *
1844d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1845d500549bSAnthony Wilson  * response.
1846d500549bSAnthony Wilson  *
1847d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1848d500549bSAnthony Wilson  * been obtained.
1849d500549bSAnthony Wilson  *
1850d500549bSAnthony Wilson  * The callback must have the following signature:
1851d500549bSAnthony Wilson  *   @code
185242cbe538SGunnar Mills  *   callback()
1853d500549bSAnthony Wilson  *   @endcode
1854d500549bSAnthony Wilson  *
1855d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1856d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1857d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1858d500549bSAnthony Wilson  */
1859d500549bSAnthony Wilson template <typename Callback>
1860d500549bSAnthony Wilson void getInventoryLeds(
1861d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1862d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1863d500549bSAnthony Wilson     Callback&& callback)
1864d500549bSAnthony Wilson {
1865d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1866d500549bSAnthony Wilson 
1867d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1868d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
1869d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1870d500549bSAnthony Wilson 
1871d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
1872002d39b4SEd Tanous     auto respHandler =
1873002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1874002d39b4SEd Tanous          inventoryItems](
1875b9d36b47SEd Tanous             const boost::system::error_code ec,
1876002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1877d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1878d500549bSAnthony Wilson         if (ec)
1879d500549bSAnthony Wilson         {
18808d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
1881d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1882d500549bSAnthony Wilson                              << ec;
1883d500549bSAnthony Wilson             return;
1884d500549bSAnthony Wilson         }
1885d500549bSAnthony Wilson 
1886d500549bSAnthony Wilson         // Build map of LED object paths to connections
1887fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1888fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
1889d500549bSAnthony Wilson 
1890d500549bSAnthony Wilson         // Loop through objects from GetSubTree
1891d500549bSAnthony Wilson         for (const std::pair<
1892d500549bSAnthony Wilson                  std::string,
1893d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1894d500549bSAnthony Wilson                  object : subtree)
1895d500549bSAnthony Wilson         {
1896d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
1897d500549bSAnthony Wilson             // items
1898d500549bSAnthony Wilson             const std::string& ledPath = object.first;
1899d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1900d500549bSAnthony Wilson             {
1901d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
1902d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
1903d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
1904d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1905d500549bSAnthony Wilson                                  << connection;
1906d500549bSAnthony Wilson             }
1907d500549bSAnthony Wilson         }
1908d500549bSAnthony Wilson 
1909d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1910d500549bSAnthony Wilson                             std::move(callback));
1911d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1912d500549bSAnthony Wilson     };
1913d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
1914d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
1915d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1916d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
1917d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1918d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1919d500549bSAnthony Wilson }
1920d500549bSAnthony Wilson 
1921d500549bSAnthony Wilson /**
192242cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
192342cbe538SGunnar Mills  *
192442cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
192542cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
192642cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
192742cbe538SGunnar Mills  *
192842cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
192942cbe538SGunnar Mills  * response.
193042cbe538SGunnar Mills  *
193142cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
193242cbe538SGunnar Mills  * when data has been obtained.
193342cbe538SGunnar Mills  *
193442cbe538SGunnar Mills  * The callback must have the following signature:
193542cbe538SGunnar Mills  *   @code
193642cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
193742cbe538SGunnar Mills  *   @endcode
193842cbe538SGunnar Mills  *
193942cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
194042cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
194142cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
194242cbe538SGunnar Mills  *        Supply Attributes
194342cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
194442cbe538SGunnar Mills  */
194542cbe538SGunnar Mills template <typename Callback>
194642cbe538SGunnar Mills void getPowerSupplyAttributesData(
1947b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
194842cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1949fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
195042cbe538SGunnar Mills     Callback&& callback)
195142cbe538SGunnar Mills {
195242cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
195342cbe538SGunnar Mills 
195442cbe538SGunnar Mills     if (psAttributesConnections.empty())
195542cbe538SGunnar Mills     {
195642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
195742cbe538SGunnar Mills         callback(inventoryItems);
195842cbe538SGunnar Mills         return;
195942cbe538SGunnar Mills     }
196042cbe538SGunnar Mills 
196142cbe538SGunnar Mills     // Assuming just one connection (service) for now
1962fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
196342cbe538SGunnar Mills 
196442cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
196542cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
196642cbe538SGunnar Mills 
196742cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
1968002d39b4SEd Tanous     auto respHandler =
1969002d39b4SEd Tanous         [sensorsAsyncResp, inventoryItems,
1970f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
1971002d39b4SEd Tanous             const boost::system::error_code ec, const uint32_t value) {
197242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
197342cbe538SGunnar Mills         if (ec)
197442cbe538SGunnar Mills         {
197542cbe538SGunnar Mills             BMCWEB_LOG_ERROR
197642cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
19778d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
197842cbe538SGunnar Mills             return;
197942cbe538SGunnar Mills         }
198042cbe538SGunnar Mills 
19811e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
198242cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
198342cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
198442cbe538SGunnar Mills         {
198555f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
198642cbe538SGunnar Mills             {
198742cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
19881e1e598dSJonathan Doman                     static_cast<int>(value);
198942cbe538SGunnar Mills             }
199042cbe538SGunnar Mills         }
199142cbe538SGunnar Mills 
199242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
199342cbe538SGunnar Mills         callback(inventoryItems);
199442cbe538SGunnar Mills     };
199542cbe538SGunnar Mills 
199642cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
199742cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
19981e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
19991e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
20001e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
20011e1e598dSJonathan Doman         std::move(respHandler));
200242cbe538SGunnar Mills 
200342cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
200442cbe538SGunnar Mills }
200542cbe538SGunnar Mills 
200642cbe538SGunnar Mills /**
200742cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
200842cbe538SGunnar Mills  *
200942cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
201042cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
201142cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
201242cbe538SGunnar Mills  * item.
201342cbe538SGunnar Mills  *
201442cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
201542cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
201642cbe538SGunnar Mills  *
201742cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
201842cbe538SGunnar Mills  * when information has been obtained.
201942cbe538SGunnar Mills  *
202042cbe538SGunnar Mills  * The callback must have the following signature:
202142cbe538SGunnar Mills  *   @code
202242cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
202342cbe538SGunnar Mills  *   @endcode
202442cbe538SGunnar Mills  *
202542cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
202642cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
202742cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
202842cbe538SGunnar Mills  */
202942cbe538SGunnar Mills template <typename Callback>
203042cbe538SGunnar Mills void getPowerSupplyAttributes(
203142cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
203242cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
203342cbe538SGunnar Mills     Callback&& callback)
203442cbe538SGunnar Mills {
203542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
203642cbe538SGunnar Mills 
203742cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2038a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
203942cbe538SGunnar Mills     {
204042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
204142cbe538SGunnar Mills         callback(inventoryItems);
204242cbe538SGunnar Mills         return;
204342cbe538SGunnar Mills     }
204442cbe538SGunnar Mills 
204542cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
204642cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
204742cbe538SGunnar Mills 
204842cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2049b9d36b47SEd Tanous     auto respHandler =
2050b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2051b9d36b47SEd Tanous          inventoryItems](
2052b9d36b47SEd Tanous             const boost::system::error_code ec,
2053b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
205442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
205542cbe538SGunnar Mills         if (ec)
205642cbe538SGunnar Mills         {
20578d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
205842cbe538SGunnar Mills             BMCWEB_LOG_ERROR
205942cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
206042cbe538SGunnar Mills             return;
206142cbe538SGunnar Mills         }
206226f6976fSEd Tanous         if (subtree.empty())
206342cbe538SGunnar Mills         {
206442cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
206542cbe538SGunnar Mills             callback(inventoryItems);
206642cbe538SGunnar Mills             return;
206742cbe538SGunnar Mills         }
206842cbe538SGunnar Mills 
206942cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
207042cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
207142cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
2072fe04d49cSNan Zhou         std::map<std::string, std::string> psAttributesConnections;
207342cbe538SGunnar Mills 
207442cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
207542cbe538SGunnar Mills         {
207642cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
207742cbe538SGunnar Mills             callback(inventoryItems);
207842cbe538SGunnar Mills             return;
207942cbe538SGunnar Mills         }
208042cbe538SGunnar Mills 
208142cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
208242cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
208342cbe538SGunnar Mills 
208442cbe538SGunnar Mills         if (connection.empty())
208542cbe538SGunnar Mills         {
208642cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
208742cbe538SGunnar Mills             callback(inventoryItems);
208842cbe538SGunnar Mills             return;
208942cbe538SGunnar Mills         }
209042cbe538SGunnar Mills 
209142cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
209242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
209342cbe538SGunnar Mills                          << connection;
209442cbe538SGunnar Mills 
209542cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
209642cbe538SGunnar Mills                                      psAttributesConnections,
209742cbe538SGunnar Mills                                      std::move(callback));
209842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
209942cbe538SGunnar Mills     };
210042cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
210142cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
210242cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
210342cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
210442cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
210542cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
210642cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
210742cbe538SGunnar Mills }
210842cbe538SGunnar Mills 
210942cbe538SGunnar Mills /**
2110adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
21118fb49dd6SShawn McCarney  *
21128fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2113adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
21148fb49dd6SShawn McCarney  *
2115adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2116adc4f0dbSShawn McCarney  * response.
21178fb49dd6SShawn McCarney  *
2118adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2119adc4f0dbSShawn McCarney  * inventory items have been obtained.
2120adc4f0dbSShawn McCarney  *
2121adc4f0dbSShawn McCarney  * The callback must have the following signature:
2122adc4f0dbSShawn McCarney  *   @code
2123adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2124adc4f0dbSShawn McCarney  *   @endcode
21258fb49dd6SShawn McCarney  *
21268fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
21278fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
21288fb49dd6SShawn McCarney  * implements ObjectManager.
2129adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
21308fb49dd6SShawn McCarney  */
2131adc4f0dbSShawn McCarney template <typename Callback>
2132d0090733SEd Tanous static void
2133d0090733SEd Tanous     getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2134fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>> sensorNames,
2135adc4f0dbSShawn McCarney                       Callback&& callback)
21368fb49dd6SShawn McCarney {
2137adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2138adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2139d0090733SEd Tanous         [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
2140adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2141adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
21428fb49dd6SShawn McCarney         auto getInventoryItemsConnectionsCb =
2143d0090733SEd Tanous             [sensorsAsyncResp, inventoryItems,
2144f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
2145fe04d49cSNan Zhou                 std::shared_ptr<std::set<std::string>> invConnections) {
21468fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2147002d39b4SEd Tanous             auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2148d500549bSAnthony Wilson                                             callback{std::move(callback)}]() {
2149d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
215042cbe538SGunnar Mills 
2151002d39b4SEd Tanous                 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2152002d39b4SEd Tanous                                            callback{std::move(callback)}]() {
215342cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
215442cbe538SGunnar Mills                     // Find Power Supply Attributes and get the data
2155002d39b4SEd Tanous                     getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
215642cbe538SGunnar Mills                                              std::move(callback));
215742cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
215842cbe538SGunnar Mills                 };
215942cbe538SGunnar Mills 
2160d500549bSAnthony Wilson                 // Find led connections and get the data
2161d500549bSAnthony Wilson                 getInventoryLeds(sensorsAsyncResp, inventoryItems,
216242cbe538SGunnar Mills                                  std::move(getInventoryLedsCb));
2163d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2164d500549bSAnthony Wilson             };
21658fb49dd6SShawn McCarney 
2166adc4f0dbSShawn McCarney             // Get inventory item data from connections
2167adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2168d0090733SEd Tanous                                   invConnections,
2169d500549bSAnthony Wilson                                   std::move(getInventoryItemsDataCb));
21708fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
21718fb49dd6SShawn McCarney         };
21728fb49dd6SShawn McCarney 
2173adc4f0dbSShawn McCarney         // Get connections that provide inventory item data
2174002d39b4SEd Tanous         getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
21758fb49dd6SShawn McCarney                                      std::move(getInventoryItemsConnectionsCb));
2176adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
21778fb49dd6SShawn McCarney     };
21788fb49dd6SShawn McCarney 
2179adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2180d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
2181adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2182adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2183adc4f0dbSShawn McCarney }
2184adc4f0dbSShawn McCarney 
2185adc4f0dbSShawn McCarney /**
2186adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2187adc4f0dbSShawn McCarney  *
2188adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2189adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2190adc4f0dbSShawn McCarney  * array.
2191adc4f0dbSShawn McCarney  *
2192adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2193adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2194adc4f0dbSShawn McCarney  * object.
2195adc4f0dbSShawn McCarney  *
2196adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2197adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2198adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2199adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2200adc4f0dbSShawn McCarney  */
220123a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2202adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2203adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2204adc4f0dbSShawn McCarney {
2205adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2206adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2207adc4f0dbSShawn McCarney     {
2208adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2209adc4f0dbSShawn McCarney         {
2210adc4f0dbSShawn McCarney             return powerSupply;
2211adc4f0dbSShawn McCarney         }
2212adc4f0dbSShawn McCarney     }
2213adc4f0dbSShawn McCarney 
2214adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2215adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2216adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2217adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2218adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2219adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2220adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2221adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2222adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2223adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2224adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2225d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2226adc4f0dbSShawn McCarney 
222742cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
222842cbe538SGunnar Mills     {
222942cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
223042cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
223142cbe538SGunnar Mills     }
223242cbe538SGunnar Mills 
223342cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2234adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2235adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2236adc4f0dbSShawn McCarney 
2237adc4f0dbSShawn McCarney     return powerSupply;
22388fb49dd6SShawn McCarney }
22398fb49dd6SShawn McCarney 
22408fb49dd6SShawn McCarney /**
2241de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2242de629b6eSShawn McCarney  *
2243de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2244de629b6eSShawn McCarney  *
2245de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2246de629b6eSShawn McCarney  * information has been obtained.
2247de629b6eSShawn McCarney  *
2248adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2249de629b6eSShawn McCarney  *
2250de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2251de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2252de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2253de629b6eSShawn McCarney  *
2254de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2255de629b6eSShawn McCarney  *
2256adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2257adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2258adc4f0dbSShawn McCarney  *
2259de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2260adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2261de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2262de629b6eSShawn McCarney  * implements ObjectManager.
2263adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2264de629b6eSShawn McCarney  */
226523a21a1cSEd Tanous inline void getSensorData(
226681ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2267fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2268fe04d49cSNan Zhou     const std::set<std::string>& connections,
2269b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2270de629b6eSShawn McCarney {
2271de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2272de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2273de629b6eSShawn McCarney     for (const std::string& connection : connections)
2274de629b6eSShawn McCarney     {
2275de629b6eSShawn McCarney         // Response handler to process managed objects
2276002d39b4SEd Tanous         auto getManagedObjectsCb =
2277002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
2278002d39b4SEd Tanous              inventoryItems](const boost::system::error_code ec,
227902cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
2280de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2281de629b6eSShawn McCarney             if (ec)
2282de629b6eSShawn McCarney             {
2283de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
22848d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2285de629b6eSShawn McCarney                 return;
2286de629b6eSShawn McCarney             }
2287de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2288de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2289de629b6eSShawn McCarney             {
2290de629b6eSShawn McCarney                 const std::string& objPath =
2291de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2292de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2293de629b6eSShawn McCarney                                  << objPath;
2294de629b6eSShawn McCarney 
2295de629b6eSShawn McCarney                 std::vector<std::string> split;
2296de629b6eSShawn McCarney                 // Reserve space for
2297de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2298de629b6eSShawn McCarney                 split.reserve(6);
2299de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2300de629b6eSShawn McCarney                 if (split.size() < 6)
2301de629b6eSShawn McCarney                 {
2302de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2303de629b6eSShawn McCarney                                      << objPath;
2304de629b6eSShawn McCarney                     continue;
2305de629b6eSShawn McCarney                 }
2306de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2307de629b6eSShawn McCarney                 // string at the beginning
2308de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2309de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2310de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2311de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
231249c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2313de629b6eSShawn McCarney                 {
2314accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2315de629b6eSShawn McCarney                     continue;
2316de629b6eSShawn McCarney                 }
2317de629b6eSShawn McCarney 
2318adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2319adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2320adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2321adc4f0dbSShawn McCarney 
232295a3ecadSAnthony Wilson                 const std::string& sensorSchema =
232381ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
232495a3ecadSAnthony Wilson 
232595a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
232695a3ecadSAnthony Wilson 
2327928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2328928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
232995a3ecadSAnthony Wilson                 {
2330c1d019a6SEd Tanous                     std::string sensorTypeEscaped(sensorType);
2331c1d019a6SEd Tanous                     sensorTypeEscaped.erase(
2332c1d019a6SEd Tanous                         std::remove(sensorTypeEscaped.begin(),
2333c1d019a6SEd Tanous                                     sensorTypeEscaped.end(), '_'),
2334c1d019a6SEd Tanous                         sensorTypeEscaped.end());
2335c1d019a6SEd Tanous                     std::string sensorId(sensorTypeEscaped);
2336c1d019a6SEd Tanous                     sensorId += "_";
2337c1d019a6SEd Tanous                     sensorId += sensorName;
2338c1d019a6SEd Tanous 
23398d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
2340c1d019a6SEd Tanous                         crow::utility::urlFromPieces(
2341c1d019a6SEd Tanous                             "redfish", "v1", "Chassis",
2342c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
2343c1d019a6SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
23448d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
234595a3ecadSAnthony Wilson                 }
234695a3ecadSAnthony Wilson                 else
234795a3ecadSAnthony Wilson                 {
2348271584abSEd Tanous                     std::string fieldName;
2349928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2350928fefb9SNan Zhou                     {
2351928fefb9SNan Zhou                         fieldName = "Members";
2352928fefb9SNan Zhou                     }
2353928fefb9SNan Zhou                     else if (sensorType == "temperature")
2354de629b6eSShawn McCarney                     {
2355de629b6eSShawn McCarney                         fieldName = "Temperatures";
2356de629b6eSShawn McCarney                     }
2357de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2358de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2359de629b6eSShawn McCarney                     {
2360de629b6eSShawn McCarney                         fieldName = "Fans";
2361de629b6eSShawn McCarney                     }
2362de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2363de629b6eSShawn McCarney                     {
2364de629b6eSShawn McCarney                         fieldName = "Voltages";
2365de629b6eSShawn McCarney                     }
2366de629b6eSShawn McCarney                     else if (sensorType == "power")
2367de629b6eSShawn McCarney                     {
236855f79e6fSEd Tanous                         if (sensorName == "total_power")
2369028f7ebcSEddie James                         {
2370028f7ebcSEddie James                             fieldName = "PowerControl";
2371028f7ebcSEddie James                         }
2372adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2373adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2374028f7ebcSEddie James                         {
2375de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2376de629b6eSShawn McCarney                         }
2377adc4f0dbSShawn McCarney                         else
2378adc4f0dbSShawn McCarney                         {
2379adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2380adc4f0dbSShawn McCarney                             continue;
2381adc4f0dbSShawn McCarney                         }
2382028f7ebcSEddie James                     }
2383de629b6eSShawn McCarney                     else
2384de629b6eSShawn McCarney                     {
2385de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2386de629b6eSShawn McCarney                                          << sensorType;
2387de629b6eSShawn McCarney                         continue;
2388de629b6eSShawn McCarney                     }
2389de629b6eSShawn McCarney 
2390de629b6eSShawn McCarney                     nlohmann::json& tempArray =
23918d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2392adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
239349c53ac9SJohnathan Mantey                     {
2394adc4f0dbSShawn McCarney                         if (tempArray.empty())
23957ab06f49SGunnar Mills                         {
239695a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
239795a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
239895a3ecadSAnthony Wilson                             // naming in power.hpp.
23991476687dSEd Tanous                             nlohmann::json::object_t power;
24001476687dSEd Tanous                             power["@odata.id"] =
2401adc4f0dbSShawn McCarney                                 "/redfish/v1/Chassis/" +
240281ce609eSEd Tanous                                 sensorsAsyncResp->chassisId + "/" +
240381ce609eSEd Tanous                                 sensorsAsyncResp->chassisSubNode + "#/" +
24041476687dSEd Tanous                                 fieldName + "/0";
24051476687dSEd Tanous                             tempArray.push_back(std::move(power));
2406adc4f0dbSShawn McCarney                         }
2407adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2408adc4f0dbSShawn McCarney                     }
2409adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2410adc4f0dbSShawn McCarney                     {
2411adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2412adc4f0dbSShawn McCarney                         {
2413adc4f0dbSShawn McCarney                             sensorJson =
2414adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
241581ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2416adc4f0dbSShawn McCarney                         }
241749c53ac9SJohnathan Mantey                     }
2418928fefb9SNan Zhou                     else if (fieldName == "Members")
2419928fefb9SNan Zhou                     {
2420677bb756SEd Tanous                         std::string sensorTypeEscaped(sensorType);
2421677bb756SEd Tanous                         sensorTypeEscaped.erase(
2422677bb756SEd Tanous                             std::remove(sensorTypeEscaped.begin(),
2423677bb756SEd Tanous                                         sensorTypeEscaped.end(), '_'),
2424677bb756SEd Tanous                             sensorTypeEscaped.end());
2425677bb756SEd Tanous                         std::string sensorId(sensorTypeEscaped);
2426677bb756SEd Tanous                         sensorId += "_";
2427677bb756SEd Tanous                         sensorId += sensorName;
2428677bb756SEd Tanous 
24291476687dSEd Tanous                         nlohmann::json::object_t member;
2430677bb756SEd Tanous                         member["@odata.id"] = crow::utility::urlFromPieces(
2431677bb756SEd Tanous                             "redfish", "v1", "Chassis",
2432677bb756SEd Tanous                             sensorsAsyncResp->chassisId,
2433677bb756SEd Tanous                             sensorsAsyncResp->chassisSubNode, sensorId);
24341476687dSEd Tanous                         tempArray.push_back(std::move(member));
2435928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2436928fefb9SNan Zhou                     }
243749c53ac9SJohnathan Mantey                     else
243849c53ac9SJohnathan Mantey                     {
24391476687dSEd Tanous                         nlohmann::json::object_t member;
24401476687dSEd Tanous                         member["@odata.id"] = "/redfish/v1/Chassis/" +
24411476687dSEd Tanous                                               sensorsAsyncResp->chassisId +
24421476687dSEd Tanous                                               "/" +
24431476687dSEd Tanous                                               sensorsAsyncResp->chassisSubNode +
24441476687dSEd Tanous                                               "#/" + fieldName + "/";
24451476687dSEd Tanous                         tempArray.push_back(std::move(member));
2446adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
244749c53ac9SJohnathan Mantey                     }
244895a3ecadSAnthony Wilson                 }
2449de629b6eSShawn McCarney 
2450adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2451adc4f0dbSShawn McCarney                 {
24521d7c0054SEd Tanous                     objectInterfacesToJson(sensorName, sensorType,
24531d7c0054SEd Tanous                                            sensorsAsyncResp->chassisSubNode,
24541d7c0054SEd Tanous                                            objDictEntry.second, *sensorJson,
24551d7c0054SEd Tanous                                            inventoryItem);
24561d7c0054SEd Tanous 
24571d7c0054SEd Tanous                     std::string path = "/xyz/openbmc_project/sensors/";
24581d7c0054SEd Tanous                     path += sensorType;
24591d7c0054SEd Tanous                     path += "/";
24601d7c0054SEd Tanous                     path += sensorName;
2461c1d019a6SEd Tanous                     sensorsAsyncResp->addMetadata(*sensorJson, path);
2462adc4f0dbSShawn McCarney                 }
2463de629b6eSShawn McCarney             }
246481ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
246549c53ac9SJohnathan Mantey             {
246681ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2467928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2468928fefb9SNan Zhou                         sensors::node::sensors &&
2469928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2470928fefb9SNan Zhou                 {
2471928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2472928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2473928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2474928fefb9SNan Zhou                             .size();
2475928fefb9SNan Zhou                 }
2476928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2477928fefb9SNan Zhou                          sensors::node::thermal)
24788bd25ccdSJames Feist                 {
247981ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
24808bd25ccdSJames Feist                 }
248149c53ac9SJohnathan Mantey             }
2482de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2483de629b6eSShawn McCarney         };
2484de629b6eSShawn McCarney 
2485de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2486d0090733SEd Tanous             getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
2487de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
248823a21a1cSEd Tanous     }
2489de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2490de629b6eSShawn McCarney }
2491de629b6eSShawn McCarney 
2492fe04d49cSNan Zhou inline void
2493fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2494fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
24951abe55efSEd Tanous {
2496fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2497fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
249855c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2499adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2500d0090733SEd Tanous             [sensorsAsyncResp, sensorNames,
2501d0090733SEd Tanous              connections](const std::shared_ptr<std::vector<InventoryItem>>&
2502adc4f0dbSShawn McCarney                               inventoryItems) {
2503adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
250449c53ac9SJohnathan Mantey             // Get sensor data and store results in JSON
2505002d39b4SEd Tanous             getSensorData(sensorsAsyncResp, sensorNames, connections,
2506d0090733SEd Tanous                           inventoryItems);
2507adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2508adc4f0dbSShawn McCarney         };
2509adc4f0dbSShawn McCarney 
2510adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2511d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2512adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2513adc4f0dbSShawn McCarney 
251455c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb exit";
251508777fb0SLewanczyk, Dawid     };
2516de629b6eSShawn McCarney 
2517de629b6eSShawn McCarney     // Get set of connections that provide sensor values
251881ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
251995a3ecadSAnthony Wilson }
252095a3ecadSAnthony Wilson 
252195a3ecadSAnthony Wilson /**
252295a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
252395a3ecadSAnthony Wilson  *        chassis.
252495a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
252595a3ecadSAnthony Wilson  */
2526b5a76932SEd Tanous inline void
252781ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
252895a3ecadSAnthony Wilson {
252995a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
253095a3ecadSAnthony Wilson     auto getChassisCb =
253181ce609eSEd Tanous         [sensorsAsyncResp](
2532fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
253395a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "getChassisCb enter";
253481ce609eSEd Tanous         processSensorList(sensorsAsyncResp, sensorNames);
253555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassisCb exit";
253608777fb0SLewanczyk, Dawid     };
2537928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2538928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2539928fefb9SNan Zhou     {
25408d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
25418d1b46d7Szhanghch05             nlohmann::json::array();
2542928fefb9SNan Zhou     }
254326f03899SShawn McCarney     // Get set of sensors in chassis
25447f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
25457f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
25467f1cc26dSEd Tanous                std::move(getChassisCb));
254755c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2548271584abSEd Tanous }
254908777fb0SLewanczyk, Dawid 
2550413961deSRichard Marian Thomaiyar /**
255149c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
255249c53ac9SJohnathan Mantey  * the chassis node
255349c53ac9SJohnathan Mantey  *
255449c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
255549c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
255649c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
255749c53ac9SJohnathan Mantey  *                         repeated calls to this function
255849c53ac9SJohnathan Mantey  */
2559fe04d49cSNan Zhou inline bool
2560fe04d49cSNan Zhou     findSensorNameUsingSensorPath(std::string_view sensorName,
256102cad96eSEd Tanous                                   const std::set<std::string>& sensorsList,
2562fe04d49cSNan Zhou                                   std::set<std::string>& sensorsModified)
256349c53ac9SJohnathan Mantey {
2564fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
256549c53ac9SJohnathan Mantey     {
256628aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2567b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
256828aa8de5SGeorge Liu         if (thisSensorName.empty())
256949c53ac9SJohnathan Mantey         {
257049c53ac9SJohnathan Mantey             continue;
257149c53ac9SJohnathan Mantey         }
257249c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
257349c53ac9SJohnathan Mantey         {
257449c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
257549c53ac9SJohnathan Mantey             return true;
257649c53ac9SJohnathan Mantey         }
257749c53ac9SJohnathan Mantey     }
257849c53ac9SJohnathan Mantey     return false;
257949c53ac9SJohnathan Mantey }
258049c53ac9SJohnathan Mantey 
2581c71d6125SEd Tanous inline std::pair<std::string, std::string>
2582c71d6125SEd Tanous     splitSensorNameAndType(std::string_view sensorId)
2583c71d6125SEd Tanous {
2584c71d6125SEd Tanous     size_t index = sensorId.find('_');
2585c71d6125SEd Tanous     if (index == std::string::npos)
2586c71d6125SEd Tanous     {
2587c71d6125SEd Tanous         return std::make_pair<std::string, std::string>("", "");
2588c71d6125SEd Tanous     }
2589c71d6125SEd Tanous     std::string sensorType{sensorId.substr(0, index)};
2590c71d6125SEd Tanous     std::string sensorName{sensorId.substr(index + 1)};
2591c71d6125SEd Tanous     // fan_pwm and fan_tach need special handling
2592c71d6125SEd Tanous     if (sensorType == "fantach" || sensorType == "fanpwm")
2593c71d6125SEd Tanous     {
2594c71d6125SEd Tanous         sensorType.insert(3, 1, '_');
2595c71d6125SEd Tanous     }
2596c71d6125SEd Tanous     return std::make_pair(sensorType, sensorName);
2597c71d6125SEd Tanous }
2598c71d6125SEd Tanous 
259949c53ac9SJohnathan Mantey /**
2600413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2601413961deSRichard Marian Thomaiyar  *
26028d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
26034bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2604413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2605413961deSRichard Marian Thomaiyar  */
260623a21a1cSEd Tanous inline void setSensorsOverride(
2607b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
26084bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2609397fd61fSjayaprakash Mutyala         allCollections)
2610413961deSRichard Marian Thomaiyar {
261170d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
26124bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2613413961deSRichard Marian Thomaiyar 
2614543f4400SEd Tanous     const char* propertyValueName = nullptr;
2615f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2616413961deSRichard Marian Thomaiyar     std::string memberId;
2617543f4400SEd Tanous     double value = 0.0;
2618f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2619f65af9e8SRichard Marian Thomaiyar     {
2620f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2621f65af9e8SRichard Marian Thomaiyar         {
2622f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2623f65af9e8SRichard Marian Thomaiyar         }
2624f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2625f65af9e8SRichard Marian Thomaiyar         {
2626f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2627f65af9e8SRichard Marian Thomaiyar         }
2628f65af9e8SRichard Marian Thomaiyar         else
2629f65af9e8SRichard Marian Thomaiyar         {
2630f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2631f65af9e8SRichard Marian Thomaiyar         }
2632f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2633f65af9e8SRichard Marian Thomaiyar         {
26348d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
26358d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
26368d1b46d7Szhanghch05                                      value))
2637413961deSRichard Marian Thomaiyar             {
2638413961deSRichard Marian Thomaiyar                 return;
2639413961deSRichard Marian Thomaiyar             }
2640f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2641f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2642f65af9e8SRichard Marian Thomaiyar         }
2643f65af9e8SRichard Marian Thomaiyar     }
26444bb3dc34SCarol Wang 
2645002d39b4SEd Tanous     auto getChassisSensorListCb =
2646002d39b4SEd Tanous         [sensorAsyncResp, overrideMap](
2647fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorsList) {
264849c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
264949c53ac9SJohnathan Mantey         // chassis node
2650fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2651fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2652f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2653413961deSRichard Marian Thomaiyar         {
2654f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2655c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
2656c71d6125SEd Tanous                 splitSensorNameAndType(sensor);
2657c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2658c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2659f65af9e8SRichard Marian Thomaiyar             {
2660f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
26618d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2662f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2663413961deSRichard Marian Thomaiyar                 return;
2664413961deSRichard Marian Thomaiyar             }
2665f65af9e8SRichard Marian Thomaiyar         }
2666413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2667002d39b4SEd Tanous         auto getObjectsWithConnectionCb =
2668fe04d49cSNan Zhou             [sensorAsyncResp,
2669fe04d49cSNan Zhou              overrideMap](const std::set<std::string>& /*connections*/,
2670002d39b4SEd Tanous                           const std::set<std::pair<std::string, std::string>>&
2671413961deSRichard Marian Thomaiyar                               objectsWithConnection) {
2672f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2673413961deSRichard Marian Thomaiyar             {
2674413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2675f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2676f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2677f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
26784f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2679a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2680a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2681413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2682413961deSRichard Marian Thomaiyar                                                : "Voltages",
2683f65af9e8SRichard Marian Thomaiyar                                            "Count");
2684f65af9e8SRichard Marian Thomaiyar                 return;
2685f65af9e8SRichard Marian Thomaiyar             }
2686f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2687f65af9e8SRichard Marian Thomaiyar             {
268828aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
268928aa8de5SGeorge Liu                 std::string sensorName = path.filename();
269028aa8de5SGeorge Liu                 if (sensorName.empty())
2691f65af9e8SRichard Marian Thomaiyar                 {
26924f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2693f65af9e8SRichard Marian Thomaiyar                     return;
2694f65af9e8SRichard Marian Thomaiyar                 }
2695f65af9e8SRichard Marian Thomaiyar 
2696f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2697f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2698f65af9e8SRichard Marian Thomaiyar                 {
2699f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2700f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
27014f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2702413961deSRichard Marian Thomaiyar                     return;
2703413961deSRichard Marian Thomaiyar                 }
2704413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2705f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2706413961deSRichard Marian Thomaiyar                     if (ec)
2707413961deSRichard Marian Thomaiyar                     {
27084f277b54SJayaprakash Mutyala                         if (ec.value() ==
27094f277b54SJayaprakash Mutyala                             boost::system::errc::permission_denied)
27104f277b54SJayaprakash Mutyala                         {
27114f277b54SJayaprakash Mutyala                             BMCWEB_LOG_WARNING
27124f277b54SJayaprakash Mutyala                                 << "Manufacturing mode is not Enabled...can't "
27134f277b54SJayaprakash Mutyala                                    "Override the sensor value. ";
27144f277b54SJayaprakash Mutyala 
27154f277b54SJayaprakash Mutyala                             messages::insufficientPrivilege(
27168d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
2717413961deSRichard Marian Thomaiyar                             return;
2718413961deSRichard Marian Thomaiyar                         }
27194f277b54SJayaprakash Mutyala                         BMCWEB_LOG_DEBUG
27204f277b54SJayaprakash Mutyala                             << "setOverrideValueStatus DBUS error: " << ec;
27214f277b54SJayaprakash Mutyala                         messages::internalError(
27224f277b54SJayaprakash Mutyala                             sensorAsyncResp->asyncResp->res);
27234f277b54SJayaprakash Mutyala                     }
2724413961deSRichard Marian Thomaiyar                     },
27254f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
27264f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2727168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2728f65af9e8SRichard Marian Thomaiyar             }
2729413961deSRichard Marian Thomaiyar         };
2730413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2731413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2732413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2733413961deSRichard Marian Thomaiyar     };
2734413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
27357f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
27367f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
27377f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2738413961deSRichard Marian Thomaiyar }
2739413961deSRichard Marian Thomaiyar 
2740a0ec28b6SAdrian Ambrożewicz /**
2741a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2742a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2743a0ec28b6SAdrian Ambrożewicz  *
2744a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2745a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2746a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2747a0ec28b6SAdrian Ambrożewicz  *
2748a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2749a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2750a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2751a0ec28b6SAdrian Ambrożewicz  */
2752021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2753021d32cfSKrzysztof Grobelny                                  const std::string& node,
2754a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2755a0ec28b6SAdrian Ambrożewicz {
275602da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
275702da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
275802da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
275902da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2760a0ec28b6SAdrian Ambrożewicz     {
2761a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2762a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2763a0ec28b6SAdrian Ambrożewicz         return;
2764a0ec28b6SAdrian Ambrożewicz     }
2765d51e072fSKrzysztof Grobelny 
276672374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2767fe04d49cSNan Zhou     auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2768a0ec28b6SAdrian Ambrożewicz                         const boost::beast::http::status status,
2769fe04d49cSNan Zhou                         const std::map<std::string, std::string>& uriToDbus) {
2770fe04d49cSNan Zhou         mapCompleteCb(status, uriToDbus);
2771fe04d49cSNan Zhou     };
2772a0ec28b6SAdrian Ambrożewicz 
2773a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2774d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2775a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2776a0ec28b6SAdrian Ambrożewicz }
2777a0ec28b6SAdrian Ambrożewicz 
2778bacb2162SNan Zhou namespace sensors
2779bacb2162SNan Zhou {
2780928fefb9SNan Zhou 
2781bacb2162SNan Zhou inline void getChassisCallback(
2782c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2783c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2784fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2785bacb2162SNan Zhou {
2786bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
2787bacb2162SNan Zhou 
2788c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2789c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2790bacb2162SNan Zhou     {
2791bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2792bacb2162SNan Zhou 
2793bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2794bacb2162SNan Zhou         std::string sensorName = path.filename();
2795bacb2162SNan Zhou         if (sensorName.empty())
2796bacb2162SNan Zhou         {
2797bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2798c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2799bacb2162SNan Zhou             return;
2800bacb2162SNan Zhou         }
2801c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
2802c1d019a6SEd Tanous         // fan_tach has an underscore in it, so remove it to "normalize" the
2803c1d019a6SEd Tanous         // type in the URI
2804c1d019a6SEd Tanous         type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2805c1d019a6SEd Tanous 
28061476687dSEd Tanous         nlohmann::json::object_t member;
2807c1d019a6SEd Tanous         std::string id = type;
2808c1d019a6SEd Tanous         id += "_";
2809c1d019a6SEd Tanous         id += sensorName;
2810c1d019a6SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
2811c1d019a6SEd Tanous             "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2812c1d019a6SEd Tanous 
28131476687dSEd Tanous         entriesArray.push_back(std::move(member));
2814bacb2162SNan Zhou     }
2815bacb2162SNan Zhou 
2816c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
2817bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2818bacb2162SNan Zhou }
2819e6bd846dSNan Zhou 
2820de167a6fSNan Zhou inline void
2821de167a6fSNan Zhou     handleSensorCollectionGet(App& app, const crow::Request& req,
2822de167a6fSNan Zhou                               const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2823de167a6fSNan Zhou                               const std::string& chassisId)
2824de167a6fSNan Zhou {
2825de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2826de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2827de167a6fSNan Zhou     };
2828de167a6fSNan Zhou     query_param::Query delegatedQuery;
28293ba00073SCarson Labrado     if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
2830de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2831de167a6fSNan Zhou     {
2832de167a6fSNan Zhou         return;
2833de167a6fSNan Zhou     }
2834de167a6fSNan Zhou 
2835de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2836de167a6fSNan Zhou     {
2837de167a6fSNan Zhou         // we perform efficient expand.
2838de167a6fSNan Zhou         auto asyncResp = std::make_shared<SensorsAsyncResp>(
2839de167a6fSNan Zhou             aResp, chassisId, sensors::dbus::sensorPaths,
2840de167a6fSNan Zhou             sensors::node::sensors,
2841de167a6fSNan Zhou             /*efficientExpand=*/true);
2842de167a6fSNan Zhou         getChassisData(asyncResp);
2843de167a6fSNan Zhou 
2844de167a6fSNan Zhou         BMCWEB_LOG_DEBUG
2845de167a6fSNan Zhou             << "SensorCollection doGet exit via efficient expand handler";
2846de167a6fSNan Zhou         return;
28470bad320cSEd Tanous     }
2848de167a6fSNan Zhou 
2849de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2850de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
2851c1d019a6SEd Tanous     getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2852c1d019a6SEd Tanous                std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2853c1d019a6SEd Tanous                                sensors::node::sensors));
2854c1d019a6SEd Tanous }
28557f1cc26dSEd Tanous 
2856c1d019a6SEd Tanous inline void
2857c1d019a6SEd Tanous     getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2858c1d019a6SEd Tanous                       const std::string& sensorPath,
2859c1d019a6SEd Tanous                       const ::dbus::utility::MapperGetObject& mapperResponse)
2860c1d019a6SEd Tanous {
2861c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2862c1d019a6SEd Tanous     {
2863c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2864c1d019a6SEd Tanous         return;
2865c1d019a6SEd Tanous     }
2866c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2867c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
2868c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2869c1d019a6SEd Tanous     BMCWEB_LOG_DEBUG << "Path " << sensorPath;
2870c1343bf6SKrzysztof Grobelny 
2871c1343bf6SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2872c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2873c1d019a6SEd Tanous         [asyncResp,
2874c1d019a6SEd Tanous          sensorPath](const boost::system::error_code ec,
2875c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2876c1d019a6SEd Tanous         if (ec)
2877c1d019a6SEd Tanous         {
2878c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2879c1d019a6SEd Tanous             return;
2880c1d019a6SEd Tanous         }
2881c1d019a6SEd Tanous         sdbusplus::message::object_path path(sensorPath);
2882c1d019a6SEd Tanous         std::string name = path.filename();
2883c1d019a6SEd Tanous         path = path.parent_path();
2884c1d019a6SEd Tanous         std::string type = path.filename();
2885c1d019a6SEd Tanous         objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2886c1d019a6SEd Tanous                                asyncResp->res.jsonValue, nullptr);
2887c1343bf6SKrzysztof Grobelny         });
2888de167a6fSNan Zhou }
2889de167a6fSNan Zhou 
2890e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2891c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2892677bb756SEd Tanous                             const std::string& chassisId,
2893c1d019a6SEd Tanous                             const std::string& sensorId)
2894e6bd846dSNan Zhou {
2895c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2896e6bd846dSNan Zhou     {
2897e6bd846dSNan Zhou         return;
2898e6bd846dSNan Zhou     }
2899c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
2900c71d6125SEd Tanous         splitSensorNameAndType(sensorId);
2901c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2902c1d019a6SEd Tanous     {
2903c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2904c1d019a6SEd Tanous         return;
2905c1d019a6SEd Tanous     }
2906c71d6125SEd Tanous 
2907677bb756SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2908677bb756SEd Tanous         "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
2909c1d019a6SEd Tanous 
2910e6bd846dSNan Zhou     BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2911e6bd846dSNan Zhou 
2912e6bd846dSNan Zhou     const std::array<const char*, 1> interfaces = {
2913e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2914c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2915c71d6125SEd Tanous                              '/' + nameType.second;
2916e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2917e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
2918e6bd846dSNan Zhou     crow::connections::systemBus->async_method_call(
2919c71d6125SEd Tanous         [asyncResp,
2920c71d6125SEd Tanous          sensorPath](const boost::system::error_code ec,
2921c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
2922e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 enter";
2923e6bd846dSNan Zhou         if (ec)
2924e6bd846dSNan Zhou         {
2925c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2926e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2927e6bd846dSNan Zhou                              << "Dbus error " << ec;
2928e6bd846dSNan Zhou             return;
2929e6bd846dSNan Zhou         }
2930c1d019a6SEd Tanous         getSensorFromDbus(asyncResp, sensorPath, subtree);
2931e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 exit";
2932e6bd846dSNan Zhou         },
2933e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper",
2934e6bd846dSNan Zhou         "/xyz/openbmc_project/object_mapper",
2935c1d019a6SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
2936c1d019a6SEd Tanous         interfaces);
2937e6bd846dSNan Zhou }
2938e6bd846dSNan Zhou 
2939bacb2162SNan Zhou } // namespace sensors
2940bacb2162SNan Zhou 
29417e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
294295a3ecadSAnthony Wilson {
29437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2944ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2945002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2946de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
294795a3ecadSAnthony Wilson }
294895a3ecadSAnthony Wilson 
29497e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
295095a3ecadSAnthony Wilson {
29517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2952ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2953002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2954e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
295595a3ecadSAnthony Wilson }
295695a3ecadSAnthony Wilson 
295708777fb0SLewanczyk, Dawid } // namespace redfish
2958