xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 86d89ed7ad605b0f7dbe2c11200363fd34c6364c)
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 
187e860f15SJohn Edward Broadbent #include <app.hpp>
1911ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
2008777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2108777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
221abe55efSEd Tanous #include <dbus_singleton.hpp>
23168e20c1SEd Tanous #include <dbus_utility.hpp>
2445ca1b86SEd Tanous #include <query.hpp>
25ed398213SEd Tanous #include <registries/privilege_registry.hpp>
261e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
27*86d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
28*86d89ed7SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
29413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
30928fefb9SNan Zhou #include <utils/query_param.hpp>
311214b7e7SGunnar Mills 
321214b7e7SGunnar Mills #include <cmath>
33fe04d49cSNan Zhou #include <iterator>
34fe04d49cSNan Zhou #include <map>
35fe04d49cSNan Zhou #include <set>
36b5a76932SEd Tanous #include <utility>
37abf2add6SEd Tanous #include <variant>
3808777fb0SLewanczyk, Dawid 
391abe55efSEd Tanous namespace redfish
401abe55efSEd Tanous {
4108777fb0SLewanczyk, Dawid 
42a0ec28b6SAdrian Ambrożewicz namespace sensors
43a0ec28b6SAdrian Ambrożewicz {
44a0ec28b6SAdrian Ambrożewicz namespace node
45a0ec28b6SAdrian Ambrożewicz {
46a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
47a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
48a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
49a0ec28b6SAdrian Ambrożewicz } // namespace node
50a0ec28b6SAdrian Ambrożewicz 
5102da7c5aSEd Tanous // clang-format off
52a0ec28b6SAdrian Ambrożewicz namespace dbus
53a0ec28b6SAdrian Ambrożewicz {
544ee8e211SEd Tanous static auto powerPaths = std::to_array<std::string_view>({
5502da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
5602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
5702da7c5aSEd Tanous });
58c2bf7f99SWludzik, Jozef 
594ee8e211SEd Tanous static auto sensorPaths = std::to_array<std::string_view>({
6002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power",
61a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/current",
627088690cSBasheer Ahmed Muddebihal     "/xyz/openbmc_project/sensors/airflow",
635deabed9SGunnar Mills     "/xyz/openbmc_project/sensors/humidity",
64e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
65e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/voltage",
66e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_tach",
67e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/temperature",
68e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_pwm",
69e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/altitude",
70e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/energy",
71e8204933SGeorge Liu #endif
7202da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/utilization"
7302da7c5aSEd Tanous });
7402da7c5aSEd Tanous 
754ee8e211SEd Tanous static auto thermalPaths = std::to_array<std::string_view>({
7602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
77a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
7802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
7902da7c5aSEd Tanous });
8002da7c5aSEd Tanous 
81c2bf7f99SWludzik, Jozef } // namespace dbus
8202da7c5aSEd Tanous // clang-format on
8302da7c5aSEd Tanous 
8402da7c5aSEd Tanous using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
8502da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
8602da7c5aSEd Tanous     {{node::power, std::span<std::string_view>(dbus::powerPaths)},
8702da7c5aSEd Tanous      {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
8802da7c5aSEd Tanous      {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
89c2bf7f99SWludzik, Jozef 
90c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType)
91c2bf7f99SWludzik, Jozef {
92c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
93c2bf7f99SWludzik, Jozef     {
94c2bf7f99SWludzik, Jozef         return "Voltage";
95c2bf7f99SWludzik, Jozef     }
96c2bf7f99SWludzik, Jozef     if (sensorType == "power")
97c2bf7f99SWludzik, Jozef     {
98c2bf7f99SWludzik, Jozef         return "Power";
99c2bf7f99SWludzik, Jozef     }
100c2bf7f99SWludzik, Jozef     if (sensorType == "current")
101c2bf7f99SWludzik, Jozef     {
102c2bf7f99SWludzik, Jozef         return "Current";
103c2bf7f99SWludzik, Jozef     }
104c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
105c2bf7f99SWludzik, Jozef     {
106c2bf7f99SWludzik, Jozef         return "Rotational";
107c2bf7f99SWludzik, Jozef     }
108c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
109c2bf7f99SWludzik, Jozef     {
110c2bf7f99SWludzik, Jozef         return "Temperature";
111c2bf7f99SWludzik, Jozef     }
112c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
113c2bf7f99SWludzik, Jozef     {
114c2bf7f99SWludzik, Jozef         return "Percent";
115c2bf7f99SWludzik, Jozef     }
1165deabed9SGunnar Mills     if (sensorType == "humidity")
1175deabed9SGunnar Mills     {
1185deabed9SGunnar Mills         return "Humidity";
1195deabed9SGunnar Mills     }
120c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
121c2bf7f99SWludzik, Jozef     {
122c2bf7f99SWludzik, Jozef         return "Altitude";
123c2bf7f99SWludzik, Jozef     }
124c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
125c2bf7f99SWludzik, Jozef     {
126c2bf7f99SWludzik, Jozef         return "AirFlow";
127c2bf7f99SWludzik, Jozef     }
128c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
129c2bf7f99SWludzik, Jozef     {
130c2bf7f99SWludzik, Jozef         return "EnergyJoules";
131c2bf7f99SWludzik, Jozef     }
132c2bf7f99SWludzik, Jozef     return "";
133c2bf7f99SWludzik, Jozef }
134c2bf7f99SWludzik, Jozef 
135c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType)
136c2bf7f99SWludzik, Jozef {
137c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
138c2bf7f99SWludzik, Jozef     {
139c2bf7f99SWludzik, Jozef         return "V";
140c2bf7f99SWludzik, Jozef     }
141c2bf7f99SWludzik, Jozef     if (sensorType == "power")
142c2bf7f99SWludzik, Jozef     {
143c2bf7f99SWludzik, Jozef         return "W";
144c2bf7f99SWludzik, Jozef     }
145c2bf7f99SWludzik, Jozef     if (sensorType == "current")
146c2bf7f99SWludzik, Jozef     {
147c2bf7f99SWludzik, Jozef         return "A";
148c2bf7f99SWludzik, Jozef     }
149c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
150c2bf7f99SWludzik, Jozef     {
151c2bf7f99SWludzik, Jozef         return "RPM";
152c2bf7f99SWludzik, Jozef     }
153c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
154c2bf7f99SWludzik, Jozef     {
155c2bf7f99SWludzik, Jozef         return "Cel";
156c2bf7f99SWludzik, Jozef     }
1575deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
1585deabed9SGunnar Mills         sensorType == "humidity")
159c2bf7f99SWludzik, Jozef     {
160c2bf7f99SWludzik, Jozef         return "%";
161c2bf7f99SWludzik, Jozef     }
162c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
163c2bf7f99SWludzik, Jozef     {
164c2bf7f99SWludzik, Jozef         return "m";
165c2bf7f99SWludzik, Jozef     }
166c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
167c2bf7f99SWludzik, Jozef     {
168c2bf7f99SWludzik, Jozef         return "cft_i/min";
169c2bf7f99SWludzik, Jozef     }
170c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
171c2bf7f99SWludzik, Jozef     {
172c2bf7f99SWludzik, Jozef         return "J";
173c2bf7f99SWludzik, Jozef     }
174c2bf7f99SWludzik, Jozef     return "";
175a0ec28b6SAdrian Ambrożewicz }
176a0ec28b6SAdrian Ambrożewicz } // namespace sensors
177a0ec28b6SAdrian Ambrożewicz 
17808777fb0SLewanczyk, Dawid /**
179588c3f0dSKowalski, Kamil  * SensorsAsyncResp
18008777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
18108777fb0SLewanczyk, Dawid  */
1821abe55efSEd Tanous class SensorsAsyncResp
1831abe55efSEd Tanous {
18408777fb0SLewanczyk, Dawid   public:
185a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
186a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
187fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
188a0ec28b6SAdrian Ambrożewicz 
189a0ec28b6SAdrian Ambrożewicz     struct SensorData
190a0ec28b6SAdrian Ambrożewicz     {
191a0ec28b6SAdrian Ambrożewicz         const std::string name;
192a0ec28b6SAdrian Ambrożewicz         std::string uri;
193a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
194a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
195a0ec28b6SAdrian Ambrożewicz     };
196a0ec28b6SAdrian Ambrożewicz 
1978a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1988d1b46d7Szhanghch05                      const std::string& chassisIdIn,
19902da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
20002da7c5aSEd Tanous                      std::string_view subNode) :
2018a592810SEd Tanous         asyncResp(asyncRespIn),
202928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
203928fefb9SNan Zhou         efficientExpand(false)
2041214b7e7SGunnar Mills     {}
20508777fb0SLewanczyk, Dawid 
206a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
2078a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
2088d1b46d7Szhanghch05                      const std::string& chassisIdIn,
20902da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
21002da7c5aSEd Tanous                      std::string_view subNode,
211a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
2128a592810SEd Tanous         asyncResp(asyncRespIn),
213928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
214928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
215a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
216a0ec28b6SAdrian Ambrożewicz     {}
217a0ec28b6SAdrian Ambrożewicz 
218928fefb9SNan Zhou     // sensor collections expand
2198a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
220928fefb9SNan Zhou                      const std::string& chassisIdIn,
22102da7c5aSEd Tanous                      const std::span<std::string_view> typesIn,
2228a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
2238a592810SEd Tanous         asyncResp(asyncRespIn),
224928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
2258a592810SEd Tanous         efficientExpand(efficientExpandIn)
226928fefb9SNan Zhou     {}
227928fefb9SNan Zhou 
2281abe55efSEd Tanous     ~SensorsAsyncResp()
2291abe55efSEd Tanous     {
2308d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2318d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2321abe55efSEd Tanous         {
2331abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2341abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2351abe55efSEd Tanous             // proper code
2368d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
23708777fb0SLewanczyk, Dawid         }
238a0ec28b6SAdrian Ambrożewicz 
239a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
240a0ec28b6SAdrian Ambrożewicz         {
241fe04d49cSNan Zhou             std::map<std::string, std::string> map;
2428d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
243a0ec28b6SAdrian Ambrożewicz             {
244a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
245a0ec28b6SAdrian Ambrożewicz                 {
246a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
247a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
248a0ec28b6SAdrian Ambrożewicz                 }
249a0ec28b6SAdrian Ambrożewicz             }
2508d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
251a0ec28b6SAdrian Ambrożewicz         }
25208777fb0SLewanczyk, Dawid     }
253588c3f0dSKowalski, Kamil 
254ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
255ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
256ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
257ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
258ecd6a3a2SEd Tanous 
259a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
260a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
261a0ec28b6SAdrian Ambrożewicz     {
262a0ec28b6SAdrian Ambrożewicz         if (metadata)
263a0ec28b6SAdrian Ambrożewicz         {
264a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
265a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
266a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
267a0ec28b6SAdrian Ambrożewicz         }
268a0ec28b6SAdrian Ambrożewicz     }
269a0ec28b6SAdrian Ambrożewicz 
270a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
271a0ec28b6SAdrian Ambrożewicz     {
272a0ec28b6SAdrian Ambrożewicz         if (metadata)
273a0ec28b6SAdrian Ambrożewicz         {
274a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
275a0ec28b6SAdrian Ambrożewicz             {
276a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
277a0ec28b6SAdrian Ambrożewicz                 {
278a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
279a0ec28b6SAdrian Ambrożewicz                 }
280a0ec28b6SAdrian Ambrożewicz             }
281a0ec28b6SAdrian Ambrożewicz         }
282a0ec28b6SAdrian Ambrożewicz     }
283a0ec28b6SAdrian Ambrożewicz 
2848d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
285a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
28602da7c5aSEd Tanous     const std::span<std::string_view> types;
287a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
288928fefb9SNan Zhou     const bool efficientExpand;
289a0ec28b6SAdrian Ambrożewicz 
290a0ec28b6SAdrian Ambrożewicz   private:
291a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
292a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
29308777fb0SLewanczyk, Dawid };
29408777fb0SLewanczyk, Dawid 
29508777fb0SLewanczyk, Dawid /**
296d500549bSAnthony Wilson  * Possible states for physical inventory leds
297d500549bSAnthony Wilson  */
298d500549bSAnthony Wilson enum class LedState
299d500549bSAnthony Wilson {
300d500549bSAnthony Wilson     OFF,
301d500549bSAnthony Wilson     ON,
302d500549bSAnthony Wilson     BLINK,
303d500549bSAnthony Wilson     UNKNOWN
304d500549bSAnthony Wilson };
305d500549bSAnthony Wilson 
306d500549bSAnthony Wilson /**
307adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
308adc4f0dbSShawn McCarney  */
309adc4f0dbSShawn McCarney class InventoryItem
310adc4f0dbSShawn McCarney {
311adc4f0dbSShawn McCarney   public:
3124e23a444SEd Tanous     explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
313adc4f0dbSShawn McCarney     {
314adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
31528aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
31628aa8de5SGeorge Liu         name = path.filename();
31728aa8de5SGeorge Liu         if (name.empty())
318adc4f0dbSShawn McCarney         {
31928aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
320adc4f0dbSShawn McCarney         }
321adc4f0dbSShawn McCarney     }
322adc4f0dbSShawn McCarney 
323adc4f0dbSShawn McCarney     std::string objectPath;
324adc4f0dbSShawn McCarney     std::string name;
325e05aec50SEd Tanous     bool isPresent = true;
326e05aec50SEd Tanous     bool isFunctional = true;
327e05aec50SEd Tanous     bool isPowerSupply = false;
328e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
329adc4f0dbSShawn McCarney     std::string manufacturer;
330adc4f0dbSShawn McCarney     std::string model;
331adc4f0dbSShawn McCarney     std::string partNumber;
332adc4f0dbSShawn McCarney     std::string serialNumber;
333adc4f0dbSShawn McCarney     std::set<std::string> sensors;
334d500549bSAnthony Wilson     std::string ledObjectPath;
335e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
336adc4f0dbSShawn McCarney };
337adc4f0dbSShawn McCarney 
338adc4f0dbSShawn McCarney /**
339413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
340588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
34108777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
34208777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
34308777fb0SLewanczyk, Dawid  */
34408777fb0SLewanczyk, Dawid template <typename Callback>
345413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
34681ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
347fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
3481abe55efSEd Tanous     Callback&& callback)
3491abe55efSEd Tanous {
350413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
35103b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
35208777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
35308777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
35408777fb0SLewanczyk, Dawid 
35508777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
356002d39b4SEd Tanous     auto respHandler =
357002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
358002d39b4SEd Tanous          sensorNames](const boost::system::error_code ec,
359002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
360413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3611abe55efSEd Tanous         if (ec)
3621abe55efSEd Tanous         {
3638d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
364413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
365413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
36608777fb0SLewanczyk, Dawid             return;
36708777fb0SLewanczyk, Dawid         }
36808777fb0SLewanczyk, Dawid 
36955c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
37008777fb0SLewanczyk, Dawid 
37108777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
37208777fb0SLewanczyk, Dawid         // found in the chassis
373fe04d49cSNan Zhou         std::set<std::string> connections;
374413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
37508777fb0SLewanczyk, Dawid 
37649c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
37749c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3781abe55efSEd Tanous         {
37955c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
38008777fb0SLewanczyk, Dawid         }
38108777fb0SLewanczyk, Dawid 
38208777fb0SLewanczyk, Dawid         for (const std::pair<
38308777fb0SLewanczyk, Dawid                  std::string,
38408777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3851abe55efSEd Tanous                  object : subtree)
3861abe55efSEd Tanous         {
38749c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3881abe55efSEd Tanous             {
38949c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3901abe55efSEd Tanous                          objData : object.second)
3911abe55efSEd Tanous                 {
39249c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
39308777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
394de629b6eSShawn McCarney                     objectsWithConnection.insert(
395de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
39608777fb0SLewanczyk, Dawid                 }
39708777fb0SLewanczyk, Dawid             }
39808777fb0SLewanczyk, Dawid         }
39955c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
400413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
401413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
40208777fb0SLewanczyk, Dawid     };
40308777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
40455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
40555c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
4061abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
4071abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
408413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
409413961deSRichard Marian Thomaiyar }
410413961deSRichard Marian Thomaiyar 
411413961deSRichard Marian Thomaiyar /**
412413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
413413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
414413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
415413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
416413961deSRichard Marian Thomaiyar  */
417413961deSRichard Marian Thomaiyar template <typename Callback>
418fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
419fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
420413961deSRichard Marian Thomaiyar                     Callback&& callback)
421413961deSRichard Marian Thomaiyar {
422413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
423fe04d49cSNan Zhou         [callback](const std::set<std::string>& connections,
424413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4253174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
42681ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
427413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
42808777fb0SLewanczyk, Dawid }
42908777fb0SLewanczyk, Dawid 
43008777fb0SLewanczyk, Dawid /**
43149c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43249c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43349c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
43449c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
43549c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
43649c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
43749c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
43849c53ac9SJohnathan Mantey  */
43923a21a1cSEd Tanous inline void reduceSensorList(
44081ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
44149c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
442fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
44349c53ac9SJohnathan Mantey {
44481ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
44549c53ac9SJohnathan Mantey     {
44649c53ac9SJohnathan Mantey         return;
44749c53ac9SJohnathan Mantey     }
44849c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
44949c53ac9SJohnathan Mantey     {
45049c53ac9SJohnathan Mantey         messages::resourceNotFound(
4518d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
45281ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
453a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
45449c53ac9SJohnathan Mantey                 : "Voltages");
45549c53ac9SJohnathan Mantey 
45649c53ac9SJohnathan Mantey         return;
45749c53ac9SJohnathan Mantey     }
45849c53ac9SJohnathan Mantey     if (allSensors->empty())
45949c53ac9SJohnathan Mantey     {
46049c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
46149c53ac9SJohnathan Mantey         return;
46249c53ac9SJohnathan Mantey     }
46349c53ac9SJohnathan Mantey 
46402da7c5aSEd Tanous     for (std::string_view type : sensorsAsyncResp->types)
46549c53ac9SJohnathan Mantey     {
46649c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46749c53ac9SJohnathan Mantey         {
46811ba3979SEd Tanous             if (sensor.starts_with(type))
46949c53ac9SJohnathan Mantey             {
47049c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
47149c53ac9SJohnathan Mantey             }
47249c53ac9SJohnathan Mantey         }
47349c53ac9SJohnathan Mantey     }
47449c53ac9SJohnathan Mantey }
47549c53ac9SJohnathan Mantey 
47649c53ac9SJohnathan Mantey /**
47708777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
478588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
47908777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
48008777fb0SLewanczyk, Dawid  */
48108777fb0SLewanczyk, Dawid template <typename Callback>
482b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
4831abe55efSEd Tanous                 Callback&& callback)
4841abe55efSEd Tanous {
48555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
486adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
48749c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
488adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
489002d39b4SEd Tanous     auto respHandler =
490002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
49149c53ac9SJohnathan Mantey             const boost::system::error_code ec,
492002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
49355c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
4941abe55efSEd Tanous         if (ec)
4951abe55efSEd Tanous         {
49655c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
4978d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
49808777fb0SLewanczyk, Dawid             return;
49908777fb0SLewanczyk, Dawid         }
50008777fb0SLewanczyk, Dawid 
50149c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
50249c53ac9SJohnathan Mantey         std::string chassisName;
50349c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5041abe55efSEd Tanous         {
50528aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
50628aa8de5SGeorge Liu             chassisName = path.filename();
50728aa8de5SGeorge Liu             if (chassisName.empty())
5081abe55efSEd Tanous             {
50949c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
510daf36e2eSEd Tanous                 continue;
511daf36e2eSEd Tanous             }
51249c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5131abe55efSEd Tanous             {
51449c53ac9SJohnathan Mantey                 chassisPath = &chassis;
51549c53ac9SJohnathan Mantey                 break;
516daf36e2eSEd Tanous             }
51749c53ac9SJohnathan Mantey         }
51849c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5191abe55efSEd Tanous         {
5208d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
5218d1b46d7Szhanghch05                                        "Chassis", sensorsAsyncResp->chassisId);
52249c53ac9SJohnathan Mantey             return;
5231abe55efSEd Tanous         }
52408777fb0SLewanczyk, Dawid 
52549c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
526a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
52749c53ac9SJohnathan Mantey         {
5288d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
52949c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
53049c53ac9SJohnathan Mantey         }
531a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
53249c53ac9SJohnathan Mantey         {
5338d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
53449c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5358d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
5368d1b46d7Szhanghch05                 nlohmann::json::array();
5378d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
5384f9a2130SJennifer Lee                 nlohmann::json::array();
53949c53ac9SJohnathan Mantey         }
540a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
54195a3ecadSAnthony Wilson         {
5428d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
54395a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
5448d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
54595a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
5468d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
54795a3ecadSAnthony Wilson                 nlohmann::json::array();
5488d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
5498d1b46d7Szhanghch05                 0;
55095a3ecadSAnthony Wilson         }
55195a3ecadSAnthony Wilson 
552a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
55395a3ecadSAnthony Wilson         {
5548d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
55595a3ecadSAnthony Wilson         }
55695a3ecadSAnthony Wilson 
5578d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
55849c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
55949c53ac9SJohnathan Mantey             chassisSubNode;
5608d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
5618fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5628fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
5631e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
5641e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
5651e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
566f94c4ecfSEd Tanous             [sensorsAsyncResp,
567f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
568271584abSEd Tanous                 const boost::system::error_code& e,
5691e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
570271584abSEd Tanous             if (e)
57149c53ac9SJohnathan Mantey             {
572271584abSEd Tanous                 if (e.value() != EBADR)
57349c53ac9SJohnathan Mantey                 {
574002d39b4SEd Tanous                     messages::internalError(sensorsAsyncResp->asyncResp->res);
57549c53ac9SJohnathan Mantey                     return;
57649c53ac9SJohnathan Mantey                 }
57749c53ac9SJohnathan Mantey             }
578fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>> culledSensorList =
579fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
5801e1e598dSJonathan Doman             reduceSensorList(sensorsAsyncResp, &nodeSensorList,
58149c53ac9SJohnathan Mantey                              culledSensorList);
58249c53ac9SJohnathan Mantey             callback(culledSensorList);
5831e1e598dSJonathan Doman             });
58449c53ac9SJohnathan Mantey     };
58549c53ac9SJohnathan Mantey 
58649c53ac9SJohnathan Mantey     // Get the Chassis Collection
58749c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
58849c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
58949c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
59049c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
591271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
59255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
59308777fb0SLewanczyk, Dawid }
59408777fb0SLewanczyk, Dawid 
59508777fb0SLewanczyk, Dawid /**
596de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
597de629b6eSShawn McCarney  *
598de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
599de629b6eSShawn McCarney  *
600de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
601de629b6eSShawn McCarney  * been obtained.
602de629b6eSShawn McCarney  *
603de629b6eSShawn McCarney  * The callback must have the following signature:
604de629b6eSShawn McCarney  *   @code
605fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
606de629b6eSShawn McCarney  *   @endcode
607de629b6eSShawn McCarney  *
60849c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
609de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
610de629b6eSShawn McCarney  */
611de629b6eSShawn McCarney template <typename Callback>
612b5a76932SEd Tanous void getObjectManagerPaths(
61381ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
614de629b6eSShawn McCarney     Callback&& callback)
615de629b6eSShawn McCarney {
616de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
617de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
618de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
619de629b6eSShawn McCarney 
620de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
621002d39b4SEd Tanous     auto respHandler =
622002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
623b9d36b47SEd Tanous             const boost::system::error_code ec,
624002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
625de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
626de629b6eSShawn McCarney         if (ec)
627de629b6eSShawn McCarney         {
6288d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
629de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
630de629b6eSShawn McCarney                              << ec;
631de629b6eSShawn McCarney             return;
632de629b6eSShawn McCarney         }
633de629b6eSShawn McCarney 
634de629b6eSShawn McCarney         // Loop over returned object paths
635fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
636fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
637de629b6eSShawn McCarney         for (const std::pair<
638de629b6eSShawn McCarney                  std::string,
639de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
640de629b6eSShawn McCarney                  object : subtree)
641de629b6eSShawn McCarney         {
642de629b6eSShawn McCarney             // Loop over connections for current object path
643de629b6eSShawn McCarney             const std::string& objectPath = object.first;
644de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
645de629b6eSShawn McCarney                      objData : object.second)
646de629b6eSShawn McCarney             {
647de629b6eSShawn McCarney                 // Add mapping from connection to object path
648de629b6eSShawn McCarney                 const std::string& connection = objData.first;
6498fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
650de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
651de629b6eSShawn McCarney                                  << objectPath;
652de629b6eSShawn McCarney             }
653de629b6eSShawn McCarney         }
6548fb49dd6SShawn McCarney         callback(objectMgrPaths);
655de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
656de629b6eSShawn McCarney     };
657de629b6eSShawn McCarney 
658de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
659de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
660de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
661de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
662271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
663de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
664de629b6eSShawn McCarney }
665de629b6eSShawn McCarney 
666de629b6eSShawn McCarney /**
667adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
668adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
669adc4f0dbSShawn McCarney  * @return State value for inventory item.
67034dd179eSJames Feist  */
67123a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
672adc4f0dbSShawn McCarney {
673adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
674adc4f0dbSShawn McCarney     {
675adc4f0dbSShawn McCarney         return "Absent";
676adc4f0dbSShawn McCarney     }
67734dd179eSJames Feist 
678adc4f0dbSShawn McCarney     return "Enabled";
679adc4f0dbSShawn McCarney }
680adc4f0dbSShawn McCarney 
681adc4f0dbSShawn McCarney /**
682adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
683adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
684adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
685adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
686adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
687adc4f0dbSShawn McCarney  * @return Health value for sensor.
688adc4f0dbSShawn McCarney  */
689711ac7a9SEd Tanous inline std::string
690711ac7a9SEd Tanous     getHealth(nlohmann::json& sensorJson,
691711ac7a9SEd Tanous               const dbus::utility::DBusInteracesMap& interfacesDict,
692adc4f0dbSShawn McCarney               const InventoryItem* inventoryItem)
69334dd179eSJames Feist {
694adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
695adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
696adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
697adc4f0dbSShawn McCarney     std::string currentHealth;
698adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
699adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
700adc4f0dbSShawn McCarney     {
701adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
702adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
703adc4f0dbSShawn McCarney         {
704adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
705adc4f0dbSShawn McCarney             if (health != nullptr)
706adc4f0dbSShawn McCarney             {
707adc4f0dbSShawn McCarney                 currentHealth = *health;
708adc4f0dbSShawn McCarney             }
709adc4f0dbSShawn McCarney         }
710adc4f0dbSShawn McCarney     }
711adc4f0dbSShawn McCarney 
712adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
713adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
714adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
715adc4f0dbSShawn McCarney     {
716adc4f0dbSShawn McCarney         return "Critical";
717adc4f0dbSShawn McCarney     }
718adc4f0dbSShawn McCarney 
719adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
720711ac7a9SEd Tanous 
7219eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
72234dd179eSJames Feist     {
723711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
72434dd179eSJames Feist         {
7259eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
726711ac7a9SEd Tanous             {
727711ac7a9SEd Tanous                 if (valueName == "CriticalAlarmHigh" ||
728711ac7a9SEd Tanous                     valueName == "CriticalAlarmLow")
729711ac7a9SEd Tanous                 {
730711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
73134dd179eSJames Feist                     if (asserted == nullptr)
73234dd179eSJames Feist                     {
73334dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
73434dd179eSJames Feist                     }
73534dd179eSJames Feist                     else if (*asserted)
73634dd179eSJames Feist                     {
73734dd179eSJames Feist                         return "Critical";
73834dd179eSJames Feist                     }
73934dd179eSJames Feist                 }
74034dd179eSJames Feist             }
74134dd179eSJames Feist         }
74234dd179eSJames Feist     }
74334dd179eSJames Feist 
744adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
745adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
746adc4f0dbSShawn McCarney     {
747adc4f0dbSShawn McCarney         return "Critical";
748adc4f0dbSShawn McCarney     }
749adc4f0dbSShawn McCarney 
750adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
751adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
752adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
753adc4f0dbSShawn McCarney     {
754adc4f0dbSShawn McCarney         return "Warning";
755adc4f0dbSShawn McCarney     }
756adc4f0dbSShawn McCarney 
757adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
7589eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
75934dd179eSJames Feist     {
760711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
76134dd179eSJames Feist         {
7629eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
763711ac7a9SEd Tanous             {
764711ac7a9SEd Tanous                 if (valueName == "WarningAlarmHigh" ||
765711ac7a9SEd Tanous                     valueName == "WarningAlarmLow")
766711ac7a9SEd Tanous                 {
767711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
76834dd179eSJames Feist                     if (asserted == nullptr)
76934dd179eSJames Feist                     {
77034dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
77134dd179eSJames Feist                     }
77234dd179eSJames Feist                     else if (*asserted)
77334dd179eSJames Feist                     {
774ebe4d91eSEd Tanous                         return "Warning";
77534dd179eSJames Feist                     }
77634dd179eSJames Feist                 }
77734dd179eSJames Feist             }
77834dd179eSJames Feist         }
77934dd179eSJames Feist     }
780adc4f0dbSShawn McCarney 
78134dd179eSJames Feist     return "OK";
78234dd179eSJames Feist }
78334dd179eSJames Feist 
78423a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
785d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
786d500549bSAnthony Wilson {
787d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
788d500549bSAnthony Wilson     {
789d500549bSAnthony Wilson         switch (inventoryItem->ledState)
790d500549bSAnthony Wilson         {
791d500549bSAnthony Wilson             case LedState::OFF:
792d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
793d500549bSAnthony Wilson                 break;
794d500549bSAnthony Wilson             case LedState::ON:
795d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
796d500549bSAnthony Wilson                 break;
797d500549bSAnthony Wilson             case LedState::BLINK:
798d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
799d500549bSAnthony Wilson                 break;
80023a21a1cSEd Tanous             case LedState::UNKNOWN:
801d500549bSAnthony Wilson                 break;
802d500549bSAnthony Wilson         }
803d500549bSAnthony Wilson     }
804d500549bSAnthony Wilson }
805d500549bSAnthony Wilson 
80634dd179eSJames Feist /**
80708777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
80808777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
809274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
81008777fb0SLewanczyk, Dawid  * build
811a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
81208777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
81308777fb0SLewanczyk, Dawid  * interfaces to be built from
81408777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
815adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
816adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
81708777fb0SLewanczyk, Dawid  */
81823a21a1cSEd Tanous inline void objectInterfacesToJson(
81908777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
820b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
821711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
82281ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8231abe55efSEd Tanous {
82408777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
82508777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
8269eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8271abe55efSEd Tanous     {
828711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Value")
829711ac7a9SEd Tanous         {
8309eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
831711ac7a9SEd Tanous             {
832711ac7a9SEd Tanous                 if (valueName == "Scale")
833711ac7a9SEd Tanous                 {
834711ac7a9SEd Tanous                     const int64_t* int64Value = std::get_if<int64_t>(&value);
8351abe55efSEd Tanous                     if (int64Value != nullptr)
8361abe55efSEd Tanous                     {
83708777fb0SLewanczyk, Dawid                         scaleMultiplier = *int64Value;
83808777fb0SLewanczyk, Dawid                     }
83908777fb0SLewanczyk, Dawid                 }
840711ac7a9SEd Tanous             }
841711ac7a9SEd Tanous         }
842711ac7a9SEd Tanous     }
84308777fb0SLewanczyk, Dawid 
844a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
845adc4f0dbSShawn McCarney     {
84695a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
84795a3ecadSAnthony Wilson         // including power sensors.
84881ce609eSEd Tanous         sensorJson["Id"] = sensorName;
84981ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
85095a3ecadSAnthony Wilson     }
85195a3ecadSAnthony Wilson     else if (sensorType != "power")
85295a3ecadSAnthony Wilson     {
85395a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
85495a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
85595a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
85681ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
85781ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
858adc4f0dbSShawn McCarney     }
859e742b6ccSEd Tanous 
86081ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
86181ce609eSEd Tanous     sensorJson["Status"]["Health"] =
86281ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
86308777fb0SLewanczyk, Dawid 
86408777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
86508777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
86608777fb0SLewanczyk, Dawid     // that require integers, not floats.
86708777fb0SLewanczyk, Dawid     bool forceToInt = false;
86808777fb0SLewanczyk, Dawid 
8693929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
870a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
87195a3ecadSAnthony Wilson     {
8722a4ba195SShounak Mitra         sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
873c2bf7f99SWludzik, Jozef 
874c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
875c2bf7f99SWludzik, Jozef         if (readingType.empty())
87695a3ecadSAnthony Wilson         {
877c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
878c2bf7f99SWludzik, Jozef                              << sensorType;
87995a3ecadSAnthony Wilson         }
880c2bf7f99SWludzik, Jozef         else
88195a3ecadSAnthony Wilson         {
882c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
88395a3ecadSAnthony Wilson         }
884c2bf7f99SWludzik, Jozef 
885c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
886c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
887f8ede15eSAdrian Ambrożewicz         {
888c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
889c2bf7f99SWludzik, Jozef                              << sensorType;
890c2bf7f99SWludzik, Jozef         }
891c2bf7f99SWludzik, Jozef         else
892c2bf7f99SWludzik, Jozef         {
893c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
894f8ede15eSAdrian Ambrożewicz         }
89595a3ecadSAnthony Wilson     }
89695a3ecadSAnthony Wilson     else if (sensorType == "temperature")
8971abe55efSEd Tanous     {
8983929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
89981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
90008777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
90108777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9021abe55efSEd Tanous     }
9031abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9041abe55efSEd Tanous     {
9053929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
90681ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
90781ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
90881ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
90908777fb0SLewanczyk, Dawid         forceToInt = true;
9101abe55efSEd Tanous     }
9116f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9126f6d0d32SEd Tanous     {
9133929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
91481ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
91581ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
91681ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9176f6d0d32SEd Tanous         forceToInt = true;
9186f6d0d32SEd Tanous     }
9191abe55efSEd Tanous     else if (sensorType == "voltage")
9201abe55efSEd Tanous     {
9213929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
92281ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9231abe55efSEd Tanous     }
9242474adfaSEd Tanous     else if (sensorType == "power")
9252474adfaSEd Tanous     {
92649c53ac9SJohnathan Mantey         std::string sensorNameLower =
92749c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
92849c53ac9SJohnathan Mantey 
92955f79e6fSEd Tanous         if (sensorName == "total_power")
930028f7ebcSEddie James         {
93181ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
9327ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
9337ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
93481ce609eSEd Tanous             sensorJson["MemberId"] = "0";
93581ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
9363929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
937028f7ebcSEddie James         }
938028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
93949c53ac9SJohnathan Mantey         {
9403929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
94149c53ac9SJohnathan Mantey         }
94249c53ac9SJohnathan Mantey         else
94349c53ac9SJohnathan Mantey         {
9443929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
94549c53ac9SJohnathan Mantey         }
9462474adfaSEd Tanous     }
9471abe55efSEd Tanous     else
9481abe55efSEd Tanous     {
94955c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
95008777fb0SLewanczyk, Dawid         return;
95108777fb0SLewanczyk, Dawid     }
95208777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
9533929aca1SAnthony Wilson     std::vector<
9543929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
9553929aca1SAnthony Wilson         properties;
95608777fb0SLewanczyk, Dawid     properties.reserve(7);
95708777fb0SLewanczyk, Dawid 
95808777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
959de629b6eSShawn McCarney 
960a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
9613929aca1SAnthony Wilson     {
9623929aca1SAnthony Wilson         properties.emplace_back(
9633929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
9643929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
9653929aca1SAnthony Wilson         properties.emplace_back(
9663929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
9673929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
9683929aca1SAnthony Wilson         properties.emplace_back(
9693929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
9703929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
9713929aca1SAnthony Wilson         properties.emplace_back(
9723929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
9733929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
9743929aca1SAnthony Wilson     }
9753929aca1SAnthony Wilson     else if (sensorType != "power")
976de629b6eSShawn McCarney     {
97708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
9783929aca1SAnthony Wilson                                 "WarningHigh",
9793929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
98008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
9813929aca1SAnthony Wilson                                 "WarningLow",
9823929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
98308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
9843929aca1SAnthony Wilson                                 "CriticalHigh",
9853929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
98608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
9873929aca1SAnthony Wilson                                 "CriticalLow",
9883929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
989de629b6eSShawn McCarney     }
99008777fb0SLewanczyk, Dawid 
9912474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
9922474adfaSEd Tanous 
993a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
99495a3ecadSAnthony Wilson     {
99595a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9963929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
99795a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9983929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
99995a3ecadSAnthony Wilson     }
100095a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10011abe55efSEd Tanous     {
100208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10033929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
100408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10053929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10061abe55efSEd Tanous     }
1007adc4f0dbSShawn McCarney     else if (sensorType != "power")
10081abe55efSEd Tanous     {
100908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10103929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
101108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10123929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
101308777fb0SLewanczyk, Dawid     }
101408777fb0SLewanczyk, Dawid 
10153929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10163929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10171abe55efSEd Tanous     {
101855f79e6fSEd Tanous         for (const auto& [interface, values] : interfacesDict)
10191abe55efSEd Tanous         {
1020711ac7a9SEd Tanous             if (interface != std::get<0>(p))
10211abe55efSEd Tanous             {
1022711ac7a9SEd Tanous                 continue;
1023711ac7a9SEd Tanous             }
102455f79e6fSEd Tanous             for (const auto& [valueName, valueVariant] : values)
1025711ac7a9SEd Tanous             {
1026711ac7a9SEd Tanous                 if (valueName != std::get<1>(p))
1027711ac7a9SEd Tanous                 {
1028711ac7a9SEd Tanous                     continue;
1029711ac7a9SEd Tanous                 }
10303929aca1SAnthony Wilson 
10313929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10323929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
10333929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
10343929aca1SAnthony Wilson 
103508777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1036abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
103708777fb0SLewanczyk, Dawid 
1038abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1039028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
10406f6d0d32SEd Tanous                 double temp = 0.0;
10416f6d0d32SEd Tanous                 if (int64Value != nullptr)
10421abe55efSEd Tanous                 {
1043271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
10446f6d0d32SEd Tanous                 }
10456f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
10461abe55efSEd Tanous                 {
10476f6d0d32SEd Tanous                     temp = *doubleValue;
10481abe55efSEd Tanous                 }
1049028f7ebcSEddie James                 else if (uValue != nullptr)
1050028f7ebcSEddie James                 {
1051028f7ebcSEddie James                     temp = *uValue;
1052028f7ebcSEddie James                 }
10531abe55efSEd Tanous                 else
10541abe55efSEd Tanous                 {
10556f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
10566f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
10576f6d0d32SEd Tanous                     continue;
105808777fb0SLewanczyk, Dawid                 }
10596f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
10606f6d0d32SEd Tanous                 if (forceToInt)
10616f6d0d32SEd Tanous                 {
106281ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
10636f6d0d32SEd Tanous                 }
10646f6d0d32SEd Tanous                 else
10656f6d0d32SEd Tanous                 {
106681ce609eSEd Tanous                     sensorJson[key] = temp;
106708777fb0SLewanczyk, Dawid                 }
106808777fb0SLewanczyk, Dawid             }
106908777fb0SLewanczyk, Dawid         }
107008777fb0SLewanczyk, Dawid     }
1071a0ec28b6SAdrian Ambrożewicz 
107281ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1073a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1074a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1075a0ec28b6SAdrian Ambrożewicz 
107655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
107708777fb0SLewanczyk, Dawid }
107808777fb0SLewanczyk, Dawid 
1079b5a76932SEd Tanous inline void populateFanRedundancy(
1080b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
10818bd25ccdSJames Feist {
10828bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
1083b9d36b47SEd Tanous         [sensorsAsyncResp](
1084b9d36b47SEd Tanous             const boost::system::error_code ec,
1085b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
10868bd25ccdSJames Feist         if (ec)
10878bd25ccdSJames Feist         {
10888bd25ccdSJames Feist             return; // don't have to have this interface
10898bd25ccdSJames Feist         }
1090002d39b4SEd Tanous         for (const std::pair<
1091002d39b4SEd Tanous                  std::string,
1092002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1093e278c18fSEd Tanous                  pathPair : resp)
10948bd25ccdSJames Feist         {
1095e278c18fSEd Tanous             const std::string& path = pathPair.first;
1096002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
1097002d39b4SEd Tanous                 objDict = pathPair.second;
10988bd25ccdSJames Feist             if (objDict.empty())
10998bd25ccdSJames Feist             {
11008bd25ccdSJames Feist                 continue; // this should be impossible
11018bd25ccdSJames Feist             }
11028bd25ccdSJames Feist 
11038bd25ccdSJames Feist             const std::string& owner = objDict.begin()->first;
11041e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::vector<std::string>>(
11051e1e598dSJonathan Doman                 *crow::connections::systemBus,
11061e1e598dSJonathan Doman                 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11071e1e598dSJonathan Doman                 "xyz.openbmc_project.Association", "endpoints",
1108002d39b4SEd Tanous                 [path, owner,
1109002d39b4SEd Tanous                  sensorsAsyncResp](const boost::system::error_code e,
11101e1e598dSJonathan Doman                                    const std::vector<std::string>& endpoints) {
1111271584abSEd Tanous                 if (e)
11128bd25ccdSJames Feist                 {
11138bd25ccdSJames Feist                     return; // if they don't have an association we
11148bd25ccdSJames Feist                             // can't tell what chassis is
11158bd25ccdSJames Feist                 }
1116002d39b4SEd Tanous                 auto found =
1117002d39b4SEd Tanous                     std::find_if(endpoints.begin(), endpoints.end(),
11188bd25ccdSJames Feist                                  [sensorsAsyncResp](const std::string& entry) {
1119002d39b4SEd Tanous                     return entry.find(sensorsAsyncResp->chassisId) !=
11208bd25ccdSJames Feist                            std::string::npos;
11218bd25ccdSJames Feist                     });
11228bd25ccdSJames Feist 
11231e1e598dSJonathan Doman                 if (found == endpoints.end())
11248bd25ccdSJames Feist                 {
11258bd25ccdSJames Feist                     return;
11268bd25ccdSJames Feist                 }
1127*86d89ed7SKrzysztof Grobelny                 sdbusplus::asio::getAllProperties(
1128*86d89ed7SKrzysztof Grobelny                     *crow::connections::systemBus, owner, path,
1129*86d89ed7SKrzysztof Grobelny                     "xyz.openbmc_project.Control.FanRedundancy",
11308bd25ccdSJames Feist                     [path, sensorsAsyncResp](
1131271584abSEd Tanous                         const boost::system::error_code& err,
1132*86d89ed7SKrzysztof Grobelny                         const dbus::utility::DBusPropertiesMap& ret) {
1133271584abSEd Tanous                     if (err)
11348bd25ccdSJames Feist                     {
11358bd25ccdSJames Feist                         return; // don't have to have this
11368bd25ccdSJames Feist                                 // interface
11378bd25ccdSJames Feist                     }
11388bd25ccdSJames Feist 
1139*86d89ed7SKrzysztof Grobelny                     const uint8_t* allowedFailures = nullptr;
1140*86d89ed7SKrzysztof Grobelny                     const std::vector<std::string>* collection = nullptr;
1141*86d89ed7SKrzysztof Grobelny                     const std::string* status = nullptr;
1142*86d89ed7SKrzysztof Grobelny 
1143*86d89ed7SKrzysztof Grobelny                     const bool success = sdbusplus::unpackPropertiesNoThrow(
1144*86d89ed7SKrzysztof Grobelny                         dbus_utils::UnpackErrorPrinter(), ret,
1145*86d89ed7SKrzysztof Grobelny                         "AllowedFailures", allowedFailures, "Collection",
1146*86d89ed7SKrzysztof Grobelny                         collection, "Status", status);
1147*86d89ed7SKrzysztof Grobelny 
1148*86d89ed7SKrzysztof Grobelny                     if (!success)
1149*86d89ed7SKrzysztof Grobelny                     {
1150*86d89ed7SKrzysztof Grobelny                         messages::internalError(
1151*86d89ed7SKrzysztof Grobelny                             sensorsAsyncResp->asyncResp->res);
1152*86d89ed7SKrzysztof Grobelny                         return;
1153*86d89ed7SKrzysztof Grobelny                     }
1154*86d89ed7SKrzysztof Grobelny 
1155*86d89ed7SKrzysztof Grobelny                     if (allowedFailures == nullptr || collection == nullptr ||
1156*86d89ed7SKrzysztof Grobelny                         status == nullptr)
11578bd25ccdSJames Feist                     {
1158002d39b4SEd Tanous                         BMCWEB_LOG_ERROR << "Invalid redundancy interface";
11598bd25ccdSJames Feist                         messages::internalError(
11608d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
11618bd25ccdSJames Feist                         return;
11628bd25ccdSJames Feist                     }
11638bd25ccdSJames Feist 
1164002d39b4SEd Tanous                     sdbusplus::message::object_path objectPath(path);
116528aa8de5SGeorge Liu                     std::string name = objectPath.filename();
116628aa8de5SGeorge Liu                     if (name.empty())
11678bd25ccdSJames Feist                     {
11688bd25ccdSJames Feist                         // this should be impossible
11698bd25ccdSJames Feist                         messages::internalError(
11708d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
11718bd25ccdSJames Feist                         return;
11728bd25ccdSJames Feist                     }
1173002d39b4SEd Tanous                     std::replace(name.begin(), name.end(), '_', ' ');
11748bd25ccdSJames Feist 
11758bd25ccdSJames Feist                     std::string health;
11768bd25ccdSJames Feist 
117711ba3979SEd Tanous                     if (status->ends_with("Full"))
11788bd25ccdSJames Feist                     {
11798bd25ccdSJames Feist                         health = "OK";
11808bd25ccdSJames Feist                     }
118111ba3979SEd Tanous                     else if (status->ends_with("Degraded"))
11828bd25ccdSJames Feist                     {
11838bd25ccdSJames Feist                         health = "Warning";
11848bd25ccdSJames Feist                     }
11858bd25ccdSJames Feist                     else
11868bd25ccdSJames Feist                     {
11878bd25ccdSJames Feist                         health = "Critical";
11888bd25ccdSJames Feist                     }
11891476687dSEd Tanous                     nlohmann::json::array_t redfishCollection;
11908bd25ccdSJames Feist                     const auto& fanRedfish =
1191002d39b4SEd Tanous                         sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
11928bd25ccdSJames Feist                     for (const std::string& item : *collection)
11938bd25ccdSJames Feist                     {
11948a592810SEd Tanous                         sdbusplus::message::object_path itemPath(item);
11958a592810SEd Tanous                         std::string itemName = itemPath.filename();
119628aa8de5SGeorge Liu                         if (itemName.empty())
119728aa8de5SGeorge Liu                         {
119828aa8de5SGeorge Liu                             continue;
119928aa8de5SGeorge Liu                         }
12008bd25ccdSJames Feist                         /*
12018bd25ccdSJames Feist                         todo(ed): merge patch that fixes the names
12028bd25ccdSJames Feist                         std::replace(itemName.begin(),
12038bd25ccdSJames Feist                                      itemName.end(), '_', ' ');*/
1204002d39b4SEd Tanous                         auto schemaItem =
1205002d39b4SEd Tanous                             std::find_if(fanRedfish.begin(), fanRedfish.end(),
12068bd25ccdSJames Feist                                          [itemName](const nlohmann::json& fan) {
12078bd25ccdSJames Feist                             return fan["MemberId"] == itemName;
12088bd25ccdSJames Feist                             });
12098bd25ccdSJames Feist                         if (schemaItem != fanRedfish.end())
12108bd25ccdSJames Feist                         {
12118a592810SEd Tanous                             nlohmann::json::object_t collectionId;
12128a592810SEd Tanous                             collectionId["@odata.id"] =
12131476687dSEd Tanous                                 (*schemaItem)["@odata.id"];
12141476687dSEd Tanous                             redfishCollection.emplace_back(
12158a592810SEd Tanous                                 std::move(collectionId));
12168bd25ccdSJames Feist                         }
12178bd25ccdSJames Feist                         else
12188bd25ccdSJames Feist                         {
1219002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "failed to find fan in schema";
12208bd25ccdSJames Feist                             messages::internalError(
12218d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
12228bd25ccdSJames Feist                             return;
12238bd25ccdSJames Feist                         }
12248bd25ccdSJames Feist                     }
12258bd25ccdSJames Feist 
12263e9e72ebSKuiying Wang                     size_t minNumNeeded =
122726f6976fSEd Tanous                         collection->empty()
122826f6976fSEd Tanous                             ? 0
122926f6976fSEd Tanous                             : collection->size() - *allowedFailures;
1230002d39b4SEd Tanous                     nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
12318bd25ccdSJames Feist                                                 .jsonValue["Redundancy"];
12321476687dSEd Tanous 
12331476687dSEd Tanous                     nlohmann::json::object_t redundancy;
12341476687dSEd Tanous                     redundancy["@odata.id"] =
1235002d39b4SEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1236002d39b4SEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode +
1237002d39b4SEd Tanous                         "#/Redundancy/" + std::to_string(jResp.size());
1238002d39b4SEd Tanous                     redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
12391476687dSEd Tanous                     redundancy["MinNumNeeded"] = minNumNeeded;
12401476687dSEd Tanous                     redundancy["MemberId"] = name;
12411476687dSEd Tanous                     redundancy["Mode"] = "N+m";
12421476687dSEd Tanous                     redundancy["Name"] = name;
12431476687dSEd Tanous                     redundancy["RedundancySet"] = redfishCollection;
12441476687dSEd Tanous                     redundancy["Status"]["Health"] = health;
12451476687dSEd Tanous                     redundancy["Status"]["State"] = "Enabled";
12461476687dSEd Tanous 
12471476687dSEd Tanous                     jResp.push_back(std::move(redundancy));
1248*86d89ed7SKrzysztof Grobelny                     });
12491e1e598dSJonathan Doman                 });
12508bd25ccdSJames Feist         }
12518bd25ccdSJames Feist         },
12528bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
12538bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
12548bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
12558bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
12568bd25ccdSJames Feist         std::array<const char*, 1>{
12578bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
12588bd25ccdSJames Feist }
12598bd25ccdSJames Feist 
1260b5a76932SEd Tanous inline void
126181ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
126249c53ac9SJohnathan Mantey {
12638d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
126449c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
126581ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
126649c53ac9SJohnathan Mantey     {
126749c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
126849c53ac9SJohnathan Mantey     }
126949c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
127049c53ac9SJohnathan Mantey     {
127149c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
127249c53ac9SJohnathan Mantey         if (entry != response.end())
127349c53ac9SJohnathan Mantey         {
127449c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
127502cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
127649c53ac9SJohnathan Mantey                 return c1["Name"] < c2["Name"];
127749c53ac9SJohnathan Mantey             });
127849c53ac9SJohnathan Mantey 
127949c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
128049c53ac9SJohnathan Mantey             size_t count = 0;
128149c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
128249c53ac9SJohnathan Mantey             {
128349c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
128449c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
128549c53ac9SJohnathan Mantey                 {
128649c53ac9SJohnathan Mantey                     continue;
128749c53ac9SJohnathan Mantey                 }
128849c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
128949c53ac9SJohnathan Mantey                 if (value != nullptr)
129049c53ac9SJohnathan Mantey                 {
129149c53ac9SJohnathan Mantey                     *value += std::to_string(count);
129249c53ac9SJohnathan Mantey                     count++;
129381ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
129449c53ac9SJohnathan Mantey                 }
129549c53ac9SJohnathan Mantey             }
129649c53ac9SJohnathan Mantey         }
129749c53ac9SJohnathan Mantey     }
129849c53ac9SJohnathan Mantey }
129949c53ac9SJohnathan Mantey 
130008777fb0SLewanczyk, Dawid /**
1301adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1302adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1303adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1304adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13058fb49dd6SShawn McCarney  */
130623a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1307b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1308adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
13098fb49dd6SShawn McCarney {
1310adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13118fb49dd6SShawn McCarney     {
1312adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13138fb49dd6SShawn McCarney         {
1314adc4f0dbSShawn McCarney             return &inventoryItem;
13158fb49dd6SShawn McCarney         }
13168fb49dd6SShawn McCarney     }
13178fb49dd6SShawn McCarney     return nullptr;
13188fb49dd6SShawn McCarney }
13198fb49dd6SShawn McCarney 
13208fb49dd6SShawn McCarney /**
1321adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1322adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1323adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1324adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13258fb49dd6SShawn McCarney  */
132623a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1327b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1328adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1329adc4f0dbSShawn McCarney {
1330adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1331adc4f0dbSShawn McCarney     {
1332adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1333adc4f0dbSShawn McCarney         {
1334adc4f0dbSShawn McCarney             return &inventoryItem;
1335adc4f0dbSShawn McCarney         }
1336adc4f0dbSShawn McCarney     }
1337adc4f0dbSShawn McCarney     return nullptr;
1338adc4f0dbSShawn McCarney }
1339adc4f0dbSShawn McCarney 
1340adc4f0dbSShawn McCarney /**
1341d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1342d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1343d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1344d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1345d500549bSAnthony Wilson  */
1346d500549bSAnthony Wilson inline InventoryItem*
1347d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1348d500549bSAnthony Wilson                             const std::string& ledObjPath)
1349d500549bSAnthony Wilson {
1350d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1351d500549bSAnthony Wilson     {
1352d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1353d500549bSAnthony Wilson         {
1354d500549bSAnthony Wilson             return &inventoryItem;
1355d500549bSAnthony Wilson         }
1356d500549bSAnthony Wilson     }
1357d500549bSAnthony Wilson     return nullptr;
1358d500549bSAnthony Wilson }
1359d500549bSAnthony Wilson 
1360d500549bSAnthony Wilson /**
1361adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1362adc4f0dbSShawn McCarney  *
1363adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1364adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1365adc4f0dbSShawn McCarney  * added to the vector.
1366adc4f0dbSShawn McCarney  *
1367adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1368adc4f0dbSShawn McCarney  * InventoryItem.
1369adc4f0dbSShawn McCarney  *
1370adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1371adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1372adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1373adc4f0dbSShawn McCarney  */
1374b5a76932SEd Tanous inline void addInventoryItem(
1375b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1376b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1377adc4f0dbSShawn McCarney {
1378adc4f0dbSShawn McCarney     // Look for inventory item in vector
1379adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1380adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1381adc4f0dbSShawn McCarney 
1382adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1383adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1384adc4f0dbSShawn McCarney     {
1385adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1386adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1387adc4f0dbSShawn McCarney     }
1388adc4f0dbSShawn McCarney 
1389adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1390adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1391adc4f0dbSShawn McCarney }
1392adc4f0dbSShawn McCarney 
1393adc4f0dbSShawn McCarney /**
1394adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1395adc4f0dbSShawn McCarney  *
1396adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1397adc4f0dbSShawn McCarney  * specified InventoryItem.
1398adc4f0dbSShawn McCarney  *
1399adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1400adc4f0dbSShawn McCarney  * response.
1401adc4f0dbSShawn McCarney  *
1402adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1403adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1404adc4f0dbSShawn McCarney  * for the specified inventory item.
1405adc4f0dbSShawn McCarney  */
140623a21a1cSEd Tanous inline void storeInventoryItemData(
1407adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1408711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
14098fb49dd6SShawn McCarney {
1410adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1411711ac7a9SEd Tanous 
14129eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
14138fb49dd6SShawn McCarney     {
1414711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
14158fb49dd6SShawn McCarney         {
14169eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1417711ac7a9SEd Tanous             {
1418711ac7a9SEd Tanous                 if (name == "Present")
1419711ac7a9SEd Tanous                 {
1420711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1421adc4f0dbSShawn McCarney                     if (value != nullptr)
14228fb49dd6SShawn McCarney                     {
1423adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
14248fb49dd6SShawn McCarney                     }
14258fb49dd6SShawn McCarney                 }
14268fb49dd6SShawn McCarney             }
1427711ac7a9SEd Tanous         }
1428adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1429711ac7a9SEd Tanous 
1430711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
14318fb49dd6SShawn McCarney         {
1432adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
14338fb49dd6SShawn McCarney         }
1434adc4f0dbSShawn McCarney 
1435adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1436711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1437adc4f0dbSShawn McCarney         {
14389eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1439711ac7a9SEd Tanous             {
1440711ac7a9SEd Tanous                 if (name == "Manufacturer")
1441adc4f0dbSShawn McCarney                 {
1442adc4f0dbSShawn McCarney                     const std::string* value =
1443711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1444adc4f0dbSShawn McCarney                     if (value != nullptr)
1445adc4f0dbSShawn McCarney                     {
1446adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1447adc4f0dbSShawn McCarney                     }
1448adc4f0dbSShawn McCarney                 }
1449711ac7a9SEd Tanous                 if (name == "Model")
1450adc4f0dbSShawn McCarney                 {
1451adc4f0dbSShawn McCarney                     const std::string* value =
1452711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1453adc4f0dbSShawn McCarney                     if (value != nullptr)
1454adc4f0dbSShawn McCarney                     {
1455adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1456adc4f0dbSShawn McCarney                     }
1457adc4f0dbSShawn McCarney                 }
1458711ac7a9SEd Tanous                 if (name == "SerialNumber")
1459adc4f0dbSShawn McCarney                 {
1460adc4f0dbSShawn McCarney                     const std::string* value =
1461711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1462adc4f0dbSShawn McCarney                     if (value != nullptr)
1463adc4f0dbSShawn McCarney                     {
1464adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1465adc4f0dbSShawn McCarney                     }
1466adc4f0dbSShawn McCarney                 }
1467711ac7a9SEd Tanous                 if (name == "PartNumber")
1468711ac7a9SEd Tanous                 {
1469711ac7a9SEd Tanous                     const std::string* value =
1470711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1471711ac7a9SEd Tanous                     if (value != nullptr)
1472711ac7a9SEd Tanous                     {
1473711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1474711ac7a9SEd Tanous                     }
1475711ac7a9SEd Tanous                 }
1476711ac7a9SEd Tanous             }
1477adc4f0dbSShawn McCarney         }
1478adc4f0dbSShawn McCarney 
1479711ac7a9SEd Tanous         if (interface ==
1480711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1481adc4f0dbSShawn McCarney         {
14829eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1483adc4f0dbSShawn McCarney             {
1484711ac7a9SEd Tanous                 if (name == "Functional")
1485711ac7a9SEd Tanous                 {
1486711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1487adc4f0dbSShawn McCarney                     if (value != nullptr)
1488adc4f0dbSShawn McCarney                     {
1489adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
14908fb49dd6SShawn McCarney                     }
14918fb49dd6SShawn McCarney                 }
14928fb49dd6SShawn McCarney             }
14938fb49dd6SShawn McCarney         }
1494711ac7a9SEd Tanous     }
1495711ac7a9SEd Tanous }
14968fb49dd6SShawn McCarney 
14978fb49dd6SShawn McCarney /**
1498adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
14998fb49dd6SShawn McCarney  *
1500adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1501adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1502adc4f0dbSShawn McCarney  * inventoryItems vector.
15038fb49dd6SShawn McCarney  *
1504adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1505adc4f0dbSShawn McCarney  * response.
1506adc4f0dbSShawn McCarney  *
1507adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1508adc4f0dbSShawn McCarney  * been obtained.
1509adc4f0dbSShawn McCarney  *
1510adc4f0dbSShawn McCarney  * The callback must have the following signature:
1511adc4f0dbSShawn McCarney  *   @code
1512d500549bSAnthony Wilson  *   callback(void)
1513adc4f0dbSShawn McCarney  *   @endcode
1514adc4f0dbSShawn McCarney  *
1515adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1516adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1517adc4f0dbSShawn McCarney  * last asynchronous function has completed.
15188fb49dd6SShawn McCarney  *
15198fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1520adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1521adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
15228fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
15238fb49dd6SShawn McCarney  * implements ObjectManager.
1524adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1525adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1526adc4f0dbSShawn McCarney  * in recursive calls to this function.
15278fb49dd6SShawn McCarney  */
1528adc4f0dbSShawn McCarney template <typename Callback>
1529adc4f0dbSShawn McCarney static void getInventoryItemsData(
15308fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1531adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1532fe04d49cSNan Zhou     std::shared_ptr<std::set<std::string>> invConnections,
1533fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
1534271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
15358fb49dd6SShawn McCarney {
1536adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
15378fb49dd6SShawn McCarney 
1538adc4f0dbSShawn McCarney     // If no more connections left, call callback
1539adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
15408fb49dd6SShawn McCarney     {
1541d500549bSAnthony Wilson         callback();
1542adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1543adc4f0dbSShawn McCarney         return;
1544adc4f0dbSShawn McCarney     }
1545adc4f0dbSShawn McCarney 
1546adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1547fe04d49cSNan Zhou     auto it = invConnections->begin();
1548fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1549adc4f0dbSShawn McCarney     if (it != invConnections->end())
1550adc4f0dbSShawn McCarney     {
1551adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1552adc4f0dbSShawn McCarney 
15538fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1554002d39b4SEd Tanous         auto respHandler =
1555002d39b4SEd Tanous             [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
155602cad96eSEd Tanous              callback{std::forward<Callback>(callback)}, invConnectionsIndex](
155702cad96eSEd Tanous                 const boost::system::error_code ec,
155802cad96eSEd Tanous                 const dbus::utility::ManagedObjectType& resp) {
1559adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
15608fb49dd6SShawn McCarney             if (ec)
15618fb49dd6SShawn McCarney             {
15628fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1563adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
15648d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
15658fb49dd6SShawn McCarney                 return;
15668fb49dd6SShawn McCarney             }
15678fb49dd6SShawn McCarney 
15688fb49dd6SShawn McCarney             // Loop through returned object paths
15698fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
15708fb49dd6SShawn McCarney             {
15718fb49dd6SShawn McCarney                 const std::string& objPath =
15728fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
15738fb49dd6SShawn McCarney 
1574adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1575adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1576adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1577adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
15788fb49dd6SShawn McCarney                 {
1579adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1580adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
15818fb49dd6SShawn McCarney                 }
15828fb49dd6SShawn McCarney             }
15838fb49dd6SShawn McCarney 
1584adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1585adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1586adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1587adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1588adc4f0dbSShawn McCarney 
1589adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
15908fb49dd6SShawn McCarney         };
15918fb49dd6SShawn McCarney 
15928fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
15938fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
15948fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
15958fb49dd6SShawn McCarney         const std::string& objectMgrPath =
15968fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
15978fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
15988fb49dd6SShawn McCarney                          << objectMgrPath;
15998fb49dd6SShawn McCarney 
16008fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16018fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16028fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16038fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16048fb49dd6SShawn McCarney     }
16058fb49dd6SShawn McCarney 
1606adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16078fb49dd6SShawn McCarney }
16088fb49dd6SShawn McCarney 
16098fb49dd6SShawn McCarney /**
1610adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16118fb49dd6SShawn McCarney  *
1612adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1613adc4f0dbSShawn McCarney  * items that are associated with sensors.
16148fb49dd6SShawn McCarney  *
16158fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
16168fb49dd6SShawn McCarney  * been obtained.
16178fb49dd6SShawn McCarney  *
16188fb49dd6SShawn McCarney  * The callback must have the following signature:
16198fb49dd6SShawn McCarney  *   @code
1620fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
16218fb49dd6SShawn McCarney  *   @endcode
16228fb49dd6SShawn McCarney  *
16238fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1624adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
16258fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
16268fb49dd6SShawn McCarney  */
16278fb49dd6SShawn McCarney template <typename Callback>
16288fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1629b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1630b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
16318fb49dd6SShawn McCarney     Callback&& callback)
16328fb49dd6SShawn McCarney {
16338fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
16348fb49dd6SShawn McCarney 
16358fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1636adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
16378fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1638adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1639adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
16408fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
16418fb49dd6SShawn McCarney 
16428fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1643002d39b4SEd Tanous     auto respHandler =
1644002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1645002d39b4SEd Tanous          inventoryItems](
1646b9d36b47SEd Tanous             const boost::system::error_code ec,
1647002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
16488fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
16498fb49dd6SShawn McCarney         if (ec)
16508fb49dd6SShawn McCarney         {
16518d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
16528fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
16538fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
16548fb49dd6SShawn McCarney             return;
16558fb49dd6SShawn McCarney         }
16568fb49dd6SShawn McCarney 
16578fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
1658fe04d49cSNan Zhou         std::shared_ptr<std::set<std::string>> invConnections =
1659fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
16608fb49dd6SShawn McCarney 
16618fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
16628fb49dd6SShawn McCarney         for (const std::pair<
16638fb49dd6SShawn McCarney                  std::string,
16648fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
16658fb49dd6SShawn McCarney                  object : subtree)
16668fb49dd6SShawn McCarney         {
1667adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
16688fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1669adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
16708fb49dd6SShawn McCarney             {
16718fb49dd6SShawn McCarney                 // Store all connections to inventory item
16728fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
16738fb49dd6SShawn McCarney                          objData : object.second)
16748fb49dd6SShawn McCarney                 {
16758fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
16768fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
16778fb49dd6SShawn McCarney                 }
16788fb49dd6SShawn McCarney             }
16798fb49dd6SShawn McCarney         }
1680d500549bSAnthony Wilson 
16818fb49dd6SShawn McCarney         callback(invConnections);
16828fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
16838fb49dd6SShawn McCarney     };
16848fb49dd6SShawn McCarney 
16858fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
16868fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
16878fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
16888fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
16898fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
16908fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
16918fb49dd6SShawn McCarney }
16928fb49dd6SShawn McCarney 
16938fb49dd6SShawn McCarney /**
1694adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
16958fb49dd6SShawn McCarney  *
16968fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1697d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1698d500549bSAnthony Wilson  * their LEDs, if any.
16998fb49dd6SShawn McCarney  *
17008fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17018fb49dd6SShawn McCarney  * has been obtained.
17028fb49dd6SShawn McCarney  *
17038fb49dd6SShawn McCarney  * The callback must have the following signature:
17048fb49dd6SShawn McCarney  *   @code
1705adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17068fb49dd6SShawn McCarney  *   @endcode
17078fb49dd6SShawn McCarney  *
17088fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17098fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17108fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
17118fb49dd6SShawn McCarney  * implements ObjectManager.
17128fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17138fb49dd6SShawn McCarney  */
17148fb49dd6SShawn McCarney template <typename Callback>
1715adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1716b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1717fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
1718fe04d49cSNan Zhou     const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
17198fb49dd6SShawn McCarney     Callback&& callback)
17208fb49dd6SShawn McCarney {
1721adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
17228fb49dd6SShawn McCarney 
17238fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
172402cad96eSEd Tanous     auto respHandler =
172502cad96eSEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
17268fb49dd6SShawn McCarney          sensorNames](const boost::system::error_code ec,
172702cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
1728adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
17298fb49dd6SShawn McCarney         if (ec)
17308fb49dd6SShawn McCarney         {
1731adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1732adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
17338d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17348fb49dd6SShawn McCarney             return;
17358fb49dd6SShawn McCarney         }
17368fb49dd6SShawn McCarney 
1737adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1738adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1739adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1740adc4f0dbSShawn McCarney 
17418fb49dd6SShawn McCarney         // Loop through returned object paths
17428fb49dd6SShawn McCarney         std::string sensorAssocPath;
17438fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
17448fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
17458fb49dd6SShawn McCarney         {
17468fb49dd6SShawn McCarney             const std::string& objPath =
17478fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
17488fb49dd6SShawn McCarney 
17498fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
17508fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
17518fb49dd6SShawn McCarney             {
17528fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
17538fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
17548fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
17558fb49dd6SShawn McCarney                 {
17568fb49dd6SShawn McCarney                     // Get Association interface for object path
1757711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
17588fb49dd6SShawn McCarney                     {
1759711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1760711ac7a9SEd Tanous                         {
1761711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1762711ac7a9SEd Tanous                             {
1763711ac7a9SEd Tanous                                 if (valueName == "endpoints")
17648fb49dd6SShawn McCarney                                 {
17658fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
17668fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1767711ac7a9SEd Tanous                                             &value);
1768711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1769711ac7a9SEd Tanous                                         !endpoints->empty())
17708fb49dd6SShawn McCarney                                     {
1771adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1772adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1773adc4f0dbSShawn McCarney                                             endpoints->front();
1774711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1775711ac7a9SEd Tanous                                                          invItemPath,
1776adc4f0dbSShawn McCarney                                                          sensorName);
17778fb49dd6SShawn McCarney                                     }
17788fb49dd6SShawn McCarney                                 }
17798fb49dd6SShawn McCarney                             }
1780711ac7a9SEd Tanous                         }
1781711ac7a9SEd Tanous                     }
17828fb49dd6SShawn McCarney                     break;
17838fb49dd6SShawn McCarney                 }
17848fb49dd6SShawn McCarney             }
17858fb49dd6SShawn McCarney         }
17868fb49dd6SShawn McCarney 
1787d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1788d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1789d500549bSAnthony Wilson         std::string inventoryAssocPath;
1790d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1791d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1792d500549bSAnthony Wilson         {
1793d500549bSAnthony Wilson             const std::string& objPath =
1794d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1795d500549bSAnthony Wilson 
1796d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1797d500549bSAnthony Wilson             {
1798d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1799d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1800d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1801d500549bSAnthony Wilson                 {
1802711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1803d500549bSAnthony Wilson                     {
1804711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1805711ac7a9SEd Tanous                         {
1806711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1807711ac7a9SEd Tanous                             {
1808711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1809d500549bSAnthony Wilson                                 {
1810d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1811d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1812711ac7a9SEd Tanous                                             &value);
1813711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1814711ac7a9SEd Tanous                                         !endpoints->empty())
1815d500549bSAnthony Wilson                                     {
1816711ac7a9SEd Tanous                                         // Add inventory item to vector
1817d500549bSAnthony Wilson                                         // Store LED path in inventory item
1818711ac7a9SEd Tanous                                         const std::string& ledPath =
1819711ac7a9SEd Tanous                                             endpoints->front();
1820d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1821d500549bSAnthony Wilson                                     }
1822d500549bSAnthony Wilson                                 }
1823d500549bSAnthony Wilson                             }
1824711ac7a9SEd Tanous                         }
1825711ac7a9SEd Tanous                     }
1826711ac7a9SEd Tanous 
1827d500549bSAnthony Wilson                     break;
1828d500549bSAnthony Wilson                 }
1829d500549bSAnthony Wilson             }
1830d500549bSAnthony Wilson         }
1831adc4f0dbSShawn McCarney         callback(inventoryItems);
1832adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
18338fb49dd6SShawn McCarney     };
18348fb49dd6SShawn McCarney 
18358fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
18368fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
18378fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
18388fb49dd6SShawn McCarney     const std::string& objectMgrPath =
18398fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
18408fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
18418fb49dd6SShawn McCarney                      << objectMgrPath;
18428fb49dd6SShawn McCarney 
18438fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
18448fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
18458fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
18468fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18478fb49dd6SShawn McCarney 
1848adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
18498fb49dd6SShawn McCarney }
18508fb49dd6SShawn McCarney 
18518fb49dd6SShawn McCarney /**
1852d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1853d500549bSAnthony Wilson  *
1854d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1855d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1856d500549bSAnthony Wilson  * inventoryItems vector.
1857d500549bSAnthony Wilson  *
1858d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1859d500549bSAnthony Wilson  * response.
1860d500549bSAnthony Wilson  *
1861d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1862d500549bSAnthony Wilson  * has been obtained.
1863d500549bSAnthony Wilson  *
1864d500549bSAnthony Wilson  * The callback must have the following signature:
1865d500549bSAnthony Wilson  *   @code
186642cbe538SGunnar Mills  *   callback()
1867d500549bSAnthony Wilson  *   @endcode
1868d500549bSAnthony Wilson  *
1869d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1870d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1871d500549bSAnthony Wilson  * last asynchronous function has completed.
1872d500549bSAnthony Wilson  *
1873d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1874d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1875d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1876d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1877d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1878d500549bSAnthony Wilson  * in recursive calls to this function.
1879d500549bSAnthony Wilson  */
1880d500549bSAnthony Wilson template <typename Callback>
1881d500549bSAnthony Wilson void getInventoryLedData(
1882d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1883d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1884fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1885d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1886d500549bSAnthony Wilson {
1887d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1888d500549bSAnthony Wilson 
1889d500549bSAnthony Wilson     // If no more connections left, call callback
1890d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1891d500549bSAnthony Wilson     {
189242cbe538SGunnar Mills         callback();
1893d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1894d500549bSAnthony Wilson         return;
1895d500549bSAnthony Wilson     }
1896d500549bSAnthony Wilson 
1897d500549bSAnthony Wilson     // Get inventory item data from current connection
1898fe04d49cSNan Zhou     auto it = ledConnections->begin();
1899fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1900d500549bSAnthony Wilson     if (it != ledConnections->end())
1901d500549bSAnthony Wilson     {
1902d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1903d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1904d500549bSAnthony Wilson         // Response handler for Get State property
19051e1e598dSJonathan Doman         auto respHandler =
19061e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1907f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
19081e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1909d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1910d500549bSAnthony Wilson             if (ec)
1911d500549bSAnthony Wilson             {
1912d500549bSAnthony Wilson                 BMCWEB_LOG_ERROR
1913d500549bSAnthony Wilson                     << "getInventoryLedData respHandler DBus error " << ec;
19148d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1915d500549bSAnthony Wilson                 return;
1916d500549bSAnthony Wilson             }
1917d500549bSAnthony Wilson 
19181e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Led state: " << state;
1919d500549bSAnthony Wilson             // Find inventory item with this LED object path
1920d500549bSAnthony Wilson             InventoryItem* inventoryItem =
1921d500549bSAnthony Wilson                 findInventoryItemForLed(*inventoryItems, ledPath);
1922d500549bSAnthony Wilson             if (inventoryItem != nullptr)
1923d500549bSAnthony Wilson             {
1924d500549bSAnthony Wilson                 // Store LED state in InventoryItem
192511ba3979SEd Tanous                 if (state.ends_with("On"))
1926d500549bSAnthony Wilson                 {
1927d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::ON;
1928d500549bSAnthony Wilson                 }
192911ba3979SEd Tanous                 else if (state.ends_with("Blink"))
1930d500549bSAnthony Wilson                 {
1931d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::BLINK;
1932d500549bSAnthony Wilson                 }
193311ba3979SEd Tanous                 else if (state.ends_with("Off"))
1934d500549bSAnthony Wilson                 {
1935d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::OFF;
1936d500549bSAnthony Wilson                 }
1937d500549bSAnthony Wilson                 else
1938d500549bSAnthony Wilson                 {
1939d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::UNKNOWN;
1940d500549bSAnthony Wilson                 }
1941d500549bSAnthony Wilson             }
1942d500549bSAnthony Wilson 
1943d500549bSAnthony Wilson             // Recurse to get LED data from next connection
1944d500549bSAnthony Wilson             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1945d500549bSAnthony Wilson                                 ledConnections, std::move(callback),
1946d500549bSAnthony Wilson                                 ledConnectionsIndex + 1);
1947d500549bSAnthony Wilson 
1948d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1949d500549bSAnthony Wilson         };
1950d500549bSAnthony Wilson 
1951d500549bSAnthony Wilson         // Get the State property for the current LED
19521e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
19531e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
19541e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
19551e1e598dSJonathan Doman             std::move(respHandler));
1956d500549bSAnthony Wilson     }
1957d500549bSAnthony Wilson 
1958d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1959d500549bSAnthony Wilson }
1960d500549bSAnthony Wilson 
1961d500549bSAnthony Wilson /**
1962d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1963d500549bSAnthony Wilson  *
1964d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1965d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1966d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1967d500549bSAnthony Wilson  *
1968d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1969d500549bSAnthony Wilson  * response.
1970d500549bSAnthony Wilson  *
1971d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1972d500549bSAnthony Wilson  * been obtained.
1973d500549bSAnthony Wilson  *
1974d500549bSAnthony Wilson  * The callback must have the following signature:
1975d500549bSAnthony Wilson  *   @code
197642cbe538SGunnar Mills  *   callback()
1977d500549bSAnthony Wilson  *   @endcode
1978d500549bSAnthony Wilson  *
1979d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1980d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1981d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1982d500549bSAnthony Wilson  */
1983d500549bSAnthony Wilson template <typename Callback>
1984d500549bSAnthony Wilson void getInventoryLeds(
1985d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1986d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1987d500549bSAnthony Wilson     Callback&& callback)
1988d500549bSAnthony Wilson {
1989d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1990d500549bSAnthony Wilson 
1991d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1992d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
1993d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1994d500549bSAnthony Wilson 
1995d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
1996002d39b4SEd Tanous     auto respHandler =
1997002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1998002d39b4SEd Tanous          inventoryItems](
1999b9d36b47SEd Tanous             const boost::system::error_code ec,
2000002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
2001d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2002d500549bSAnthony Wilson         if (ec)
2003d500549bSAnthony Wilson         {
20048d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2005d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2006d500549bSAnthony Wilson                              << ec;
2007d500549bSAnthony Wilson             return;
2008d500549bSAnthony Wilson         }
2009d500549bSAnthony Wilson 
2010d500549bSAnthony Wilson         // Build map of LED object paths to connections
2011fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> ledConnections =
2012fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
2013d500549bSAnthony Wilson 
2014d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2015d500549bSAnthony Wilson         for (const std::pair<
2016d500549bSAnthony Wilson                  std::string,
2017d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2018d500549bSAnthony Wilson                  object : subtree)
2019d500549bSAnthony Wilson         {
2020d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2021d500549bSAnthony Wilson             // items
2022d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2023d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2024d500549bSAnthony Wilson             {
2025d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2026d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2027d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2028d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2029d500549bSAnthony Wilson                                  << connection;
2030d500549bSAnthony Wilson             }
2031d500549bSAnthony Wilson         }
2032d500549bSAnthony Wilson 
2033d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2034d500549bSAnthony Wilson                             std::move(callback));
2035d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2036d500549bSAnthony Wilson     };
2037d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2038d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2039d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2040d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2041d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2042d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2043d500549bSAnthony Wilson }
2044d500549bSAnthony Wilson 
2045d500549bSAnthony Wilson /**
204642cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
204742cbe538SGunnar Mills  *
204842cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
204942cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
205042cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
205142cbe538SGunnar Mills  *
205242cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
205342cbe538SGunnar Mills  * response.
205442cbe538SGunnar Mills  *
205542cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
205642cbe538SGunnar Mills  * when data has been obtained.
205742cbe538SGunnar Mills  *
205842cbe538SGunnar Mills  * The callback must have the following signature:
205942cbe538SGunnar Mills  *   @code
206042cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
206142cbe538SGunnar Mills  *   @endcode
206242cbe538SGunnar Mills  *
206342cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
206442cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
206542cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
206642cbe538SGunnar Mills  *        Supply Attributes
206742cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
206842cbe538SGunnar Mills  */
206942cbe538SGunnar Mills template <typename Callback>
207042cbe538SGunnar Mills void getPowerSupplyAttributesData(
2071b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
207242cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2073fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
207442cbe538SGunnar Mills     Callback&& callback)
207542cbe538SGunnar Mills {
207642cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
207742cbe538SGunnar Mills 
207842cbe538SGunnar Mills     if (psAttributesConnections.empty())
207942cbe538SGunnar Mills     {
208042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
208142cbe538SGunnar Mills         callback(inventoryItems);
208242cbe538SGunnar Mills         return;
208342cbe538SGunnar Mills     }
208442cbe538SGunnar Mills 
208542cbe538SGunnar Mills     // Assuming just one connection (service) for now
2086fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
208742cbe538SGunnar Mills 
208842cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
208942cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
209042cbe538SGunnar Mills 
209142cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
2092002d39b4SEd Tanous     auto respHandler =
2093002d39b4SEd Tanous         [sensorsAsyncResp, inventoryItems,
2094f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2095002d39b4SEd Tanous             const boost::system::error_code ec, const uint32_t value) {
209642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
209742cbe538SGunnar Mills         if (ec)
209842cbe538SGunnar Mills         {
209942cbe538SGunnar Mills             BMCWEB_LOG_ERROR
210042cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
21018d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
210242cbe538SGunnar Mills             return;
210342cbe538SGunnar Mills         }
210442cbe538SGunnar Mills 
21051e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
210642cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
210742cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
210842cbe538SGunnar Mills         {
210955f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
211042cbe538SGunnar Mills             {
211142cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
21121e1e598dSJonathan Doman                     static_cast<int>(value);
211342cbe538SGunnar Mills             }
211442cbe538SGunnar Mills         }
211542cbe538SGunnar Mills 
211642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
211742cbe538SGunnar Mills         callback(inventoryItems);
211842cbe538SGunnar Mills     };
211942cbe538SGunnar Mills 
212042cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
212142cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
21221e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
21231e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
21241e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
21251e1e598dSJonathan Doman         std::move(respHandler));
212642cbe538SGunnar Mills 
212742cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
212842cbe538SGunnar Mills }
212942cbe538SGunnar Mills 
213042cbe538SGunnar Mills /**
213142cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
213242cbe538SGunnar Mills  *
213342cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
213442cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
213542cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
213642cbe538SGunnar Mills  * item.
213742cbe538SGunnar Mills  *
213842cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
213942cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
214042cbe538SGunnar Mills  *
214142cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
214242cbe538SGunnar Mills  * when information has been obtained.
214342cbe538SGunnar Mills  *
214442cbe538SGunnar Mills  * The callback must have the following signature:
214542cbe538SGunnar Mills  *   @code
214642cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
214742cbe538SGunnar Mills  *   @endcode
214842cbe538SGunnar Mills  *
214942cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
215042cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
215142cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
215242cbe538SGunnar Mills  */
215342cbe538SGunnar Mills template <typename Callback>
215442cbe538SGunnar Mills void getPowerSupplyAttributes(
215542cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
215642cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
215742cbe538SGunnar Mills     Callback&& callback)
215842cbe538SGunnar Mills {
215942cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
216042cbe538SGunnar Mills 
216142cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2162a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
216342cbe538SGunnar Mills     {
216442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
216542cbe538SGunnar Mills         callback(inventoryItems);
216642cbe538SGunnar Mills         return;
216742cbe538SGunnar Mills     }
216842cbe538SGunnar Mills 
216942cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
217042cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
217142cbe538SGunnar Mills 
217242cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2173b9d36b47SEd Tanous     auto respHandler =
2174b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2175b9d36b47SEd Tanous          inventoryItems](
2176b9d36b47SEd Tanous             const boost::system::error_code ec,
2177b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
217842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
217942cbe538SGunnar Mills         if (ec)
218042cbe538SGunnar Mills         {
21818d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
218242cbe538SGunnar Mills             BMCWEB_LOG_ERROR
218342cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
218442cbe538SGunnar Mills             return;
218542cbe538SGunnar Mills         }
218626f6976fSEd Tanous         if (subtree.empty())
218742cbe538SGunnar Mills         {
218842cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
218942cbe538SGunnar Mills             callback(inventoryItems);
219042cbe538SGunnar Mills             return;
219142cbe538SGunnar Mills         }
219242cbe538SGunnar Mills 
219342cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
219442cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
219542cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
2196fe04d49cSNan Zhou         std::map<std::string, std::string> psAttributesConnections;
219742cbe538SGunnar Mills 
219842cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
219942cbe538SGunnar Mills         {
220042cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
220142cbe538SGunnar Mills             callback(inventoryItems);
220242cbe538SGunnar Mills             return;
220342cbe538SGunnar Mills         }
220442cbe538SGunnar Mills 
220542cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
220642cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
220742cbe538SGunnar Mills 
220842cbe538SGunnar Mills         if (connection.empty())
220942cbe538SGunnar Mills         {
221042cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
221142cbe538SGunnar Mills             callback(inventoryItems);
221242cbe538SGunnar Mills             return;
221342cbe538SGunnar Mills         }
221442cbe538SGunnar Mills 
221542cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
221642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
221742cbe538SGunnar Mills                          << connection;
221842cbe538SGunnar Mills 
221942cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
222042cbe538SGunnar Mills                                      psAttributesConnections,
222142cbe538SGunnar Mills                                      std::move(callback));
222242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
222342cbe538SGunnar Mills     };
222442cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
222542cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
222642cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
222742cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
222842cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
222942cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
223042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
223142cbe538SGunnar Mills }
223242cbe538SGunnar Mills 
223342cbe538SGunnar Mills /**
2234adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
22358fb49dd6SShawn McCarney  *
22368fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2237adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
22388fb49dd6SShawn McCarney  *
2239adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2240adc4f0dbSShawn McCarney  * response.
22418fb49dd6SShawn McCarney  *
2242adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2243adc4f0dbSShawn McCarney  * inventory items have been obtained.
2244adc4f0dbSShawn McCarney  *
2245adc4f0dbSShawn McCarney  * The callback must have the following signature:
2246adc4f0dbSShawn McCarney  *   @code
2247adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2248adc4f0dbSShawn McCarney  *   @endcode
22498fb49dd6SShawn McCarney  *
22508fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
22518fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
22528fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
22538fb49dd6SShawn McCarney  * implements ObjectManager.
2254adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
22558fb49dd6SShawn McCarney  */
2256adc4f0dbSShawn McCarney template <typename Callback>
2257adc4f0dbSShawn McCarney static void getInventoryItems(
22588fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2259fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>> sensorNames,
2260fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
2261adc4f0dbSShawn McCarney     Callback&& callback)
22628fb49dd6SShawn McCarney {
2263adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2264adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2265f94c4ecfSEd Tanous         [sensorsAsyncResp, objectMgrPaths,
2266f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2267adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2268adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
22698fb49dd6SShawn McCarney         auto getInventoryItemsConnectionsCb =
2270adc4f0dbSShawn McCarney             [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2271f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
2272fe04d49cSNan Zhou                 std::shared_ptr<std::set<std::string>> invConnections) {
22738fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2274002d39b4SEd Tanous             auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2275d500549bSAnthony Wilson                                             callback{std::move(callback)}]() {
2276d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
227742cbe538SGunnar Mills 
2278002d39b4SEd Tanous                 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2279002d39b4SEd Tanous                                            callback{std::move(callback)}]() {
228042cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
228142cbe538SGunnar Mills                     // Find Power Supply Attributes and get the data
2282002d39b4SEd Tanous                     getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
228342cbe538SGunnar Mills                                              std::move(callback));
228442cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
228542cbe538SGunnar Mills                 };
228642cbe538SGunnar Mills 
2287d500549bSAnthony Wilson                 // Find led connections and get the data
2288d500549bSAnthony Wilson                 getInventoryLeds(sensorsAsyncResp, inventoryItems,
228942cbe538SGunnar Mills                                  std::move(getInventoryLedsCb));
2290d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2291d500549bSAnthony Wilson             };
22928fb49dd6SShawn McCarney 
2293adc4f0dbSShawn McCarney             // Get inventory item data from connections
2294adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2295adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
2296d500549bSAnthony Wilson                                   std::move(getInventoryItemsDataCb));
22978fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
22988fb49dd6SShawn McCarney         };
22998fb49dd6SShawn McCarney 
2300adc4f0dbSShawn McCarney         // Get connections that provide inventory item data
2301002d39b4SEd Tanous         getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
23028fb49dd6SShawn McCarney                                      std::move(getInventoryItemsConnectionsCb));
2303adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
23048fb49dd6SShawn McCarney     };
23058fb49dd6SShawn McCarney 
2306adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2307adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2308adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2309adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2310adc4f0dbSShawn McCarney }
2311adc4f0dbSShawn McCarney 
2312adc4f0dbSShawn McCarney /**
2313adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2314adc4f0dbSShawn McCarney  *
2315adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2316adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2317adc4f0dbSShawn McCarney  * array.
2318adc4f0dbSShawn McCarney  *
2319adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2320adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2321adc4f0dbSShawn McCarney  * object.
2322adc4f0dbSShawn McCarney  *
2323adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2324adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2325adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2326adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2327adc4f0dbSShawn McCarney  */
232823a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2329adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2330adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2331adc4f0dbSShawn McCarney {
2332adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2333adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2334adc4f0dbSShawn McCarney     {
2335adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2336adc4f0dbSShawn McCarney         {
2337adc4f0dbSShawn McCarney             return powerSupply;
2338adc4f0dbSShawn McCarney         }
2339adc4f0dbSShawn McCarney     }
2340adc4f0dbSShawn McCarney 
2341adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2342adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2343adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2344adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2345adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2346adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2347adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2348adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2349adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2350adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2351adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2352d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2353adc4f0dbSShawn McCarney 
235442cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
235542cbe538SGunnar Mills     {
235642cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
235742cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
235842cbe538SGunnar Mills     }
235942cbe538SGunnar Mills 
236042cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2361adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2362adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2363adc4f0dbSShawn McCarney 
2364adc4f0dbSShawn McCarney     return powerSupply;
23658fb49dd6SShawn McCarney }
23668fb49dd6SShawn McCarney 
23678fb49dd6SShawn McCarney /**
2368de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2369de629b6eSShawn McCarney  *
2370de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2371de629b6eSShawn McCarney  *
2372de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2373de629b6eSShawn McCarney  * information has been obtained.
2374de629b6eSShawn McCarney  *
2375adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2376de629b6eSShawn McCarney  *
2377de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2378de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2379de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2380de629b6eSShawn McCarney  *
2381de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2382de629b6eSShawn McCarney  *
2383de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2384de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2385de629b6eSShawn McCarney  *
2386adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2387adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2388adc4f0dbSShawn McCarney  *
2389de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2390adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2391de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2392de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2393de629b6eSShawn McCarney  * implements ObjectManager.
2394adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2395de629b6eSShawn McCarney  */
239623a21a1cSEd Tanous inline void getSensorData(
239781ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2398fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2399fe04d49cSNan Zhou     const std::set<std::string>& connections,
2400fe04d49cSNan Zhou     const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
2401b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2402de629b6eSShawn McCarney {
2403de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2404de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2405de629b6eSShawn McCarney     for (const std::string& connection : connections)
2406de629b6eSShawn McCarney     {
2407de629b6eSShawn McCarney         // Response handler to process managed objects
2408002d39b4SEd Tanous         auto getManagedObjectsCb =
2409002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
2410002d39b4SEd Tanous              inventoryItems](const boost::system::error_code ec,
241102cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
2412de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2413de629b6eSShawn McCarney             if (ec)
2414de629b6eSShawn McCarney             {
2415de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
24168d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2417de629b6eSShawn McCarney                 return;
2418de629b6eSShawn McCarney             }
2419de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2420de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2421de629b6eSShawn McCarney             {
2422de629b6eSShawn McCarney                 const std::string& objPath =
2423de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2424de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2425de629b6eSShawn McCarney                                  << objPath;
2426de629b6eSShawn McCarney 
2427de629b6eSShawn McCarney                 std::vector<std::string> split;
2428de629b6eSShawn McCarney                 // Reserve space for
2429de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2430de629b6eSShawn McCarney                 split.reserve(6);
2431de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2432de629b6eSShawn McCarney                 if (split.size() < 6)
2433de629b6eSShawn McCarney                 {
2434de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2435de629b6eSShawn McCarney                                      << objPath;
2436de629b6eSShawn McCarney                     continue;
2437de629b6eSShawn McCarney                 }
2438de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2439de629b6eSShawn McCarney                 // string at the beginning
2440de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2441de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2442de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2443de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
244449c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2445de629b6eSShawn McCarney                 {
2446accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2447de629b6eSShawn McCarney                     continue;
2448de629b6eSShawn McCarney                 }
2449de629b6eSShawn McCarney 
2450adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2451adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2452adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2453adc4f0dbSShawn McCarney 
245495a3ecadSAnthony Wilson                 const std::string& sensorSchema =
245581ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
245695a3ecadSAnthony Wilson 
245795a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
245895a3ecadSAnthony Wilson 
2459928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2460928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
246195a3ecadSAnthony Wilson                 {
24628d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
246381ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
246481ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
246595a3ecadSAnthony Wilson                         sensorName;
24668d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
246795a3ecadSAnthony Wilson                 }
246895a3ecadSAnthony Wilson                 else
246995a3ecadSAnthony Wilson                 {
2470271584abSEd Tanous                     std::string fieldName;
2471928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2472928fefb9SNan Zhou                     {
2473928fefb9SNan Zhou                         fieldName = "Members";
2474928fefb9SNan Zhou                     }
2475928fefb9SNan Zhou                     else if (sensorType == "temperature")
2476de629b6eSShawn McCarney                     {
2477de629b6eSShawn McCarney                         fieldName = "Temperatures";
2478de629b6eSShawn McCarney                     }
2479de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2480de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2481de629b6eSShawn McCarney                     {
2482de629b6eSShawn McCarney                         fieldName = "Fans";
2483de629b6eSShawn McCarney                     }
2484de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2485de629b6eSShawn McCarney                     {
2486de629b6eSShawn McCarney                         fieldName = "Voltages";
2487de629b6eSShawn McCarney                     }
2488de629b6eSShawn McCarney                     else if (sensorType == "power")
2489de629b6eSShawn McCarney                     {
249055f79e6fSEd Tanous                         if (sensorName == "total_power")
2491028f7ebcSEddie James                         {
2492028f7ebcSEddie James                             fieldName = "PowerControl";
2493028f7ebcSEddie James                         }
2494adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2495adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2496028f7ebcSEddie James                         {
2497de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2498de629b6eSShawn McCarney                         }
2499adc4f0dbSShawn McCarney                         else
2500adc4f0dbSShawn McCarney                         {
2501adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2502adc4f0dbSShawn McCarney                             continue;
2503adc4f0dbSShawn McCarney                         }
2504028f7ebcSEddie James                     }
2505de629b6eSShawn McCarney                     else
2506de629b6eSShawn McCarney                     {
2507de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2508de629b6eSShawn McCarney                                          << sensorType;
2509de629b6eSShawn McCarney                         continue;
2510de629b6eSShawn McCarney                     }
2511de629b6eSShawn McCarney 
2512de629b6eSShawn McCarney                     nlohmann::json& tempArray =
25138d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2514adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
251549c53ac9SJohnathan Mantey                     {
2516adc4f0dbSShawn McCarney                         if (tempArray.empty())
25177ab06f49SGunnar Mills                         {
251895a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
251995a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
252095a3ecadSAnthony Wilson                             // naming in power.hpp.
25211476687dSEd Tanous                             nlohmann::json::object_t power;
25221476687dSEd Tanous                             power["@odata.id"] =
2523adc4f0dbSShawn McCarney                                 "/redfish/v1/Chassis/" +
252481ce609eSEd Tanous                                 sensorsAsyncResp->chassisId + "/" +
252581ce609eSEd Tanous                                 sensorsAsyncResp->chassisSubNode + "#/" +
25261476687dSEd Tanous                                 fieldName + "/0";
25271476687dSEd Tanous                             tempArray.push_back(std::move(power));
2528adc4f0dbSShawn McCarney                         }
2529adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2530adc4f0dbSShawn McCarney                     }
2531adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2532adc4f0dbSShawn McCarney                     {
2533adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2534adc4f0dbSShawn McCarney                         {
2535adc4f0dbSShawn McCarney                             sensorJson =
2536adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
253781ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2538adc4f0dbSShawn McCarney                         }
253949c53ac9SJohnathan Mantey                     }
2540928fefb9SNan Zhou                     else if (fieldName == "Members")
2541928fefb9SNan Zhou                     {
25421476687dSEd Tanous                         nlohmann::json::object_t member;
25431476687dSEd Tanous                         member["@odata.id"] =
2544928fefb9SNan Zhou                             "/redfish/v1/Chassis/" +
2545928fefb9SNan Zhou                             sensorsAsyncResp->chassisId + "/" +
25461476687dSEd Tanous                             sensorsAsyncResp->chassisSubNode + "/" + sensorName;
25471476687dSEd Tanous                         tempArray.push_back(std::move(member));
2548928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2549928fefb9SNan Zhou                     }
255049c53ac9SJohnathan Mantey                     else
255149c53ac9SJohnathan Mantey                     {
25521476687dSEd Tanous                         nlohmann::json::object_t member;
25531476687dSEd Tanous                         member["@odata.id"] = "/redfish/v1/Chassis/" +
25541476687dSEd Tanous                                               sensorsAsyncResp->chassisId +
25551476687dSEd Tanous                                               "/" +
25561476687dSEd Tanous                                               sensorsAsyncResp->chassisSubNode +
25571476687dSEd Tanous                                               "#/" + fieldName + "/";
25581476687dSEd Tanous                         tempArray.push_back(std::move(member));
2559adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
256049c53ac9SJohnathan Mantey                     }
256195a3ecadSAnthony Wilson                 }
2562de629b6eSShawn McCarney 
2563adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2564adc4f0dbSShawn McCarney                 {
2565a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
256681ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2567a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2568adc4f0dbSShawn McCarney                 }
2569de629b6eSShawn McCarney             }
257081ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
257149c53ac9SJohnathan Mantey             {
257281ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2573928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2574928fefb9SNan Zhou                         sensors::node::sensors &&
2575928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2576928fefb9SNan Zhou                 {
2577928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2578928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2579928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2580928fefb9SNan Zhou                             .size();
2581928fefb9SNan Zhou                 }
2582928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2583928fefb9SNan Zhou                          sensors::node::thermal)
25848bd25ccdSJames Feist                 {
258581ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
25868bd25ccdSJames Feist                 }
258749c53ac9SJohnathan Mantey             }
2588de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2589de629b6eSShawn McCarney         };
2590de629b6eSShawn McCarney 
2591de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2592de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
25938fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2594de629b6eSShawn McCarney         const std::string& objectMgrPath =
25958fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2596de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2597de629b6eSShawn McCarney                          << objectMgrPath;
2598de629b6eSShawn McCarney 
2599de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2600de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2601de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
260223a21a1cSEd Tanous     }
2603de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2604de629b6eSShawn McCarney }
2605de629b6eSShawn McCarney 
2606fe04d49cSNan Zhou inline void
2607fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2608fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
26091abe55efSEd Tanous {
2610fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2611fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
261255c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2613de629b6eSShawn McCarney         auto getObjectManagerPathsCb =
2614fe04d49cSNan Zhou             [sensorsAsyncResp, sensorNames, connections](
2615fe04d49cSNan Zhou                 const std::shared_ptr<std::map<std::string, std::string>>&
2616fe04d49cSNan Zhou                     objectMgrPaths) {
2617de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2618adc4f0dbSShawn McCarney             auto getInventoryItemsCb =
2619002d39b4SEd Tanous                 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2620f23b7296SEd Tanous                     const std::shared_ptr<std::vector<InventoryItem>>&
2621adc4f0dbSShawn McCarney                         inventoryItems) {
2622adc4f0dbSShawn McCarney                 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
262349c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2624002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2625002d39b4SEd Tanous                               objectMgrPaths, inventoryItems);
2626adc4f0dbSShawn McCarney                 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2627adc4f0dbSShawn McCarney             };
2628adc4f0dbSShawn McCarney 
2629adc4f0dbSShawn McCarney             // Get inventory items associated with sensors
2630002d39b4SEd Tanous             getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2631adc4f0dbSShawn McCarney                               std::move(getInventoryItemsCb));
2632adc4f0dbSShawn McCarney 
2633de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
263408777fb0SLewanczyk, Dawid         };
2635de629b6eSShawn McCarney 
263649c53ac9SJohnathan Mantey         // Get mapping from connection names to the DBus object
263749c53ac9SJohnathan Mantey         // paths that implement the ObjectManager interface
263881ce609eSEd Tanous         getObjectManagerPaths(sensorsAsyncResp,
2639de629b6eSShawn McCarney                               std::move(getObjectManagerPathsCb));
264055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb exit";
264108777fb0SLewanczyk, Dawid     };
2642de629b6eSShawn McCarney 
2643de629b6eSShawn McCarney     // Get set of connections that provide sensor values
264481ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
264595a3ecadSAnthony Wilson }
264695a3ecadSAnthony Wilson 
264795a3ecadSAnthony Wilson /**
264895a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
264995a3ecadSAnthony Wilson  *        chassis.
265095a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
265195a3ecadSAnthony Wilson  */
2652b5a76932SEd Tanous inline void
265381ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
265495a3ecadSAnthony Wilson {
265595a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
265695a3ecadSAnthony Wilson     auto getChassisCb =
265781ce609eSEd Tanous         [sensorsAsyncResp](
2658fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
265995a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "getChassisCb enter";
266081ce609eSEd Tanous         processSensorList(sensorsAsyncResp, sensorNames);
266155c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassisCb exit";
266208777fb0SLewanczyk, Dawid     };
2663928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2664928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2665928fefb9SNan Zhou     {
26668d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
26678d1b46d7Szhanghch05             nlohmann::json::array();
2668928fefb9SNan Zhou     }
266926f03899SShawn McCarney     // Get set of sensors in chassis
267081ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
267155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2672271584abSEd Tanous }
267308777fb0SLewanczyk, Dawid 
2674413961deSRichard Marian Thomaiyar /**
267549c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
267649c53ac9SJohnathan Mantey  * the chassis node
267749c53ac9SJohnathan Mantey  *
267849c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
267949c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
268049c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
268149c53ac9SJohnathan Mantey  *                         repeated calls to this function
268249c53ac9SJohnathan Mantey  */
2683fe04d49cSNan Zhou inline bool
2684fe04d49cSNan Zhou     findSensorNameUsingSensorPath(std::string_view sensorName,
268502cad96eSEd Tanous                                   const std::set<std::string>& sensorsList,
2686fe04d49cSNan Zhou                                   std::set<std::string>& sensorsModified)
268749c53ac9SJohnathan Mantey {
2688fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
268949c53ac9SJohnathan Mantey     {
269028aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2691b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
269228aa8de5SGeorge Liu         if (thisSensorName.empty())
269349c53ac9SJohnathan Mantey         {
269449c53ac9SJohnathan Mantey             continue;
269549c53ac9SJohnathan Mantey         }
269649c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
269749c53ac9SJohnathan Mantey         {
269849c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
269949c53ac9SJohnathan Mantey             return true;
270049c53ac9SJohnathan Mantey         }
270149c53ac9SJohnathan Mantey     }
270249c53ac9SJohnathan Mantey     return false;
270349c53ac9SJohnathan Mantey }
270449c53ac9SJohnathan Mantey 
270549c53ac9SJohnathan Mantey /**
2706413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2707413961deSRichard Marian Thomaiyar  *
27088d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
27094bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2710413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2711413961deSRichard Marian Thomaiyar  */
271223a21a1cSEd Tanous inline void setSensorsOverride(
2713b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
27144bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2715397fd61fSjayaprakash Mutyala         allCollections)
2716413961deSRichard Marian Thomaiyar {
271770d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
27184bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2719413961deSRichard Marian Thomaiyar 
2720543f4400SEd Tanous     const char* propertyValueName = nullptr;
2721f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2722413961deSRichard Marian Thomaiyar     std::string memberId;
2723543f4400SEd Tanous     double value = 0.0;
2724f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2725f65af9e8SRichard Marian Thomaiyar     {
2726f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2727f65af9e8SRichard Marian Thomaiyar         {
2728f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2729f65af9e8SRichard Marian Thomaiyar         }
2730f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2731f65af9e8SRichard Marian Thomaiyar         {
2732f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2733f65af9e8SRichard Marian Thomaiyar         }
2734f65af9e8SRichard Marian Thomaiyar         else
2735f65af9e8SRichard Marian Thomaiyar         {
2736f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2737f65af9e8SRichard Marian Thomaiyar         }
2738f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2739f65af9e8SRichard Marian Thomaiyar         {
27408d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
27418d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
27428d1b46d7Szhanghch05                                      value))
2743413961deSRichard Marian Thomaiyar             {
2744413961deSRichard Marian Thomaiyar                 return;
2745413961deSRichard Marian Thomaiyar             }
2746f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2747f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2748f65af9e8SRichard Marian Thomaiyar         }
2749f65af9e8SRichard Marian Thomaiyar     }
27504bb3dc34SCarol Wang 
2751002d39b4SEd Tanous     auto getChassisSensorListCb =
2752002d39b4SEd Tanous         [sensorAsyncResp, overrideMap](
2753fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorsList) {
275449c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
275549c53ac9SJohnathan Mantey         // chassis node
2756fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2757fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2758f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2759413961deSRichard Marian Thomaiyar         {
2760f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
276149c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
276249c53ac9SJohnathan Mantey                                                *sensorNames))
2763f65af9e8SRichard Marian Thomaiyar             {
2764f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
27658d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2766f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2767413961deSRichard Marian Thomaiyar                 return;
2768413961deSRichard Marian Thomaiyar             }
2769f65af9e8SRichard Marian Thomaiyar         }
2770413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2771002d39b4SEd Tanous         auto getObjectsWithConnectionCb =
2772fe04d49cSNan Zhou             [sensorAsyncResp,
2773fe04d49cSNan Zhou              overrideMap](const std::set<std::string>& /*connections*/,
2774002d39b4SEd Tanous                           const std::set<std::pair<std::string, std::string>>&
2775413961deSRichard Marian Thomaiyar                               objectsWithConnection) {
2776f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2777413961deSRichard Marian Thomaiyar             {
2778413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2779f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2780f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2781f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
27824f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2783a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2784a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2785413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2786413961deSRichard Marian Thomaiyar                                                : "Voltages",
2787f65af9e8SRichard Marian Thomaiyar                                            "Count");
2788f65af9e8SRichard Marian Thomaiyar                 return;
2789f65af9e8SRichard Marian Thomaiyar             }
2790f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2791f65af9e8SRichard Marian Thomaiyar             {
279228aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
279328aa8de5SGeorge Liu                 std::string sensorName = path.filename();
279428aa8de5SGeorge Liu                 if (sensorName.empty())
2795f65af9e8SRichard Marian Thomaiyar                 {
27964f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2797f65af9e8SRichard Marian Thomaiyar                     return;
2798f65af9e8SRichard Marian Thomaiyar                 }
2799f65af9e8SRichard Marian Thomaiyar 
2800f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2801f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2802f65af9e8SRichard Marian Thomaiyar                 {
2803f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2804f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
28054f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2806413961deSRichard Marian Thomaiyar                     return;
2807413961deSRichard Marian Thomaiyar                 }
2808413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2809f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2810413961deSRichard Marian Thomaiyar                     if (ec)
2811413961deSRichard Marian Thomaiyar                     {
28124f277b54SJayaprakash Mutyala                         if (ec.value() ==
28134f277b54SJayaprakash Mutyala                             boost::system::errc::permission_denied)
28144f277b54SJayaprakash Mutyala                         {
28154f277b54SJayaprakash Mutyala                             BMCWEB_LOG_WARNING
28164f277b54SJayaprakash Mutyala                                 << "Manufacturing mode is not Enabled...can't "
28174f277b54SJayaprakash Mutyala                                    "Override the sensor value. ";
28184f277b54SJayaprakash Mutyala 
28194f277b54SJayaprakash Mutyala                             messages::insufficientPrivilege(
28208d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
2821413961deSRichard Marian Thomaiyar                             return;
2822413961deSRichard Marian Thomaiyar                         }
28234f277b54SJayaprakash Mutyala                         BMCWEB_LOG_DEBUG
28244f277b54SJayaprakash Mutyala                             << "setOverrideValueStatus DBUS error: " << ec;
28254f277b54SJayaprakash Mutyala                         messages::internalError(
28264f277b54SJayaprakash Mutyala                             sensorAsyncResp->asyncResp->res);
28274f277b54SJayaprakash Mutyala                     }
2828413961deSRichard Marian Thomaiyar                     },
28294f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
28304f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2831168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2832f65af9e8SRichard Marian Thomaiyar             }
2833413961deSRichard Marian Thomaiyar         };
2834413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2835413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2836413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2837413961deSRichard Marian Thomaiyar     };
2838413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2839413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2840413961deSRichard Marian Thomaiyar }
2841413961deSRichard Marian Thomaiyar 
2842a0ec28b6SAdrian Ambrożewicz /**
2843a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2844a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2845a0ec28b6SAdrian Ambrożewicz  *
2846a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2847a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2848a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2849a0ec28b6SAdrian Ambrożewicz  *
2850a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2851a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2852a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2853a0ec28b6SAdrian Ambrożewicz  */
2854021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2855021d32cfSKrzysztof Grobelny                                  const std::string& node,
2856a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2857a0ec28b6SAdrian Ambrożewicz {
285802da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
285902da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
286002da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
286102da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2862a0ec28b6SAdrian Ambrożewicz     {
2863a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2864a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2865a0ec28b6SAdrian Ambrożewicz         return;
2866a0ec28b6SAdrian Ambrożewicz     }
2867d51e072fSKrzysztof Grobelny 
286872374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2869fe04d49cSNan Zhou     auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2870a0ec28b6SAdrian Ambrożewicz                         const boost::beast::http::status status,
2871fe04d49cSNan Zhou                         const std::map<std::string, std::string>& uriToDbus) {
2872fe04d49cSNan Zhou         mapCompleteCb(status, uriToDbus);
2873fe04d49cSNan Zhou     };
2874a0ec28b6SAdrian Ambrożewicz 
2875a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2876d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2877a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2878a0ec28b6SAdrian Ambrożewicz }
2879a0ec28b6SAdrian Ambrożewicz 
2880bacb2162SNan Zhou namespace sensors
2881bacb2162SNan Zhou {
2882928fefb9SNan Zhou 
2883bacb2162SNan Zhou inline void getChassisCallback(
2884bacb2162SNan Zhou     const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2885fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2886bacb2162SNan Zhou {
2887bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2888bacb2162SNan Zhou 
2889bacb2162SNan Zhou     nlohmann::json& entriesArray =
2890bacb2162SNan Zhou         asyncResp->asyncResp->res.jsonValue["Members"];
2891fe04d49cSNan Zhou     for (const auto& sensor : *sensorNames)
2892bacb2162SNan Zhou     {
2893bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2894bacb2162SNan Zhou 
2895bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2896bacb2162SNan Zhou         std::string sensorName = path.filename();
2897bacb2162SNan Zhou         if (sensorName.empty())
2898bacb2162SNan Zhou         {
2899bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2900bacb2162SNan Zhou             messages::internalError(asyncResp->asyncResp->res);
2901bacb2162SNan Zhou             return;
2902bacb2162SNan Zhou         }
29031476687dSEd Tanous         nlohmann::json::object_t member;
29041476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
29051476687dSEd Tanous                               "/" + asyncResp->chassisSubNode + "/" +
29061476687dSEd Tanous                               sensorName;
29071476687dSEd Tanous         entriesArray.push_back(std::move(member));
2908bacb2162SNan Zhou     }
2909bacb2162SNan Zhou 
2910bacb2162SNan Zhou     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2911bacb2162SNan Zhou         entriesArray.size();
2912bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2913bacb2162SNan Zhou }
2914e6bd846dSNan Zhou 
2915de167a6fSNan Zhou inline void
2916de167a6fSNan Zhou     handleSensorCollectionGet(App& app, const crow::Request& req,
2917de167a6fSNan Zhou                               const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2918de167a6fSNan Zhou                               const std::string& chassisId)
2919de167a6fSNan Zhou {
2920de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2921de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2922de167a6fSNan Zhou     };
2923de167a6fSNan Zhou     query_param::Query delegatedQuery;
29243ba00073SCarson Labrado     if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
2925de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2926de167a6fSNan Zhou     {
2927de167a6fSNan Zhou         return;
2928de167a6fSNan Zhou     }
2929de167a6fSNan Zhou 
2930de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2931de167a6fSNan Zhou     {
2932de167a6fSNan Zhou         // we perform efficient expand.
2933de167a6fSNan Zhou         auto asyncResp = std::make_shared<SensorsAsyncResp>(
2934de167a6fSNan Zhou             aResp, chassisId, sensors::dbus::sensorPaths,
2935de167a6fSNan Zhou             sensors::node::sensors,
2936de167a6fSNan Zhou             /*efficientExpand=*/true);
2937de167a6fSNan Zhou         getChassisData(asyncResp);
2938de167a6fSNan Zhou 
2939de167a6fSNan Zhou         BMCWEB_LOG_DEBUG
2940de167a6fSNan Zhou             << "SensorCollection doGet exit via efficient expand handler";
2941de167a6fSNan Zhou         return;
29420bad320cSEd Tanous     }
2943de167a6fSNan Zhou 
2944de167a6fSNan Zhou     // if there's no efficient expand available, we use the default
2945de167a6fSNan Zhou     // Query Parameters route
2946de167a6fSNan Zhou     auto asyncResp = std::make_shared<SensorsAsyncResp>(
2947de167a6fSNan Zhou         aResp, chassisId, sensors::dbus::sensorPaths, sensors::node::sensors);
2948de167a6fSNan Zhou 
2949de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2950de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
2951de167a6fSNan Zhou     getChassis(asyncResp,
2952de167a6fSNan Zhou                std::bind_front(sensors::getChassisCallback, asyncResp));
2953de167a6fSNan Zhou     BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
2954de167a6fSNan Zhou }
2955de167a6fSNan Zhou 
2956e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2957e6bd846dSNan Zhou                             const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2958e6bd846dSNan Zhou                             const std::string& chassisId,
2959e6bd846dSNan Zhou                             const std::string& sensorName)
2960e6bd846dSNan Zhou {
29613ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, aResp))
2962e6bd846dSNan Zhou     {
2963e6bd846dSNan Zhou         return;
2964e6bd846dSNan Zhou     }
2965e6bd846dSNan Zhou     BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2966e6bd846dSNan Zhou     std::shared_ptr<SensorsAsyncResp> asyncResp =
2967e6bd846dSNan Zhou         std::make_shared<SensorsAsyncResp>(aResp, chassisId,
2968e6bd846dSNan Zhou                                            std::span<std::string_view>(),
2969e6bd846dSNan Zhou                                            sensors::node::sensors);
2970e6bd846dSNan Zhou 
2971e6bd846dSNan Zhou     const std::array<const char*, 1> interfaces = {
2972e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2973e6bd846dSNan Zhou 
2974e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2975e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
2976e6bd846dSNan Zhou     crow::connections::systemBus->async_method_call(
2977e6bd846dSNan Zhou         [asyncResp,
2978e6bd846dSNan Zhou          sensorName](const boost::system::error_code ec,
2979e6bd846dSNan Zhou                      const ::dbus::utility::MapperGetSubTreeResponse& subtree) {
2980e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 enter";
2981e6bd846dSNan Zhou         if (ec)
2982e6bd846dSNan Zhou         {
2983e6bd846dSNan Zhou             messages::internalError(asyncResp->asyncResp->res);
2984e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2985e6bd846dSNan Zhou                              << "Dbus error " << ec;
2986e6bd846dSNan Zhou             return;
2987e6bd846dSNan Zhou         }
2988e6bd846dSNan Zhou 
2989e6bd846dSNan Zhou         ::dbus::utility::MapperGetSubTreeResponse::const_iterator it =
2990e6bd846dSNan Zhou             std::find_if(
2991e6bd846dSNan Zhou                 subtree.begin(), subtree.end(),
2992e6bd846dSNan Zhou                 [sensorName](
2993e6bd846dSNan Zhou                     const std::pair<
2994e6bd846dSNan Zhou                         std::string,
2995e6bd846dSNan Zhou                         std::vector<std::pair<
2996e6bd846dSNan Zhou                             std::string, std::vector<std::string>>>>& object) {
2997e6bd846dSNan Zhou             sdbusplus::message::object_path path(object.first);
2998e6bd846dSNan Zhou             std::string name = path.filename();
2999e6bd846dSNan Zhou             if (name.empty())
3000e6bd846dSNan Zhou             {
3001e6bd846dSNan Zhou                 BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first;
3002e6bd846dSNan Zhou                 return false;
3003e6bd846dSNan Zhou             }
3004e6bd846dSNan Zhou 
3005e6bd846dSNan Zhou             return name == sensorName;
3006e6bd846dSNan Zhou                 });
3007e6bd846dSNan Zhou 
3008e6bd846dSNan Zhou         if (it == subtree.end())
3009e6bd846dSNan Zhou         {
3010e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3011e6bd846dSNan Zhou                              << sensorName;
3012e6bd846dSNan Zhou             messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor",
3013e6bd846dSNan Zhou                                        sensorName);
3014e6bd846dSNan Zhou             return;
3015e6bd846dSNan Zhou         }
3016e6bd846dSNan Zhou         std::string_view sensorPath = (*it).first;
3017e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
3018e6bd846dSNan Zhou                          << "': " << sensorPath;
3019e6bd846dSNan Zhou 
3020fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorList =
3021fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
3022e6bd846dSNan Zhou 
3023e6bd846dSNan Zhou         sensorList->emplace(sensorPath);
3024e6bd846dSNan Zhou         processSensorList(asyncResp, sensorList);
3025e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 exit";
3026e6bd846dSNan Zhou         },
3027e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper",
3028e6bd846dSNan Zhou         "/xyz/openbmc_project/object_mapper",
3029e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3030e6bd846dSNan Zhou         "/xyz/openbmc_project/sensors", 2, interfaces);
3031e6bd846dSNan Zhou }
3032e6bd846dSNan Zhou 
3033bacb2162SNan Zhou } // namespace sensors
3034bacb2162SNan Zhou 
30357e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
303695a3ecadSAnthony Wilson {
30377e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
3038ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
3039002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3040de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
304195a3ecadSAnthony Wilson }
304295a3ecadSAnthony Wilson 
30437e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
304495a3ecadSAnthony Wilson {
30457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3046ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
3047002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3048e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
304995a3ecadSAnthony Wilson }
305095a3ecadSAnthony Wilson 
305108777fb0SLewanczyk, Dawid } // namespace redfish
3052