xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision c2bf7f9911c3be7606809e682e00c0389f114c08)
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 
1895a3ecadSAnthony Wilson #include "node.hpp"
1995a3ecadSAnthony Wilson 
2008777fb0SLewanczyk, Dawid #include <boost/algorithm/string/predicate.hpp>
2108777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2208777fb0SLewanczyk, Dawid #include <boost/container/flat_map.hpp>
2308777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
241abe55efSEd Tanous #include <dbus_singleton.hpp>
25413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
261214b7e7SGunnar Mills 
271214b7e7SGunnar Mills #include <cmath>
28b5a76932SEd Tanous #include <utility>
29abf2add6SEd Tanous #include <variant>
3008777fb0SLewanczyk, Dawid 
311abe55efSEd Tanous namespace redfish
321abe55efSEd Tanous {
3308777fb0SLewanczyk, Dawid 
3408777fb0SLewanczyk, Dawid using GetSubTreeType = std::vector<
3508777fb0SLewanczyk, Dawid     std::pair<std::string,
3608777fb0SLewanczyk, Dawid               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
3708777fb0SLewanczyk, Dawid 
38adc4f0dbSShawn McCarney using SensorVariant =
39adc4f0dbSShawn McCarney     std::variant<int64_t, double, uint32_t, bool, std::string>;
40aa2e59c1SEd Tanous 
4108777fb0SLewanczyk, Dawid using ManagedObjectsVectorType = std::vector<std::pair<
42aa2e59c1SEd Tanous     sdbusplus::message::object_path,
4308777fb0SLewanczyk, Dawid     boost::container::flat_map<
44aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
4508777fb0SLewanczyk, Dawid 
46a0ec28b6SAdrian Ambrożewicz namespace sensors
47a0ec28b6SAdrian Ambrożewicz {
48a0ec28b6SAdrian Ambrożewicz namespace node
49a0ec28b6SAdrian Ambrożewicz {
50a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
51a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
52a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
53a0ec28b6SAdrian Ambrożewicz } // namespace node
54a0ec28b6SAdrian Ambrożewicz 
55a0ec28b6SAdrian Ambrożewicz namespace dbus
56a0ec28b6SAdrian Ambrożewicz {
57*c2bf7f99SWludzik, Jozef 
58a0ec28b6SAdrian Ambrożewicz static const boost::container::flat_map<std::string_view,
59a0ec28b6SAdrian Ambrożewicz                                         std::vector<const char*>>
60*c2bf7f99SWludzik, Jozef     paths = {{node::power,
61a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/voltage",
62a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/power"}},
63a0ec28b6SAdrian Ambrożewicz              {node::sensors,
64a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/power",
65a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/current",
66a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/utilization"}},
67a0ec28b6SAdrian Ambrożewicz              {node::thermal,
68a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/fan_tach",
69a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/temperature",
70a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/fan_pwm"}}};
71*c2bf7f99SWludzik, Jozef } // namespace dbus
72*c2bf7f99SWludzik, Jozef 
73*c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType)
74*c2bf7f99SWludzik, Jozef {
75*c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
76*c2bf7f99SWludzik, Jozef     {
77*c2bf7f99SWludzik, Jozef         return "Voltage";
78*c2bf7f99SWludzik, Jozef     }
79*c2bf7f99SWludzik, Jozef     if (sensorType == "power")
80*c2bf7f99SWludzik, Jozef     {
81*c2bf7f99SWludzik, Jozef         return "Power";
82*c2bf7f99SWludzik, Jozef     }
83*c2bf7f99SWludzik, Jozef     if (sensorType == "current")
84*c2bf7f99SWludzik, Jozef     {
85*c2bf7f99SWludzik, Jozef         return "Current";
86*c2bf7f99SWludzik, Jozef     }
87*c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
88*c2bf7f99SWludzik, Jozef     {
89*c2bf7f99SWludzik, Jozef         return "Rotational";
90*c2bf7f99SWludzik, Jozef     }
91*c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
92*c2bf7f99SWludzik, Jozef     {
93*c2bf7f99SWludzik, Jozef         return "Temperature";
94*c2bf7f99SWludzik, Jozef     }
95*c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
96*c2bf7f99SWludzik, Jozef     {
97*c2bf7f99SWludzik, Jozef         return "Percent";
98*c2bf7f99SWludzik, Jozef     }
99*c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
100*c2bf7f99SWludzik, Jozef     {
101*c2bf7f99SWludzik, Jozef         return "Altitude";
102*c2bf7f99SWludzik, Jozef     }
103*c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
104*c2bf7f99SWludzik, Jozef     {
105*c2bf7f99SWludzik, Jozef         return "AirFlow";
106*c2bf7f99SWludzik, Jozef     }
107*c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
108*c2bf7f99SWludzik, Jozef     {
109*c2bf7f99SWludzik, Jozef         return "EnergyJoules";
110*c2bf7f99SWludzik, Jozef     }
111*c2bf7f99SWludzik, Jozef     return "";
112*c2bf7f99SWludzik, Jozef }
113*c2bf7f99SWludzik, Jozef 
114*c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType)
115*c2bf7f99SWludzik, Jozef {
116*c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
117*c2bf7f99SWludzik, Jozef     {
118*c2bf7f99SWludzik, Jozef         return "V";
119*c2bf7f99SWludzik, Jozef     }
120*c2bf7f99SWludzik, Jozef     if (sensorType == "power")
121*c2bf7f99SWludzik, Jozef     {
122*c2bf7f99SWludzik, Jozef         return "W";
123*c2bf7f99SWludzik, Jozef     }
124*c2bf7f99SWludzik, Jozef     if (sensorType == "current")
125*c2bf7f99SWludzik, Jozef     {
126*c2bf7f99SWludzik, Jozef         return "A";
127*c2bf7f99SWludzik, Jozef     }
128*c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
129*c2bf7f99SWludzik, Jozef     {
130*c2bf7f99SWludzik, Jozef         return "RPM";
131*c2bf7f99SWludzik, Jozef     }
132*c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
133*c2bf7f99SWludzik, Jozef     {
134*c2bf7f99SWludzik, Jozef         return "Cel";
135*c2bf7f99SWludzik, Jozef     }
136*c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
137*c2bf7f99SWludzik, Jozef     {
138*c2bf7f99SWludzik, Jozef         return "%";
139*c2bf7f99SWludzik, Jozef     }
140*c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
141*c2bf7f99SWludzik, Jozef     {
142*c2bf7f99SWludzik, Jozef         return "m";
143*c2bf7f99SWludzik, Jozef     }
144*c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
145*c2bf7f99SWludzik, Jozef     {
146*c2bf7f99SWludzik, Jozef         return "cft_i/min";
147*c2bf7f99SWludzik, Jozef     }
148*c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
149*c2bf7f99SWludzik, Jozef     {
150*c2bf7f99SWludzik, Jozef         return "J";
151*c2bf7f99SWludzik, Jozef     }
152*c2bf7f99SWludzik, Jozef     return "";
153a0ec28b6SAdrian Ambrożewicz }
154a0ec28b6SAdrian Ambrożewicz } // namespace sensors
155a0ec28b6SAdrian Ambrożewicz 
15608777fb0SLewanczyk, Dawid /**
157588c3f0dSKowalski, Kamil  * SensorsAsyncResp
15808777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
15908777fb0SLewanczyk, Dawid  */
1601abe55efSEd Tanous class SensorsAsyncResp
1611abe55efSEd Tanous {
16208777fb0SLewanczyk, Dawid   public:
163a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
164a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
165a0ec28b6SAdrian Ambrożewicz         const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
166a0ec28b6SAdrian Ambrożewicz 
167a0ec28b6SAdrian Ambrożewicz     struct SensorData
168a0ec28b6SAdrian Ambrożewicz     {
169a0ec28b6SAdrian Ambrożewicz         const std::string name;
170a0ec28b6SAdrian Ambrożewicz         std::string uri;
171a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
172a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
173a0ec28b6SAdrian Ambrożewicz     };
174a0ec28b6SAdrian Ambrożewicz 
175271584abSEd Tanous     SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
176b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
177a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode) :
17843b761d0SEd Tanous         res(response),
179271584abSEd Tanous         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
1801214b7e7SGunnar Mills     {}
18108777fb0SLewanczyk, Dawid 
182a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
183a0ec28b6SAdrian Ambrożewicz     SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
184b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
185a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode,
186a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
187a0ec28b6SAdrian Ambrożewicz         res(response),
188a0ec28b6SAdrian Ambrożewicz         chassisId(chassisIdIn), types(typesIn),
189a0ec28b6SAdrian Ambrożewicz         chassisSubNode(subNode), metadata{std::vector<SensorData>()},
190a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
191a0ec28b6SAdrian Ambrożewicz     {}
192a0ec28b6SAdrian Ambrożewicz 
1931abe55efSEd Tanous     ~SensorsAsyncResp()
1941abe55efSEd Tanous     {
1951abe55efSEd Tanous         if (res.result() == boost::beast::http::status::internal_server_error)
1961abe55efSEd Tanous         {
1971abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
1981abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
1991abe55efSEd Tanous             // proper code
20055c7b7a2SEd Tanous             res.jsonValue = nlohmann::json::object();
20108777fb0SLewanczyk, Dawid         }
202a0ec28b6SAdrian Ambrożewicz 
203a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
204a0ec28b6SAdrian Ambrożewicz         {
205a0ec28b6SAdrian Ambrożewicz             boost::container::flat_map<std::string, std::string> map;
206a0ec28b6SAdrian Ambrożewicz             if (res.result() == boost::beast::http::status::ok)
207a0ec28b6SAdrian Ambrożewicz             {
208a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
209a0ec28b6SAdrian Ambrożewicz                 {
210a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
211a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
212a0ec28b6SAdrian Ambrożewicz                 }
213a0ec28b6SAdrian Ambrożewicz             }
214a0ec28b6SAdrian Ambrożewicz             dataComplete(res.result(), map);
215a0ec28b6SAdrian Ambrożewicz         }
216a0ec28b6SAdrian Ambrożewicz 
21708777fb0SLewanczyk, Dawid         res.end();
21808777fb0SLewanczyk, Dawid     }
219588c3f0dSKowalski, Kamil 
220a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
221a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
222a0ec28b6SAdrian Ambrożewicz     {
223a0ec28b6SAdrian Ambrożewicz         if (metadata)
224a0ec28b6SAdrian Ambrożewicz         {
225a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
226a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
227a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
228a0ec28b6SAdrian Ambrożewicz         }
229a0ec28b6SAdrian Ambrożewicz     }
230a0ec28b6SAdrian Ambrożewicz 
231a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
232a0ec28b6SAdrian Ambrożewicz     {
233a0ec28b6SAdrian Ambrożewicz         if (metadata)
234a0ec28b6SAdrian Ambrożewicz         {
235a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
236a0ec28b6SAdrian Ambrożewicz             {
237a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
238a0ec28b6SAdrian Ambrożewicz                 {
239a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
240a0ec28b6SAdrian Ambrożewicz                 }
241a0ec28b6SAdrian Ambrożewicz             }
242a0ec28b6SAdrian Ambrożewicz         }
243a0ec28b6SAdrian Ambrożewicz     }
244a0ec28b6SAdrian Ambrożewicz 
24555c7b7a2SEd Tanous     crow::Response& res;
246a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
24708777fb0SLewanczyk, Dawid     const std::vector<const char*> types;
248a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
249a0ec28b6SAdrian Ambrożewicz 
250a0ec28b6SAdrian Ambrożewicz   private:
251a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
252a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
25308777fb0SLewanczyk, Dawid };
25408777fb0SLewanczyk, Dawid 
25508777fb0SLewanczyk, Dawid /**
256d500549bSAnthony Wilson  * Possible states for physical inventory leds
257d500549bSAnthony Wilson  */
258d500549bSAnthony Wilson enum class LedState
259d500549bSAnthony Wilson {
260d500549bSAnthony Wilson     OFF,
261d500549bSAnthony Wilson     ON,
262d500549bSAnthony Wilson     BLINK,
263d500549bSAnthony Wilson     UNKNOWN
264d500549bSAnthony Wilson };
265d500549bSAnthony Wilson 
266d500549bSAnthony Wilson /**
267adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
268adc4f0dbSShawn McCarney  */
269adc4f0dbSShawn McCarney class InventoryItem
270adc4f0dbSShawn McCarney {
271adc4f0dbSShawn McCarney   public:
272adc4f0dbSShawn McCarney     InventoryItem(const std::string& objPath) :
273adc4f0dbSShawn McCarney         objectPath(objPath), name(), isPresent(true), isFunctional(true),
27442cbe538SGunnar Mills         isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
27542cbe538SGunnar Mills         model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""),
276d500549bSAnthony Wilson         ledState(LedState::UNKNOWN)
277adc4f0dbSShawn McCarney     {
278adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
27928aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
28028aa8de5SGeorge Liu         name = path.filename();
28128aa8de5SGeorge Liu         if (name.empty())
282adc4f0dbSShawn McCarney         {
28328aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
284adc4f0dbSShawn McCarney         }
285adc4f0dbSShawn McCarney     }
286adc4f0dbSShawn McCarney 
287adc4f0dbSShawn McCarney     std::string objectPath;
288adc4f0dbSShawn McCarney     std::string name;
289adc4f0dbSShawn McCarney     bool isPresent;
290adc4f0dbSShawn McCarney     bool isFunctional;
291adc4f0dbSShawn McCarney     bool isPowerSupply;
29242cbe538SGunnar Mills     int powerSupplyEfficiencyPercent;
293adc4f0dbSShawn McCarney     std::string manufacturer;
294adc4f0dbSShawn McCarney     std::string model;
295adc4f0dbSShawn McCarney     std::string partNumber;
296adc4f0dbSShawn McCarney     std::string serialNumber;
297adc4f0dbSShawn McCarney     std::set<std::string> sensors;
298d500549bSAnthony Wilson     std::string ledObjectPath;
299d500549bSAnthony Wilson     LedState ledState;
300adc4f0dbSShawn McCarney };
301adc4f0dbSShawn McCarney 
302adc4f0dbSShawn McCarney /**
303413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
304588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
30508777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
30608777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
30708777fb0SLewanczyk, Dawid  */
30808777fb0SLewanczyk, Dawid template <typename Callback>
309413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
31081ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
311b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
3121abe55efSEd Tanous     Callback&& callback)
3131abe55efSEd Tanous {
314413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
31503b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
31608777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
31708777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
31808777fb0SLewanczyk, Dawid 
31908777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
32081ce609eSEd Tanous     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
3211abe55efSEd Tanous                         sensorNames](const boost::system::error_code ec,
3221abe55efSEd Tanous                                      const GetSubTreeType& subtree) {
323413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3241abe55efSEd Tanous         if (ec)
3251abe55efSEd Tanous         {
32681ce609eSEd Tanous             messages::internalError(sensorsAsyncResp->res);
327413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
328413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
32908777fb0SLewanczyk, Dawid             return;
33008777fb0SLewanczyk, Dawid         }
33108777fb0SLewanczyk, Dawid 
33255c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
33308777fb0SLewanczyk, Dawid 
33408777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
33508777fb0SLewanczyk, Dawid         // found in the chassis
33608777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
337413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
3381abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
3391abe55efSEd Tanous         // producers
34008777fb0SLewanczyk, Dawid         connections.reserve(8);
34108777fb0SLewanczyk, Dawid 
34249c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
34349c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3441abe55efSEd Tanous         {
34555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
34608777fb0SLewanczyk, Dawid         }
34708777fb0SLewanczyk, Dawid 
34808777fb0SLewanczyk, Dawid         for (const std::pair<
34908777fb0SLewanczyk, Dawid                  std::string,
35008777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3511abe55efSEd Tanous                  object : subtree)
3521abe55efSEd Tanous         {
35349c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3541abe55efSEd Tanous             {
35549c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3561abe55efSEd Tanous                          objData : object.second)
3571abe55efSEd Tanous                 {
35849c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
35908777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
360de629b6eSShawn McCarney                     objectsWithConnection.insert(
361de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
36208777fb0SLewanczyk, Dawid                 }
36308777fb0SLewanczyk, Dawid             }
36408777fb0SLewanczyk, Dawid         }
36555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
366413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
367413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
36808777fb0SLewanczyk, Dawid     };
36908777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
37055c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
37155c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
3721abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
3731abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
374413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
375413961deSRichard Marian Thomaiyar }
376413961deSRichard Marian Thomaiyar 
377413961deSRichard Marian Thomaiyar /**
378413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
379413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
380413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
381413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
382413961deSRichard Marian Thomaiyar  */
383413961deSRichard Marian Thomaiyar template <typename Callback>
38449c53ac9SJohnathan Mantey void getConnections(
38581ce609eSEd Tanous     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
38649c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
387413961deSRichard Marian Thomaiyar     Callback&& callback)
388413961deSRichard Marian Thomaiyar {
389413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
390413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
391413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
3923174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
39381ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
394413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
39508777fb0SLewanczyk, Dawid }
39608777fb0SLewanczyk, Dawid 
39708777fb0SLewanczyk, Dawid /**
39849c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
39949c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
40049c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
40149c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
40249c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
40349c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
40449c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
40549c53ac9SJohnathan Mantey  */
40623a21a1cSEd Tanous inline void reduceSensorList(
40781ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
40849c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
409b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>&
410b5a76932SEd Tanous         activeSensors)
41149c53ac9SJohnathan Mantey {
41281ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
41349c53ac9SJohnathan Mantey     {
41449c53ac9SJohnathan Mantey         return;
41549c53ac9SJohnathan Mantey     }
41649c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
41749c53ac9SJohnathan Mantey     {
41849c53ac9SJohnathan Mantey         messages::resourceNotFound(
41981ce609eSEd Tanous             sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
42081ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
421a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
42249c53ac9SJohnathan Mantey                 : "Voltages");
42349c53ac9SJohnathan Mantey 
42449c53ac9SJohnathan Mantey         return;
42549c53ac9SJohnathan Mantey     }
42649c53ac9SJohnathan Mantey     if (allSensors->empty())
42749c53ac9SJohnathan Mantey     {
42849c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
42949c53ac9SJohnathan Mantey         return;
43049c53ac9SJohnathan Mantey     }
43149c53ac9SJohnathan Mantey 
43281ce609eSEd Tanous     for (const char* type : sensorsAsyncResp->types)
43349c53ac9SJohnathan Mantey     {
43449c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
43549c53ac9SJohnathan Mantey         {
43649c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
43749c53ac9SJohnathan Mantey             {
43849c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
43949c53ac9SJohnathan Mantey             }
44049c53ac9SJohnathan Mantey         }
44149c53ac9SJohnathan Mantey     }
44249c53ac9SJohnathan Mantey }
44349c53ac9SJohnathan Mantey 
44449c53ac9SJohnathan Mantey /**
4454bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
4464bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
4474bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
4484bb3dc34SCarol Wang  */
4494bb3dc34SCarol Wang template <typename Callback>
450b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
4514bb3dc34SCarol Wang                          Callback&& callback)
4524bb3dc34SCarol Wang {
4534bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
4544bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
4554bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
4564bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
4574bb3dc34SCarol Wang 
4584bb3dc34SCarol Wang     auto respHandler =
4594bb3dc34SCarol Wang         [callback{std::move(callback)},
4604bb3dc34SCarol Wang          asyncResp](const boost::system::error_code ec,
4614bb3dc34SCarol Wang                     const std::vector<std::string>& chassisPaths) mutable {
4624bb3dc34SCarol Wang             BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
4634bb3dc34SCarol Wang             if (ec)
4644bb3dc34SCarol Wang             {
4654bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR
4664bb3dc34SCarol Wang                     << "getValidChassisPath respHandler DBUS error: " << ec;
4674bb3dc34SCarol Wang                 messages::internalError(asyncResp->res);
4684bb3dc34SCarol Wang                 return;
4694bb3dc34SCarol Wang             }
4704bb3dc34SCarol Wang 
4714bb3dc34SCarol Wang             std::optional<std::string> chassisPath;
4724bb3dc34SCarol Wang             std::string chassisName;
4734bb3dc34SCarol Wang             for (const std::string& chassis : chassisPaths)
4744bb3dc34SCarol Wang             {
47528aa8de5SGeorge Liu                 sdbusplus::message::object_path path(chassis);
47628aa8de5SGeorge Liu                 chassisName = path.filename();
47728aa8de5SGeorge Liu                 if (chassisName.empty())
4784bb3dc34SCarol Wang                 {
4794bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
4804bb3dc34SCarol Wang                     continue;
4814bb3dc34SCarol Wang                 }
4824bb3dc34SCarol Wang                 if (chassisName == asyncResp->chassisId)
4834bb3dc34SCarol Wang                 {
4844bb3dc34SCarol Wang                     chassisPath = chassis;
4854bb3dc34SCarol Wang                     break;
4864bb3dc34SCarol Wang                 }
4874bb3dc34SCarol Wang             }
4884bb3dc34SCarol Wang             callback(chassisPath);
4894bb3dc34SCarol Wang         };
4904bb3dc34SCarol Wang 
4914bb3dc34SCarol Wang     // Get the Chassis Collection
4924bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
4934bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
4944bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
4954bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
4964bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
4974bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
4984bb3dc34SCarol Wang }
4994bb3dc34SCarol Wang 
5004bb3dc34SCarol Wang /**
50108777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
502588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
50308777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
50408777fb0SLewanczyk, Dawid  */
50508777fb0SLewanczyk, Dawid template <typename Callback>
506b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
5071abe55efSEd Tanous                 Callback&& callback)
5081abe55efSEd Tanous {
50955c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
510adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
51149c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
512adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
51349c53ac9SJohnathan Mantey     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
51449c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
51549c53ac9SJohnathan Mantey                            const std::vector<std::string>& chassisPaths) {
51655c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5171abe55efSEd Tanous         if (ec)
5181abe55efSEd Tanous         {
51955c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
52049c53ac9SJohnathan Mantey             messages::internalError(sensorsAsyncResp->res);
52108777fb0SLewanczyk, Dawid             return;
52208777fb0SLewanczyk, Dawid         }
52308777fb0SLewanczyk, Dawid 
52449c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
52549c53ac9SJohnathan Mantey         std::string chassisName;
52649c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5271abe55efSEd Tanous         {
52828aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
52928aa8de5SGeorge Liu             chassisName = path.filename();
53028aa8de5SGeorge Liu             if (chassisName.empty())
5311abe55efSEd Tanous             {
53249c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
533daf36e2eSEd Tanous                 continue;
534daf36e2eSEd Tanous             }
53549c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5361abe55efSEd Tanous             {
53749c53ac9SJohnathan Mantey                 chassisPath = &chassis;
53849c53ac9SJohnathan Mantey                 break;
539daf36e2eSEd Tanous             }
54049c53ac9SJohnathan Mantey         }
54149c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5421abe55efSEd Tanous         {
54349c53ac9SJohnathan Mantey             messages::resourceNotFound(sensorsAsyncResp->res, "Chassis",
54449c53ac9SJohnathan Mantey                                        sensorsAsyncResp->chassisId);
54549c53ac9SJohnathan Mantey             return;
5461abe55efSEd Tanous         }
54708777fb0SLewanczyk, Dawid 
54849c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
549a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
55049c53ac9SJohnathan Mantey         {
55149c53ac9SJohnathan Mantey             sensorsAsyncResp->res.jsonValue["@odata.type"] =
55249c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
55349c53ac9SJohnathan Mantey         }
554a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
55549c53ac9SJohnathan Mantey         {
55649c53ac9SJohnathan Mantey             sensorsAsyncResp->res.jsonValue["@odata.type"] =
55749c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5584f9a2130SJennifer Lee             sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array();
5594f9a2130SJennifer Lee             sensorsAsyncResp->res.jsonValue["Temperatures"] =
5604f9a2130SJennifer Lee                 nlohmann::json::array();
56149c53ac9SJohnathan Mantey         }
562a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
56395a3ecadSAnthony Wilson         {
56495a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["@odata.type"] =
56595a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
56695a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Description"] =
56795a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
56895a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members"] =
56995a3ecadSAnthony Wilson                 nlohmann::json::array();
57095a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0;
57195a3ecadSAnthony Wilson         }
57295a3ecadSAnthony Wilson 
573a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
57495a3ecadSAnthony Wilson         {
57595a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode;
57695a3ecadSAnthony Wilson         }
57795a3ecadSAnthony Wilson 
57849c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["@odata.id"] =
57949c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
58049c53ac9SJohnathan Mantey             chassisSubNode;
58149c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode;
58249c53ac9SJohnathan Mantey 
5838fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5848fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
58555c7b7a2SEd Tanous         crow::connections::systemBus->async_method_call(
58649c53ac9SJohnathan Mantey             [sensorsAsyncResp, callback{std::move(callback)}](
587271584abSEd Tanous                 const boost::system::error_code& e,
58849c53ac9SJohnathan Mantey                 const std::variant<std::vector<std::string>>&
58949c53ac9SJohnathan Mantey                     variantEndpoints) {
590271584abSEd Tanous                 if (e)
59149c53ac9SJohnathan Mantey                 {
592271584abSEd Tanous                     if (e.value() != EBADR)
59349c53ac9SJohnathan Mantey                     {
59449c53ac9SJohnathan Mantey                         messages::internalError(sensorsAsyncResp->res);
59549c53ac9SJohnathan Mantey                         return;
59649c53ac9SJohnathan Mantey                     }
59749c53ac9SJohnathan Mantey                 }
59849c53ac9SJohnathan Mantey                 const std::vector<std::string>* nodeSensorList =
59949c53ac9SJohnathan Mantey                     std::get_if<std::vector<std::string>>(&(variantEndpoints));
60049c53ac9SJohnathan Mantey                 if (nodeSensorList == nullptr)
60149c53ac9SJohnathan Mantey                 {
60249c53ac9SJohnathan Mantey                     messages::resourceNotFound(
60349c53ac9SJohnathan Mantey                         sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
604a0ec28b6SAdrian Ambrożewicz                         sensorsAsyncResp->chassisSubNode ==
605a0ec28b6SAdrian Ambrożewicz                                 sensors::node::thermal
60649c53ac9SJohnathan Mantey                             ? "Temperatures"
607a0ec28b6SAdrian Ambrożewicz                         : sensorsAsyncResp->chassisSubNode ==
608a0ec28b6SAdrian Ambrożewicz                                 sensors::node::power
60995a3ecadSAnthony Wilson                             ? "Voltages"
61095a3ecadSAnthony Wilson                             : "Sensors");
61149c53ac9SJohnathan Mantey                     return;
61249c53ac9SJohnathan Mantey                 }
61349c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
61449c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
61549c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
61649c53ac9SJohnathan Mantey                 reduceSensorList(sensorsAsyncResp, nodeSensorList,
61749c53ac9SJohnathan Mantey                                  culledSensorList);
61849c53ac9SJohnathan Mantey                 callback(culledSensorList);
61949c53ac9SJohnathan Mantey             },
62049c53ac9SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", sensorPath,
62149c53ac9SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Get",
62249c53ac9SJohnathan Mantey             "xyz.openbmc_project.Association", "endpoints");
62349c53ac9SJohnathan Mantey     };
62449c53ac9SJohnathan Mantey 
62549c53ac9SJohnathan Mantey     // Get the Chassis Collection
62649c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
62749c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
62849c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
62949c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
630271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
63155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
63208777fb0SLewanczyk, Dawid }
63308777fb0SLewanczyk, Dawid 
63408777fb0SLewanczyk, Dawid /**
635de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
636de629b6eSShawn McCarney  *
637de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
638de629b6eSShawn McCarney  *
639de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
640de629b6eSShawn McCarney  * been obtained.
641de629b6eSShawn McCarney  *
642de629b6eSShawn McCarney  * The callback must have the following signature:
643de629b6eSShawn McCarney  *   @code
6448fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
6458fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
646de629b6eSShawn McCarney  *   @endcode
647de629b6eSShawn McCarney  *
64849c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
649de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
650de629b6eSShawn McCarney  */
651de629b6eSShawn McCarney template <typename Callback>
652b5a76932SEd Tanous void getObjectManagerPaths(
65381ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
654de629b6eSShawn McCarney     Callback&& callback)
655de629b6eSShawn McCarney {
656de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
657de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
658de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
659de629b6eSShawn McCarney 
660de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
661de629b6eSShawn McCarney     auto respHandler = [callback{std::move(callback)},
66281ce609eSEd Tanous                         sensorsAsyncResp](const boost::system::error_code ec,
663de629b6eSShawn McCarney                                           const GetSubTreeType& subtree) {
664de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
665de629b6eSShawn McCarney         if (ec)
666de629b6eSShawn McCarney         {
66781ce609eSEd Tanous             messages::internalError(sensorsAsyncResp->res);
668de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
669de629b6eSShawn McCarney                              << ec;
670de629b6eSShawn McCarney             return;
671de629b6eSShawn McCarney         }
672de629b6eSShawn McCarney 
673de629b6eSShawn McCarney         // Loop over returned object paths
6748fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
6758fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
6768fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
677de629b6eSShawn McCarney         for (const std::pair<
678de629b6eSShawn McCarney                  std::string,
679de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
680de629b6eSShawn McCarney                  object : subtree)
681de629b6eSShawn McCarney         {
682de629b6eSShawn McCarney             // Loop over connections for current object path
683de629b6eSShawn McCarney             const std::string& objectPath = object.first;
684de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
685de629b6eSShawn McCarney                      objData : object.second)
686de629b6eSShawn McCarney             {
687de629b6eSShawn McCarney                 // Add mapping from connection to object path
688de629b6eSShawn McCarney                 const std::string& connection = objData.first;
6898fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
690de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
691de629b6eSShawn McCarney                                  << objectPath;
692de629b6eSShawn McCarney             }
693de629b6eSShawn McCarney         }
6948fb49dd6SShawn McCarney         callback(objectMgrPaths);
695de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
696de629b6eSShawn McCarney     };
697de629b6eSShawn McCarney 
698de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
699de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
700de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
701de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
702271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
703de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
704de629b6eSShawn McCarney }
705de629b6eSShawn McCarney 
706de629b6eSShawn McCarney /**
707adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
708adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
709adc4f0dbSShawn McCarney  * @return State value for inventory item.
71034dd179eSJames Feist  */
71123a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
712adc4f0dbSShawn McCarney {
713adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
714adc4f0dbSShawn McCarney     {
715adc4f0dbSShawn McCarney         return "Absent";
716adc4f0dbSShawn McCarney     }
71734dd179eSJames Feist 
718adc4f0dbSShawn McCarney     return "Enabled";
719adc4f0dbSShawn McCarney }
720adc4f0dbSShawn McCarney 
721adc4f0dbSShawn McCarney /**
722adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
723adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
724adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
725adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
726adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
727adc4f0dbSShawn McCarney  * @return Health value for sensor.
728adc4f0dbSShawn McCarney  */
72923a21a1cSEd Tanous inline std::string getHealth(
730adc4f0dbSShawn McCarney     nlohmann::json& sensorJson,
73134dd179eSJames Feist     const boost::container::flat_map<
73234dd179eSJames Feist         std::string, boost::container::flat_map<std::string, SensorVariant>>&
733adc4f0dbSShawn McCarney         interfacesDict,
734adc4f0dbSShawn McCarney     const InventoryItem* inventoryItem)
73534dd179eSJames Feist {
736adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
737adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
738adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
739adc4f0dbSShawn McCarney     std::string currentHealth;
740adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
741adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
742adc4f0dbSShawn McCarney     {
743adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
744adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
745adc4f0dbSShawn McCarney         {
746adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
747adc4f0dbSShawn McCarney             if (health != nullptr)
748adc4f0dbSShawn McCarney             {
749adc4f0dbSShawn McCarney                 currentHealth = *health;
750adc4f0dbSShawn McCarney             }
751adc4f0dbSShawn McCarney         }
752adc4f0dbSShawn McCarney     }
753adc4f0dbSShawn McCarney 
754adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
755adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
756adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
757adc4f0dbSShawn McCarney     {
758adc4f0dbSShawn McCarney         return "Critical";
759adc4f0dbSShawn McCarney     }
760adc4f0dbSShawn McCarney 
761adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
76234dd179eSJames Feist     auto criticalThresholdIt =
76334dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
76434dd179eSJames Feist     if (criticalThresholdIt != interfacesDict.end())
76534dd179eSJames Feist     {
76634dd179eSJames Feist         auto thresholdHighIt =
76734dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmHigh");
76834dd179eSJames Feist         auto thresholdLowIt =
76934dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmLow");
77034dd179eSJames Feist         if (thresholdHighIt != criticalThresholdIt->second.end())
77134dd179eSJames Feist         {
77234dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
77334dd179eSJames Feist             if (asserted == nullptr)
77434dd179eSJames Feist             {
77534dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
77634dd179eSJames Feist             }
77734dd179eSJames Feist             else if (*asserted)
77834dd179eSJames Feist             {
77934dd179eSJames Feist                 return "Critical";
78034dd179eSJames Feist             }
78134dd179eSJames Feist         }
78234dd179eSJames Feist         if (thresholdLowIt != criticalThresholdIt->second.end())
78334dd179eSJames Feist         {
78434dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
78534dd179eSJames Feist             if (asserted == nullptr)
78634dd179eSJames Feist             {
78734dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
78834dd179eSJames Feist             }
78934dd179eSJames Feist             else if (*asserted)
79034dd179eSJames Feist             {
79134dd179eSJames Feist                 return "Critical";
79234dd179eSJames Feist             }
79334dd179eSJames Feist         }
79434dd179eSJames Feist     }
79534dd179eSJames Feist 
796adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
797adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
798adc4f0dbSShawn McCarney     {
799adc4f0dbSShawn McCarney         return "Critical";
800adc4f0dbSShawn McCarney     }
801adc4f0dbSShawn McCarney 
802adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
803adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
804adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
805adc4f0dbSShawn McCarney     {
806adc4f0dbSShawn McCarney         return "Warning";
807adc4f0dbSShawn McCarney     }
808adc4f0dbSShawn McCarney 
809adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
81034dd179eSJames Feist     auto warningThresholdIt =
81134dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
81234dd179eSJames Feist     if (warningThresholdIt != interfacesDict.end())
81334dd179eSJames Feist     {
81434dd179eSJames Feist         auto thresholdHighIt =
81534dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmHigh");
81634dd179eSJames Feist         auto thresholdLowIt =
81734dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmLow");
81834dd179eSJames Feist         if (thresholdHighIt != warningThresholdIt->second.end())
81934dd179eSJames Feist         {
82034dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
82134dd179eSJames Feist             if (asserted == nullptr)
82234dd179eSJames Feist             {
82334dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
82434dd179eSJames Feist             }
82534dd179eSJames Feist             else if (*asserted)
82634dd179eSJames Feist             {
82734dd179eSJames Feist                 return "Warning";
82834dd179eSJames Feist             }
82934dd179eSJames Feist         }
83034dd179eSJames Feist         if (thresholdLowIt != warningThresholdIt->second.end())
83134dd179eSJames Feist         {
83234dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
83334dd179eSJames Feist             if (asserted == nullptr)
83434dd179eSJames Feist             {
83534dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
83634dd179eSJames Feist             }
83734dd179eSJames Feist             else if (*asserted)
83834dd179eSJames Feist             {
83934dd179eSJames Feist                 return "Warning";
84034dd179eSJames Feist             }
84134dd179eSJames Feist         }
84234dd179eSJames Feist     }
843adc4f0dbSShawn McCarney 
84434dd179eSJames Feist     return "OK";
84534dd179eSJames Feist }
84634dd179eSJames Feist 
84723a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
848d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
849d500549bSAnthony Wilson {
850d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
851d500549bSAnthony Wilson     {
852d500549bSAnthony Wilson         switch (inventoryItem->ledState)
853d500549bSAnthony Wilson         {
854d500549bSAnthony Wilson             case LedState::OFF:
855d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
856d500549bSAnthony Wilson                 break;
857d500549bSAnthony Wilson             case LedState::ON:
858d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
859d500549bSAnthony Wilson                 break;
860d500549bSAnthony Wilson             case LedState::BLINK:
861d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
862d500549bSAnthony Wilson                 break;
86323a21a1cSEd Tanous             case LedState::UNKNOWN:
864d500549bSAnthony Wilson                 break;
865d500549bSAnthony Wilson         }
866d500549bSAnthony Wilson     }
867d500549bSAnthony Wilson }
868d500549bSAnthony Wilson 
86934dd179eSJames Feist /**
87008777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
87108777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
872274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
87308777fb0SLewanczyk, Dawid  * build
874a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
87508777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
87608777fb0SLewanczyk, Dawid  * interfaces to be built from
87708777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
878adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
879adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
88008777fb0SLewanczyk, Dawid  */
88123a21a1cSEd Tanous inline void objectInterfacesToJson(
88208777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
883b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
88408777fb0SLewanczyk, Dawid     const boost::container::flat_map<
885aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>&
88608777fb0SLewanczyk, Dawid         interfacesDict,
88781ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8881abe55efSEd Tanous {
88908777fb0SLewanczyk, Dawid     // We need a value interface before we can do anything with it
89055c7b7a2SEd Tanous     auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
8911abe55efSEd Tanous     if (valueIt == interfacesDict.end())
8921abe55efSEd Tanous     {
89355c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
89408777fb0SLewanczyk, Dawid         return;
89508777fb0SLewanczyk, Dawid     }
89608777fb0SLewanczyk, Dawid 
89708777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
89808777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
89908777fb0SLewanczyk, Dawid 
90055c7b7a2SEd Tanous     auto scaleIt = valueIt->second.find("Scale");
90108777fb0SLewanczyk, Dawid     // If a scale exists, pull value as int64, and use the scaling.
9021abe55efSEd Tanous     if (scaleIt != valueIt->second.end())
9031abe55efSEd Tanous     {
904abf2add6SEd Tanous         const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
9051abe55efSEd Tanous         if (int64Value != nullptr)
9061abe55efSEd Tanous         {
90708777fb0SLewanczyk, Dawid             scaleMultiplier = *int64Value;
90808777fb0SLewanczyk, Dawid         }
90908777fb0SLewanczyk, Dawid     }
91008777fb0SLewanczyk, Dawid 
911a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
912adc4f0dbSShawn McCarney     {
91395a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
91495a3ecadSAnthony Wilson         // including power sensors.
91581ce609eSEd Tanous         sensorJson["Id"] = sensorName;
91681ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
91795a3ecadSAnthony Wilson     }
91895a3ecadSAnthony Wilson     else if (sensorType != "power")
91995a3ecadSAnthony Wilson     {
92095a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
92195a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
92295a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
92381ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
92481ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
925adc4f0dbSShawn McCarney     }
926e742b6ccSEd Tanous 
92781ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
92881ce609eSEd Tanous     sensorJson["Status"]["Health"] =
92981ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
93008777fb0SLewanczyk, Dawid 
93108777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
93208777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
93308777fb0SLewanczyk, Dawid     // that require integers, not floats.
93408777fb0SLewanczyk, Dawid     bool forceToInt = false;
93508777fb0SLewanczyk, Dawid 
9363929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
937a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
93895a3ecadSAnthony Wilson     {
93981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
940*c2bf7f99SWludzik, Jozef 
941*c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
942*c2bf7f99SWludzik, Jozef         if (readingType.empty())
94395a3ecadSAnthony Wilson         {
944*c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
945*c2bf7f99SWludzik, Jozef                              << sensorType;
94695a3ecadSAnthony Wilson         }
947*c2bf7f99SWludzik, Jozef         else
94895a3ecadSAnthony Wilson         {
949*c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
95095a3ecadSAnthony Wilson         }
951*c2bf7f99SWludzik, Jozef 
952*c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
953*c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
954f8ede15eSAdrian Ambrożewicz         {
955*c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
956*c2bf7f99SWludzik, Jozef                              << sensorType;
957*c2bf7f99SWludzik, Jozef         }
958*c2bf7f99SWludzik, Jozef         else
959*c2bf7f99SWludzik, Jozef         {
960*c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
961f8ede15eSAdrian Ambrożewicz         }
96295a3ecadSAnthony Wilson     }
96395a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9641abe55efSEd Tanous     {
9653929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
96681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
96708777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
96808777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9691abe55efSEd Tanous     }
9701abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9711abe55efSEd Tanous     {
9723929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
97381ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
97481ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
97581ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
97608777fb0SLewanczyk, Dawid         forceToInt = true;
9771abe55efSEd Tanous     }
9786f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9796f6d0d32SEd Tanous     {
9803929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
98181ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
98281ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
98381ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9846f6d0d32SEd Tanous         forceToInt = true;
9856f6d0d32SEd Tanous     }
9861abe55efSEd Tanous     else if (sensorType == "voltage")
9871abe55efSEd Tanous     {
9883929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
98981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9901abe55efSEd Tanous     }
9912474adfaSEd Tanous     else if (sensorType == "power")
9922474adfaSEd Tanous     {
99349c53ac9SJohnathan Mantey         std::string sensorNameLower =
99449c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
99549c53ac9SJohnathan Mantey 
996028f7ebcSEddie James         if (!sensorName.compare("total_power"))
997028f7ebcSEddie James         {
99881ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
9997ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
10007ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
100181ce609eSEd Tanous             sensorJson["MemberId"] = "0";
100281ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
10033929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
1004028f7ebcSEddie James         }
1005028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
100649c53ac9SJohnathan Mantey         {
10073929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
100849c53ac9SJohnathan Mantey         }
100949c53ac9SJohnathan Mantey         else
101049c53ac9SJohnathan Mantey         {
10113929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
101249c53ac9SJohnathan Mantey         }
10132474adfaSEd Tanous     }
10141abe55efSEd Tanous     else
10151abe55efSEd Tanous     {
101655c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
101708777fb0SLewanczyk, Dawid         return;
101808777fb0SLewanczyk, Dawid     }
101908777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
10203929aca1SAnthony Wilson     std::vector<
10213929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
10223929aca1SAnthony Wilson         properties;
102308777fb0SLewanczyk, Dawid     properties.reserve(7);
102408777fb0SLewanczyk, Dawid 
102508777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
1026de629b6eSShawn McCarney 
1027a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
10283929aca1SAnthony Wilson     {
10293929aca1SAnthony Wilson         properties.emplace_back(
10303929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
10313929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
10323929aca1SAnthony Wilson         properties.emplace_back(
10333929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
10343929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
10353929aca1SAnthony Wilson         properties.emplace_back(
10363929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
10373929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
10383929aca1SAnthony Wilson         properties.emplace_back(
10393929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
10403929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
10413929aca1SAnthony Wilson     }
10423929aca1SAnthony Wilson     else if (sensorType != "power")
1043de629b6eSShawn McCarney     {
104408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10453929aca1SAnthony Wilson                                 "WarningHigh",
10463929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
104708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10483929aca1SAnthony Wilson                                 "WarningLow",
10493929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
105008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10513929aca1SAnthony Wilson                                 "CriticalHigh",
10523929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
105308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10543929aca1SAnthony Wilson                                 "CriticalLow",
10553929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
1056de629b6eSShawn McCarney     }
105708777fb0SLewanczyk, Dawid 
10582474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
10592474adfaSEd Tanous 
1060a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
106195a3ecadSAnthony Wilson     {
106295a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10633929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
106495a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10653929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
106695a3ecadSAnthony Wilson     }
106795a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10681abe55efSEd Tanous     {
106908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10703929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
107108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10723929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10731abe55efSEd Tanous     }
1074adc4f0dbSShawn McCarney     else if (sensorType != "power")
10751abe55efSEd Tanous     {
107608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10773929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
107808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10793929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
108008777fb0SLewanczyk, Dawid     }
108108777fb0SLewanczyk, Dawid 
10823929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10833929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10841abe55efSEd Tanous     {
108508777fb0SLewanczyk, Dawid         auto interfaceProperties = interfacesDict.find(std::get<0>(p));
10861abe55efSEd Tanous         if (interfaceProperties != interfacesDict.end())
10871abe55efSEd Tanous         {
1088271584abSEd Tanous             auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
1089271584abSEd Tanous             if (thisValueIt != interfaceProperties->second.end())
10901abe55efSEd Tanous             {
1091271584abSEd Tanous                 const SensorVariant& valueVariant = thisValueIt->second;
10923929aca1SAnthony Wilson 
10933929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10943929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
10953929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
10963929aca1SAnthony Wilson 
109708777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1098abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
109908777fb0SLewanczyk, Dawid 
1100abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1101028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
11026f6d0d32SEd Tanous                 double temp = 0.0;
11036f6d0d32SEd Tanous                 if (int64Value != nullptr)
11041abe55efSEd Tanous                 {
1105271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
11066f6d0d32SEd Tanous                 }
11076f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
11081abe55efSEd Tanous                 {
11096f6d0d32SEd Tanous                     temp = *doubleValue;
11101abe55efSEd Tanous                 }
1111028f7ebcSEddie James                 else if (uValue != nullptr)
1112028f7ebcSEddie James                 {
1113028f7ebcSEddie James                     temp = *uValue;
1114028f7ebcSEddie James                 }
11151abe55efSEd Tanous                 else
11161abe55efSEd Tanous                 {
11176f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
11186f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
11196f6d0d32SEd Tanous                     continue;
112008777fb0SLewanczyk, Dawid                 }
11216f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
11226f6d0d32SEd Tanous                 if (forceToInt)
11236f6d0d32SEd Tanous                 {
112481ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
11256f6d0d32SEd Tanous                 }
11266f6d0d32SEd Tanous                 else
11276f6d0d32SEd Tanous                 {
112881ce609eSEd Tanous                     sensorJson[key] = temp;
112908777fb0SLewanczyk, Dawid                 }
113008777fb0SLewanczyk, Dawid             }
113108777fb0SLewanczyk, Dawid         }
113208777fb0SLewanczyk, Dawid     }
1133a0ec28b6SAdrian Ambrożewicz 
113481ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1135a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1136a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1137a0ec28b6SAdrian Ambrożewicz 
113855c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
113908777fb0SLewanczyk, Dawid }
114008777fb0SLewanczyk, Dawid 
1141b5a76932SEd Tanous inline void populateFanRedundancy(
1142b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
11438bd25ccdSJames Feist {
11448bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
11458bd25ccdSJames Feist         [sensorsAsyncResp](const boost::system::error_code ec,
11468bd25ccdSJames Feist                            const GetSubTreeType& resp) {
11478bd25ccdSJames Feist             if (ec)
11488bd25ccdSJames Feist             {
11498bd25ccdSJames Feist                 return; // don't have to have this interface
11508bd25ccdSJames Feist             }
1151e278c18fSEd Tanous             for (const std::pair<std::string,
1152e278c18fSEd Tanous                                  std::vector<std::pair<
1153e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
1154e278c18fSEd Tanous                      pathPair : resp)
11558bd25ccdSJames Feist             {
1156e278c18fSEd Tanous                 const std::string& path = pathPair.first;
1157e278c18fSEd Tanous                 const std::vector<
1158e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
1159e278c18fSEd Tanous                     pathPair.second;
11608bd25ccdSJames Feist                 if (objDict.empty())
11618bd25ccdSJames Feist                 {
11628bd25ccdSJames Feist                     continue; // this should be impossible
11638bd25ccdSJames Feist                 }
11648bd25ccdSJames Feist 
11658bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
11668bd25ccdSJames Feist                 crow::connections::systemBus->async_method_call(
11678bd25ccdSJames Feist                     [path, owner,
1168271584abSEd Tanous                      sensorsAsyncResp](const boost::system::error_code e,
11698bd25ccdSJames Feist                                        std::variant<std::vector<std::string>>
11708bd25ccdSJames Feist                                            variantEndpoints) {
1171271584abSEd Tanous                         if (e)
11728bd25ccdSJames Feist                         {
11738bd25ccdSJames Feist                             return; // if they don't have an association we
11748bd25ccdSJames Feist                                     // can't tell what chassis is
11758bd25ccdSJames Feist                         }
11768bd25ccdSJames Feist                         // verify part of the right chassis
11778bd25ccdSJames Feist                         auto endpoints = std::get_if<std::vector<std::string>>(
11788bd25ccdSJames Feist                             &variantEndpoints);
11798bd25ccdSJames Feist 
11808bd25ccdSJames Feist                         if (endpoints == nullptr)
11818bd25ccdSJames Feist                         {
11828bd25ccdSJames Feist                             BMCWEB_LOG_ERROR << "Invalid association interface";
11838bd25ccdSJames Feist                             messages::internalError(sensorsAsyncResp->res);
11848bd25ccdSJames Feist                             return;
11858bd25ccdSJames Feist                         }
11868bd25ccdSJames Feist 
11878bd25ccdSJames Feist                         auto found = std::find_if(
11888bd25ccdSJames Feist                             endpoints->begin(), endpoints->end(),
11898bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
11908bd25ccdSJames Feist                                 return entry.find(
11918bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
11928bd25ccdSJames Feist                                        std::string::npos;
11938bd25ccdSJames Feist                             });
11948bd25ccdSJames Feist 
11958bd25ccdSJames Feist                         if (found == endpoints->end())
11968bd25ccdSJames Feist                         {
11978bd25ccdSJames Feist                             return;
11988bd25ccdSJames Feist                         }
11998bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
12008bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1201271584abSEd Tanous                                 const boost::system::error_code& err,
12028bd25ccdSJames Feist                                 const boost::container::flat_map<
12038bd25ccdSJames Feist                                     std::string,
12048bd25ccdSJames Feist                                     std::variant<uint8_t,
12058bd25ccdSJames Feist                                                  std::vector<std::string>,
12068bd25ccdSJames Feist                                                  std::string>>& ret) {
1207271584abSEd Tanous                                 if (err)
12088bd25ccdSJames Feist                                 {
12098bd25ccdSJames Feist                                     return; // don't have to have this
12108bd25ccdSJames Feist                                             // interface
12118bd25ccdSJames Feist                                 }
12128bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
12138bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
12148bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
12158bd25ccdSJames Feist 
12168bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
12178bd25ccdSJames Feist                                     findCollection == ret.end() ||
12188bd25ccdSJames Feist                                     findStatus == ret.end())
12198bd25ccdSJames Feist                                 {
12208bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12218bd25ccdSJames Feist                                         << "Invalid redundancy interface";
12228bd25ccdSJames Feist                                     messages::internalError(
12238bd25ccdSJames Feist                                         sensorsAsyncResp->res);
12248bd25ccdSJames Feist                                     return;
12258bd25ccdSJames Feist                                 }
12268bd25ccdSJames Feist 
12278bd25ccdSJames Feist                                 auto allowedFailures = std::get_if<uint8_t>(
12288bd25ccdSJames Feist                                     &(findFailures->second));
12298bd25ccdSJames Feist                                 auto collection =
12308bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
12318bd25ccdSJames Feist                                         &(findCollection->second));
12328bd25ccdSJames Feist                                 auto status = std::get_if<std::string>(
12338bd25ccdSJames Feist                                     &(findStatus->second));
12348bd25ccdSJames Feist 
12358bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
12368bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
12378bd25ccdSJames Feist                                 {
12388bd25ccdSJames Feist 
12398bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12408bd25ccdSJames Feist                                         << "Invalid redundancy interface "
12418bd25ccdSJames Feist                                            "types";
12428bd25ccdSJames Feist                                     messages::internalError(
12438bd25ccdSJames Feist                                         sensorsAsyncResp->res);
12448bd25ccdSJames Feist                                     return;
12458bd25ccdSJames Feist                                 }
124628aa8de5SGeorge Liu                                 sdbusplus::message::object_path objectPath(
124728aa8de5SGeorge Liu                                     path);
124828aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
124928aa8de5SGeorge Liu                                 if (name.empty())
12508bd25ccdSJames Feist                                 {
12518bd25ccdSJames Feist                                     // this should be impossible
12528bd25ccdSJames Feist                                     messages::internalError(
12538bd25ccdSJames Feist                                         sensorsAsyncResp->res);
12548bd25ccdSJames Feist                                     return;
12558bd25ccdSJames Feist                                 }
12568bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
12578bd25ccdSJames Feist                                              ' ');
12588bd25ccdSJames Feist 
12598bd25ccdSJames Feist                                 std::string health;
12608bd25ccdSJames Feist 
12618bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
12628bd25ccdSJames Feist                                 {
12638bd25ccdSJames Feist                                     health = "OK";
12648bd25ccdSJames Feist                                 }
12658bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
12668bd25ccdSJames Feist                                 {
12678bd25ccdSJames Feist                                     health = "Warning";
12688bd25ccdSJames Feist                                 }
12698bd25ccdSJames Feist                                 else
12708bd25ccdSJames Feist                                 {
12718bd25ccdSJames Feist                                     health = "Critical";
12728bd25ccdSJames Feist                                 }
12738bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
12748bd25ccdSJames Feist                                 const auto& fanRedfish =
12758bd25ccdSJames Feist                                     sensorsAsyncResp->res.jsonValue["Fans"];
12768bd25ccdSJames Feist                                 for (const std::string& item : *collection)
12778bd25ccdSJames Feist                                 {
127828aa8de5SGeorge Liu                                     sdbusplus::message::object_path path(item);
127928aa8de5SGeorge Liu                                     std::string itemName = path.filename();
128028aa8de5SGeorge Liu                                     if (itemName.empty())
128128aa8de5SGeorge Liu                                     {
128228aa8de5SGeorge Liu                                         continue;
128328aa8de5SGeorge Liu                                     }
12848bd25ccdSJames Feist                                     /*
12858bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
12868bd25ccdSJames Feist                                     std::replace(itemName.begin(),
12878bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
12888bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
12898bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
12908bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
12918bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
12928bd25ccdSJames Feist                                         });
12938bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
12948bd25ccdSJames Feist                                     {
12958bd25ccdSJames Feist                                         redfishCollection.push_back(
12968bd25ccdSJames Feist                                             {{"@odata.id",
12978bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
12988bd25ccdSJames Feist                                     }
12998bd25ccdSJames Feist                                     else
13008bd25ccdSJames Feist                                     {
13018bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
13028bd25ccdSJames Feist                                             << "failed to find fan in schema";
13038bd25ccdSJames Feist                                         messages::internalError(
13048bd25ccdSJames Feist                                             sensorsAsyncResp->res);
13058bd25ccdSJames Feist                                         return;
13068bd25ccdSJames Feist                                     }
13078bd25ccdSJames Feist                                 }
13088bd25ccdSJames Feist 
13093e9e72ebSKuiying Wang                                 size_t minNumNeeded =
13103e9e72ebSKuiying Wang                                     collection->size() > 0
13113e9e72ebSKuiying Wang                                         ? collection->size() - *allowedFailures
13123e9e72ebSKuiying Wang                                         : 0;
1313271584abSEd Tanous                                 nlohmann::json& jResp =
1314271584abSEd Tanous                                     sensorsAsyncResp->res
13158bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1316271584abSEd Tanous                                 jResp.push_back(
13178bd25ccdSJames Feist                                     {{"@odata.id",
1318717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
13198bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
13208bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
13218bd25ccdSJames Feist                                           "#/Redundancy/" +
1322271584abSEd Tanous                                           std::to_string(jResp.size())},
13238bd25ccdSJames Feist                                      {"@odata.type",
13248bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
13253e9e72ebSKuiying Wang                                      {"MinNumNeeded", minNumNeeded},
13268bd25ccdSJames Feist                                      {"MemberId", name},
13278bd25ccdSJames Feist                                      {"Mode", "N+m"},
13288bd25ccdSJames Feist                                      {"Name", name},
13298bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
13308bd25ccdSJames Feist                                      {"Status",
13318bd25ccdSJames Feist                                       {{"Health", health},
13328bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
13338bd25ccdSJames Feist                             },
13348bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
13358bd25ccdSJames Feist                             "GetAll",
13368bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
13378bd25ccdSJames Feist                     },
133802e92e32SJames Feist                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
13398bd25ccdSJames Feist                     "org.freedesktop.DBus.Properties", "Get",
13408bd25ccdSJames Feist                     "xyz.openbmc_project.Association", "endpoints");
13418bd25ccdSJames Feist             }
13428bd25ccdSJames Feist         },
13438bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13448bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13458bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13468bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13478bd25ccdSJames Feist         std::array<const char*, 1>{
13488bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13498bd25ccdSJames Feist }
13508bd25ccdSJames Feist 
1351b5a76932SEd Tanous inline void
135281ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
135349c53ac9SJohnathan Mantey {
135481ce609eSEd Tanous     nlohmann::json& response = sensorsAsyncResp->res.jsonValue;
135549c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
135681ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
135749c53ac9SJohnathan Mantey     {
135849c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
135949c53ac9SJohnathan Mantey     }
136049c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
136149c53ac9SJohnathan Mantey     {
136249c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
136349c53ac9SJohnathan Mantey         if (entry != response.end())
136449c53ac9SJohnathan Mantey         {
136549c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
136649c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
136749c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
136849c53ac9SJohnathan Mantey                       });
136949c53ac9SJohnathan Mantey 
137049c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
137149c53ac9SJohnathan Mantey             size_t count = 0;
137249c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
137349c53ac9SJohnathan Mantey             {
137449c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
137549c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
137649c53ac9SJohnathan Mantey                 {
137749c53ac9SJohnathan Mantey                     continue;
137849c53ac9SJohnathan Mantey                 }
137949c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
138049c53ac9SJohnathan Mantey                 if (value != nullptr)
138149c53ac9SJohnathan Mantey                 {
138249c53ac9SJohnathan Mantey                     *value += std::to_string(count);
138349c53ac9SJohnathan Mantey                     count++;
138481ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
138549c53ac9SJohnathan Mantey                 }
138649c53ac9SJohnathan Mantey             }
138749c53ac9SJohnathan Mantey         }
138849c53ac9SJohnathan Mantey     }
138949c53ac9SJohnathan Mantey }
139049c53ac9SJohnathan Mantey 
139108777fb0SLewanczyk, Dawid /**
1392adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1393adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1394adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1395adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13968fb49dd6SShawn McCarney  */
139723a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1398b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1399adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
14008fb49dd6SShawn McCarney {
1401adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
14028fb49dd6SShawn McCarney     {
1403adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
14048fb49dd6SShawn McCarney         {
1405adc4f0dbSShawn McCarney             return &inventoryItem;
14068fb49dd6SShawn McCarney         }
14078fb49dd6SShawn McCarney     }
14088fb49dd6SShawn McCarney     return nullptr;
14098fb49dd6SShawn McCarney }
14108fb49dd6SShawn McCarney 
14118fb49dd6SShawn McCarney /**
1412adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1413adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1414adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1415adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
14168fb49dd6SShawn McCarney  */
141723a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1418b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1419adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1420adc4f0dbSShawn McCarney {
1421adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1422adc4f0dbSShawn McCarney     {
1423adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1424adc4f0dbSShawn McCarney         {
1425adc4f0dbSShawn McCarney             return &inventoryItem;
1426adc4f0dbSShawn McCarney         }
1427adc4f0dbSShawn McCarney     }
1428adc4f0dbSShawn McCarney     return nullptr;
1429adc4f0dbSShawn McCarney }
1430adc4f0dbSShawn McCarney 
1431adc4f0dbSShawn McCarney /**
1432d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1433d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1434d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1435d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1436d500549bSAnthony Wilson  */
1437d500549bSAnthony Wilson inline InventoryItem*
1438d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1439d500549bSAnthony Wilson                             const std::string& ledObjPath)
1440d500549bSAnthony Wilson {
1441d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1442d500549bSAnthony Wilson     {
1443d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1444d500549bSAnthony Wilson         {
1445d500549bSAnthony Wilson             return &inventoryItem;
1446d500549bSAnthony Wilson         }
1447d500549bSAnthony Wilson     }
1448d500549bSAnthony Wilson     return nullptr;
1449d500549bSAnthony Wilson }
1450d500549bSAnthony Wilson 
1451d500549bSAnthony Wilson /**
1452adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1453adc4f0dbSShawn McCarney  *
1454adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1455adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1456adc4f0dbSShawn McCarney  * added to the vector.
1457adc4f0dbSShawn McCarney  *
1458adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1459adc4f0dbSShawn McCarney  * InventoryItem.
1460adc4f0dbSShawn McCarney  *
1461adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1462adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1463adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1464adc4f0dbSShawn McCarney  */
1465b5a76932SEd Tanous inline void addInventoryItem(
1466b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1467b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1468adc4f0dbSShawn McCarney {
1469adc4f0dbSShawn McCarney     // Look for inventory item in vector
1470adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1471adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1472adc4f0dbSShawn McCarney 
1473adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1474adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1475adc4f0dbSShawn McCarney     {
1476adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1477adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1478adc4f0dbSShawn McCarney     }
1479adc4f0dbSShawn McCarney 
1480adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1481adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1482adc4f0dbSShawn McCarney }
1483adc4f0dbSShawn McCarney 
1484adc4f0dbSShawn McCarney /**
1485adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1486adc4f0dbSShawn McCarney  *
1487adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1488adc4f0dbSShawn McCarney  * specified InventoryItem.
1489adc4f0dbSShawn McCarney  *
1490adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1491adc4f0dbSShawn McCarney  * response.
1492adc4f0dbSShawn McCarney  *
1493adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1494adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1495adc4f0dbSShawn McCarney  * for the specified inventory item.
1496adc4f0dbSShawn McCarney  */
149723a21a1cSEd Tanous inline void storeInventoryItemData(
1498adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
14998fb49dd6SShawn McCarney     const boost::container::flat_map<
15008fb49dd6SShawn McCarney         std::string, boost::container::flat_map<std::string, SensorVariant>>&
15018fb49dd6SShawn McCarney         interfacesDict)
15028fb49dd6SShawn McCarney {
1503adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1504adc4f0dbSShawn McCarney     auto interfaceIt =
1505adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1506adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
15078fb49dd6SShawn McCarney     {
1508adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Present");
1509adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
15108fb49dd6SShawn McCarney         {
1511adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1512adc4f0dbSShawn McCarney             if (value != nullptr)
15138fb49dd6SShawn McCarney             {
1514adc4f0dbSShawn McCarney                 inventoryItem.isPresent = *value;
15158fb49dd6SShawn McCarney             }
15168fb49dd6SShawn McCarney         }
15178fb49dd6SShawn McCarney     }
15188fb49dd6SShawn McCarney 
1519adc4f0dbSShawn McCarney     // Check if Inventory.Item.PowerSupply interface is present
1520adc4f0dbSShawn McCarney     interfaceIt =
1521adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1522adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
15238fb49dd6SShawn McCarney     {
1524adc4f0dbSShawn McCarney         inventoryItem.isPowerSupply = true;
15258fb49dd6SShawn McCarney     }
1526adc4f0dbSShawn McCarney 
1527adc4f0dbSShawn McCarney     // Get properties from Inventory.Decorator.Asset interface
1528adc4f0dbSShawn McCarney     interfaceIt =
1529adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1530adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1531adc4f0dbSShawn McCarney     {
1532adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Manufacturer");
1533adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1534adc4f0dbSShawn McCarney         {
1535adc4f0dbSShawn McCarney             const std::string* value =
1536adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1537adc4f0dbSShawn McCarney             if (value != nullptr)
1538adc4f0dbSShawn McCarney             {
1539adc4f0dbSShawn McCarney                 inventoryItem.manufacturer = *value;
1540adc4f0dbSShawn McCarney             }
1541adc4f0dbSShawn McCarney         }
1542adc4f0dbSShawn McCarney 
1543adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("Model");
1544adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1545adc4f0dbSShawn McCarney         {
1546adc4f0dbSShawn McCarney             const std::string* value =
1547adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1548adc4f0dbSShawn McCarney             if (value != nullptr)
1549adc4f0dbSShawn McCarney             {
1550adc4f0dbSShawn McCarney                 inventoryItem.model = *value;
1551adc4f0dbSShawn McCarney             }
1552adc4f0dbSShawn McCarney         }
1553adc4f0dbSShawn McCarney 
1554adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("PartNumber");
1555adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1556adc4f0dbSShawn McCarney         {
1557adc4f0dbSShawn McCarney             const std::string* value =
1558adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1559adc4f0dbSShawn McCarney             if (value != nullptr)
1560adc4f0dbSShawn McCarney             {
1561adc4f0dbSShawn McCarney                 inventoryItem.partNumber = *value;
1562adc4f0dbSShawn McCarney             }
1563adc4f0dbSShawn McCarney         }
1564adc4f0dbSShawn McCarney 
1565adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("SerialNumber");
1566adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1567adc4f0dbSShawn McCarney         {
1568adc4f0dbSShawn McCarney             const std::string* value =
1569adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1570adc4f0dbSShawn McCarney             if (value != nullptr)
1571adc4f0dbSShawn McCarney             {
1572adc4f0dbSShawn McCarney                 inventoryItem.serialNumber = *value;
1573adc4f0dbSShawn McCarney             }
1574adc4f0dbSShawn McCarney         }
1575adc4f0dbSShawn McCarney     }
1576adc4f0dbSShawn McCarney 
1577adc4f0dbSShawn McCarney     // Get properties from State.Decorator.OperationalStatus interface
1578adc4f0dbSShawn McCarney     interfaceIt = interfacesDict.find(
1579adc4f0dbSShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus");
1580adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1581adc4f0dbSShawn McCarney     {
1582adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Functional");
1583adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1584adc4f0dbSShawn McCarney         {
1585adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1586adc4f0dbSShawn McCarney             if (value != nullptr)
1587adc4f0dbSShawn McCarney             {
1588adc4f0dbSShawn McCarney                 inventoryItem.isFunctional = *value;
15898fb49dd6SShawn McCarney             }
15908fb49dd6SShawn McCarney         }
15918fb49dd6SShawn McCarney     }
15928fb49dd6SShawn McCarney }
15938fb49dd6SShawn McCarney 
15948fb49dd6SShawn McCarney /**
1595adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
15968fb49dd6SShawn McCarney  *
1597adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1598adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1599adc4f0dbSShawn McCarney  * inventoryItems vector.
16008fb49dd6SShawn McCarney  *
1601adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1602adc4f0dbSShawn McCarney  * response.
1603adc4f0dbSShawn McCarney  *
1604adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1605adc4f0dbSShawn McCarney  * been obtained.
1606adc4f0dbSShawn McCarney  *
1607adc4f0dbSShawn McCarney  * The callback must have the following signature:
1608adc4f0dbSShawn McCarney  *   @code
1609d500549bSAnthony Wilson  *   callback(void)
1610adc4f0dbSShawn McCarney  *   @endcode
1611adc4f0dbSShawn McCarney  *
1612adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1613adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1614adc4f0dbSShawn McCarney  * last asynchronous function has completed.
16158fb49dd6SShawn McCarney  *
16168fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1617adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1618adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
16198fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
16208fb49dd6SShawn McCarney  * implements ObjectManager.
1621adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1622adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1623adc4f0dbSShawn McCarney  * in recursive calls to this function.
16248fb49dd6SShawn McCarney  */
1625adc4f0dbSShawn McCarney template <typename Callback>
1626adc4f0dbSShawn McCarney static void getInventoryItemsData(
16278fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1628adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
16298fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
16308fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1631adc4f0dbSShawn McCarney         objectMgrPaths,
1632271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
16338fb49dd6SShawn McCarney {
1634adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
16358fb49dd6SShawn McCarney 
1636adc4f0dbSShawn McCarney     // If no more connections left, call callback
1637adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
16388fb49dd6SShawn McCarney     {
1639d500549bSAnthony Wilson         callback();
1640adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1641adc4f0dbSShawn McCarney         return;
1642adc4f0dbSShawn McCarney     }
1643adc4f0dbSShawn McCarney 
1644adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1645adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1646adc4f0dbSShawn McCarney     if (it != invConnections->end())
1647adc4f0dbSShawn McCarney     {
1648adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1649adc4f0dbSShawn McCarney 
16508fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1651adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1652adc4f0dbSShawn McCarney                             objectMgrPaths, callback{std::move(callback)},
1653adc4f0dbSShawn McCarney                             invConnectionsIndex](
1654adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
16558fb49dd6SShawn McCarney                                ManagedObjectsVectorType& resp) {
1656adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16578fb49dd6SShawn McCarney             if (ec)
16588fb49dd6SShawn McCarney             {
16598fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1660adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16618fb49dd6SShawn McCarney                 messages::internalError(sensorsAsyncResp->res);
16628fb49dd6SShawn McCarney                 return;
16638fb49dd6SShawn McCarney             }
16648fb49dd6SShawn McCarney 
16658fb49dd6SShawn McCarney             // Loop through returned object paths
16668fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16678fb49dd6SShawn McCarney             {
16688fb49dd6SShawn McCarney                 const std::string& objPath =
16698fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16708fb49dd6SShawn McCarney 
1671adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1672adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1673adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1674adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16758fb49dd6SShawn McCarney                 {
1676adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1677adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16788fb49dd6SShawn McCarney                 }
16798fb49dd6SShawn McCarney             }
16808fb49dd6SShawn McCarney 
1681adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1682adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1683adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1684adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1685adc4f0dbSShawn McCarney 
1686adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16878fb49dd6SShawn McCarney         };
16888fb49dd6SShawn McCarney 
16898fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16908fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16918fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16928fb49dd6SShawn McCarney         const std::string& objectMgrPath =
16938fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
16948fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
16958fb49dd6SShawn McCarney                          << objectMgrPath;
16968fb49dd6SShawn McCarney 
16978fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16988fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16998fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
17008fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17018fb49dd6SShawn McCarney     }
17028fb49dd6SShawn McCarney 
1703adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
17048fb49dd6SShawn McCarney }
17058fb49dd6SShawn McCarney 
17068fb49dd6SShawn McCarney /**
1707adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
17088fb49dd6SShawn McCarney  *
1709adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1710adc4f0dbSShawn McCarney  * items that are associated with sensors.
17118fb49dd6SShawn McCarney  *
17128fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
17138fb49dd6SShawn McCarney  * been obtained.
17148fb49dd6SShawn McCarney  *
17158fb49dd6SShawn McCarney  * The callback must have the following signature:
17168fb49dd6SShawn McCarney  *   @code
17178fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
17188fb49dd6SShawn McCarney  *            invConnections)
17198fb49dd6SShawn McCarney  *   @endcode
17208fb49dd6SShawn McCarney  *
17218fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1722adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
17238fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
17248fb49dd6SShawn McCarney  */
17258fb49dd6SShawn McCarney template <typename Callback>
17268fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1727b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1728b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
17298fb49dd6SShawn McCarney     Callback&& callback)
17308fb49dd6SShawn McCarney {
17318fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
17328fb49dd6SShawn McCarney 
17338fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1734adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
17358fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1736adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1737adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
17388fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
17398fb49dd6SShawn McCarney 
17408fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
17418fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1742adc4f0dbSShawn McCarney                         inventoryItems](const boost::system::error_code ec,
17438fb49dd6SShawn McCarney                                         const GetSubTreeType& subtree) {
17448fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17458fb49dd6SShawn McCarney         if (ec)
17468fb49dd6SShawn McCarney         {
17478fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
17488fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17498fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17508fb49dd6SShawn McCarney             return;
17518fb49dd6SShawn McCarney         }
17528fb49dd6SShawn McCarney 
17538fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
17548fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
17558fb49dd6SShawn McCarney             invConnections =
17568fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
17578fb49dd6SShawn McCarney         invConnections->reserve(8);
17588fb49dd6SShawn McCarney 
17598fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17608fb49dd6SShawn McCarney         for (const std::pair<
17618fb49dd6SShawn McCarney                  std::string,
17628fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17638fb49dd6SShawn McCarney                  object : subtree)
17648fb49dd6SShawn McCarney         {
1765adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17668fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1767adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17688fb49dd6SShawn McCarney             {
17698fb49dd6SShawn McCarney                 // Store all connections to inventory item
17708fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17718fb49dd6SShawn McCarney                          objData : object.second)
17728fb49dd6SShawn McCarney                 {
17738fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17748fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17758fb49dd6SShawn McCarney                 }
17768fb49dd6SShawn McCarney             }
17778fb49dd6SShawn McCarney         }
1778d500549bSAnthony Wilson 
17798fb49dd6SShawn McCarney         callback(invConnections);
17808fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17818fb49dd6SShawn McCarney     };
17828fb49dd6SShawn McCarney 
17838fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17848fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17858fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17868fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17878fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17888fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17898fb49dd6SShawn McCarney }
17908fb49dd6SShawn McCarney 
17918fb49dd6SShawn McCarney /**
1792adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
17938fb49dd6SShawn McCarney  *
17948fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1795d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1796d500549bSAnthony Wilson  * their LEDs, if any.
17978fb49dd6SShawn McCarney  *
17988fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17998fb49dd6SShawn McCarney  * has been obtained.
18008fb49dd6SShawn McCarney  *
18018fb49dd6SShawn McCarney  * The callback must have the following signature:
18028fb49dd6SShawn McCarney  *   @code
1803adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
18048fb49dd6SShawn McCarney  *   @endcode
18058fb49dd6SShawn McCarney  *
18068fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
18078fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
18088fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
18098fb49dd6SShawn McCarney  * implements ObjectManager.
18108fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
18118fb49dd6SShawn McCarney  */
18128fb49dd6SShawn McCarney template <typename Callback>
1813adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1814b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1815b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1816b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
18178fb49dd6SShawn McCarney         objectMgrPaths,
18188fb49dd6SShawn McCarney     Callback&& callback)
18198fb49dd6SShawn McCarney {
1820adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
18218fb49dd6SShawn McCarney 
18228fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
18238fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
18248fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
18258fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1826adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
18278fb49dd6SShawn McCarney         if (ec)
18288fb49dd6SShawn McCarney         {
1829adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1830adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
18318fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
18328fb49dd6SShawn McCarney             return;
18338fb49dd6SShawn McCarney         }
18348fb49dd6SShawn McCarney 
1835adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1836adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1837adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1838adc4f0dbSShawn McCarney 
18398fb49dd6SShawn McCarney         // Loop through returned object paths
18408fb49dd6SShawn McCarney         std::string sensorAssocPath;
18418fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18428fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18438fb49dd6SShawn McCarney         {
18448fb49dd6SShawn McCarney             const std::string& objPath =
18458fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18468fb49dd6SShawn McCarney             const boost::container::flat_map<
18478fb49dd6SShawn McCarney                 std::string, boost::container::flat_map<
18488fb49dd6SShawn McCarney                                  std::string, dbus::utility::DbusVariantType>>&
18498fb49dd6SShawn McCarney                 interfacesDict = objDictEntry.second;
18508fb49dd6SShawn McCarney 
18518fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18528fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18538fb49dd6SShawn McCarney             {
18548fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18558fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18568fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18578fb49dd6SShawn McCarney                 {
18588fb49dd6SShawn McCarney                     // Get Association interface for object path
18598fb49dd6SShawn McCarney                     auto assocIt =
18608fb49dd6SShawn McCarney                         interfacesDict.find("xyz.openbmc_project.Association");
18618fb49dd6SShawn McCarney                     if (assocIt != interfacesDict.end())
18628fb49dd6SShawn McCarney                     {
18638fb49dd6SShawn McCarney                         // Get inventory item from end point
18648fb49dd6SShawn McCarney                         auto endpointsIt = assocIt->second.find("endpoints");
18658fb49dd6SShawn McCarney                         if (endpointsIt != assocIt->second.end())
18668fb49dd6SShawn McCarney                         {
18678fb49dd6SShawn McCarney                             const std::vector<std::string>* endpoints =
18688fb49dd6SShawn McCarney                                 std::get_if<std::vector<std::string>>(
18698fb49dd6SShawn McCarney                                     &endpointsIt->second);
18708fb49dd6SShawn McCarney                             if ((endpoints != nullptr) && !endpoints->empty())
18718fb49dd6SShawn McCarney                             {
1872adc4f0dbSShawn McCarney                                 // Add inventory item to vector
1873adc4f0dbSShawn McCarney                                 const std::string& invItemPath =
1874adc4f0dbSShawn McCarney                                     endpoints->front();
1875adc4f0dbSShawn McCarney                                 addInventoryItem(inventoryItems, invItemPath,
1876adc4f0dbSShawn McCarney                                                  sensorName);
18778fb49dd6SShawn McCarney                             }
18788fb49dd6SShawn McCarney                         }
18798fb49dd6SShawn McCarney                     }
18808fb49dd6SShawn McCarney                     break;
18818fb49dd6SShawn McCarney                 }
18828fb49dd6SShawn McCarney             }
18838fb49dd6SShawn McCarney         }
18848fb49dd6SShawn McCarney 
1885d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1886d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1887d500549bSAnthony Wilson         std::string inventoryAssocPath;
1888d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1889d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1890d500549bSAnthony Wilson         {
1891d500549bSAnthony Wilson             const std::string& objPath =
1892d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1893d500549bSAnthony Wilson             const boost::container::flat_map<
1894d500549bSAnthony Wilson                 std::string, boost::container::flat_map<
1895d500549bSAnthony Wilson                                  std::string, dbus::utility::DbusVariantType>>&
1896d500549bSAnthony Wilson                 interfacesDict = objDictEntry.second;
1897d500549bSAnthony Wilson 
1898d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1899d500549bSAnthony Wilson             {
1900d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1901d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1902d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1903d500549bSAnthony Wilson                 {
1904d500549bSAnthony Wilson                     // Get Association interface for object path
1905d500549bSAnthony Wilson                     auto assocIt =
1906d500549bSAnthony Wilson                         interfacesDict.find("xyz.openbmc_project.Association");
1907d500549bSAnthony Wilson                     if (assocIt != interfacesDict.end())
1908d500549bSAnthony Wilson                     {
1909d500549bSAnthony Wilson                         // Get inventory item from end point
1910d500549bSAnthony Wilson                         auto endpointsIt = assocIt->second.find("endpoints");
1911d500549bSAnthony Wilson                         if (endpointsIt != assocIt->second.end())
1912d500549bSAnthony Wilson                         {
1913d500549bSAnthony Wilson                             const std::vector<std::string>* endpoints =
1914d500549bSAnthony Wilson                                 std::get_if<std::vector<std::string>>(
1915d500549bSAnthony Wilson                                     &endpointsIt->second);
1916d500549bSAnthony Wilson                             if ((endpoints != nullptr) && !endpoints->empty())
1917d500549bSAnthony Wilson                             {
1918d500549bSAnthony Wilson                                 // Store LED path in inventory item
1919d500549bSAnthony Wilson                                 const std::string& ledPath = endpoints->front();
1920d500549bSAnthony Wilson                                 inventoryItem.ledObjectPath = ledPath;
1921d500549bSAnthony Wilson                             }
1922d500549bSAnthony Wilson                         }
1923d500549bSAnthony Wilson                     }
1924d500549bSAnthony Wilson                     break;
1925d500549bSAnthony Wilson                 }
1926d500549bSAnthony Wilson             }
1927d500549bSAnthony Wilson         }
1928adc4f0dbSShawn McCarney         callback(inventoryItems);
1929adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
19308fb49dd6SShawn McCarney     };
19318fb49dd6SShawn McCarney 
19328fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
19338fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
19348fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
19358fb49dd6SShawn McCarney     const std::string& objectMgrPath =
19368fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
19378fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
19388fb49dd6SShawn McCarney                      << objectMgrPath;
19398fb49dd6SShawn McCarney 
19408fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19418fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19428fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19438fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19448fb49dd6SShawn McCarney 
1945adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19468fb49dd6SShawn McCarney }
19478fb49dd6SShawn McCarney 
19488fb49dd6SShawn McCarney /**
1949d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1950d500549bSAnthony Wilson  *
1951d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1952d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1953d500549bSAnthony Wilson  * inventoryItems vector.
1954d500549bSAnthony Wilson  *
1955d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1956d500549bSAnthony Wilson  * response.
1957d500549bSAnthony Wilson  *
1958d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1959d500549bSAnthony Wilson  * has been obtained.
1960d500549bSAnthony Wilson  *
1961d500549bSAnthony Wilson  * The callback must have the following signature:
1962d500549bSAnthony Wilson  *   @code
196342cbe538SGunnar Mills  *   callback()
1964d500549bSAnthony Wilson  *   @endcode
1965d500549bSAnthony Wilson  *
1966d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1967d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1968d500549bSAnthony Wilson  * last asynchronous function has completed.
1969d500549bSAnthony Wilson  *
1970d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1971d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1972d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1973d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1974d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1975d500549bSAnthony Wilson  * in recursive calls to this function.
1976d500549bSAnthony Wilson  */
1977d500549bSAnthony Wilson template <typename Callback>
1978d500549bSAnthony Wilson void getInventoryLedData(
1979d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1980d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1981d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1982d500549bSAnthony Wilson         ledConnections,
1983d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1984d500549bSAnthony Wilson {
1985d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1986d500549bSAnthony Wilson 
1987d500549bSAnthony Wilson     // If no more connections left, call callback
1988d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1989d500549bSAnthony Wilson     {
199042cbe538SGunnar Mills         callback();
1991d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1992d500549bSAnthony Wilson         return;
1993d500549bSAnthony Wilson     }
1994d500549bSAnthony Wilson 
1995d500549bSAnthony Wilson     // Get inventory item data from current connection
1996d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1997d500549bSAnthony Wilson     if (it != ledConnections->end())
1998d500549bSAnthony Wilson     {
1999d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
2000d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
2001d500549bSAnthony Wilson         // Response handler for Get State property
2002d500549bSAnthony Wilson         auto respHandler =
2003d500549bSAnthony Wilson             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
2004d500549bSAnthony Wilson              callback{std::move(callback)},
2005d500549bSAnthony Wilson              ledConnectionsIndex](const boost::system::error_code ec,
2006d500549bSAnthony Wilson                                   const std::variant<std::string>& ledState) {
2007d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
2008d500549bSAnthony Wilson                 if (ec)
2009d500549bSAnthony Wilson                 {
2010d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
2011d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
2012d500549bSAnthony Wilson                     messages::internalError(sensorsAsyncResp->res);
2013d500549bSAnthony Wilson                     return;
2014d500549bSAnthony Wilson                 }
2015d500549bSAnthony Wilson 
2016d500549bSAnthony Wilson                 const std::string* state = std::get_if<std::string>(&ledState);
2017d500549bSAnthony Wilson                 if (state != nullptr)
2018d500549bSAnthony Wilson                 {
2019d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Led state: " << *state;
2020d500549bSAnthony Wilson                     // Find inventory item with this LED object path
2021d500549bSAnthony Wilson                     InventoryItem* inventoryItem =
2022d500549bSAnthony Wilson                         findInventoryItemForLed(*inventoryItems, ledPath);
2023d500549bSAnthony Wilson                     if (inventoryItem != nullptr)
2024d500549bSAnthony Wilson                     {
2025d500549bSAnthony Wilson                         // Store LED state in InventoryItem
2026d500549bSAnthony Wilson                         if (boost::ends_with(*state, "On"))
2027d500549bSAnthony Wilson                         {
2028d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::ON;
2029d500549bSAnthony Wilson                         }
2030d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Blink"))
2031d500549bSAnthony Wilson                         {
2032d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::BLINK;
2033d500549bSAnthony Wilson                         }
2034d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Off"))
2035d500549bSAnthony Wilson                         {
2036d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::OFF;
2037d500549bSAnthony Wilson                         }
2038d500549bSAnthony Wilson                         else
2039d500549bSAnthony Wilson                         {
2040d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::UNKNOWN;
2041d500549bSAnthony Wilson                         }
2042d500549bSAnthony Wilson                     }
2043d500549bSAnthony Wilson                 }
2044d500549bSAnthony Wilson                 else
2045d500549bSAnthony Wilson                 {
2046d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
2047d500549bSAnthony Wilson                                      << ledPath;
2048d500549bSAnthony Wilson                 }
2049d500549bSAnthony Wilson 
2050d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
2051d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2052d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
2053d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
2054d500549bSAnthony Wilson 
2055d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2056d500549bSAnthony Wilson             };
2057d500549bSAnthony Wilson 
2058d500549bSAnthony Wilson         // Get the State property for the current LED
2059d500549bSAnthony Wilson         crow::connections::systemBus->async_method_call(
2060d500549bSAnthony Wilson             std::move(respHandler), ledConnection, ledPath,
2061d500549bSAnthony Wilson             "org.freedesktop.DBus.Properties", "Get",
2062d500549bSAnthony Wilson             "xyz.openbmc_project.Led.Physical", "State");
2063d500549bSAnthony Wilson     }
2064d500549bSAnthony Wilson 
2065d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2066d500549bSAnthony Wilson }
2067d500549bSAnthony Wilson 
2068d500549bSAnthony Wilson /**
2069d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2070d500549bSAnthony Wilson  *
2071d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2072d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2073d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2074d500549bSAnthony Wilson  *
2075d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2076d500549bSAnthony Wilson  * response.
2077d500549bSAnthony Wilson  *
2078d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2079d500549bSAnthony Wilson  * been obtained.
2080d500549bSAnthony Wilson  *
2081d500549bSAnthony Wilson  * The callback must have the following signature:
2082d500549bSAnthony Wilson  *   @code
208342cbe538SGunnar Mills  *   callback()
2084d500549bSAnthony Wilson  *   @endcode
2085d500549bSAnthony Wilson  *
2086d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2087d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2088d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2089d500549bSAnthony Wilson  */
2090d500549bSAnthony Wilson template <typename Callback>
2091d500549bSAnthony Wilson void getInventoryLeds(
2092d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2093d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2094d500549bSAnthony Wilson     Callback&& callback)
2095d500549bSAnthony Wilson {
2096d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2097d500549bSAnthony Wilson 
2098d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2099d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2100d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2101d500549bSAnthony Wilson 
2102d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2103d500549bSAnthony Wilson     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2104d500549bSAnthony Wilson                         inventoryItems](const boost::system::error_code ec,
2105d500549bSAnthony Wilson                                         const GetSubTreeType& subtree) {
2106d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2107d500549bSAnthony Wilson         if (ec)
2108d500549bSAnthony Wilson         {
2109d500549bSAnthony Wilson             messages::internalError(sensorsAsyncResp->res);
2110d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2111d500549bSAnthony Wilson                              << ec;
2112d500549bSAnthony Wilson             return;
2113d500549bSAnthony Wilson         }
2114d500549bSAnthony Wilson 
2115d500549bSAnthony Wilson         // Build map of LED object paths to connections
2116d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2117d500549bSAnthony Wilson             ledConnections = std::make_shared<
2118d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2119d500549bSAnthony Wilson 
2120d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2121d500549bSAnthony Wilson         for (const std::pair<
2122d500549bSAnthony Wilson                  std::string,
2123d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2124d500549bSAnthony Wilson                  object : subtree)
2125d500549bSAnthony Wilson         {
2126d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2127d500549bSAnthony Wilson             // items
2128d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2129d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2130d500549bSAnthony Wilson             {
2131d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2132d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2133d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2134d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2135d500549bSAnthony Wilson                                  << connection;
2136d500549bSAnthony Wilson             }
2137d500549bSAnthony Wilson         }
2138d500549bSAnthony Wilson 
2139d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2140d500549bSAnthony Wilson                             std::move(callback));
2141d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2142d500549bSAnthony Wilson     };
2143d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2144d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2145d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2146d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2147d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2148d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2149d500549bSAnthony Wilson }
2150d500549bSAnthony Wilson 
2151d500549bSAnthony Wilson /**
215242cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
215342cbe538SGunnar Mills  *
215442cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
215542cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
215642cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
215742cbe538SGunnar Mills  *
215842cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
215942cbe538SGunnar Mills  * response.
216042cbe538SGunnar Mills  *
216142cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
216242cbe538SGunnar Mills  * when data has been obtained.
216342cbe538SGunnar Mills  *
216442cbe538SGunnar Mills  * The callback must have the following signature:
216542cbe538SGunnar Mills  *   @code
216642cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
216742cbe538SGunnar Mills  *   @endcode
216842cbe538SGunnar Mills  *
216942cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
217042cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
217142cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
217242cbe538SGunnar Mills  *        Supply Attributes
217342cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
217442cbe538SGunnar Mills  */
217542cbe538SGunnar Mills template <typename Callback>
217642cbe538SGunnar Mills void getPowerSupplyAttributesData(
2177b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
217842cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
217942cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
218042cbe538SGunnar Mills         psAttributesConnections,
218142cbe538SGunnar Mills     Callback&& callback)
218242cbe538SGunnar Mills {
218342cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
218442cbe538SGunnar Mills 
218542cbe538SGunnar Mills     if (psAttributesConnections.empty())
218642cbe538SGunnar Mills     {
218742cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
218842cbe538SGunnar Mills         callback(inventoryItems);
218942cbe538SGunnar Mills         return;
219042cbe538SGunnar Mills     }
219142cbe538SGunnar Mills 
219242cbe538SGunnar Mills     // Assuming just one connection (service) for now
219342cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
219442cbe538SGunnar Mills 
219542cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
219642cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
219742cbe538SGunnar Mills 
219842cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
219942cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
220042cbe538SGunnar Mills                         callback{std::move(callback)}](
220142cbe538SGunnar Mills                            const boost::system::error_code ec,
220242cbe538SGunnar Mills                            const std::variant<uint32_t>& deratingFactor) {
220342cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
220442cbe538SGunnar Mills         if (ec)
220542cbe538SGunnar Mills         {
220642cbe538SGunnar Mills             BMCWEB_LOG_ERROR
220742cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
220842cbe538SGunnar Mills             messages::internalError(sensorsAsyncResp->res);
220942cbe538SGunnar Mills             return;
221042cbe538SGunnar Mills         }
221142cbe538SGunnar Mills 
221242cbe538SGunnar Mills         const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
221342cbe538SGunnar Mills         if (value != nullptr)
221442cbe538SGunnar Mills         {
221542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
221642cbe538SGunnar Mills             // Store value in Power Supply Inventory Items
221742cbe538SGunnar Mills             for (InventoryItem& inventoryItem : *inventoryItems)
221842cbe538SGunnar Mills             {
221942cbe538SGunnar Mills                 if (inventoryItem.isPowerSupply == true)
222042cbe538SGunnar Mills                 {
222142cbe538SGunnar Mills                     inventoryItem.powerSupplyEfficiencyPercent =
222242cbe538SGunnar Mills                         static_cast<int>(*value);
222342cbe538SGunnar Mills                 }
222442cbe538SGunnar Mills             }
222542cbe538SGunnar Mills         }
222642cbe538SGunnar Mills         else
222742cbe538SGunnar Mills         {
222842cbe538SGunnar Mills             BMCWEB_LOG_DEBUG
222942cbe538SGunnar Mills                 << "Failed to find EfficiencyPercent value for PowerSupplies";
223042cbe538SGunnar Mills         }
223142cbe538SGunnar Mills 
223242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
223342cbe538SGunnar Mills         callback(inventoryItems);
223442cbe538SGunnar Mills     };
223542cbe538SGunnar Mills 
223642cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
223742cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
223842cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
223942cbe538SGunnar Mills         std::move(respHandler), psAttributesConnection, psAttributesPath,
224042cbe538SGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
224142cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
224242cbe538SGunnar Mills 
224342cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
224442cbe538SGunnar Mills }
224542cbe538SGunnar Mills 
224642cbe538SGunnar Mills /**
224742cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
224842cbe538SGunnar Mills  *
224942cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
225042cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
225142cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
225242cbe538SGunnar Mills  * item.
225342cbe538SGunnar Mills  *
225442cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
225542cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
225642cbe538SGunnar Mills  *
225742cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
225842cbe538SGunnar Mills  * when information has been obtained.
225942cbe538SGunnar Mills  *
226042cbe538SGunnar Mills  * The callback must have the following signature:
226142cbe538SGunnar Mills  *   @code
226242cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
226342cbe538SGunnar Mills  *   @endcode
226442cbe538SGunnar Mills  *
226542cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
226642cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
226742cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
226842cbe538SGunnar Mills  */
226942cbe538SGunnar Mills template <typename Callback>
227042cbe538SGunnar Mills void getPowerSupplyAttributes(
227142cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
227242cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
227342cbe538SGunnar Mills     Callback&& callback)
227442cbe538SGunnar Mills {
227542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
227642cbe538SGunnar Mills 
227742cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2278a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
227942cbe538SGunnar Mills     {
228042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
228142cbe538SGunnar Mills         callback(inventoryItems);
228242cbe538SGunnar Mills         return;
228342cbe538SGunnar Mills     }
228442cbe538SGunnar Mills 
228542cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
228642cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
228742cbe538SGunnar Mills 
228842cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
228942cbe538SGunnar Mills     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
229042cbe538SGunnar Mills                         inventoryItems](const boost::system::error_code ec,
229142cbe538SGunnar Mills                                         const GetSubTreeType& subtree) {
229242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
229342cbe538SGunnar Mills         if (ec)
229442cbe538SGunnar Mills         {
229542cbe538SGunnar Mills             messages::internalError(sensorsAsyncResp->res);
229642cbe538SGunnar Mills             BMCWEB_LOG_ERROR
229742cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
229842cbe538SGunnar Mills             return;
229942cbe538SGunnar Mills         }
230042cbe538SGunnar Mills         if (subtree.size() == 0)
230142cbe538SGunnar Mills         {
230242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
230342cbe538SGunnar Mills             callback(inventoryItems);
230442cbe538SGunnar Mills             return;
230542cbe538SGunnar Mills         }
230642cbe538SGunnar Mills 
230742cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
230842cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
230942cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
231042cbe538SGunnar Mills         boost::container::flat_map<std::string, std::string>
231142cbe538SGunnar Mills             psAttributesConnections;
231242cbe538SGunnar Mills 
231342cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
231442cbe538SGunnar Mills         {
231542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
231642cbe538SGunnar Mills             callback(inventoryItems);
231742cbe538SGunnar Mills             return;
231842cbe538SGunnar Mills         }
231942cbe538SGunnar Mills 
232042cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
232142cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
232242cbe538SGunnar Mills 
232342cbe538SGunnar Mills         if (connection.empty())
232442cbe538SGunnar Mills         {
232542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
232642cbe538SGunnar Mills             callback(inventoryItems);
232742cbe538SGunnar Mills             return;
232842cbe538SGunnar Mills         }
232942cbe538SGunnar Mills 
233042cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
233142cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
233242cbe538SGunnar Mills                          << connection;
233342cbe538SGunnar Mills 
233442cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
233542cbe538SGunnar Mills                                      psAttributesConnections,
233642cbe538SGunnar Mills                                      std::move(callback));
233742cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
233842cbe538SGunnar Mills     };
233942cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
234042cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
234142cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
234242cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
234342cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
234442cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
234542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
234642cbe538SGunnar Mills }
234742cbe538SGunnar Mills 
234842cbe538SGunnar Mills /**
2349adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
23508fb49dd6SShawn McCarney  *
23518fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2352adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
23538fb49dd6SShawn McCarney  *
2354adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2355adc4f0dbSShawn McCarney  * response.
23568fb49dd6SShawn McCarney  *
2357adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2358adc4f0dbSShawn McCarney  * inventory items have been obtained.
2359adc4f0dbSShawn McCarney  *
2360adc4f0dbSShawn McCarney  * The callback must have the following signature:
2361adc4f0dbSShawn McCarney  *   @code
2362adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2363adc4f0dbSShawn McCarney  *   @endcode
23648fb49dd6SShawn McCarney  *
23658fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23668fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23678fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23688fb49dd6SShawn McCarney  * implements ObjectManager.
2369adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23708fb49dd6SShawn McCarney  */
2371adc4f0dbSShawn McCarney template <typename Callback>
2372adc4f0dbSShawn McCarney static void getInventoryItems(
23738fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
23748fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
23758fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2376adc4f0dbSShawn McCarney         objectMgrPaths,
2377adc4f0dbSShawn McCarney     Callback&& callback)
23788fb49dd6SShawn McCarney {
2379adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2380adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2381adc4f0dbSShawn McCarney         [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2382adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2383adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23848fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2385adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2386adc4f0dbSShawn McCarney                  callback{std::move(callback)}](
23878fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
23888fb49dd6SShawn McCarney                         invConnections) {
23898fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2390d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2391d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2392d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2393d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
239442cbe538SGunnar Mills 
239542cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
239642cbe538SGunnar Mills                                                        inventoryItems,
239742cbe538SGunnar Mills                                                        callback{std::move(
239842cbe538SGunnar Mills                                                            callback)}]() {
239942cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
240042cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
240142cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
240242cbe538SGunnar Mills                                                          inventoryItems,
240342cbe538SGunnar Mills                                                          std::move(callback));
240442cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
240542cbe538SGunnar Mills                             };
240642cbe538SGunnar Mills 
2407d500549bSAnthony Wilson                             // Find led connections and get the data
2408d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
240942cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2410d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2411d500549bSAnthony Wilson                         };
24128fb49dd6SShawn McCarney 
2413adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2414adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2415adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2416d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
24178fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
24188fb49dd6SShawn McCarney                 };
24198fb49dd6SShawn McCarney 
2420adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
24218fb49dd6SShawn McCarney             getInventoryItemsConnections(
2422adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
24238fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2424adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
24258fb49dd6SShawn McCarney         };
24268fb49dd6SShawn McCarney 
2427adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2428adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2429adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2430adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2431adc4f0dbSShawn McCarney }
2432adc4f0dbSShawn McCarney 
2433adc4f0dbSShawn McCarney /**
2434adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2435adc4f0dbSShawn McCarney  *
2436adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2437adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2438adc4f0dbSShawn McCarney  * array.
2439adc4f0dbSShawn McCarney  *
2440adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2441adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2442adc4f0dbSShawn McCarney  * object.
2443adc4f0dbSShawn McCarney  *
2444adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2445adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2446adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2447adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2448adc4f0dbSShawn McCarney  */
244923a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2450adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2451adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2452adc4f0dbSShawn McCarney {
2453adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2454adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2455adc4f0dbSShawn McCarney     {
2456adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2457adc4f0dbSShawn McCarney         {
2458adc4f0dbSShawn McCarney             return powerSupply;
2459adc4f0dbSShawn McCarney         }
2460adc4f0dbSShawn McCarney     }
2461adc4f0dbSShawn McCarney 
2462adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2463adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2464adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2465adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2466adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2467adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2468adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2469adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2470adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2471adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2472adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2473d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2474adc4f0dbSShawn McCarney 
247542cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
247642cbe538SGunnar Mills     {
247742cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
247842cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
247942cbe538SGunnar Mills     }
248042cbe538SGunnar Mills 
248142cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2482adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2483adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2484adc4f0dbSShawn McCarney 
2485adc4f0dbSShawn McCarney     return powerSupply;
24868fb49dd6SShawn McCarney }
24878fb49dd6SShawn McCarney 
24888fb49dd6SShawn McCarney /**
2489de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2490de629b6eSShawn McCarney  *
2491de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2492de629b6eSShawn McCarney  *
2493de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2494de629b6eSShawn McCarney  * information has been obtained.
2495de629b6eSShawn McCarney  *
2496adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2497de629b6eSShawn McCarney  *
2498de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2499de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2500de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2501de629b6eSShawn McCarney  *
2502de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2503de629b6eSShawn McCarney  *
2504de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2505de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2506de629b6eSShawn McCarney  *
2507adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2508adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2509adc4f0dbSShawn McCarney  *
2510de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2511adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2512de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2513de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2514de629b6eSShawn McCarney  * implements ObjectManager.
2515adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2516de629b6eSShawn McCarney  */
251723a21a1cSEd Tanous inline void getSensorData(
251881ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2519b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
2520de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
2521b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
2522adc4f0dbSShawn McCarney         objectMgrPaths,
2523b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2524de629b6eSShawn McCarney {
2525de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2526de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2527de629b6eSShawn McCarney     for (const std::string& connection : connections)
2528de629b6eSShawn McCarney     {
2529de629b6eSShawn McCarney         // Response handler to process managed objects
253081ce609eSEd Tanous         auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
2531adc4f0dbSShawn McCarney                                     inventoryItems](
2532de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2533de629b6eSShawn McCarney                                        ManagedObjectsVectorType& resp) {
2534de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2535de629b6eSShawn McCarney             if (ec)
2536de629b6eSShawn McCarney             {
2537de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
253881ce609eSEd Tanous                 messages::internalError(sensorsAsyncResp->res);
2539de629b6eSShawn McCarney                 return;
2540de629b6eSShawn McCarney             }
2541de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2542de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2543de629b6eSShawn McCarney             {
2544de629b6eSShawn McCarney                 const std::string& objPath =
2545de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2546de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2547de629b6eSShawn McCarney                                  << objPath;
2548de629b6eSShawn McCarney 
2549de629b6eSShawn McCarney                 std::vector<std::string> split;
2550de629b6eSShawn McCarney                 // Reserve space for
2551de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2552de629b6eSShawn McCarney                 split.reserve(6);
2553de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2554de629b6eSShawn McCarney                 if (split.size() < 6)
2555de629b6eSShawn McCarney                 {
2556de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2557de629b6eSShawn McCarney                                      << objPath;
2558de629b6eSShawn McCarney                     continue;
2559de629b6eSShawn McCarney                 }
2560de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2561de629b6eSShawn McCarney                 // string at the beginning
2562de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2563de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2564de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2565de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
256649c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2567de629b6eSShawn McCarney                 {
2568de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2569de629b6eSShawn McCarney                     continue;
2570de629b6eSShawn McCarney                 }
2571de629b6eSShawn McCarney 
2572adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2573adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2574adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2575adc4f0dbSShawn McCarney 
257695a3ecadSAnthony Wilson                 const std::string& sensorSchema =
257781ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
257895a3ecadSAnthony Wilson 
257995a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
258095a3ecadSAnthony Wilson 
2581a0ec28b6SAdrian Ambrożewicz                 if (sensorSchema == sensors::node::sensors)
258295a3ecadSAnthony Wilson                 {
258381ce609eSEd Tanous                     sensorsAsyncResp->res.jsonValue["@odata.id"] =
258481ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
258581ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
258695a3ecadSAnthony Wilson                         sensorName;
258781ce609eSEd Tanous                     sensorJson = &(sensorsAsyncResp->res.jsonValue);
258895a3ecadSAnthony Wilson                 }
258995a3ecadSAnthony Wilson                 else
259095a3ecadSAnthony Wilson                 {
2591271584abSEd Tanous                     std::string fieldName;
2592de629b6eSShawn McCarney                     if (sensorType == "temperature")
2593de629b6eSShawn McCarney                     {
2594de629b6eSShawn McCarney                         fieldName = "Temperatures";
2595de629b6eSShawn McCarney                     }
2596de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2597de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2598de629b6eSShawn McCarney                     {
2599de629b6eSShawn McCarney                         fieldName = "Fans";
2600de629b6eSShawn McCarney                     }
2601de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2602de629b6eSShawn McCarney                     {
2603de629b6eSShawn McCarney                         fieldName = "Voltages";
2604de629b6eSShawn McCarney                     }
2605de629b6eSShawn McCarney                     else if (sensorType == "power")
2606de629b6eSShawn McCarney                     {
2607028f7ebcSEddie James                         if (!sensorName.compare("total_power"))
2608028f7ebcSEddie James                         {
2609028f7ebcSEddie James                             fieldName = "PowerControl";
2610028f7ebcSEddie James                         }
2611adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2612adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2613028f7ebcSEddie James                         {
2614de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2615de629b6eSShawn McCarney                         }
2616adc4f0dbSShawn McCarney                         else
2617adc4f0dbSShawn McCarney                         {
2618adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2619adc4f0dbSShawn McCarney                             continue;
2620adc4f0dbSShawn McCarney                         }
2621028f7ebcSEddie James                     }
2622de629b6eSShawn McCarney                     else
2623de629b6eSShawn McCarney                     {
2624de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2625de629b6eSShawn McCarney                                          << sensorType;
2626de629b6eSShawn McCarney                         continue;
2627de629b6eSShawn McCarney                     }
2628de629b6eSShawn McCarney 
2629de629b6eSShawn McCarney                     nlohmann::json& tempArray =
263081ce609eSEd Tanous                         sensorsAsyncResp->res.jsonValue[fieldName];
2631adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
263249c53ac9SJohnathan Mantey                     {
2633adc4f0dbSShawn McCarney                         if (tempArray.empty())
26347ab06f49SGunnar Mills                         {
263595a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
263695a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
263795a3ecadSAnthony Wilson                             // naming in power.hpp.
26387ab06f49SGunnar Mills                             tempArray.push_back(
2639adc4f0dbSShawn McCarney                                 {{"@odata.id",
2640adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
264181ce609eSEd Tanous                                       sensorsAsyncResp->chassisId + "/" +
264281ce609eSEd Tanous                                       sensorsAsyncResp->chassisSubNode + "#/" +
2643adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2644adc4f0dbSShawn McCarney                         }
2645adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2646adc4f0dbSShawn McCarney                     }
2647adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2648adc4f0dbSShawn McCarney                     {
2649adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2650adc4f0dbSShawn McCarney                         {
2651adc4f0dbSShawn McCarney                             sensorJson =
2652adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
265381ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2654adc4f0dbSShawn McCarney                         }
265549c53ac9SJohnathan Mantey                     }
265649c53ac9SJohnathan Mantey                     else
265749c53ac9SJohnathan Mantey                     {
2658de629b6eSShawn McCarney                         tempArray.push_back(
265995a3ecadSAnthony Wilson                             {{"@odata.id",
266095a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
266181ce609eSEd Tanous                                   sensorsAsyncResp->chassisId + "/" +
266281ce609eSEd Tanous                                   sensorsAsyncResp->chassisSubNode + "#/" +
266395a3ecadSAnthony Wilson                                   fieldName + "/"}});
2664adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
266549c53ac9SJohnathan Mantey                     }
266695a3ecadSAnthony Wilson                 }
2667de629b6eSShawn McCarney 
2668adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2669adc4f0dbSShawn McCarney                 {
2670a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
267181ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2672a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2673adc4f0dbSShawn McCarney                 }
2674de629b6eSShawn McCarney             }
267581ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
267649c53ac9SJohnathan Mantey             {
267781ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
267881ce609eSEd Tanous                 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
26798bd25ccdSJames Feist                 {
268081ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26818bd25ccdSJames Feist                 }
268249c53ac9SJohnathan Mantey             }
2683de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2684de629b6eSShawn McCarney         };
2685de629b6eSShawn McCarney 
2686de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2687de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26888fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2689de629b6eSShawn McCarney         const std::string& objectMgrPath =
26908fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2691de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2692de629b6eSShawn McCarney                          << objectMgrPath;
2693de629b6eSShawn McCarney 
2694de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2695de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2696de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
269723a21a1cSEd Tanous     }
2698de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2699de629b6eSShawn McCarney }
2700de629b6eSShawn McCarney 
270123a21a1cSEd Tanous inline void processSensorList(
270281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2703b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
27041abe55efSEd Tanous {
270595a3ecadSAnthony Wilson     auto getConnectionCb =
270681ce609eSEd Tanous         [sensorsAsyncResp, sensorNames](
270795a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
270855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2709de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
271081ce609eSEd Tanous                 [sensorsAsyncResp, sensorNames,
2711b5a76932SEd Tanous                  connections](const std::shared_ptr<boost::container::flat_map<
2712b5a76932SEd Tanous                                   std::string, std::string>>& objectMgrPaths) {
2713de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2714adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
271581ce609eSEd Tanous                         [sensorsAsyncResp, sensorNames, connections,
2716adc4f0dbSShawn McCarney                          objectMgrPaths](
2717f23b7296SEd Tanous                             const std::shared_ptr<std::vector<InventoryItem>>&
2718adc4f0dbSShawn McCarney                                 inventoryItems) {
2719adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
272049c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
272181ce609eSEd Tanous                             getSensorData(sensorsAsyncResp, sensorNames,
2722adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2723f23b7296SEd Tanous                                           inventoryItems);
2724adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2725adc4f0dbSShawn McCarney                         };
2726adc4f0dbSShawn McCarney 
2727adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
272881ce609eSEd Tanous                     getInventoryItems(sensorsAsyncResp, sensorNames,
2729adc4f0dbSShawn McCarney                                       objectMgrPaths,
2730adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2731adc4f0dbSShawn McCarney 
2732de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
273308777fb0SLewanczyk, Dawid                 };
2734de629b6eSShawn McCarney 
273549c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
273649c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
273781ce609eSEd Tanous             getObjectManagerPaths(sensorsAsyncResp,
2738de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
273955c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
274008777fb0SLewanczyk, Dawid         };
2741de629b6eSShawn McCarney 
2742de629b6eSShawn McCarney     // Get set of connections that provide sensor values
274381ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
274495a3ecadSAnthony Wilson }
274595a3ecadSAnthony Wilson 
274695a3ecadSAnthony Wilson /**
274795a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
274895a3ecadSAnthony Wilson  *        chassis.
274995a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
275095a3ecadSAnthony Wilson  */
2751b5a76932SEd Tanous inline void
275281ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
275395a3ecadSAnthony Wilson {
275495a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
275595a3ecadSAnthony Wilson     auto getChassisCb =
275681ce609eSEd Tanous         [sensorsAsyncResp](
2757f23b7296SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
275895a3ecadSAnthony Wilson                 sensorNames) {
275995a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
276081ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
276155c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
276208777fb0SLewanczyk, Dawid         };
276381ce609eSEd Tanous     sensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
276408777fb0SLewanczyk, Dawid 
276526f03899SShawn McCarney     // Get set of sensors in chassis
276681ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
276755c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2768271584abSEd Tanous }
276908777fb0SLewanczyk, Dawid 
2770413961deSRichard Marian Thomaiyar /**
277149c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
277249c53ac9SJohnathan Mantey  * the chassis node
277349c53ac9SJohnathan Mantey  *
277449c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
277549c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
277649c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
277749c53ac9SJohnathan Mantey  *                         repeated calls to this function
277849c53ac9SJohnathan Mantey  */
277923a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
27800a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
278149c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
278249c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
278349c53ac9SJohnathan Mantey {
278428aa8de5SGeorge Liu     for (auto& chassisSensor : sensorsList)
278549c53ac9SJohnathan Mantey     {
278628aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2787b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
278828aa8de5SGeorge Liu         if (thisSensorName.empty())
278949c53ac9SJohnathan Mantey         {
279049c53ac9SJohnathan Mantey             continue;
279149c53ac9SJohnathan Mantey         }
279249c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
279349c53ac9SJohnathan Mantey         {
279449c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
279549c53ac9SJohnathan Mantey             return true;
279649c53ac9SJohnathan Mantey         }
279749c53ac9SJohnathan Mantey     }
279849c53ac9SJohnathan Mantey     return false;
279949c53ac9SJohnathan Mantey }
280049c53ac9SJohnathan Mantey 
280149c53ac9SJohnathan Mantey /**
2802413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2803413961deSRichard Marian Thomaiyar  *
2804413961deSRichard Marian Thomaiyar  * @param res   response object
28054bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2806413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2807413961deSRichard Marian Thomaiyar  */
280823a21a1cSEd Tanous inline void setSensorsOverride(
2809b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
28104bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2811397fd61fSjayaprakash Mutyala         allCollections)
2812413961deSRichard Marian Thomaiyar {
281370d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
28144bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2815413961deSRichard Marian Thomaiyar 
2816f65af9e8SRichard Marian Thomaiyar     const char* propertyValueName;
2817f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2818413961deSRichard Marian Thomaiyar     std::string memberId;
2819413961deSRichard Marian Thomaiyar     double value;
2820f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2821f65af9e8SRichard Marian Thomaiyar     {
2822f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2823f65af9e8SRichard Marian Thomaiyar         {
2824f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2825f65af9e8SRichard Marian Thomaiyar         }
2826f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2827f65af9e8SRichard Marian Thomaiyar         {
2828f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2829f65af9e8SRichard Marian Thomaiyar         }
2830f65af9e8SRichard Marian Thomaiyar         else
2831f65af9e8SRichard Marian Thomaiyar         {
2832f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2833f65af9e8SRichard Marian Thomaiyar         }
2834f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2835f65af9e8SRichard Marian Thomaiyar         {
28364bb3dc34SCarol Wang             if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId",
28374bb3dc34SCarol Wang                                      memberId, propertyValueName, value))
2838413961deSRichard Marian Thomaiyar             {
2839413961deSRichard Marian Thomaiyar                 return;
2840413961deSRichard Marian Thomaiyar             }
2841f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2842f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2843f65af9e8SRichard Marian Thomaiyar         }
2844f65af9e8SRichard Marian Thomaiyar     }
28454bb3dc34SCarol Wang 
2846b5a76932SEd Tanous     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2847b5a76932SEd Tanous                                       const std::shared_ptr<
284849c53ac9SJohnathan Mantey                                           boost::container::flat_set<
2849b5a76932SEd Tanous                                               std::string>>& sensorsList) {
285049c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
285149c53ac9SJohnathan Mantey         // chassis node
285249c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
285349c53ac9SJohnathan Mantey             sensorNames =
285449c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2855f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2856413961deSRichard Marian Thomaiyar         {
2857f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
285849c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
285949c53ac9SJohnathan Mantey                                                *sensorNames))
2860f65af9e8SRichard Marian Thomaiyar             {
2861f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
2862413961deSRichard Marian Thomaiyar                 messages::resourceNotFound(sensorAsyncResp->res,
2863f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2864413961deSRichard Marian Thomaiyar                 return;
2865413961deSRichard Marian Thomaiyar             }
2866f65af9e8SRichard Marian Thomaiyar         }
2867413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2868413961deSRichard Marian Thomaiyar         auto getObjectsWithConnectionCb =
2869f65af9e8SRichard Marian Thomaiyar             [sensorAsyncResp, overrideMap](
2870cb13a392SEd Tanous                 const boost::container::flat_set<std::string>& /*connections*/,
2871413961deSRichard Marian Thomaiyar                 const std::set<std::pair<std::string, std::string>>&
2872413961deSRichard Marian Thomaiyar                     objectsWithConnection) {
2873f65af9e8SRichard Marian Thomaiyar                 if (objectsWithConnection.size() != overrideMap.size())
2874413961deSRichard Marian Thomaiyar                 {
2875413961deSRichard Marian Thomaiyar                     BMCWEB_LOG_INFO
2876f65af9e8SRichard Marian Thomaiyar                         << "Unable to find all objects with proper connection "
2877f65af9e8SRichard Marian Thomaiyar                         << objectsWithConnection.size() << " requested "
2878f65af9e8SRichard Marian Thomaiyar                         << overrideMap.size() << "\n";
2879413961deSRichard Marian Thomaiyar                     messages::resourceNotFound(
2880413961deSRichard Marian Thomaiyar                         sensorAsyncResp->res,
2881a0ec28b6SAdrian Ambrożewicz                         sensorAsyncResp->chassisSubNode ==
2882a0ec28b6SAdrian Ambrożewicz                                 sensors::node::thermal
2883413961deSRichard Marian Thomaiyar                             ? "Temperatures"
2884413961deSRichard Marian Thomaiyar                             : "Voltages",
2885f65af9e8SRichard Marian Thomaiyar                         "Count");
2886f65af9e8SRichard Marian Thomaiyar                     return;
2887f65af9e8SRichard Marian Thomaiyar                 }
2888f65af9e8SRichard Marian Thomaiyar                 for (const auto& item : objectsWithConnection)
2889f65af9e8SRichard Marian Thomaiyar                 {
289028aa8de5SGeorge Liu                     sdbusplus::message::object_path path(item.first);
289128aa8de5SGeorge Liu                     std::string sensorName = path.filename();
289228aa8de5SGeorge Liu                     if (sensorName.empty())
2893f65af9e8SRichard Marian Thomaiyar                     {
2894f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2895f65af9e8SRichard Marian Thomaiyar                         return;
2896f65af9e8SRichard Marian Thomaiyar                     }
2897f65af9e8SRichard Marian Thomaiyar 
2898f65af9e8SRichard Marian Thomaiyar                     const auto& iterator = overrideMap.find(sensorName);
2899f65af9e8SRichard Marian Thomaiyar                     if (iterator == overrideMap.end())
2900f65af9e8SRichard Marian Thomaiyar                     {
2901f65af9e8SRichard Marian Thomaiyar                         BMCWEB_LOG_INFO << "Unable to find sensor object"
2902f65af9e8SRichard Marian Thomaiyar                                         << item.first << "\n";
2903f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2904413961deSRichard Marian Thomaiyar                         return;
2905413961deSRichard Marian Thomaiyar                     }
2906413961deSRichard Marian Thomaiyar                     crow::connections::systemBus->async_method_call(
2907f65af9e8SRichard Marian Thomaiyar                         [sensorAsyncResp](const boost::system::error_code ec) {
2908413961deSRichard Marian Thomaiyar                             if (ec)
2909413961deSRichard Marian Thomaiyar                             {
2910413961deSRichard Marian Thomaiyar                                 BMCWEB_LOG_DEBUG
2911f65af9e8SRichard Marian Thomaiyar                                     << "setOverrideValueStatus DBUS error: "
2912413961deSRichard Marian Thomaiyar                                     << ec;
2913413961deSRichard Marian Thomaiyar                                 messages::internalError(sensorAsyncResp->res);
2914413961deSRichard Marian Thomaiyar                                 return;
2915413961deSRichard Marian Thomaiyar                             }
2916413961deSRichard Marian Thomaiyar                         },
2917f65af9e8SRichard Marian Thomaiyar                         item.second, item.first,
2918413961deSRichard Marian Thomaiyar                         "org.freedesktop.DBus.Properties", "Set",
2919413961deSRichard Marian Thomaiyar                         "xyz.openbmc_project.Sensor.Value", "Value",
292019bd78d9SPatrick Williams                         std::variant<double>(iterator->second.first));
2921f65af9e8SRichard Marian Thomaiyar                 }
2922413961deSRichard Marian Thomaiyar             };
2923413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2924413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2925413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2926413961deSRichard Marian Thomaiyar     };
2927413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2928413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2929413961deSRichard Marian Thomaiyar }
2930413961deSRichard Marian Thomaiyar 
293123a21a1cSEd Tanous inline bool isOverridingAllowed(const std::string& manufacturingModeStatus)
293270d1d0aaSjayaprakash Mutyala {
293370d1d0aaSjayaprakash Mutyala     if (manufacturingModeStatus ==
293470d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.Control.Security.SpecialMode.Modes.Manufacturing")
293570d1d0aaSjayaprakash Mutyala     {
293670d1d0aaSjayaprakash Mutyala         return true;
293770d1d0aaSjayaprakash Mutyala     }
293870d1d0aaSjayaprakash Mutyala 
293970d1d0aaSjayaprakash Mutyala #ifdef BMCWEB_ENABLE_VALIDATION_UNSECURE_FEATURE
294070d1d0aaSjayaprakash Mutyala     if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
294170d1d0aaSjayaprakash Mutyala                                    "SpecialMode.Modes.ValidationUnsecure")
294270d1d0aaSjayaprakash Mutyala     {
294370d1d0aaSjayaprakash Mutyala         return true;
294470d1d0aaSjayaprakash Mutyala     }
294570d1d0aaSjayaprakash Mutyala 
294670d1d0aaSjayaprakash Mutyala #endif
294770d1d0aaSjayaprakash Mutyala 
294870d1d0aaSjayaprakash Mutyala     return false;
294970d1d0aaSjayaprakash Mutyala }
295070d1d0aaSjayaprakash Mutyala 
295170d1d0aaSjayaprakash Mutyala /**
295270d1d0aaSjayaprakash Mutyala  * @brief Entry point for Checking the manufacturing mode before doing sensor
295370d1d0aaSjayaprakash Mutyala  * override values of given sensor
295470d1d0aaSjayaprakash Mutyala  *
295570d1d0aaSjayaprakash Mutyala  * @param res   response object
295670d1d0aaSjayaprakash Mutyala  * @param allCollections   Collections extract from sensors' request patch info
295770d1d0aaSjayaprakash Mutyala  * @param chassisSubNode   Chassis Node for which the query has to happen
295870d1d0aaSjayaprakash Mutyala  */
295923a21a1cSEd Tanous inline void checkAndDoSensorsOverride(
2960b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
2961397fd61fSjayaprakash Mutyala     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2962397fd61fSjayaprakash Mutyala         allCollections)
296370d1d0aaSjayaprakash Mutyala {
296470d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "checkAndDoSensorsOverride for subnode"
296570d1d0aaSjayaprakash Mutyala                     << sensorAsyncResp->chassisSubNode << "\n";
296670d1d0aaSjayaprakash Mutyala 
296770d1d0aaSjayaprakash Mutyala     const std::array<std::string, 1> interfaces = {
296870d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.Security.SpecialMode"};
296970d1d0aaSjayaprakash Mutyala 
297070d1d0aaSjayaprakash Mutyala     crow::connections::systemBus->async_method_call(
297123a21a1cSEd Tanous         [sensorAsyncResp, allCollections](const boost::system::error_code ec2,
297270d1d0aaSjayaprakash Mutyala                                           const GetSubTreeType& resp) mutable {
297323a21a1cSEd Tanous             if (ec2)
297470d1d0aaSjayaprakash Mutyala             {
297570d1d0aaSjayaprakash Mutyala                 BMCWEB_LOG_DEBUG
297670d1d0aaSjayaprakash Mutyala                     << "Error in querying GetSubTree with Object Mapper. "
297723a21a1cSEd Tanous                     << ec2;
297870d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
297970d1d0aaSjayaprakash Mutyala                 return;
298070d1d0aaSjayaprakash Mutyala             }
298191e130a3Sjayaprakash Mutyala #ifdef BMCWEB_INSECURE_UNRESTRICTED_SENSOR_OVERRIDE
298291e130a3Sjayaprakash Mutyala             // Proceed with sensor override
2983397fd61fSjayaprakash Mutyala             setSensorsOverride(sensorAsyncResp, allCollections);
298470d1d0aaSjayaprakash Mutyala             return;
298591e130a3Sjayaprakash Mutyala #endif
298670d1d0aaSjayaprakash Mutyala 
298770d1d0aaSjayaprakash Mutyala             if (resp.size() != 1)
298870d1d0aaSjayaprakash Mutyala             {
298991e130a3Sjayaprakash Mutyala                 BMCWEB_LOG_WARNING
299091e130a3Sjayaprakash Mutyala                     << "Overriding sensor value is not allowed - Internal "
299191e130a3Sjayaprakash Mutyala                        "error in querying SpecialMode property.";
299270d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
299370d1d0aaSjayaprakash Mutyala                 return;
299470d1d0aaSjayaprakash Mutyala             }
299570d1d0aaSjayaprakash Mutyala             const std::string& path = resp[0].first;
299670d1d0aaSjayaprakash Mutyala             const std::string& serviceName = resp[0].second.begin()->first;
299770d1d0aaSjayaprakash Mutyala 
299870d1d0aaSjayaprakash Mutyala             if (path.empty() || serviceName.empty())
299970d1d0aaSjayaprakash Mutyala             {
300070d1d0aaSjayaprakash Mutyala                 BMCWEB_LOG_DEBUG
300170d1d0aaSjayaprakash Mutyala                     << "Path or service name is returned as empty. ";
300270d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
300370d1d0aaSjayaprakash Mutyala                 return;
300470d1d0aaSjayaprakash Mutyala             }
300570d1d0aaSjayaprakash Mutyala 
300670d1d0aaSjayaprakash Mutyala             // Sensor override is allowed only in manufacturing mode or
300770d1d0aaSjayaprakash Mutyala             // validation unsecure mode .
300870d1d0aaSjayaprakash Mutyala             crow::connections::systemBus->async_method_call(
3009397fd61fSjayaprakash Mutyala                 [sensorAsyncResp, allCollections,
301070d1d0aaSjayaprakash Mutyala                  path](const boost::system::error_code ec,
301170d1d0aaSjayaprakash Mutyala                        std::variant<std::string>& getManufactMode) mutable {
301270d1d0aaSjayaprakash Mutyala                     if (ec)
301370d1d0aaSjayaprakash Mutyala                     {
301470d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_DEBUG
301570d1d0aaSjayaprakash Mutyala                             << "Error in querying Special mode property " << ec;
301670d1d0aaSjayaprakash Mutyala                         messages::internalError(sensorAsyncResp->res);
301770d1d0aaSjayaprakash Mutyala                         return;
301870d1d0aaSjayaprakash Mutyala                     }
301970d1d0aaSjayaprakash Mutyala 
302070d1d0aaSjayaprakash Mutyala                     const std::string* manufacturingModeStatus =
302170d1d0aaSjayaprakash Mutyala                         std::get_if<std::string>(&getManufactMode);
302270d1d0aaSjayaprakash Mutyala 
302370d1d0aaSjayaprakash Mutyala                     if (nullptr == manufacturingModeStatus)
302470d1d0aaSjayaprakash Mutyala                     {
302570d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_DEBUG << "Sensor override mode is not "
302670d1d0aaSjayaprakash Mutyala                                             "Enabled. Returning ... ";
302770d1d0aaSjayaprakash Mutyala                         messages::internalError(sensorAsyncResp->res);
302870d1d0aaSjayaprakash Mutyala                         return;
302970d1d0aaSjayaprakash Mutyala                     }
303070d1d0aaSjayaprakash Mutyala 
303170d1d0aaSjayaprakash Mutyala                     if (isOverridingAllowed(*manufacturingModeStatus))
303270d1d0aaSjayaprakash Mutyala                     {
303370d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_INFO << "Manufacturing mode is Enabled. "
303470d1d0aaSjayaprakash Mutyala                                            "Proceeding further... ";
3035397fd61fSjayaprakash Mutyala                         setSensorsOverride(sensorAsyncResp, allCollections);
303670d1d0aaSjayaprakash Mutyala                     }
303770d1d0aaSjayaprakash Mutyala                     else
303870d1d0aaSjayaprakash Mutyala                     {
303970d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_WARNING
304070d1d0aaSjayaprakash Mutyala                             << "Manufacturing mode is not Enabled...can't "
304170d1d0aaSjayaprakash Mutyala                                "Override the sensor value. ";
304270d1d0aaSjayaprakash Mutyala 
304370d1d0aaSjayaprakash Mutyala                         messages::actionNotSupported(
304470d1d0aaSjayaprakash Mutyala                             sensorAsyncResp->res,
304570d1d0aaSjayaprakash Mutyala                             "Overriding of Sensor Value for non "
304670d1d0aaSjayaprakash Mutyala                             "manufacturing mode");
304770d1d0aaSjayaprakash Mutyala                         return;
304870d1d0aaSjayaprakash Mutyala                     }
304970d1d0aaSjayaprakash Mutyala                 },
305070d1d0aaSjayaprakash Mutyala                 serviceName, path, "org.freedesktop.DBus.Properties", "Get",
305170d1d0aaSjayaprakash Mutyala                 "xyz.openbmc_project.Security.SpecialMode", "SpecialMode");
305270d1d0aaSjayaprakash Mutyala         },
305370d1d0aaSjayaprakash Mutyala 
305470d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.ObjectMapper",
305570d1d0aaSjayaprakash Mutyala         "/xyz/openbmc_project/object_mapper",
305670d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 5, interfaces);
305770d1d0aaSjayaprakash Mutyala }
305870d1d0aaSjayaprakash Mutyala 
3059a0ec28b6SAdrian Ambrożewicz /**
3060a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
3061a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
3062a0ec28b6SAdrian Ambrożewicz  *
3063a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
3064a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
3065a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
3066a0ec28b6SAdrian Ambrożewicz  *
3067a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
3068a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
3069a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
3070a0ec28b6SAdrian Ambrożewicz  */
307123a21a1cSEd Tanous inline void retrieveUriToDbusMap(const std::string& chassis,
307223a21a1cSEd Tanous                                  const std::string& node,
3073a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
3074a0ec28b6SAdrian Ambrożewicz {
3075*c2bf7f99SWludzik, Jozef     auto pathIt = sensors::dbus::paths.find(node);
3076*c2bf7f99SWludzik, Jozef     if (pathIt == sensors::dbus::paths.end())
3077a0ec28b6SAdrian Ambrożewicz     {
3078a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
3079a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
3080a0ec28b6SAdrian Ambrożewicz         return;
3081a0ec28b6SAdrian Ambrożewicz     }
3082a0ec28b6SAdrian Ambrożewicz 
3083a0ec28b6SAdrian Ambrożewicz     auto respBuffer = std::make_shared<crow::Response>();
3084a0ec28b6SAdrian Ambrożewicz     auto callback =
3085a0ec28b6SAdrian Ambrożewicz         [respBuffer, mapCompleteCb{std::move(mapComplete)}](
3086a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
3087a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
3088a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
3089a0ec28b6SAdrian Ambrożewicz 
3090a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
3091*c2bf7f99SWludzik, Jozef         *respBuffer, chassis, pathIt->second, node, std::move(callback));
3092a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
3093a0ec28b6SAdrian Ambrożewicz }
3094a0ec28b6SAdrian Ambrożewicz 
309595a3ecadSAnthony Wilson class SensorCollection : public Node
309695a3ecadSAnthony Wilson {
309795a3ecadSAnthony Wilson   public:
309852cc112dSEd Tanous     SensorCollection(App& app) :
3099f99c379dSGunnar Mills         Node(app, "/redfish/v1/Chassis/<str>/Sensors/", std::string())
310095a3ecadSAnthony Wilson     {
310195a3ecadSAnthony Wilson         entityPrivileges = {
310295a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
310395a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
310495a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
310595a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
310695a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
310795a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
310895a3ecadSAnthony Wilson     }
310995a3ecadSAnthony Wilson 
311095a3ecadSAnthony Wilson   private:
3111cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
311295a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
311395a3ecadSAnthony Wilson     {
311495a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
311595a3ecadSAnthony Wilson         if (params.size() != 1)
311695a3ecadSAnthony Wilson         {
311795a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1";
311895a3ecadSAnthony Wilson             messages::internalError(res);
311995a3ecadSAnthony Wilson             res.end();
312095a3ecadSAnthony Wilson             return;
312195a3ecadSAnthony Wilson         }
312295a3ecadSAnthony Wilson 
312395a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
312495a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
3125a0ec28b6SAdrian Ambrożewicz             std::make_shared<SensorsAsyncResp>(
3126*c2bf7f99SWludzik, Jozef                 res, chassisId, sensors::dbus::paths.at(sensors::node::sensors),
3127a0ec28b6SAdrian Ambrożewicz                 sensors::node::sensors);
312895a3ecadSAnthony Wilson 
312995a3ecadSAnthony Wilson         auto getChassisCb =
3130b5a76932SEd Tanous             [asyncResp](
3131b5a76932SEd Tanous                 const std::shared_ptr<boost::container::flat_set<std::string>>&
313295a3ecadSAnthony Wilson                     sensorNames) {
313395a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb enter";
313495a3ecadSAnthony Wilson 
313595a3ecadSAnthony Wilson                 nlohmann::json& entriesArray =
313695a3ecadSAnthony Wilson                     asyncResp->res.jsonValue["Members"];
313795a3ecadSAnthony Wilson                 for (auto& sensor : *sensorNames)
313895a3ecadSAnthony Wilson                 {
313995a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
314095a3ecadSAnthony Wilson 
314128aa8de5SGeorge Liu                     sdbusplus::message::object_path path(sensor);
314228aa8de5SGeorge Liu                     std::string sensorName = path.filename();
314328aa8de5SGeorge Liu                     if (sensorName.empty())
314495a3ecadSAnthony Wilson                     {
314595a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
314695a3ecadSAnthony Wilson                         messages::internalError(asyncResp->res);
314795a3ecadSAnthony Wilson                         return;
314895a3ecadSAnthony Wilson                     }
314995a3ecadSAnthony Wilson                     entriesArray.push_back(
315095a3ecadSAnthony Wilson                         {{"@odata.id",
315195a3ecadSAnthony Wilson                           "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
315295a3ecadSAnthony Wilson                               asyncResp->chassisSubNode + "/" + sensorName}});
315395a3ecadSAnthony Wilson                 }
315495a3ecadSAnthony Wilson 
315595a3ecadSAnthony Wilson                 asyncResp->res.jsonValue["Members@odata.count"] =
315695a3ecadSAnthony Wilson                     entriesArray.size();
315795a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb exit";
315895a3ecadSAnthony Wilson             };
315995a3ecadSAnthony Wilson 
316095a3ecadSAnthony Wilson         // Get set of sensors in chassis
316195a3ecadSAnthony Wilson         getChassis(asyncResp, std::move(getChassisCb));
316295a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
316395a3ecadSAnthony Wilson     }
316495a3ecadSAnthony Wilson };
316595a3ecadSAnthony Wilson 
316695a3ecadSAnthony Wilson class Sensor : public Node
316795a3ecadSAnthony Wilson {
316895a3ecadSAnthony Wilson   public:
316952cc112dSEd Tanous     Sensor(App& app) :
317095a3ecadSAnthony Wilson         Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(),
317195a3ecadSAnthony Wilson              std::string())
317295a3ecadSAnthony Wilson     {
317395a3ecadSAnthony Wilson         entityPrivileges = {
317495a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
317595a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
317695a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
317795a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
317895a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
317995a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
318095a3ecadSAnthony Wilson     }
318195a3ecadSAnthony Wilson 
318295a3ecadSAnthony Wilson   private:
3183cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
318495a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
318595a3ecadSAnthony Wilson     {
318695a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "Sensor doGet enter";
318795a3ecadSAnthony Wilson         if (params.size() != 2)
318895a3ecadSAnthony Wilson         {
318995a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2";
319095a3ecadSAnthony Wilson             messages::internalError(res);
319195a3ecadSAnthony Wilson             res.end();
319295a3ecadSAnthony Wilson             return;
319395a3ecadSAnthony Wilson         }
319495a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
319595a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
3196a0ec28b6SAdrian Ambrożewicz             std::make_shared<SensorsAsyncResp>(res, chassisId,
3197a0ec28b6SAdrian Ambrożewicz                                                std::vector<const char*>(),
3198a0ec28b6SAdrian Ambrożewicz                                                sensors::node::sensors);
319995a3ecadSAnthony Wilson 
320095a3ecadSAnthony Wilson         const std::string& sensorName = params[1];
320195a3ecadSAnthony Wilson         const std::array<const char*, 1> interfaces = {
320295a3ecadSAnthony Wilson             "xyz.openbmc_project.Sensor.Value"};
320395a3ecadSAnthony Wilson 
320495a3ecadSAnthony Wilson         // Get a list of all of the sensors that implement Sensor.Value
320595a3ecadSAnthony Wilson         // and get the path and service name associated with the sensor
320695a3ecadSAnthony Wilson         crow::connections::systemBus->async_method_call(
320795a3ecadSAnthony Wilson             [asyncResp, sensorName](const boost::system::error_code ec,
320895a3ecadSAnthony Wilson                                     const GetSubTreeType& subtree) {
320995a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 enter";
321095a3ecadSAnthony Wilson                 if (ec)
321195a3ecadSAnthony Wilson                 {
321295a3ecadSAnthony Wilson                     messages::internalError(asyncResp->res);
321395a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
321495a3ecadSAnthony Wilson                                      << "Dbus error " << ec;
321595a3ecadSAnthony Wilson                     return;
321695a3ecadSAnthony Wilson                 }
321795a3ecadSAnthony Wilson 
321895a3ecadSAnthony Wilson                 GetSubTreeType::const_iterator it = std::find_if(
321995a3ecadSAnthony Wilson                     subtree.begin(), subtree.end(),
322095a3ecadSAnthony Wilson                     [sensorName](
322195a3ecadSAnthony Wilson                         const std::pair<
322295a3ecadSAnthony Wilson                             std::string,
322395a3ecadSAnthony Wilson                             std::vector<std::pair<std::string,
322495a3ecadSAnthony Wilson                                                   std::vector<std::string>>>>&
322595a3ecadSAnthony Wilson                             object) {
322628aa8de5SGeorge Liu                         sdbusplus::message::object_path path(object.first);
322728aa8de5SGeorge Liu                         std::string name = path.filename();
322828aa8de5SGeorge Liu                         if (name.empty())
322995a3ecadSAnthony Wilson                         {
323095a3ecadSAnthony Wilson                             BMCWEB_LOG_ERROR << "Invalid sensor path: "
323128aa8de5SGeorge Liu                                              << object.first;
323295a3ecadSAnthony Wilson                             return false;
323395a3ecadSAnthony Wilson                         }
323495a3ecadSAnthony Wilson 
323595a3ecadSAnthony Wilson                         return name == sensorName;
323695a3ecadSAnthony Wilson                     });
323795a3ecadSAnthony Wilson 
323895a3ecadSAnthony Wilson                 if (it == subtree.end())
323995a3ecadSAnthony Wilson                 {
324095a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Could not find path for sensor: "
324195a3ecadSAnthony Wilson                                      << sensorName;
324295a3ecadSAnthony Wilson                     messages::resourceNotFound(asyncResp->res, "Sensor",
324395a3ecadSAnthony Wilson                                                sensorName);
324495a3ecadSAnthony Wilson                     return;
324595a3ecadSAnthony Wilson                 }
324695a3ecadSAnthony Wilson                 std::string_view sensorPath = (*it).first;
324795a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
324895a3ecadSAnthony Wilson                                  << sensorName << "': " << sensorPath;
324995a3ecadSAnthony Wilson 
325095a3ecadSAnthony Wilson                 const std::shared_ptr<boost::container::flat_set<std::string>>
325195a3ecadSAnthony Wilson                     sensorList = std::make_shared<
325295a3ecadSAnthony Wilson                         boost::container::flat_set<std::string>>();
325395a3ecadSAnthony Wilson 
325495a3ecadSAnthony Wilson                 sensorList->emplace(sensorPath);
325595a3ecadSAnthony Wilson                 processSensorList(asyncResp, sensorList);
325695a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 exit";
325795a3ecadSAnthony Wilson             },
325895a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper",
325995a3ecadSAnthony Wilson             "/xyz/openbmc_project/object_mapper",
326095a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
326195a3ecadSAnthony Wilson             "/xyz/openbmc_project/sensors", 2, interfaces);
326295a3ecadSAnthony Wilson     }
326395a3ecadSAnthony Wilson };
326495a3ecadSAnthony Wilson 
326508777fb0SLewanczyk, Dawid } // namespace redfish
3266