xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision d500549b209761e46791458b49ce2fee5c8b682b)
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>
249e27a22eSEd Tanous #include <cmath>
251abe55efSEd Tanous #include <dbus_singleton.hpp>
26413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
27abf2add6SEd Tanous #include <variant>
2808777fb0SLewanczyk, Dawid 
291abe55efSEd Tanous namespace redfish
301abe55efSEd Tanous {
3108777fb0SLewanczyk, Dawid 
3208777fb0SLewanczyk, Dawid using GetSubTreeType = std::vector<
3308777fb0SLewanczyk, Dawid     std::pair<std::string,
3408777fb0SLewanczyk, Dawid               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
3508777fb0SLewanczyk, Dawid 
36adc4f0dbSShawn McCarney using SensorVariant =
37adc4f0dbSShawn McCarney     std::variant<int64_t, double, uint32_t, bool, std::string>;
38aa2e59c1SEd Tanous 
3908777fb0SLewanczyk, Dawid using ManagedObjectsVectorType = std::vector<std::pair<
40aa2e59c1SEd Tanous     sdbusplus::message::object_path,
4108777fb0SLewanczyk, Dawid     boost::container::flat_map<
42aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
4308777fb0SLewanczyk, Dawid 
4408777fb0SLewanczyk, Dawid /**
45588c3f0dSKowalski, Kamil  * SensorsAsyncResp
4608777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
4708777fb0SLewanczyk, Dawid  */
481abe55efSEd Tanous class SensorsAsyncResp
491abe55efSEd Tanous {
5008777fb0SLewanczyk, Dawid   public:
51271584abSEd Tanous     SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
52271584abSEd Tanous                      const std::vector<const char*> typesIn,
532474adfaSEd Tanous                      const std::string& subNode) :
5443b761d0SEd Tanous         res(response),
55271584abSEd Tanous         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
561abe55efSEd Tanous     {
5708777fb0SLewanczyk, Dawid     }
5808777fb0SLewanczyk, Dawid 
591abe55efSEd Tanous     ~SensorsAsyncResp()
601abe55efSEd Tanous     {
611abe55efSEd Tanous         if (res.result() == boost::beast::http::status::internal_server_error)
621abe55efSEd Tanous         {
631abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
641abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
651abe55efSEd Tanous             // proper code
6655c7b7a2SEd Tanous             res.jsonValue = nlohmann::json::object();
6708777fb0SLewanczyk, Dawid         }
6808777fb0SLewanczyk, Dawid         res.end();
6908777fb0SLewanczyk, Dawid     }
70588c3f0dSKowalski, Kamil 
7155c7b7a2SEd Tanous     crow::Response& res;
72588c3f0dSKowalski, Kamil     std::string chassisId{};
7308777fb0SLewanczyk, Dawid     const std::vector<const char*> types;
742474adfaSEd Tanous     std::string chassisSubNode{};
7508777fb0SLewanczyk, Dawid };
7608777fb0SLewanczyk, Dawid 
7708777fb0SLewanczyk, Dawid /**
78*d500549bSAnthony Wilson  * Possible states for physical inventory leds
79*d500549bSAnthony Wilson  */
80*d500549bSAnthony Wilson enum class LedState
81*d500549bSAnthony Wilson {
82*d500549bSAnthony Wilson     OFF,
83*d500549bSAnthony Wilson     ON,
84*d500549bSAnthony Wilson     BLINK,
85*d500549bSAnthony Wilson     UNKNOWN
86*d500549bSAnthony Wilson };
87*d500549bSAnthony Wilson 
88*d500549bSAnthony Wilson /**
89adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
90adc4f0dbSShawn McCarney  */
91adc4f0dbSShawn McCarney class InventoryItem
92adc4f0dbSShawn McCarney {
93adc4f0dbSShawn McCarney   public:
94adc4f0dbSShawn McCarney     InventoryItem(const std::string& objPath) :
95adc4f0dbSShawn McCarney         objectPath(objPath), name(), isPresent(true), isFunctional(true),
96adc4f0dbSShawn McCarney         isPowerSupply(false), manufacturer(), model(), partNumber(),
97*d500549bSAnthony Wilson         serialNumber(), sensors(), ledObjectPath(""),
98*d500549bSAnthony Wilson         ledState(LedState::UNKNOWN)
99adc4f0dbSShawn McCarney     {
100adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
101adc4f0dbSShawn McCarney         auto pos = objectPath.rfind('/');
102adc4f0dbSShawn McCarney         if ((pos != std::string::npos) && ((pos + 1) < objectPath.size()))
103adc4f0dbSShawn McCarney         {
104adc4f0dbSShawn McCarney             name = objectPath.substr(pos + 1);
105adc4f0dbSShawn McCarney         }
106adc4f0dbSShawn McCarney     }
107adc4f0dbSShawn McCarney 
108adc4f0dbSShawn McCarney     std::string objectPath;
109adc4f0dbSShawn McCarney     std::string name;
110adc4f0dbSShawn McCarney     bool isPresent;
111adc4f0dbSShawn McCarney     bool isFunctional;
112adc4f0dbSShawn McCarney     bool isPowerSupply;
113adc4f0dbSShawn McCarney     std::string manufacturer;
114adc4f0dbSShawn McCarney     std::string model;
115adc4f0dbSShawn McCarney     std::string partNumber;
116adc4f0dbSShawn McCarney     std::string serialNumber;
117adc4f0dbSShawn McCarney     std::set<std::string> sensors;
118*d500549bSAnthony Wilson     std::string ledObjectPath;
119*d500549bSAnthony Wilson     LedState ledState;
120adc4f0dbSShawn McCarney };
121adc4f0dbSShawn McCarney 
122adc4f0dbSShawn McCarney /**
123413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
124588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
12508777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
12608777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
12708777fb0SLewanczyk, Dawid  */
12808777fb0SLewanczyk, Dawid template <typename Callback>
129413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
130413961deSRichard Marian Thomaiyar     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
13149c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
1321abe55efSEd Tanous     Callback&& callback)
1331abe55efSEd Tanous {
134413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
13503b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
13608777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
13708777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
13808777fb0SLewanczyk, Dawid 
13908777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
1401abe55efSEd Tanous     auto respHandler = [callback{std::move(callback)}, SensorsAsyncResp,
1411abe55efSEd Tanous                         sensorNames](const boost::system::error_code ec,
1421abe55efSEd Tanous                                      const GetSubTreeType& subtree) {
143413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
1441abe55efSEd Tanous         if (ec)
1451abe55efSEd Tanous         {
1465f7d88c4SEd Tanous             messages::internalError(SensorsAsyncResp->res);
147413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
148413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
14908777fb0SLewanczyk, Dawid             return;
15008777fb0SLewanczyk, Dawid         }
15108777fb0SLewanczyk, Dawid 
15255c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
15308777fb0SLewanczyk, Dawid 
15408777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
15508777fb0SLewanczyk, Dawid         // found in the chassis
15608777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
157413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
1581abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
1591abe55efSEd Tanous         // producers
16008777fb0SLewanczyk, Dawid         connections.reserve(8);
16108777fb0SLewanczyk, Dawid 
16249c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
16349c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
1641abe55efSEd Tanous         {
16555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
16608777fb0SLewanczyk, Dawid         }
16708777fb0SLewanczyk, Dawid 
16808777fb0SLewanczyk, Dawid         for (const std::pair<
16908777fb0SLewanczyk, Dawid                  std::string,
17008777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1711abe55efSEd Tanous                  object : subtree)
1721abe55efSEd Tanous         {
17349c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
1741abe55efSEd Tanous             {
17549c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
1761abe55efSEd Tanous                          objData : object.second)
1771abe55efSEd Tanous                 {
17849c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
17908777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
180de629b6eSShawn McCarney                     objectsWithConnection.insert(
181de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
18208777fb0SLewanczyk, Dawid                 }
18308777fb0SLewanczyk, Dawid             }
18408777fb0SLewanczyk, Dawid         }
18555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
186413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
187413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
18808777fb0SLewanczyk, Dawid     };
18908777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
19055c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
19155c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1921abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
1931abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
194413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
195413961deSRichard Marian Thomaiyar }
196413961deSRichard Marian Thomaiyar 
197413961deSRichard Marian Thomaiyar /**
198413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
199413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
200413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
201413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
202413961deSRichard Marian Thomaiyar  */
203413961deSRichard Marian Thomaiyar template <typename Callback>
20449c53ac9SJohnathan Mantey void getConnections(
20549c53ac9SJohnathan Mantey     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
20649c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
207413961deSRichard Marian Thomaiyar     Callback&& callback)
208413961deSRichard Marian Thomaiyar {
209413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
210413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
211413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
212413961deSRichard Marian Thomaiyar                        objectsWithConnection) {
213413961deSRichard Marian Thomaiyar             callback(std::move(connections));
214413961deSRichard Marian Thomaiyar         };
215413961deSRichard Marian Thomaiyar     getObjectsWithConnection(SensorsAsyncResp, sensorNames,
216413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
21708777fb0SLewanczyk, Dawid }
21808777fb0SLewanczyk, Dawid 
21908777fb0SLewanczyk, Dawid /**
22049c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
22149c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
22249c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
22349c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
22449c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
22549c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
22649c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
22749c53ac9SJohnathan Mantey  */
22849c53ac9SJohnathan Mantey void reduceSensorList(
22949c53ac9SJohnathan Mantey     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
23049c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
23149c53ac9SJohnathan Mantey     std::shared_ptr<boost::container::flat_set<std::string>> activeSensors)
23249c53ac9SJohnathan Mantey {
23349c53ac9SJohnathan Mantey     if (SensorsAsyncResp == nullptr)
23449c53ac9SJohnathan Mantey     {
23549c53ac9SJohnathan Mantey         return;
23649c53ac9SJohnathan Mantey     }
23749c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
23849c53ac9SJohnathan Mantey     {
23949c53ac9SJohnathan Mantey         messages::resourceNotFound(
24049c53ac9SJohnathan Mantey             SensorsAsyncResp->res, SensorsAsyncResp->chassisSubNode,
24149c53ac9SJohnathan Mantey             SensorsAsyncResp->chassisSubNode == "Thermal" ? "Temperatures"
24249c53ac9SJohnathan Mantey                                                           : "Voltages");
24349c53ac9SJohnathan Mantey 
24449c53ac9SJohnathan Mantey         return;
24549c53ac9SJohnathan Mantey     }
24649c53ac9SJohnathan Mantey     if (allSensors->empty())
24749c53ac9SJohnathan Mantey     {
24849c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
24949c53ac9SJohnathan Mantey         return;
25049c53ac9SJohnathan Mantey     }
25149c53ac9SJohnathan Mantey 
25249c53ac9SJohnathan Mantey     for (const char* type : SensorsAsyncResp->types)
25349c53ac9SJohnathan Mantey     {
25449c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
25549c53ac9SJohnathan Mantey         {
25649c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
25749c53ac9SJohnathan Mantey             {
25849c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
25949c53ac9SJohnathan Mantey             }
26049c53ac9SJohnathan Mantey         }
26149c53ac9SJohnathan Mantey     }
26249c53ac9SJohnathan Mantey }
26349c53ac9SJohnathan Mantey 
26449c53ac9SJohnathan Mantey /**
26508777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
266588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
26708777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
26808777fb0SLewanczyk, Dawid  */
26908777fb0SLewanczyk, Dawid template <typename Callback>
27049c53ac9SJohnathan Mantey void getChassis(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2711abe55efSEd Tanous                 Callback&& callback)
2721abe55efSEd Tanous {
27355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
274adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
27549c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
276adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
27749c53ac9SJohnathan Mantey     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
27849c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
27949c53ac9SJohnathan Mantey                            const std::vector<std::string>& chassisPaths) {
28055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
2811abe55efSEd Tanous         if (ec)
2821abe55efSEd Tanous         {
28355c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
28449c53ac9SJohnathan Mantey             messages::internalError(sensorsAsyncResp->res);
28508777fb0SLewanczyk, Dawid             return;
28608777fb0SLewanczyk, Dawid         }
28708777fb0SLewanczyk, Dawid 
28849c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
28949c53ac9SJohnathan Mantey         std::string chassisName;
29049c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
2911abe55efSEd Tanous         {
29249c53ac9SJohnathan Mantey             std::size_t lastPos = chassis.rfind("/");
29349c53ac9SJohnathan Mantey             if (lastPos == std::string::npos)
2941abe55efSEd Tanous             {
29549c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
296daf36e2eSEd Tanous                 continue;
297daf36e2eSEd Tanous             }
29849c53ac9SJohnathan Mantey             chassisName = chassis.substr(lastPos + 1);
29949c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
3001abe55efSEd Tanous             {
30149c53ac9SJohnathan Mantey                 chassisPath = &chassis;
30249c53ac9SJohnathan Mantey                 break;
303daf36e2eSEd Tanous             }
30449c53ac9SJohnathan Mantey         }
30549c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
3061abe55efSEd Tanous         {
30749c53ac9SJohnathan Mantey             messages::resourceNotFound(sensorsAsyncResp->res, "Chassis",
30849c53ac9SJohnathan Mantey                                        sensorsAsyncResp->chassisId);
30949c53ac9SJohnathan Mantey             return;
3101abe55efSEd Tanous         }
31108777fb0SLewanczyk, Dawid 
31249c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
31349c53ac9SJohnathan Mantey         if (chassisSubNode == "Power")
31449c53ac9SJohnathan Mantey         {
31549c53ac9SJohnathan Mantey             sensorsAsyncResp->res.jsonValue["@odata.type"] =
31649c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
31749c53ac9SJohnathan Mantey         }
31849c53ac9SJohnathan Mantey         else if (chassisSubNode == "Thermal")
31949c53ac9SJohnathan Mantey         {
32049c53ac9SJohnathan Mantey             sensorsAsyncResp->res.jsonValue["@odata.type"] =
32149c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
3224f9a2130SJennifer Lee             sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array();
3234f9a2130SJennifer Lee             sensorsAsyncResp->res.jsonValue["Temperatures"] =
3244f9a2130SJennifer Lee                 nlohmann::json::array();
32549c53ac9SJohnathan Mantey         }
32695a3ecadSAnthony Wilson         else if (chassisSubNode == "Sensors")
32795a3ecadSAnthony Wilson         {
32895a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["@odata.type"] =
32995a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
33095a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["@odata.context"] =
33195a3ecadSAnthony Wilson                 "/redfish/v1/$metadata#SensorCollection.SensorCollection";
33295a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Description"] =
33395a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
33495a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members"] =
33595a3ecadSAnthony Wilson                 nlohmann::json::array();
33695a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0;
33795a3ecadSAnthony Wilson         }
33895a3ecadSAnthony Wilson 
33995a3ecadSAnthony Wilson         if (chassisSubNode != "Sensors")
34095a3ecadSAnthony Wilson         {
34195a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode;
34295a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["@odata.context"] =
34395a3ecadSAnthony Wilson                 "/redfish/v1/$metadata#" + chassisSubNode + "." +
34495a3ecadSAnthony Wilson                 chassisSubNode;
34595a3ecadSAnthony Wilson         }
34695a3ecadSAnthony Wilson 
34749c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["@odata.id"] =
34849c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
34949c53ac9SJohnathan Mantey             chassisSubNode;
35049c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode;
35149c53ac9SJohnathan Mantey 
3528fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
3538fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
35455c7b7a2SEd Tanous         crow::connections::systemBus->async_method_call(
35549c53ac9SJohnathan Mantey             [sensorsAsyncResp, callback{std::move(callback)}](
356271584abSEd Tanous                 const boost::system::error_code& e,
35749c53ac9SJohnathan Mantey                 const std::variant<std::vector<std::string>>&
35849c53ac9SJohnathan Mantey                     variantEndpoints) {
359271584abSEd Tanous                 if (e)
36049c53ac9SJohnathan Mantey                 {
361271584abSEd Tanous                     if (e.value() != EBADR)
36249c53ac9SJohnathan Mantey                     {
36349c53ac9SJohnathan Mantey                         messages::internalError(sensorsAsyncResp->res);
36449c53ac9SJohnathan Mantey                         return;
36549c53ac9SJohnathan Mantey                     }
36649c53ac9SJohnathan Mantey                 }
36749c53ac9SJohnathan Mantey                 const std::vector<std::string>* nodeSensorList =
36849c53ac9SJohnathan Mantey                     std::get_if<std::vector<std::string>>(&(variantEndpoints));
36949c53ac9SJohnathan Mantey                 if (nodeSensorList == nullptr)
37049c53ac9SJohnathan Mantey                 {
37149c53ac9SJohnathan Mantey                     messages::resourceNotFound(
37249c53ac9SJohnathan Mantey                         sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
37349c53ac9SJohnathan Mantey                         sensorsAsyncResp->chassisSubNode == "Thermal"
37449c53ac9SJohnathan Mantey                             ? "Temperatures"
37595a3ecadSAnthony Wilson                             : sensorsAsyncResp->chassisSubNode == "Power"
37695a3ecadSAnthony Wilson                                   ? "Voltages"
37795a3ecadSAnthony Wilson                                   : "Sensors");
37849c53ac9SJohnathan Mantey                     return;
37949c53ac9SJohnathan Mantey                 }
38049c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
38149c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
38249c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
38349c53ac9SJohnathan Mantey                 reduceSensorList(sensorsAsyncResp, nodeSensorList,
38449c53ac9SJohnathan Mantey                                  culledSensorList);
38549c53ac9SJohnathan Mantey                 callback(culledSensorList);
38649c53ac9SJohnathan Mantey             },
38749c53ac9SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", sensorPath,
38849c53ac9SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Get",
38949c53ac9SJohnathan Mantey             "xyz.openbmc_project.Association", "endpoints");
39049c53ac9SJohnathan Mantey     };
39149c53ac9SJohnathan Mantey 
39249c53ac9SJohnathan Mantey     // Get the Chassis Collection
39349c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
39449c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
39549c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
39649c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
397271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
39855c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
39908777fb0SLewanczyk, Dawid }
40008777fb0SLewanczyk, Dawid 
40108777fb0SLewanczyk, Dawid /**
402de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
403de629b6eSShawn McCarney  *
404de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
405de629b6eSShawn McCarney  *
406de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
407de629b6eSShawn McCarney  * been obtained.
408de629b6eSShawn McCarney  *
409de629b6eSShawn McCarney  * The callback must have the following signature:
410de629b6eSShawn McCarney  *   @code
4118fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
4128fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
413de629b6eSShawn McCarney  *   @endcode
414de629b6eSShawn McCarney  *
41549c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
416de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
417de629b6eSShawn McCarney  */
418de629b6eSShawn McCarney template <typename Callback>
419de629b6eSShawn McCarney void getObjectManagerPaths(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
420de629b6eSShawn McCarney                            Callback&& callback)
421de629b6eSShawn McCarney {
422de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
423de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
424de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
425de629b6eSShawn McCarney 
426de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
427de629b6eSShawn McCarney     auto respHandler = [callback{std::move(callback)},
428de629b6eSShawn McCarney                         SensorsAsyncResp](const boost::system::error_code ec,
429de629b6eSShawn McCarney                                           const GetSubTreeType& subtree) {
430de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
431de629b6eSShawn McCarney         if (ec)
432de629b6eSShawn McCarney         {
433de629b6eSShawn McCarney             messages::internalError(SensorsAsyncResp->res);
434de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
435de629b6eSShawn McCarney                              << ec;
436de629b6eSShawn McCarney             return;
437de629b6eSShawn McCarney         }
438de629b6eSShawn McCarney 
439de629b6eSShawn McCarney         // Loop over returned object paths
4408fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
4418fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
4428fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
443de629b6eSShawn McCarney         for (const std::pair<
444de629b6eSShawn McCarney                  std::string,
445de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
446de629b6eSShawn McCarney                  object : subtree)
447de629b6eSShawn McCarney         {
448de629b6eSShawn McCarney             // Loop over connections for current object path
449de629b6eSShawn McCarney             const std::string& objectPath = object.first;
450de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
451de629b6eSShawn McCarney                      objData : object.second)
452de629b6eSShawn McCarney             {
453de629b6eSShawn McCarney                 // Add mapping from connection to object path
454de629b6eSShawn McCarney                 const std::string& connection = objData.first;
4558fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
456de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
457de629b6eSShawn McCarney                                  << objectPath;
458de629b6eSShawn McCarney             }
459de629b6eSShawn McCarney         }
4608fb49dd6SShawn McCarney         callback(objectMgrPaths);
461de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
462de629b6eSShawn McCarney     };
463de629b6eSShawn McCarney 
464de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
465de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
466de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
467de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
468271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
469de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
470de629b6eSShawn McCarney }
471de629b6eSShawn McCarney 
472de629b6eSShawn McCarney /**
473adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
474adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
475adc4f0dbSShawn McCarney  * @return State value for inventory item.
47634dd179eSJames Feist  */
477adc4f0dbSShawn McCarney static std::string getState(const InventoryItem* inventoryItem)
478adc4f0dbSShawn McCarney {
479adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
480adc4f0dbSShawn McCarney     {
481adc4f0dbSShawn McCarney         return "Absent";
482adc4f0dbSShawn McCarney     }
48334dd179eSJames Feist 
484adc4f0dbSShawn McCarney     return "Enabled";
485adc4f0dbSShawn McCarney }
486adc4f0dbSShawn McCarney 
487adc4f0dbSShawn McCarney /**
488adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
489adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
490adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
491adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
492adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
493adc4f0dbSShawn McCarney  * @return Health value for sensor.
494adc4f0dbSShawn McCarney  */
49534dd179eSJames Feist static std::string getHealth(
496adc4f0dbSShawn McCarney     nlohmann::json& sensorJson,
49734dd179eSJames Feist     const boost::container::flat_map<
49834dd179eSJames Feist         std::string, boost::container::flat_map<std::string, SensorVariant>>&
499adc4f0dbSShawn McCarney         interfacesDict,
500adc4f0dbSShawn McCarney     const InventoryItem* inventoryItem)
50134dd179eSJames Feist {
502adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
503adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
504adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
505adc4f0dbSShawn McCarney     std::string currentHealth;
506adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
507adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
508adc4f0dbSShawn McCarney     {
509adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
510adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
511adc4f0dbSShawn McCarney         {
512adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
513adc4f0dbSShawn McCarney             if (health != nullptr)
514adc4f0dbSShawn McCarney             {
515adc4f0dbSShawn McCarney                 currentHealth = *health;
516adc4f0dbSShawn McCarney             }
517adc4f0dbSShawn McCarney         }
518adc4f0dbSShawn McCarney     }
519adc4f0dbSShawn McCarney 
520adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
521adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
522adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
523adc4f0dbSShawn McCarney     {
524adc4f0dbSShawn McCarney         return "Critical";
525adc4f0dbSShawn McCarney     }
526adc4f0dbSShawn McCarney 
527adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
52834dd179eSJames Feist     auto criticalThresholdIt =
52934dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
53034dd179eSJames Feist     if (criticalThresholdIt != interfacesDict.end())
53134dd179eSJames Feist     {
53234dd179eSJames Feist         auto thresholdHighIt =
53334dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmHigh");
53434dd179eSJames Feist         auto thresholdLowIt =
53534dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmLow");
53634dd179eSJames Feist         if (thresholdHighIt != criticalThresholdIt->second.end())
53734dd179eSJames Feist         {
53834dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
53934dd179eSJames Feist             if (asserted == nullptr)
54034dd179eSJames Feist             {
54134dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
54234dd179eSJames Feist             }
54334dd179eSJames Feist             else if (*asserted)
54434dd179eSJames Feist             {
54534dd179eSJames Feist                 return "Critical";
54634dd179eSJames Feist             }
54734dd179eSJames Feist         }
54834dd179eSJames Feist         if (thresholdLowIt != criticalThresholdIt->second.end())
54934dd179eSJames Feist         {
55034dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
55134dd179eSJames Feist             if (asserted == nullptr)
55234dd179eSJames Feist             {
55334dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
55434dd179eSJames Feist             }
55534dd179eSJames Feist             else if (*asserted)
55634dd179eSJames Feist             {
55734dd179eSJames Feist                 return "Critical";
55834dd179eSJames Feist             }
55934dd179eSJames Feist         }
56034dd179eSJames Feist     }
56134dd179eSJames Feist 
562adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
563adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
564adc4f0dbSShawn McCarney     {
565adc4f0dbSShawn McCarney         return "Critical";
566adc4f0dbSShawn McCarney     }
567adc4f0dbSShawn McCarney 
568adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
569adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
570adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
571adc4f0dbSShawn McCarney     {
572adc4f0dbSShawn McCarney         return "Warning";
573adc4f0dbSShawn McCarney     }
574adc4f0dbSShawn McCarney 
575adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
57634dd179eSJames Feist     auto warningThresholdIt =
57734dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
57834dd179eSJames Feist     if (warningThresholdIt != interfacesDict.end())
57934dd179eSJames Feist     {
58034dd179eSJames Feist         auto thresholdHighIt =
58134dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmHigh");
58234dd179eSJames Feist         auto thresholdLowIt =
58334dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmLow");
58434dd179eSJames Feist         if (thresholdHighIt != warningThresholdIt->second.end())
58534dd179eSJames Feist         {
58634dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
58734dd179eSJames Feist             if (asserted == nullptr)
58834dd179eSJames Feist             {
58934dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
59034dd179eSJames Feist             }
59134dd179eSJames Feist             else if (*asserted)
59234dd179eSJames Feist             {
59334dd179eSJames Feist                 return "Warning";
59434dd179eSJames Feist             }
59534dd179eSJames Feist         }
59634dd179eSJames Feist         if (thresholdLowIt != warningThresholdIt->second.end())
59734dd179eSJames Feist         {
59834dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
59934dd179eSJames Feist             if (asserted == nullptr)
60034dd179eSJames Feist             {
60134dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
60234dd179eSJames Feist             }
60334dd179eSJames Feist             else if (*asserted)
60434dd179eSJames Feist             {
60534dd179eSJames Feist                 return "Warning";
60634dd179eSJames Feist             }
60734dd179eSJames Feist         }
60834dd179eSJames Feist     }
609adc4f0dbSShawn McCarney 
61034dd179eSJames Feist     return "OK";
61134dd179eSJames Feist }
61234dd179eSJames Feist 
613*d500549bSAnthony Wilson static void setLedState(nlohmann::json& sensorJson,
614*d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
615*d500549bSAnthony Wilson {
616*d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
617*d500549bSAnthony Wilson     {
618*d500549bSAnthony Wilson         switch (inventoryItem->ledState)
619*d500549bSAnthony Wilson         {
620*d500549bSAnthony Wilson             case LedState::OFF:
621*d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
622*d500549bSAnthony Wilson                 break;
623*d500549bSAnthony Wilson             case LedState::ON:
624*d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
625*d500549bSAnthony Wilson                 break;
626*d500549bSAnthony Wilson             case LedState::BLINK:
627*d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
628*d500549bSAnthony Wilson                 break;
629*d500549bSAnthony Wilson             default:
630*d500549bSAnthony Wilson                 break;
631*d500549bSAnthony Wilson         }
632*d500549bSAnthony Wilson     }
633*d500549bSAnthony Wilson }
634*d500549bSAnthony Wilson 
63534dd179eSJames Feist /**
63608777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
63708777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
638274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
63908777fb0SLewanczyk, Dawid  * build
64095a3ecadSAnthony Wilson  * @param sensorSchema  The schema (Power, Thermal, etc) being associated with
64195a3ecadSAnthony Wilson  * the sensor to build
64208777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
64308777fb0SLewanczyk, Dawid  * interfaces to be built from
64408777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
645adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
646adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
64708777fb0SLewanczyk, Dawid  */
64808777fb0SLewanczyk, Dawid void objectInterfacesToJson(
64908777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
65095a3ecadSAnthony Wilson     const std::string& sensorSchema,
65108777fb0SLewanczyk, Dawid     const boost::container::flat_map<
652aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>&
65308777fb0SLewanczyk, Dawid         interfacesDict,
654adc4f0dbSShawn McCarney     nlohmann::json& sensor_json, InventoryItem* inventoryItem)
6551abe55efSEd Tanous {
65608777fb0SLewanczyk, Dawid     // We need a value interface before we can do anything with it
65755c7b7a2SEd Tanous     auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
6581abe55efSEd Tanous     if (valueIt == interfacesDict.end())
6591abe55efSEd Tanous     {
66055c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
66108777fb0SLewanczyk, Dawid         return;
66208777fb0SLewanczyk, Dawid     }
66308777fb0SLewanczyk, Dawid 
66408777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
66508777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
66608777fb0SLewanczyk, Dawid 
66755c7b7a2SEd Tanous     auto scaleIt = valueIt->second.find("Scale");
66808777fb0SLewanczyk, Dawid     // If a scale exists, pull value as int64, and use the scaling.
6691abe55efSEd Tanous     if (scaleIt != valueIt->second.end())
6701abe55efSEd Tanous     {
671abf2add6SEd Tanous         const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
6721abe55efSEd Tanous         if (int64Value != nullptr)
6731abe55efSEd Tanous         {
67408777fb0SLewanczyk, Dawid             scaleMultiplier = *int64Value;
67508777fb0SLewanczyk, Dawid         }
67608777fb0SLewanczyk, Dawid     }
67708777fb0SLewanczyk, Dawid 
67895a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
679adc4f0dbSShawn McCarney     {
68095a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
68195a3ecadSAnthony Wilson         // including power sensors.
68295a3ecadSAnthony Wilson         sensor_json["Id"] = sensorName;
68395a3ecadSAnthony Wilson         sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
68495a3ecadSAnthony Wilson     }
68595a3ecadSAnthony Wilson     else if (sensorType != "power")
68695a3ecadSAnthony Wilson     {
68795a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
68895a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
68995a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
69008777fb0SLewanczyk, Dawid         sensor_json["MemberId"] = sensorName;
691e742b6ccSEd Tanous         sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
692adc4f0dbSShawn McCarney     }
693e742b6ccSEd Tanous 
694adc4f0dbSShawn McCarney     sensor_json["Status"]["State"] = getState(inventoryItem);
695adc4f0dbSShawn McCarney     sensor_json["Status"]["Health"] =
696adc4f0dbSShawn McCarney         getHealth(sensor_json, interfacesDict, inventoryItem);
69708777fb0SLewanczyk, Dawid 
69808777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
69908777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
70008777fb0SLewanczyk, Dawid     // that require integers, not floats.
70108777fb0SLewanczyk, Dawid     bool forceToInt = false;
70208777fb0SLewanczyk, Dawid 
7033929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
70495a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
70595a3ecadSAnthony Wilson     {
70695a3ecadSAnthony Wilson         sensor_json["@odata.type"] = "#Sensor.v1_0_0.Sensor";
70795a3ecadSAnthony Wilson         sensor_json["@odata.context"] = "/redfish/v1/$metadata#Sensor.Sensor";
70895a3ecadSAnthony Wilson         if (sensorType == "power")
70995a3ecadSAnthony Wilson         {
71095a3ecadSAnthony Wilson             sensor_json["ReadingUnits"] = "Watts";
71195a3ecadSAnthony Wilson         }
71295a3ecadSAnthony Wilson         else if (sensorType == "current")
71395a3ecadSAnthony Wilson         {
71495a3ecadSAnthony Wilson             sensor_json["ReadingUnits"] = "Amperes";
71595a3ecadSAnthony Wilson         }
71695a3ecadSAnthony Wilson     }
71795a3ecadSAnthony Wilson     else if (sensorType == "temperature")
7181abe55efSEd Tanous     {
7193929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
7207885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature";
72108777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
72208777fb0SLewanczyk, Dawid         // implementation seems to implement fan
7231abe55efSEd Tanous     }
7241abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
7251abe55efSEd Tanous     {
7263929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
72708777fb0SLewanczyk, Dawid         sensor_json["ReadingUnits"] = "RPM";
7287885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
729*d500549bSAnthony Wilson         setLedState(sensor_json, inventoryItem);
73008777fb0SLewanczyk, Dawid         forceToInt = true;
7311abe55efSEd Tanous     }
7326f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
7336f6d0d32SEd Tanous     {
7343929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
7356f6d0d32SEd Tanous         sensor_json["ReadingUnits"] = "Percent";
7366f6d0d32SEd Tanous         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
737*d500549bSAnthony Wilson         setLedState(sensor_json, inventoryItem);
7386f6d0d32SEd Tanous         forceToInt = true;
7396f6d0d32SEd Tanous     }
7401abe55efSEd Tanous     else if (sensorType == "voltage")
7411abe55efSEd Tanous     {
7423929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
7437885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage";
7441abe55efSEd Tanous     }
7452474adfaSEd Tanous     else if (sensorType == "power")
7462474adfaSEd Tanous     {
74749c53ac9SJohnathan Mantey         std::string sensorNameLower =
74849c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
74949c53ac9SJohnathan Mantey 
750028f7ebcSEddie James         if (!sensorName.compare("total_power"))
751028f7ebcSEddie James         {
7527ab06f49SGunnar Mills             sensor_json["@odata.type"] = "#Power.v1_0_0.PowerControl";
7537ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
7547ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
7557ab06f49SGunnar Mills             sensor_json["MemberId"] = "0";
7567ab06f49SGunnar Mills             sensor_json["Name"] = "Chassis Power Control";
7573929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
758028f7ebcSEddie James         }
759028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
76049c53ac9SJohnathan Mantey         {
7613929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
76249c53ac9SJohnathan Mantey         }
76349c53ac9SJohnathan Mantey         else
76449c53ac9SJohnathan Mantey         {
7653929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
76649c53ac9SJohnathan Mantey         }
7672474adfaSEd Tanous     }
7681abe55efSEd Tanous     else
7691abe55efSEd Tanous     {
77055c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
77108777fb0SLewanczyk, Dawid         return;
77208777fb0SLewanczyk, Dawid     }
77308777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
7743929aca1SAnthony Wilson     std::vector<
7753929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
7763929aca1SAnthony Wilson         properties;
77708777fb0SLewanczyk, Dawid     properties.reserve(7);
77808777fb0SLewanczyk, Dawid 
77908777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
780de629b6eSShawn McCarney 
7813929aca1SAnthony Wilson     if (sensorSchema == "Sensors")
7823929aca1SAnthony Wilson     {
7833929aca1SAnthony Wilson         properties.emplace_back(
7843929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
7853929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
7863929aca1SAnthony Wilson         properties.emplace_back(
7873929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
7883929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
7893929aca1SAnthony Wilson         properties.emplace_back(
7903929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
7913929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
7923929aca1SAnthony Wilson         properties.emplace_back(
7933929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
7943929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
7953929aca1SAnthony Wilson     }
7963929aca1SAnthony Wilson     else if (sensorType != "power")
797de629b6eSShawn McCarney     {
79808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
7993929aca1SAnthony Wilson                                 "WarningHigh",
8003929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
80108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8023929aca1SAnthony Wilson                                 "WarningLow",
8033929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
80408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8053929aca1SAnthony Wilson                                 "CriticalHigh",
8063929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
80708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8083929aca1SAnthony Wilson                                 "CriticalLow",
8093929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
810de629b6eSShawn McCarney     }
81108777fb0SLewanczyk, Dawid 
8122474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
8132474adfaSEd Tanous 
81495a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
81595a3ecadSAnthony Wilson     {
81695a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8173929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
81895a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8193929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
82095a3ecadSAnthony Wilson     }
82195a3ecadSAnthony Wilson     else if (sensorType == "temperature")
8221abe55efSEd Tanous     {
82308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8243929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
82508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8263929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
8271abe55efSEd Tanous     }
828adc4f0dbSShawn McCarney     else if (sensorType != "power")
8291abe55efSEd Tanous     {
83008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8313929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
83208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8333929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
83408777fb0SLewanczyk, Dawid     }
83508777fb0SLewanczyk, Dawid 
8363929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
8373929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
8381abe55efSEd Tanous     {
83908777fb0SLewanczyk, Dawid         auto interfaceProperties = interfacesDict.find(std::get<0>(p));
8401abe55efSEd Tanous         if (interfaceProperties != interfacesDict.end())
8411abe55efSEd Tanous         {
842271584abSEd Tanous             auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
843271584abSEd Tanous             if (thisValueIt != interfaceProperties->second.end())
8441abe55efSEd Tanous             {
845271584abSEd Tanous                 const SensorVariant& valueVariant = thisValueIt->second;
8463929aca1SAnthony Wilson 
8473929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
8483929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
8493929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
8503929aca1SAnthony Wilson 
85108777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
852abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
85308777fb0SLewanczyk, Dawid 
854abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
855028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
8566f6d0d32SEd Tanous                 double temp = 0.0;
8576f6d0d32SEd Tanous                 if (int64Value != nullptr)
8581abe55efSEd Tanous                 {
859271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
8606f6d0d32SEd Tanous                 }
8616f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
8621abe55efSEd Tanous                 {
8636f6d0d32SEd Tanous                     temp = *doubleValue;
8641abe55efSEd Tanous                 }
865028f7ebcSEddie James                 else if (uValue != nullptr)
866028f7ebcSEddie James                 {
867028f7ebcSEddie James                     temp = *uValue;
868028f7ebcSEddie James                 }
8691abe55efSEd Tanous                 else
8701abe55efSEd Tanous                 {
8716f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
8726f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
8736f6d0d32SEd Tanous                     continue;
87408777fb0SLewanczyk, Dawid                 }
8756f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
8766f6d0d32SEd Tanous                 if (forceToInt)
8776f6d0d32SEd Tanous                 {
8783929aca1SAnthony Wilson                     sensor_json[key] = static_cast<int64_t>(temp);
8796f6d0d32SEd Tanous                 }
8806f6d0d32SEd Tanous                 else
8816f6d0d32SEd Tanous                 {
8823929aca1SAnthony Wilson                     sensor_json[key] = temp;
88308777fb0SLewanczyk, Dawid                 }
88408777fb0SLewanczyk, Dawid             }
88508777fb0SLewanczyk, Dawid         }
88608777fb0SLewanczyk, Dawid     }
88755c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
88808777fb0SLewanczyk, Dawid }
88908777fb0SLewanczyk, Dawid 
8908bd25ccdSJames Feist static void
8918bd25ccdSJames Feist     populateFanRedundancy(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp)
8928bd25ccdSJames Feist {
8938bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
8948bd25ccdSJames Feist         [sensorsAsyncResp](const boost::system::error_code ec,
8958bd25ccdSJames Feist                            const GetSubTreeType& resp) {
8968bd25ccdSJames Feist             if (ec)
8978bd25ccdSJames Feist             {
8988bd25ccdSJames Feist                 return; // don't have to have this interface
8998bd25ccdSJames Feist             }
900e278c18fSEd Tanous             for (const std::pair<std::string,
901e278c18fSEd Tanous                                  std::vector<std::pair<
902e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
903e278c18fSEd Tanous                      pathPair : resp)
9048bd25ccdSJames Feist             {
905e278c18fSEd Tanous                 const std::string& path = pathPair.first;
906e278c18fSEd Tanous                 const std::vector<
907e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
908e278c18fSEd Tanous                     pathPair.second;
9098bd25ccdSJames Feist                 if (objDict.empty())
9108bd25ccdSJames Feist                 {
9118bd25ccdSJames Feist                     continue; // this should be impossible
9128bd25ccdSJames Feist                 }
9138bd25ccdSJames Feist 
9148bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
9158bd25ccdSJames Feist                 crow::connections::systemBus->async_method_call(
9168bd25ccdSJames Feist                     [path, owner,
917271584abSEd Tanous                      sensorsAsyncResp](const boost::system::error_code e,
9188bd25ccdSJames Feist                                        std::variant<std::vector<std::string>>
9198bd25ccdSJames Feist                                            variantEndpoints) {
920271584abSEd Tanous                         if (e)
9218bd25ccdSJames Feist                         {
9228bd25ccdSJames Feist                             return; // if they don't have an association we
9238bd25ccdSJames Feist                                     // can't tell what chassis is
9248bd25ccdSJames Feist                         }
9258bd25ccdSJames Feist                         // verify part of the right chassis
9268bd25ccdSJames Feist                         auto endpoints = std::get_if<std::vector<std::string>>(
9278bd25ccdSJames Feist                             &variantEndpoints);
9288bd25ccdSJames Feist 
9298bd25ccdSJames Feist                         if (endpoints == nullptr)
9308bd25ccdSJames Feist                         {
9318bd25ccdSJames Feist                             BMCWEB_LOG_ERROR << "Invalid association interface";
9328bd25ccdSJames Feist                             messages::internalError(sensorsAsyncResp->res);
9338bd25ccdSJames Feist                             return;
9348bd25ccdSJames Feist                         }
9358bd25ccdSJames Feist 
9368bd25ccdSJames Feist                         auto found = std::find_if(
9378bd25ccdSJames Feist                             endpoints->begin(), endpoints->end(),
9388bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
9398bd25ccdSJames Feist                                 return entry.find(
9408bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
9418bd25ccdSJames Feist                                        std::string::npos;
9428bd25ccdSJames Feist                             });
9438bd25ccdSJames Feist 
9448bd25ccdSJames Feist                         if (found == endpoints->end())
9458bd25ccdSJames Feist                         {
9468bd25ccdSJames Feist                             return;
9478bd25ccdSJames Feist                         }
9488bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
9498bd25ccdSJames Feist                             [path, sensorsAsyncResp](
950271584abSEd Tanous                                 const boost::system::error_code& err,
9518bd25ccdSJames Feist                                 const boost::container::flat_map<
9528bd25ccdSJames Feist                                     std::string,
9538bd25ccdSJames Feist                                     std::variant<uint8_t,
9548bd25ccdSJames Feist                                                  std::vector<std::string>,
9558bd25ccdSJames Feist                                                  std::string>>& ret) {
956271584abSEd Tanous                                 if (err)
9578bd25ccdSJames Feist                                 {
9588bd25ccdSJames Feist                                     return; // don't have to have this
9598bd25ccdSJames Feist                                             // interface
9608bd25ccdSJames Feist                                 }
9618bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
9628bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
9638bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
9648bd25ccdSJames Feist 
9658bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
9668bd25ccdSJames Feist                                     findCollection == ret.end() ||
9678bd25ccdSJames Feist                                     findStatus == ret.end())
9688bd25ccdSJames Feist                                 {
9698bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
9708bd25ccdSJames Feist                                         << "Invalid redundancy interface";
9718bd25ccdSJames Feist                                     messages::internalError(
9728bd25ccdSJames Feist                                         sensorsAsyncResp->res);
9738bd25ccdSJames Feist                                     return;
9748bd25ccdSJames Feist                                 }
9758bd25ccdSJames Feist 
9768bd25ccdSJames Feist                                 auto allowedFailures = std::get_if<uint8_t>(
9778bd25ccdSJames Feist                                     &(findFailures->second));
9788bd25ccdSJames Feist                                 auto collection =
9798bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
9808bd25ccdSJames Feist                                         &(findCollection->second));
9818bd25ccdSJames Feist                                 auto status = std::get_if<std::string>(
9828bd25ccdSJames Feist                                     &(findStatus->second));
9838bd25ccdSJames Feist 
9848bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
9858bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
9868bd25ccdSJames Feist                                 {
9878bd25ccdSJames Feist 
9888bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
9898bd25ccdSJames Feist                                         << "Invalid redundancy interface "
9908bd25ccdSJames Feist                                            "types";
9918bd25ccdSJames Feist                                     messages::internalError(
9928bd25ccdSJames Feist                                         sensorsAsyncResp->res);
9938bd25ccdSJames Feist                                     return;
9948bd25ccdSJames Feist                                 }
9958bd25ccdSJames Feist                                 size_t lastSlash = path.rfind("/");
9968bd25ccdSJames Feist                                 if (lastSlash == std::string::npos)
9978bd25ccdSJames Feist                                 {
9988bd25ccdSJames Feist                                     // this should be impossible
9998bd25ccdSJames Feist                                     messages::internalError(
10008bd25ccdSJames Feist                                         sensorsAsyncResp->res);
10018bd25ccdSJames Feist                                     return;
10028bd25ccdSJames Feist                                 }
10038bd25ccdSJames Feist                                 std::string name = path.substr(lastSlash + 1);
10048bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
10058bd25ccdSJames Feist                                              ' ');
10068bd25ccdSJames Feist 
10078bd25ccdSJames Feist                                 std::string health;
10088bd25ccdSJames Feist 
10098bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
10108bd25ccdSJames Feist                                 {
10118bd25ccdSJames Feist                                     health = "OK";
10128bd25ccdSJames Feist                                 }
10138bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
10148bd25ccdSJames Feist                                 {
10158bd25ccdSJames Feist                                     health = "Warning";
10168bd25ccdSJames Feist                                 }
10178bd25ccdSJames Feist                                 else
10188bd25ccdSJames Feist                                 {
10198bd25ccdSJames Feist                                     health = "Critical";
10208bd25ccdSJames Feist                                 }
10218bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
10228bd25ccdSJames Feist                                 const auto& fanRedfish =
10238bd25ccdSJames Feist                                     sensorsAsyncResp->res.jsonValue["Fans"];
10248bd25ccdSJames Feist                                 for (const std::string& item : *collection)
10258bd25ccdSJames Feist                                 {
10268bd25ccdSJames Feist                                     lastSlash = item.rfind("/");
10278bd25ccdSJames Feist                                     // make a copy as collection is const
10288bd25ccdSJames Feist                                     std::string itemName =
10298bd25ccdSJames Feist                                         item.substr(lastSlash + 1);
10308bd25ccdSJames Feist                                     /*
10318bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
10328bd25ccdSJames Feist                                     std::replace(itemName.begin(),
10338bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
10348bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
10358bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
10368bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
10378bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
10388bd25ccdSJames Feist                                         });
10398bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
10408bd25ccdSJames Feist                                     {
10418bd25ccdSJames Feist                                         redfishCollection.push_back(
10428bd25ccdSJames Feist                                             {{"@odata.id",
10438bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
10448bd25ccdSJames Feist                                     }
10458bd25ccdSJames Feist                                     else
10468bd25ccdSJames Feist                                     {
10478bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
10488bd25ccdSJames Feist                                             << "failed to find fan in schema";
10498bd25ccdSJames Feist                                         messages::internalError(
10508bd25ccdSJames Feist                                             sensorsAsyncResp->res);
10518bd25ccdSJames Feist                                         return;
10528bd25ccdSJames Feist                                     }
10538bd25ccdSJames Feist                                 }
10548bd25ccdSJames Feist 
1055271584abSEd Tanous                                 nlohmann::json& jResp =
1056271584abSEd Tanous                                     sensorsAsyncResp->res
10578bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1058271584abSEd Tanous                                 jResp.push_back(
10598bd25ccdSJames Feist                                     {{"@odata.id",
1060717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
10618bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
10628bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
10638bd25ccdSJames Feist                                           "#/Redundancy/" +
1064271584abSEd Tanous                                           std::to_string(jResp.size())},
10658bd25ccdSJames Feist                                      {"@odata.type",
10668bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
10678bd25ccdSJames Feist                                      {"MinNumNeeded",
10688bd25ccdSJames Feist                                       collection->size() - *allowedFailures},
10698bd25ccdSJames Feist                                      {"MemberId", name},
10708bd25ccdSJames Feist                                      {"Mode", "N+m"},
10718bd25ccdSJames Feist                                      {"Name", name},
10728bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
10738bd25ccdSJames Feist                                      {"Status",
10748bd25ccdSJames Feist                                       {{"Health", health},
10758bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
10768bd25ccdSJames Feist                             },
10778bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
10788bd25ccdSJames Feist                             "GetAll",
10798bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
10808bd25ccdSJames Feist                     },
108102e92e32SJames Feist                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
10828bd25ccdSJames Feist                     "org.freedesktop.DBus.Properties", "Get",
10838bd25ccdSJames Feist                     "xyz.openbmc_project.Association", "endpoints");
10848bd25ccdSJames Feist             }
10858bd25ccdSJames Feist         },
10868bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
10878bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
10888bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
10898bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
10908bd25ccdSJames Feist         std::array<const char*, 1>{
10918bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
10928bd25ccdSJames Feist }
10938bd25ccdSJames Feist 
109449c53ac9SJohnathan Mantey void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
109549c53ac9SJohnathan Mantey {
109649c53ac9SJohnathan Mantey     nlohmann::json& response = SensorsAsyncResp->res.jsonValue;
109749c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
109849c53ac9SJohnathan Mantey     if (SensorsAsyncResp->chassisSubNode == "Power")
109949c53ac9SJohnathan Mantey     {
110049c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
110149c53ac9SJohnathan Mantey     }
110249c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
110349c53ac9SJohnathan Mantey     {
110449c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
110549c53ac9SJohnathan Mantey         if (entry != response.end())
110649c53ac9SJohnathan Mantey         {
110749c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
110849c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
110949c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
111049c53ac9SJohnathan Mantey                       });
111149c53ac9SJohnathan Mantey 
111249c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
111349c53ac9SJohnathan Mantey             size_t count = 0;
111449c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
111549c53ac9SJohnathan Mantey             {
111649c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
111749c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
111849c53ac9SJohnathan Mantey                 {
111949c53ac9SJohnathan Mantey                     continue;
112049c53ac9SJohnathan Mantey                 }
112149c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
112249c53ac9SJohnathan Mantey                 if (value != nullptr)
112349c53ac9SJohnathan Mantey                 {
112449c53ac9SJohnathan Mantey                     *value += std::to_string(count);
112549c53ac9SJohnathan Mantey                     count++;
112649c53ac9SJohnathan Mantey                 }
112749c53ac9SJohnathan Mantey             }
112849c53ac9SJohnathan Mantey         }
112949c53ac9SJohnathan Mantey     }
113049c53ac9SJohnathan Mantey }
113149c53ac9SJohnathan Mantey 
113208777fb0SLewanczyk, Dawid /**
1133adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1134adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1135adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1136adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
11378fb49dd6SShawn McCarney  */
1138adc4f0dbSShawn McCarney static InventoryItem* findInventoryItem(
1139adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1140adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
11418fb49dd6SShawn McCarney {
1142adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
11438fb49dd6SShawn McCarney     {
1144adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
11458fb49dd6SShawn McCarney         {
1146adc4f0dbSShawn McCarney             return &inventoryItem;
11478fb49dd6SShawn McCarney         }
11488fb49dd6SShawn McCarney     }
11498fb49dd6SShawn McCarney     return nullptr;
11508fb49dd6SShawn McCarney }
11518fb49dd6SShawn McCarney 
11528fb49dd6SShawn McCarney /**
1153adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1154adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1155adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1156adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
11578fb49dd6SShawn McCarney  */
1158adc4f0dbSShawn McCarney static InventoryItem* findInventoryItemForSensor(
1159adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1160adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1161adc4f0dbSShawn McCarney {
1162adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1163adc4f0dbSShawn McCarney     {
1164adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1165adc4f0dbSShawn McCarney         {
1166adc4f0dbSShawn McCarney             return &inventoryItem;
1167adc4f0dbSShawn McCarney         }
1168adc4f0dbSShawn McCarney     }
1169adc4f0dbSShawn McCarney     return nullptr;
1170adc4f0dbSShawn McCarney }
1171adc4f0dbSShawn McCarney 
1172adc4f0dbSShawn McCarney /**
1173*d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1174*d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1175*d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1176*d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1177*d500549bSAnthony Wilson  */
1178*d500549bSAnthony Wilson inline InventoryItem*
1179*d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1180*d500549bSAnthony Wilson                             const std::string& ledObjPath)
1181*d500549bSAnthony Wilson {
1182*d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1183*d500549bSAnthony Wilson     {
1184*d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1185*d500549bSAnthony Wilson         {
1186*d500549bSAnthony Wilson             return &inventoryItem;
1187*d500549bSAnthony Wilson         }
1188*d500549bSAnthony Wilson     }
1189*d500549bSAnthony Wilson     return nullptr;
1190*d500549bSAnthony Wilson }
1191*d500549bSAnthony Wilson 
1192*d500549bSAnthony Wilson /**
1193adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1194adc4f0dbSShawn McCarney  *
1195adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1196adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1197adc4f0dbSShawn McCarney  * added to the vector.
1198adc4f0dbSShawn McCarney  *
1199adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1200adc4f0dbSShawn McCarney  * InventoryItem.
1201adc4f0dbSShawn McCarney  *
1202adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1203adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1204adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1205adc4f0dbSShawn McCarney  */
1206adc4f0dbSShawn McCarney static void
1207adc4f0dbSShawn McCarney     addInventoryItem(std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1208adc4f0dbSShawn McCarney                      const std::string& invItemObjPath,
1209adc4f0dbSShawn McCarney                      const std::string& sensorObjPath)
1210adc4f0dbSShawn McCarney {
1211adc4f0dbSShawn McCarney     // Look for inventory item in vector
1212adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1213adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1214adc4f0dbSShawn McCarney 
1215adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1216adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1217adc4f0dbSShawn McCarney     {
1218adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1219adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1220adc4f0dbSShawn McCarney     }
1221adc4f0dbSShawn McCarney 
1222adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1223adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1224adc4f0dbSShawn McCarney }
1225adc4f0dbSShawn McCarney 
1226adc4f0dbSShawn McCarney /**
1227adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1228adc4f0dbSShawn McCarney  *
1229adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1230adc4f0dbSShawn McCarney  * specified InventoryItem.
1231adc4f0dbSShawn McCarney  *
1232adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1233adc4f0dbSShawn McCarney  * response.
1234adc4f0dbSShawn McCarney  *
1235adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1236adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1237adc4f0dbSShawn McCarney  * for the specified inventory item.
1238adc4f0dbSShawn McCarney  */
1239adc4f0dbSShawn McCarney static void storeInventoryItemData(
1240adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
12418fb49dd6SShawn McCarney     const boost::container::flat_map<
12428fb49dd6SShawn McCarney         std::string, boost::container::flat_map<std::string, SensorVariant>>&
12438fb49dd6SShawn McCarney         interfacesDict)
12448fb49dd6SShawn McCarney {
1245adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1246adc4f0dbSShawn McCarney     auto interfaceIt =
1247adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1248adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
12498fb49dd6SShawn McCarney     {
1250adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Present");
1251adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
12528fb49dd6SShawn McCarney         {
1253adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1254adc4f0dbSShawn McCarney             if (value != nullptr)
12558fb49dd6SShawn McCarney             {
1256adc4f0dbSShawn McCarney                 inventoryItem.isPresent = *value;
12578fb49dd6SShawn McCarney             }
12588fb49dd6SShawn McCarney         }
12598fb49dd6SShawn McCarney     }
12608fb49dd6SShawn McCarney 
1261adc4f0dbSShawn McCarney     // Check if Inventory.Item.PowerSupply interface is present
1262adc4f0dbSShawn McCarney     interfaceIt =
1263adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1264adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
12658fb49dd6SShawn McCarney     {
1266adc4f0dbSShawn McCarney         inventoryItem.isPowerSupply = true;
12678fb49dd6SShawn McCarney     }
1268adc4f0dbSShawn McCarney 
1269adc4f0dbSShawn McCarney     // Get properties from Inventory.Decorator.Asset interface
1270adc4f0dbSShawn McCarney     interfaceIt =
1271adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1272adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1273adc4f0dbSShawn McCarney     {
1274adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Manufacturer");
1275adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1276adc4f0dbSShawn McCarney         {
1277adc4f0dbSShawn McCarney             const std::string* value =
1278adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1279adc4f0dbSShawn McCarney             if (value != nullptr)
1280adc4f0dbSShawn McCarney             {
1281adc4f0dbSShawn McCarney                 inventoryItem.manufacturer = *value;
1282adc4f0dbSShawn McCarney             }
1283adc4f0dbSShawn McCarney         }
1284adc4f0dbSShawn McCarney 
1285adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("Model");
1286adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1287adc4f0dbSShawn McCarney         {
1288adc4f0dbSShawn McCarney             const std::string* value =
1289adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1290adc4f0dbSShawn McCarney             if (value != nullptr)
1291adc4f0dbSShawn McCarney             {
1292adc4f0dbSShawn McCarney                 inventoryItem.model = *value;
1293adc4f0dbSShawn McCarney             }
1294adc4f0dbSShawn McCarney         }
1295adc4f0dbSShawn McCarney 
1296adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("PartNumber");
1297adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1298adc4f0dbSShawn McCarney         {
1299adc4f0dbSShawn McCarney             const std::string* value =
1300adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1301adc4f0dbSShawn McCarney             if (value != nullptr)
1302adc4f0dbSShawn McCarney             {
1303adc4f0dbSShawn McCarney                 inventoryItem.partNumber = *value;
1304adc4f0dbSShawn McCarney             }
1305adc4f0dbSShawn McCarney         }
1306adc4f0dbSShawn McCarney 
1307adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("SerialNumber");
1308adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1309adc4f0dbSShawn McCarney         {
1310adc4f0dbSShawn McCarney             const std::string* value =
1311adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1312adc4f0dbSShawn McCarney             if (value != nullptr)
1313adc4f0dbSShawn McCarney             {
1314adc4f0dbSShawn McCarney                 inventoryItem.serialNumber = *value;
1315adc4f0dbSShawn McCarney             }
1316adc4f0dbSShawn McCarney         }
1317adc4f0dbSShawn McCarney     }
1318adc4f0dbSShawn McCarney 
1319adc4f0dbSShawn McCarney     // Get properties from State.Decorator.OperationalStatus interface
1320adc4f0dbSShawn McCarney     interfaceIt = interfacesDict.find(
1321adc4f0dbSShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus");
1322adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1323adc4f0dbSShawn McCarney     {
1324adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Functional");
1325adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1326adc4f0dbSShawn McCarney         {
1327adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1328adc4f0dbSShawn McCarney             if (value != nullptr)
1329adc4f0dbSShawn McCarney             {
1330adc4f0dbSShawn McCarney                 inventoryItem.isFunctional = *value;
13318fb49dd6SShawn McCarney             }
13328fb49dd6SShawn McCarney         }
13338fb49dd6SShawn McCarney     }
13348fb49dd6SShawn McCarney }
13358fb49dd6SShawn McCarney 
13368fb49dd6SShawn McCarney /**
1337adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
13388fb49dd6SShawn McCarney  *
1339adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1340adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1341adc4f0dbSShawn McCarney  * inventoryItems vector.
13428fb49dd6SShawn McCarney  *
1343adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1344adc4f0dbSShawn McCarney  * response.
1345adc4f0dbSShawn McCarney  *
1346adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1347adc4f0dbSShawn McCarney  * been obtained.
1348adc4f0dbSShawn McCarney  *
1349adc4f0dbSShawn McCarney  * The callback must have the following signature:
1350adc4f0dbSShawn McCarney  *   @code
1351*d500549bSAnthony Wilson  *   callback(void)
1352adc4f0dbSShawn McCarney  *   @endcode
1353adc4f0dbSShawn McCarney  *
1354adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1355adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1356adc4f0dbSShawn McCarney  * last asynchronous function has completed.
13578fb49dd6SShawn McCarney  *
13588fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1359adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1360adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
13618fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
13628fb49dd6SShawn McCarney  * implements ObjectManager.
1363adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1364adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1365adc4f0dbSShawn McCarney  * in recursive calls to this function.
13668fb49dd6SShawn McCarney  */
1367adc4f0dbSShawn McCarney template <typename Callback>
1368adc4f0dbSShawn McCarney static void getInventoryItemsData(
13698fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1370adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
13718fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
13728fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1373adc4f0dbSShawn McCarney         objectMgrPaths,
1374271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
13758fb49dd6SShawn McCarney {
1376adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
13778fb49dd6SShawn McCarney 
1378adc4f0dbSShawn McCarney     // If no more connections left, call callback
1379adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
13808fb49dd6SShawn McCarney     {
1381*d500549bSAnthony Wilson         callback();
1382adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1383adc4f0dbSShawn McCarney         return;
1384adc4f0dbSShawn McCarney     }
1385adc4f0dbSShawn McCarney 
1386adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1387adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1388adc4f0dbSShawn McCarney     if (it != invConnections->end())
1389adc4f0dbSShawn McCarney     {
1390adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1391adc4f0dbSShawn McCarney 
13928fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1393adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1394adc4f0dbSShawn McCarney                             objectMgrPaths, callback{std::move(callback)},
1395adc4f0dbSShawn McCarney                             invConnectionsIndex](
1396adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
13978fb49dd6SShawn McCarney                                ManagedObjectsVectorType& resp) {
1398adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
13998fb49dd6SShawn McCarney             if (ec)
14008fb49dd6SShawn McCarney             {
14018fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1402adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
14038fb49dd6SShawn McCarney                 messages::internalError(sensorsAsyncResp->res);
14048fb49dd6SShawn McCarney                 return;
14058fb49dd6SShawn McCarney             }
14068fb49dd6SShawn McCarney 
14078fb49dd6SShawn McCarney             // Loop through returned object paths
14088fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
14098fb49dd6SShawn McCarney             {
14108fb49dd6SShawn McCarney                 const std::string& objPath =
14118fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
14128fb49dd6SShawn McCarney 
1413adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1414adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1415adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1416adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
14178fb49dd6SShawn McCarney                 {
1418adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1419adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
14208fb49dd6SShawn McCarney                 }
14218fb49dd6SShawn McCarney             }
14228fb49dd6SShawn McCarney 
1423adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1424adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1425adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1426adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1427adc4f0dbSShawn McCarney 
1428adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
14298fb49dd6SShawn McCarney         };
14308fb49dd6SShawn McCarney 
14318fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
14328fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
14338fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
14348fb49dd6SShawn McCarney         const std::string& objectMgrPath =
14358fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
14368fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
14378fb49dd6SShawn McCarney                          << objectMgrPath;
14388fb49dd6SShawn McCarney 
14398fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
14408fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
14418fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
14428fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14438fb49dd6SShawn McCarney     }
14448fb49dd6SShawn McCarney 
1445adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
14468fb49dd6SShawn McCarney }
14478fb49dd6SShawn McCarney 
14488fb49dd6SShawn McCarney /**
1449adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
14508fb49dd6SShawn McCarney  *
1451adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1452adc4f0dbSShawn McCarney  * items that are associated with sensors.
14538fb49dd6SShawn McCarney  *
14548fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
14558fb49dd6SShawn McCarney  * been obtained.
14568fb49dd6SShawn McCarney  *
14578fb49dd6SShawn McCarney  * The callback must have the following signature:
14588fb49dd6SShawn McCarney  *   @code
14598fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
14608fb49dd6SShawn McCarney  *            invConnections)
14618fb49dd6SShawn McCarney  *   @endcode
14628fb49dd6SShawn McCarney  *
14638fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1464adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
14658fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
14668fb49dd6SShawn McCarney  */
14678fb49dd6SShawn McCarney template <typename Callback>
14688fb49dd6SShawn McCarney static void getInventoryItemsConnections(
14698fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1470adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
14718fb49dd6SShawn McCarney     Callback&& callback)
14728fb49dd6SShawn McCarney {
14738fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
14748fb49dd6SShawn McCarney 
14758fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1476adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
14778fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1478adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1479adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
14808fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
14818fb49dd6SShawn McCarney 
14828fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
14838fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1484adc4f0dbSShawn McCarney                         inventoryItems](const boost::system::error_code ec,
14858fb49dd6SShawn McCarney                                         const GetSubTreeType& subtree) {
14868fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
14878fb49dd6SShawn McCarney         if (ec)
14888fb49dd6SShawn McCarney         {
14898fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
14908fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
14918fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
14928fb49dd6SShawn McCarney             return;
14938fb49dd6SShawn McCarney         }
14948fb49dd6SShawn McCarney 
14958fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
14968fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
14978fb49dd6SShawn McCarney             invConnections =
14988fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
14998fb49dd6SShawn McCarney         invConnections->reserve(8);
15008fb49dd6SShawn McCarney 
15018fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
15028fb49dd6SShawn McCarney         for (const std::pair<
15038fb49dd6SShawn McCarney                  std::string,
15048fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
15058fb49dd6SShawn McCarney                  object : subtree)
15068fb49dd6SShawn McCarney         {
1507adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
15088fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1509adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
15108fb49dd6SShawn McCarney             {
15118fb49dd6SShawn McCarney                 // Store all connections to inventory item
15128fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
15138fb49dd6SShawn McCarney                          objData : object.second)
15148fb49dd6SShawn McCarney                 {
15158fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
15168fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
15178fb49dd6SShawn McCarney                 }
15188fb49dd6SShawn McCarney             }
15198fb49dd6SShawn McCarney         }
1520*d500549bSAnthony Wilson 
15218fb49dd6SShawn McCarney         callback(invConnections);
15228fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
15238fb49dd6SShawn McCarney     };
15248fb49dd6SShawn McCarney 
15258fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
15268fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
15278fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
15288fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
15298fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
15308fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
15318fb49dd6SShawn McCarney }
15328fb49dd6SShawn McCarney 
15338fb49dd6SShawn McCarney /**
1534adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
15358fb49dd6SShawn McCarney  *
15368fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1537*d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1538*d500549bSAnthony Wilson  * their LEDs, if any.
15398fb49dd6SShawn McCarney  *
15408fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
15418fb49dd6SShawn McCarney  * has been obtained.
15428fb49dd6SShawn McCarney  *
15438fb49dd6SShawn McCarney  * The callback must have the following signature:
15448fb49dd6SShawn McCarney  *   @code
1545adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
15468fb49dd6SShawn McCarney  *   @endcode
15478fb49dd6SShawn McCarney  *
15488fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
15498fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
15508fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
15518fb49dd6SShawn McCarney  * implements ObjectManager.
15528fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
15538fb49dd6SShawn McCarney  */
15548fb49dd6SShawn McCarney template <typename Callback>
1555adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
15568fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
15578fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
15588fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
15598fb49dd6SShawn McCarney         objectMgrPaths,
15608fb49dd6SShawn McCarney     Callback&& callback)
15618fb49dd6SShawn McCarney {
1562adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
15638fb49dd6SShawn McCarney 
15648fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
15658fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
15668fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
15678fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1568adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
15698fb49dd6SShawn McCarney         if (ec)
15708fb49dd6SShawn McCarney         {
1571adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1572adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
15738fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
15748fb49dd6SShawn McCarney             return;
15758fb49dd6SShawn McCarney         }
15768fb49dd6SShawn McCarney 
1577adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1578adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1579adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1580adc4f0dbSShawn McCarney 
15818fb49dd6SShawn McCarney         // Loop through returned object paths
15828fb49dd6SShawn McCarney         std::string sensorAssocPath;
15838fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
15848fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
15858fb49dd6SShawn McCarney         {
15868fb49dd6SShawn McCarney             const std::string& objPath =
15878fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
15888fb49dd6SShawn McCarney             const boost::container::flat_map<
15898fb49dd6SShawn McCarney                 std::string, boost::container::flat_map<
15908fb49dd6SShawn McCarney                                  std::string, dbus::utility::DbusVariantType>>&
15918fb49dd6SShawn McCarney                 interfacesDict = objDictEntry.second;
15928fb49dd6SShawn McCarney 
15938fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
15948fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
15958fb49dd6SShawn McCarney             {
15968fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
15978fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
15988fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
15998fb49dd6SShawn McCarney                 {
16008fb49dd6SShawn McCarney                     // Get Association interface for object path
16018fb49dd6SShawn McCarney                     auto assocIt =
16028fb49dd6SShawn McCarney                         interfacesDict.find("xyz.openbmc_project.Association");
16038fb49dd6SShawn McCarney                     if (assocIt != interfacesDict.end())
16048fb49dd6SShawn McCarney                     {
16058fb49dd6SShawn McCarney                         // Get inventory item from end point
16068fb49dd6SShawn McCarney                         auto endpointsIt = assocIt->second.find("endpoints");
16078fb49dd6SShawn McCarney                         if (endpointsIt != assocIt->second.end())
16088fb49dd6SShawn McCarney                         {
16098fb49dd6SShawn McCarney                             const std::vector<std::string>* endpoints =
16108fb49dd6SShawn McCarney                                 std::get_if<std::vector<std::string>>(
16118fb49dd6SShawn McCarney                                     &endpointsIt->second);
16128fb49dd6SShawn McCarney                             if ((endpoints != nullptr) && !endpoints->empty())
16138fb49dd6SShawn McCarney                             {
1614adc4f0dbSShawn McCarney                                 // Add inventory item to vector
1615adc4f0dbSShawn McCarney                                 const std::string& invItemPath =
1616adc4f0dbSShawn McCarney                                     endpoints->front();
1617adc4f0dbSShawn McCarney                                 addInventoryItem(inventoryItems, invItemPath,
1618adc4f0dbSShawn McCarney                                                  sensorName);
16198fb49dd6SShawn McCarney                             }
16208fb49dd6SShawn McCarney                         }
16218fb49dd6SShawn McCarney                     }
16228fb49dd6SShawn McCarney                     break;
16238fb49dd6SShawn McCarney                 }
16248fb49dd6SShawn McCarney             }
16258fb49dd6SShawn McCarney         }
16268fb49dd6SShawn McCarney 
1627*d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1628*d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1629*d500549bSAnthony Wilson         std::string inventoryAssocPath;
1630*d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1631*d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1632*d500549bSAnthony Wilson         {
1633*d500549bSAnthony Wilson             const std::string& objPath =
1634*d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1635*d500549bSAnthony Wilson             const boost::container::flat_map<
1636*d500549bSAnthony Wilson                 std::string, boost::container::flat_map<
1637*d500549bSAnthony Wilson                                  std::string, dbus::utility::DbusVariantType>>&
1638*d500549bSAnthony Wilson                 interfacesDict = objDictEntry.second;
1639*d500549bSAnthony Wilson 
1640*d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1641*d500549bSAnthony Wilson             {
1642*d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1643*d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1644*d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1645*d500549bSAnthony Wilson                 {
1646*d500549bSAnthony Wilson                     // Get Association interface for object path
1647*d500549bSAnthony Wilson                     auto assocIt =
1648*d500549bSAnthony Wilson                         interfacesDict.find("xyz.openbmc_project.Association");
1649*d500549bSAnthony Wilson                     if (assocIt != interfacesDict.end())
1650*d500549bSAnthony Wilson                     {
1651*d500549bSAnthony Wilson                         // Get inventory item from end point
1652*d500549bSAnthony Wilson                         auto endpointsIt = assocIt->second.find("endpoints");
1653*d500549bSAnthony Wilson                         if (endpointsIt != assocIt->second.end())
1654*d500549bSAnthony Wilson                         {
1655*d500549bSAnthony Wilson                             const std::vector<std::string>* endpoints =
1656*d500549bSAnthony Wilson                                 std::get_if<std::vector<std::string>>(
1657*d500549bSAnthony Wilson                                     &endpointsIt->second);
1658*d500549bSAnthony Wilson                             if ((endpoints != nullptr) && !endpoints->empty())
1659*d500549bSAnthony Wilson                             {
1660*d500549bSAnthony Wilson                                 // Store LED path in inventory item
1661*d500549bSAnthony Wilson                                 const std::string& ledPath = endpoints->front();
1662*d500549bSAnthony Wilson                                 inventoryItem.ledObjectPath = ledPath;
1663*d500549bSAnthony Wilson                             }
1664*d500549bSAnthony Wilson                         }
1665*d500549bSAnthony Wilson                     }
1666*d500549bSAnthony Wilson                     break;
1667*d500549bSAnthony Wilson                 }
1668*d500549bSAnthony Wilson             }
1669*d500549bSAnthony Wilson         }
1670adc4f0dbSShawn McCarney         callback(inventoryItems);
1671adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
16728fb49dd6SShawn McCarney     };
16738fb49dd6SShawn McCarney 
16748fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
16758fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
16768fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
16778fb49dd6SShawn McCarney     const std::string& objectMgrPath =
16788fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
16798fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
16808fb49dd6SShawn McCarney                      << objectMgrPath;
16818fb49dd6SShawn McCarney 
16828fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
16838fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
16848fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
16858fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16868fb49dd6SShawn McCarney 
1687adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
16888fb49dd6SShawn McCarney }
16898fb49dd6SShawn McCarney 
16908fb49dd6SShawn McCarney /**
1691*d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1692*d500549bSAnthony Wilson  *
1693*d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1694*d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1695*d500549bSAnthony Wilson  * inventoryItems vector.
1696*d500549bSAnthony Wilson  *
1697*d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1698*d500549bSAnthony Wilson  * response.
1699*d500549bSAnthony Wilson  *
1700*d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1701*d500549bSAnthony Wilson  * has been obtained.
1702*d500549bSAnthony Wilson  *
1703*d500549bSAnthony Wilson  * The callback must have the following signature:
1704*d500549bSAnthony Wilson  *   @code
1705*d500549bSAnthony Wilson  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1706*d500549bSAnthony Wilson  *   @endcode
1707*d500549bSAnthony Wilson  *
1708*d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1709*d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1710*d500549bSAnthony Wilson  * last asynchronous function has completed.
1711*d500549bSAnthony Wilson  *
1712*d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1713*d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1714*d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1715*d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1716*d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1717*d500549bSAnthony Wilson  * in recursive calls to this function.
1718*d500549bSAnthony Wilson  */
1719*d500549bSAnthony Wilson template <typename Callback>
1720*d500549bSAnthony Wilson void getInventoryLedData(
1721*d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1722*d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1723*d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1724*d500549bSAnthony Wilson         ledConnections,
1725*d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1726*d500549bSAnthony Wilson {
1727*d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1728*d500549bSAnthony Wilson 
1729*d500549bSAnthony Wilson     // If no more connections left, call callback
1730*d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1731*d500549bSAnthony Wilson     {
1732*d500549bSAnthony Wilson         callback(inventoryItems);
1733*d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1734*d500549bSAnthony Wilson         return;
1735*d500549bSAnthony Wilson     }
1736*d500549bSAnthony Wilson 
1737*d500549bSAnthony Wilson     // Get inventory item data from current connection
1738*d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1739*d500549bSAnthony Wilson     if (it != ledConnections->end())
1740*d500549bSAnthony Wilson     {
1741*d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1742*d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1743*d500549bSAnthony Wilson         // Response handler for Get State property
1744*d500549bSAnthony Wilson         auto respHandler =
1745*d500549bSAnthony Wilson             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1746*d500549bSAnthony Wilson              callback{std::move(callback)},
1747*d500549bSAnthony Wilson              ledConnectionsIndex](const boost::system::error_code ec,
1748*d500549bSAnthony Wilson                                   const std::variant<std::string>& ledState) {
1749*d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1750*d500549bSAnthony Wilson                 if (ec)
1751*d500549bSAnthony Wilson                 {
1752*d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
1753*d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
1754*d500549bSAnthony Wilson                     messages::internalError(sensorsAsyncResp->res);
1755*d500549bSAnthony Wilson                     return;
1756*d500549bSAnthony Wilson                 }
1757*d500549bSAnthony Wilson 
1758*d500549bSAnthony Wilson                 const std::string* state = std::get_if<std::string>(&ledState);
1759*d500549bSAnthony Wilson                 if (state != nullptr)
1760*d500549bSAnthony Wilson                 {
1761*d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Led state: " << *state;
1762*d500549bSAnthony Wilson                     // Find inventory item with this LED object path
1763*d500549bSAnthony Wilson                     InventoryItem* inventoryItem =
1764*d500549bSAnthony Wilson                         findInventoryItemForLed(*inventoryItems, ledPath);
1765*d500549bSAnthony Wilson                     if (inventoryItem != nullptr)
1766*d500549bSAnthony Wilson                     {
1767*d500549bSAnthony Wilson                         // Store LED state in InventoryItem
1768*d500549bSAnthony Wilson                         if (boost::ends_with(*state, "On"))
1769*d500549bSAnthony Wilson                         {
1770*d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::ON;
1771*d500549bSAnthony Wilson                         }
1772*d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Blink"))
1773*d500549bSAnthony Wilson                         {
1774*d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::BLINK;
1775*d500549bSAnthony Wilson                         }
1776*d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Off"))
1777*d500549bSAnthony Wilson                         {
1778*d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::OFF;
1779*d500549bSAnthony Wilson                         }
1780*d500549bSAnthony Wilson                         else
1781*d500549bSAnthony Wilson                         {
1782*d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::UNKNOWN;
1783*d500549bSAnthony Wilson                         }
1784*d500549bSAnthony Wilson                     }
1785*d500549bSAnthony Wilson                 }
1786*d500549bSAnthony Wilson                 else
1787*d500549bSAnthony Wilson                 {
1788*d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
1789*d500549bSAnthony Wilson                                      << ledPath;
1790*d500549bSAnthony Wilson                 }
1791*d500549bSAnthony Wilson 
1792*d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1793*d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1794*d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1795*d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1796*d500549bSAnthony Wilson 
1797*d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1798*d500549bSAnthony Wilson             };
1799*d500549bSAnthony Wilson 
1800*d500549bSAnthony Wilson         // Get the State property for the current LED
1801*d500549bSAnthony Wilson         crow::connections::systemBus->async_method_call(
1802*d500549bSAnthony Wilson             std::move(respHandler), ledConnection, ledPath,
1803*d500549bSAnthony Wilson             "org.freedesktop.DBus.Properties", "Get",
1804*d500549bSAnthony Wilson             "xyz.openbmc_project.Led.Physical", "State");
1805*d500549bSAnthony Wilson     }
1806*d500549bSAnthony Wilson 
1807*d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1808*d500549bSAnthony Wilson }
1809*d500549bSAnthony Wilson 
1810*d500549bSAnthony Wilson /**
1811*d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1812*d500549bSAnthony Wilson  *
1813*d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1814*d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1815*d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1816*d500549bSAnthony Wilson  *
1817*d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1818*d500549bSAnthony Wilson  * response.
1819*d500549bSAnthony Wilson  *
1820*d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1821*d500549bSAnthony Wilson  * been obtained.
1822*d500549bSAnthony Wilson  *
1823*d500549bSAnthony Wilson  * The callback must have the following signature:
1824*d500549bSAnthony Wilson  *   @code
1825*d500549bSAnthony Wilson  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1826*d500549bSAnthony Wilson  *   @endcode
1827*d500549bSAnthony Wilson  *
1828*d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1829*d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1830*d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1831*d500549bSAnthony Wilson  */
1832*d500549bSAnthony Wilson template <typename Callback>
1833*d500549bSAnthony Wilson void getInventoryLeds(
1834*d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1835*d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1836*d500549bSAnthony Wilson     Callback&& callback)
1837*d500549bSAnthony Wilson {
1838*d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1839*d500549bSAnthony Wilson 
1840*d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1841*d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
1842*d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1843*d500549bSAnthony Wilson 
1844*d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
1845*d500549bSAnthony Wilson     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1846*d500549bSAnthony Wilson                         inventoryItems](const boost::system::error_code ec,
1847*d500549bSAnthony Wilson                                         const GetSubTreeType& subtree) {
1848*d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1849*d500549bSAnthony Wilson         if (ec)
1850*d500549bSAnthony Wilson         {
1851*d500549bSAnthony Wilson             messages::internalError(sensorsAsyncResp->res);
1852*d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1853*d500549bSAnthony Wilson                              << ec;
1854*d500549bSAnthony Wilson             return;
1855*d500549bSAnthony Wilson         }
1856*d500549bSAnthony Wilson 
1857*d500549bSAnthony Wilson         // Build map of LED object paths to connections
1858*d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1859*d500549bSAnthony Wilson             ledConnections = std::make_shared<
1860*d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
1861*d500549bSAnthony Wilson 
1862*d500549bSAnthony Wilson         // Loop through objects from GetSubTree
1863*d500549bSAnthony Wilson         for (const std::pair<
1864*d500549bSAnthony Wilson                  std::string,
1865*d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1866*d500549bSAnthony Wilson                  object : subtree)
1867*d500549bSAnthony Wilson         {
1868*d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
1869*d500549bSAnthony Wilson             // items
1870*d500549bSAnthony Wilson             const std::string& ledPath = object.first;
1871*d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1872*d500549bSAnthony Wilson             {
1873*d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
1874*d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
1875*d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
1876*d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1877*d500549bSAnthony Wilson                                  << connection;
1878*d500549bSAnthony Wilson             }
1879*d500549bSAnthony Wilson         }
1880*d500549bSAnthony Wilson 
1881*d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1882*d500549bSAnthony Wilson                             std::move(callback));
1883*d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1884*d500549bSAnthony Wilson     };
1885*d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
1886*d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
1887*d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1888*d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
1889*d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1890*d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1891*d500549bSAnthony Wilson }
1892*d500549bSAnthony Wilson 
1893*d500549bSAnthony Wilson /**
1894adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
18958fb49dd6SShawn McCarney  *
18968fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
1897adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
18988fb49dd6SShawn McCarney  *
1899adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1900adc4f0dbSShawn McCarney  * response.
19018fb49dd6SShawn McCarney  *
1902adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
1903adc4f0dbSShawn McCarney  * inventory items have been obtained.
1904adc4f0dbSShawn McCarney  *
1905adc4f0dbSShawn McCarney  * The callback must have the following signature:
1906adc4f0dbSShawn McCarney  *   @code
1907adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1908adc4f0dbSShawn McCarney  *   @endcode
19098fb49dd6SShawn McCarney  *
19108fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
19118fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
19128fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
19138fb49dd6SShawn McCarney  * implements ObjectManager.
1914adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
19158fb49dd6SShawn McCarney  */
1916adc4f0dbSShawn McCarney template <typename Callback>
1917adc4f0dbSShawn McCarney static void getInventoryItems(
19188fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
19198fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
19208fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1921adc4f0dbSShawn McCarney         objectMgrPaths,
1922adc4f0dbSShawn McCarney     Callback&& callback)
19238fb49dd6SShawn McCarney {
1924adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
1925adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
1926adc4f0dbSShawn McCarney         [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
1927adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
1928adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
19298fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
1930adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
1931adc4f0dbSShawn McCarney                  callback{std::move(callback)}](
19328fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
19338fb49dd6SShawn McCarney                         invConnections) {
19348fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
1935*d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
1936*d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
1937*d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
1938*d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
1939*d500549bSAnthony Wilson                             // Find led connections and get the data
1940*d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
1941*d500549bSAnthony Wilson                                              std::move(callback));
1942*d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
1943*d500549bSAnthony Wilson                         };
19448fb49dd6SShawn McCarney 
1945adc4f0dbSShawn McCarney                     // Get inventory item data from connections
1946adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1947adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
1948*d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
19498fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
19508fb49dd6SShawn McCarney                 };
19518fb49dd6SShawn McCarney 
1952adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
19538fb49dd6SShawn McCarney             getInventoryItemsConnections(
1954adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
19558fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
1956adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
19578fb49dd6SShawn McCarney         };
19588fb49dd6SShawn McCarney 
1959adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
1960adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
1961adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
1962adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
1963adc4f0dbSShawn McCarney }
1964adc4f0dbSShawn McCarney 
1965adc4f0dbSShawn McCarney /**
1966adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
1967adc4f0dbSShawn McCarney  *
1968adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
1969adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
1970adc4f0dbSShawn McCarney  * array.
1971adc4f0dbSShawn McCarney  *
1972adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
1973adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
1974adc4f0dbSShawn McCarney  * object.
1975adc4f0dbSShawn McCarney  *
1976adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
1977adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
1978adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
1979adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
1980adc4f0dbSShawn McCarney  */
1981adc4f0dbSShawn McCarney static nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
1982adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
1983adc4f0dbSShawn McCarney                                       const std::string& chassisId)
1984adc4f0dbSShawn McCarney {
1985adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
1986adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
1987adc4f0dbSShawn McCarney     {
1988adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
1989adc4f0dbSShawn McCarney         {
1990adc4f0dbSShawn McCarney             return powerSupply;
1991adc4f0dbSShawn McCarney         }
1992adc4f0dbSShawn McCarney     }
1993adc4f0dbSShawn McCarney 
1994adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
1995adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
1996adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
1997adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
1998adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
1999adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2000adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2001adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2002adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2003adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2004adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2005*d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2006adc4f0dbSShawn McCarney     powerSupply["Status"]["State"] = getState(&inventoryItem);
2007adc4f0dbSShawn McCarney 
2008adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2009adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2010adc4f0dbSShawn McCarney 
2011adc4f0dbSShawn McCarney     return powerSupply;
20128fb49dd6SShawn McCarney }
20138fb49dd6SShawn McCarney 
20148fb49dd6SShawn McCarney /**
2015de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2016de629b6eSShawn McCarney  *
2017de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2018de629b6eSShawn McCarney  *
2019de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2020de629b6eSShawn McCarney  * information has been obtained.
2021de629b6eSShawn McCarney  *
2022adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2023de629b6eSShawn McCarney  *
2024de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2025de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2026de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2027de629b6eSShawn McCarney  *
2028de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2029de629b6eSShawn McCarney  *
2030de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2031de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2032de629b6eSShawn McCarney  *
2033adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2034adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2035adc4f0dbSShawn McCarney  *
2036de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2037adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2038de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2039de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2040de629b6eSShawn McCarney  * implements ObjectManager.
2041adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2042de629b6eSShawn McCarney  */
2043de629b6eSShawn McCarney void getSensorData(
2044de629b6eSShawn McCarney     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
204549c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2046de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
20478fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2048adc4f0dbSShawn McCarney         objectMgrPaths,
2049adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2050de629b6eSShawn McCarney {
2051de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2052de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2053de629b6eSShawn McCarney     for (const std::string& connection : connections)
2054de629b6eSShawn McCarney     {
2055de629b6eSShawn McCarney         // Response handler to process managed objects
20568fb49dd6SShawn McCarney         auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames,
2057adc4f0dbSShawn McCarney                                     inventoryItems](
2058de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2059de629b6eSShawn McCarney                                        ManagedObjectsVectorType& resp) {
2060de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2061de629b6eSShawn McCarney             if (ec)
2062de629b6eSShawn McCarney             {
2063de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
2064de629b6eSShawn McCarney                 messages::internalError(SensorsAsyncResp->res);
2065de629b6eSShawn McCarney                 return;
2066de629b6eSShawn McCarney             }
2067de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2068de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2069de629b6eSShawn McCarney             {
2070de629b6eSShawn McCarney                 const std::string& objPath =
2071de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2072de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2073de629b6eSShawn McCarney                                  << objPath;
2074de629b6eSShawn McCarney 
2075de629b6eSShawn McCarney                 std::vector<std::string> split;
2076de629b6eSShawn McCarney                 // Reserve space for
2077de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2078de629b6eSShawn McCarney                 split.reserve(6);
2079de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2080de629b6eSShawn McCarney                 if (split.size() < 6)
2081de629b6eSShawn McCarney                 {
2082de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2083de629b6eSShawn McCarney                                      << objPath;
2084de629b6eSShawn McCarney                     continue;
2085de629b6eSShawn McCarney                 }
2086de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2087de629b6eSShawn McCarney                 // string at the beginning
2088de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2089de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2090de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2091de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
209249c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2093de629b6eSShawn McCarney                 {
2094de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2095de629b6eSShawn McCarney                     continue;
2096de629b6eSShawn McCarney                 }
2097de629b6eSShawn McCarney 
2098adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2099adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2100adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2101adc4f0dbSShawn McCarney 
210295a3ecadSAnthony Wilson                 const std::string& sensorSchema =
210395a3ecadSAnthony Wilson                     SensorsAsyncResp->chassisSubNode;
210495a3ecadSAnthony Wilson 
210595a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
210695a3ecadSAnthony Wilson 
210795a3ecadSAnthony Wilson                 if (sensorSchema == "Sensors")
210895a3ecadSAnthony Wilson                 {
210995a3ecadSAnthony Wilson                     SensorsAsyncResp->res.jsonValue["@odata.id"] =
211095a3ecadSAnthony Wilson                         "/redfish/v1/Chassis/" + SensorsAsyncResp->chassisId +
211195a3ecadSAnthony Wilson                         "/" + SensorsAsyncResp->chassisSubNode + "/" +
211295a3ecadSAnthony Wilson                         sensorName;
211395a3ecadSAnthony Wilson                     sensorJson = &(SensorsAsyncResp->res.jsonValue);
211495a3ecadSAnthony Wilson                 }
211595a3ecadSAnthony Wilson                 else
211695a3ecadSAnthony Wilson                 {
2117271584abSEd Tanous                     std::string fieldName;
2118de629b6eSShawn McCarney                     if (sensorType == "temperature")
2119de629b6eSShawn McCarney                     {
2120de629b6eSShawn McCarney                         fieldName = "Temperatures";
2121de629b6eSShawn McCarney                     }
2122de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2123de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2124de629b6eSShawn McCarney                     {
2125de629b6eSShawn McCarney                         fieldName = "Fans";
2126de629b6eSShawn McCarney                     }
2127de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2128de629b6eSShawn McCarney                     {
2129de629b6eSShawn McCarney                         fieldName = "Voltages";
2130de629b6eSShawn McCarney                     }
2131de629b6eSShawn McCarney                     else if (sensorType == "power")
2132de629b6eSShawn McCarney                     {
2133028f7ebcSEddie James                         if (!sensorName.compare("total_power"))
2134028f7ebcSEddie James                         {
2135028f7ebcSEddie James                             fieldName = "PowerControl";
2136028f7ebcSEddie James                         }
2137adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2138adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2139028f7ebcSEddie James                         {
2140de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2141de629b6eSShawn McCarney                         }
2142adc4f0dbSShawn McCarney                         else
2143adc4f0dbSShawn McCarney                         {
2144adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2145adc4f0dbSShawn McCarney                             continue;
2146adc4f0dbSShawn McCarney                         }
2147028f7ebcSEddie James                     }
2148de629b6eSShawn McCarney                     else
2149de629b6eSShawn McCarney                     {
2150de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2151de629b6eSShawn McCarney                                          << sensorType;
2152de629b6eSShawn McCarney                         continue;
2153de629b6eSShawn McCarney                     }
2154de629b6eSShawn McCarney 
2155de629b6eSShawn McCarney                     nlohmann::json& tempArray =
2156de629b6eSShawn McCarney                         SensorsAsyncResp->res.jsonValue[fieldName];
2157adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
215849c53ac9SJohnathan Mantey                     {
2159adc4f0dbSShawn McCarney                         if (tempArray.empty())
21607ab06f49SGunnar Mills                         {
216195a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
216295a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
216395a3ecadSAnthony Wilson                             // naming in power.hpp.
21647ab06f49SGunnar Mills                             tempArray.push_back(
2165adc4f0dbSShawn McCarney                                 {{"@odata.id",
2166adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
21677ab06f49SGunnar Mills                                       SensorsAsyncResp->chassisId + "/" +
2168adc4f0dbSShawn McCarney                                       SensorsAsyncResp->chassisSubNode + "#/" +
2169adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2170adc4f0dbSShawn McCarney                         }
2171adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2172adc4f0dbSShawn McCarney                     }
2173adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2174adc4f0dbSShawn McCarney                     {
2175adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2176adc4f0dbSShawn McCarney                         {
2177adc4f0dbSShawn McCarney                             sensorJson =
2178adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
2179adc4f0dbSShawn McCarney                                                  SensorsAsyncResp->chassisId));
2180adc4f0dbSShawn McCarney                         }
218149c53ac9SJohnathan Mantey                     }
218249c53ac9SJohnathan Mantey                     else
218349c53ac9SJohnathan Mantey                     {
2184de629b6eSShawn McCarney                         tempArray.push_back(
218595a3ecadSAnthony Wilson                             {{"@odata.id",
218695a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
218749c53ac9SJohnathan Mantey                                   SensorsAsyncResp->chassisId + "/" +
218895a3ecadSAnthony Wilson                                   SensorsAsyncResp->chassisSubNode + "#/" +
218995a3ecadSAnthony Wilson                                   fieldName + "/"}});
2190adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
219149c53ac9SJohnathan Mantey                     }
219295a3ecadSAnthony Wilson                 }
2193de629b6eSShawn McCarney 
2194adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2195adc4f0dbSShawn McCarney                 {
2196de629b6eSShawn McCarney                     objectInterfacesToJson(sensorName, sensorType,
219795a3ecadSAnthony Wilson                                            SensorsAsyncResp->chassisSubNode,
2198adc4f0dbSShawn McCarney                                            objDictEntry.second, *sensorJson,
2199adc4f0dbSShawn McCarney                                            inventoryItem);
2200adc4f0dbSShawn McCarney                 }
2201de629b6eSShawn McCarney             }
220249c53ac9SJohnathan Mantey             if (SensorsAsyncResp.use_count() == 1)
220349c53ac9SJohnathan Mantey             {
220449c53ac9SJohnathan Mantey                 sortJSONResponse(SensorsAsyncResp);
220549c53ac9SJohnathan Mantey                 if (SensorsAsyncResp->chassisSubNode == "Thermal")
22068bd25ccdSJames Feist                 {
22078bd25ccdSJames Feist                     populateFanRedundancy(SensorsAsyncResp);
22088bd25ccdSJames Feist                 }
220949c53ac9SJohnathan Mantey             }
2210de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2211de629b6eSShawn McCarney         };
2212de629b6eSShawn McCarney 
2213de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2214de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
22158fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2216de629b6eSShawn McCarney         const std::string& objectMgrPath =
22178fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2218de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2219de629b6eSShawn McCarney                          << objectMgrPath;
2220de629b6eSShawn McCarney 
2221de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2222de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2223de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
2224de629b6eSShawn McCarney     };
2225de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2226de629b6eSShawn McCarney }
2227de629b6eSShawn McCarney 
222895a3ecadSAnthony Wilson void processSensorList(
222995a3ecadSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
223095a3ecadSAnthony Wilson     std::shared_ptr<boost::container::flat_set<std::string>> sensorNames)
22311abe55efSEd Tanous {
223295a3ecadSAnthony Wilson     auto getConnectionCb =
223395a3ecadSAnthony Wilson         [SensorsAsyncResp, sensorNames](
223495a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
223555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2236de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
223749c53ac9SJohnathan Mantey                 [SensorsAsyncResp, sensorNames, connections](
223895a3ecadSAnthony Wilson                     std::shared_ptr<
223995a3ecadSAnthony Wilson                         boost::container::flat_map<std::string, std::string>>
22408fb49dd6SShawn McCarney                         objectMgrPaths) {
2241de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2242adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
2243adc4f0dbSShawn McCarney                         [SensorsAsyncResp, sensorNames, connections,
2244adc4f0dbSShawn McCarney                          objectMgrPaths](
2245adc4f0dbSShawn McCarney                             std::shared_ptr<std::vector<InventoryItem>>
2246adc4f0dbSShawn McCarney                                 inventoryItems) {
2247adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
224849c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
2249de629b6eSShawn McCarney                             getSensorData(SensorsAsyncResp, sensorNames,
2250adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2251adc4f0dbSShawn McCarney                                           inventoryItems);
2252adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2253adc4f0dbSShawn McCarney                         };
2254adc4f0dbSShawn McCarney 
2255adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
2256adc4f0dbSShawn McCarney                     getInventoryItems(SensorsAsyncResp, sensorNames,
2257adc4f0dbSShawn McCarney                                       objectMgrPaths,
2258adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2259adc4f0dbSShawn McCarney 
2260de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
226108777fb0SLewanczyk, Dawid                 };
2262de629b6eSShawn McCarney 
226349c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
226449c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
2265de629b6eSShawn McCarney             getObjectManagerPaths(SensorsAsyncResp,
2266de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
226755c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
226808777fb0SLewanczyk, Dawid         };
2269de629b6eSShawn McCarney 
2270de629b6eSShawn McCarney     // Get set of connections that provide sensor values
227195a3ecadSAnthony Wilson     getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb));
227295a3ecadSAnthony Wilson }
227395a3ecadSAnthony Wilson 
227495a3ecadSAnthony Wilson /**
227595a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
227695a3ecadSAnthony Wilson  *        chassis.
227795a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
227895a3ecadSAnthony Wilson  */
227995a3ecadSAnthony Wilson void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
228095a3ecadSAnthony Wilson {
228195a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
228295a3ecadSAnthony Wilson     auto getChassisCb =
228395a3ecadSAnthony Wilson         [SensorsAsyncResp](
228495a3ecadSAnthony Wilson             std::shared_ptr<boost::container::flat_set<std::string>>
228595a3ecadSAnthony Wilson                 sensorNames) {
228695a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
228795a3ecadSAnthony Wilson             processSensorList(SensorsAsyncResp, sensorNames);
228855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
228908777fb0SLewanczyk, Dawid         };
22904f9a2130SJennifer Lee     SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
229108777fb0SLewanczyk, Dawid 
229226f03899SShawn McCarney     // Get set of sensors in chassis
2293588c3f0dSKowalski, Kamil     getChassis(SensorsAsyncResp, std::move(getChassisCb));
229455c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2295271584abSEd Tanous }
229608777fb0SLewanczyk, Dawid 
2297413961deSRichard Marian Thomaiyar /**
229849c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
229949c53ac9SJohnathan Mantey  * the chassis node
230049c53ac9SJohnathan Mantey  *
230149c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
230249c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
230349c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
230449c53ac9SJohnathan Mantey  *                         repeated calls to this function
230549c53ac9SJohnathan Mantey  */
230649c53ac9SJohnathan Mantey bool findSensorNameUsingSensorPath(
23070a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
230849c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
230949c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
231049c53ac9SJohnathan Mantey {
23110a86febdSRichard Marian Thomaiyar     for (std::string_view chassisSensor : sensorsList)
231249c53ac9SJohnathan Mantey     {
23130a86febdSRichard Marian Thomaiyar         std::size_t pos = chassisSensor.rfind("/");
23140a86febdSRichard Marian Thomaiyar         if (pos >= (chassisSensor.size() - 1))
231549c53ac9SJohnathan Mantey         {
231649c53ac9SJohnathan Mantey             continue;
231749c53ac9SJohnathan Mantey         }
23180a86febdSRichard Marian Thomaiyar         std::string_view thisSensorName = chassisSensor.substr(pos + 1);
231949c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
232049c53ac9SJohnathan Mantey         {
232149c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
232249c53ac9SJohnathan Mantey             return true;
232349c53ac9SJohnathan Mantey         }
232449c53ac9SJohnathan Mantey     }
232549c53ac9SJohnathan Mantey     return false;
232649c53ac9SJohnathan Mantey }
232749c53ac9SJohnathan Mantey 
232849c53ac9SJohnathan Mantey /**
2329413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2330413961deSRichard Marian Thomaiyar  *
2331413961deSRichard Marian Thomaiyar  * @param res   response object
2332413961deSRichard Marian Thomaiyar  * @param req   request object
2333413961deSRichard Marian Thomaiyar  * @param params   parameter passed for CRUD
2334413961deSRichard Marian Thomaiyar  * @param typeList   TypeList of sensors for the resource queried
2335413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2336413961deSRichard Marian Thomaiyar  */
2337413961deSRichard Marian Thomaiyar void setSensorOverride(crow::Response& res, const crow::Request& req,
2338413961deSRichard Marian Thomaiyar                        const std::vector<std::string>& params,
233985e1424fSEd Tanous                        const std::vector<const char*> typeList,
2340413961deSRichard Marian Thomaiyar                        const std::string& chassisSubNode)
2341413961deSRichard Marian Thomaiyar {
2342413961deSRichard Marian Thomaiyar 
2343413961deSRichard Marian Thomaiyar     // TODO: Need to figure out dynamic way to restrict patch (Set Sensor
2344413961deSRichard Marian Thomaiyar     // override) based on another d-bus announcement to be more generic.
2345413961deSRichard Marian Thomaiyar     if (params.size() != 1)
2346413961deSRichard Marian Thomaiyar     {
2347413961deSRichard Marian Thomaiyar         messages::internalError(res);
2348413961deSRichard Marian Thomaiyar         res.end();
2349413961deSRichard Marian Thomaiyar         return;
2350413961deSRichard Marian Thomaiyar     }
2351f65af9e8SRichard Marian Thomaiyar 
2352f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::vector<nlohmann::json>> allCollections;
2353f65af9e8SRichard Marian Thomaiyar     std::optional<std::vector<nlohmann::json>> temperatureCollections;
2354f65af9e8SRichard Marian Thomaiyar     std::optional<std::vector<nlohmann::json>> fanCollections;
2355f65af9e8SRichard Marian Thomaiyar     std::vector<nlohmann::json> voltageCollections;
2356f65af9e8SRichard Marian Thomaiyar     BMCWEB_LOG_INFO << "setSensorOverride for subNode" << chassisSubNode
2357f65af9e8SRichard Marian Thomaiyar                     << "\n";
2358f65af9e8SRichard Marian Thomaiyar 
2359413961deSRichard Marian Thomaiyar     if (chassisSubNode == "Thermal")
2360413961deSRichard Marian Thomaiyar     {
2361f65af9e8SRichard Marian Thomaiyar         if (!json_util::readJson(req, res, "Temperatures",
2362f65af9e8SRichard Marian Thomaiyar                                  temperatureCollections, "Fans",
2363f65af9e8SRichard Marian Thomaiyar                                  fanCollections))
2364f65af9e8SRichard Marian Thomaiyar         {
2365f65af9e8SRichard Marian Thomaiyar             return;
2366f65af9e8SRichard Marian Thomaiyar         }
2367f65af9e8SRichard Marian Thomaiyar         if (!temperatureCollections && !fanCollections)
2368f65af9e8SRichard Marian Thomaiyar         {
2369f65af9e8SRichard Marian Thomaiyar             messages::resourceNotFound(res, "Thermal",
2370f65af9e8SRichard Marian Thomaiyar                                        "Temperatures / Voltages");
2371f65af9e8SRichard Marian Thomaiyar             res.end();
2372f65af9e8SRichard Marian Thomaiyar             return;
2373f65af9e8SRichard Marian Thomaiyar         }
2374f65af9e8SRichard Marian Thomaiyar         if (temperatureCollections)
2375f65af9e8SRichard Marian Thomaiyar         {
2376f65af9e8SRichard Marian Thomaiyar             allCollections.emplace("Temperatures",
2377f65af9e8SRichard Marian Thomaiyar                                    *std::move(temperatureCollections));
2378f65af9e8SRichard Marian Thomaiyar         }
2379f65af9e8SRichard Marian Thomaiyar         if (fanCollections)
2380f65af9e8SRichard Marian Thomaiyar         {
2381f65af9e8SRichard Marian Thomaiyar             allCollections.emplace("Fans", *std::move(fanCollections));
2382f65af9e8SRichard Marian Thomaiyar         }
2383413961deSRichard Marian Thomaiyar     }
2384413961deSRichard Marian Thomaiyar     else if (chassisSubNode == "Power")
2385413961deSRichard Marian Thomaiyar     {
2386f65af9e8SRichard Marian Thomaiyar         if (!json_util::readJson(req, res, "Voltages", voltageCollections))
2387f65af9e8SRichard Marian Thomaiyar         {
2388f65af9e8SRichard Marian Thomaiyar             return;
2389f65af9e8SRichard Marian Thomaiyar         }
2390f65af9e8SRichard Marian Thomaiyar         allCollections.emplace("Voltages", std::move(voltageCollections));
2391413961deSRichard Marian Thomaiyar     }
2392413961deSRichard Marian Thomaiyar     else
2393413961deSRichard Marian Thomaiyar     {
2394413961deSRichard Marian Thomaiyar         res.result(boost::beast::http::status::not_found);
2395413961deSRichard Marian Thomaiyar         res.end();
2396413961deSRichard Marian Thomaiyar         return;
2397413961deSRichard Marian Thomaiyar     }
2398413961deSRichard Marian Thomaiyar 
2399f65af9e8SRichard Marian Thomaiyar     const char* propertyValueName;
2400f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2401413961deSRichard Marian Thomaiyar     std::string memberId;
2402413961deSRichard Marian Thomaiyar     double value;
2403f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2404f65af9e8SRichard Marian Thomaiyar     {
2405f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2406f65af9e8SRichard Marian Thomaiyar         {
2407f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2408f65af9e8SRichard Marian Thomaiyar         }
2409f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2410f65af9e8SRichard Marian Thomaiyar         {
2411f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2412f65af9e8SRichard Marian Thomaiyar         }
2413f65af9e8SRichard Marian Thomaiyar         else
2414f65af9e8SRichard Marian Thomaiyar         {
2415f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2416f65af9e8SRichard Marian Thomaiyar         }
2417f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2418f65af9e8SRichard Marian Thomaiyar         {
2419f65af9e8SRichard Marian Thomaiyar             if (!json_util::readJson(item, res, "MemberId", memberId,
2420413961deSRichard Marian Thomaiyar                                      propertyValueName, value))
2421413961deSRichard Marian Thomaiyar             {
2422413961deSRichard Marian Thomaiyar                 return;
2423413961deSRichard Marian Thomaiyar             }
2424f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2425f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2426f65af9e8SRichard Marian Thomaiyar         }
2427f65af9e8SRichard Marian Thomaiyar     }
2428413961deSRichard Marian Thomaiyar     const std::string& chassisName = params[0];
2429413961deSRichard Marian Thomaiyar     auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
2430413961deSRichard Marian Thomaiyar         res, chassisName, typeList, chassisSubNode);
243149c53ac9SJohnathan Mantey     auto getChassisSensorListCb = [sensorAsyncResp,
243249c53ac9SJohnathan Mantey                                    overrideMap](const std::shared_ptr<
243349c53ac9SJohnathan Mantey                                                 boost::container::flat_set<
243449c53ac9SJohnathan Mantey                                                     std::string>>
243549c53ac9SJohnathan Mantey                                                     sensorsList) {
243649c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
243749c53ac9SJohnathan Mantey         // chassis node
243849c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
243949c53ac9SJohnathan Mantey             sensorNames =
244049c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2441f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2442413961deSRichard Marian Thomaiyar         {
2443f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
244449c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
244549c53ac9SJohnathan Mantey                                                *sensorNames))
2446f65af9e8SRichard Marian Thomaiyar             {
2447f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
2448413961deSRichard Marian Thomaiyar                 messages::resourceNotFound(sensorAsyncResp->res,
2449f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2450413961deSRichard Marian Thomaiyar                 return;
2451413961deSRichard Marian Thomaiyar             }
2452f65af9e8SRichard Marian Thomaiyar         }
2453413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2454413961deSRichard Marian Thomaiyar         auto getObjectsWithConnectionCb =
2455f65af9e8SRichard Marian Thomaiyar             [sensorAsyncResp, overrideMap](
2456413961deSRichard Marian Thomaiyar                 const boost::container::flat_set<std::string>& connections,
2457413961deSRichard Marian Thomaiyar                 const std::set<std::pair<std::string, std::string>>&
2458413961deSRichard Marian Thomaiyar                     objectsWithConnection) {
2459f65af9e8SRichard Marian Thomaiyar                 if (objectsWithConnection.size() != overrideMap.size())
2460413961deSRichard Marian Thomaiyar                 {
2461413961deSRichard Marian Thomaiyar                     BMCWEB_LOG_INFO
2462f65af9e8SRichard Marian Thomaiyar                         << "Unable to find all objects with proper connection "
2463f65af9e8SRichard Marian Thomaiyar                         << objectsWithConnection.size() << " requested "
2464f65af9e8SRichard Marian Thomaiyar                         << overrideMap.size() << "\n";
2465413961deSRichard Marian Thomaiyar                     messages::resourceNotFound(
2466413961deSRichard Marian Thomaiyar                         sensorAsyncResp->res,
2467413961deSRichard Marian Thomaiyar                         sensorAsyncResp->chassisSubNode == "Thermal"
2468413961deSRichard Marian Thomaiyar                             ? "Temperatures"
2469413961deSRichard Marian Thomaiyar                             : "Voltages",
2470f65af9e8SRichard Marian Thomaiyar                         "Count");
2471f65af9e8SRichard Marian Thomaiyar                     return;
2472f65af9e8SRichard Marian Thomaiyar                 }
2473f65af9e8SRichard Marian Thomaiyar                 for (const auto& item : objectsWithConnection)
2474f65af9e8SRichard Marian Thomaiyar                 {
2475f65af9e8SRichard Marian Thomaiyar 
2476f65af9e8SRichard Marian Thomaiyar                     auto lastPos = item.first.rfind('/');
2477f65af9e8SRichard Marian Thomaiyar                     if (lastPos == std::string::npos)
2478f65af9e8SRichard Marian Thomaiyar                     {
2479f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2480f65af9e8SRichard Marian Thomaiyar                         return;
2481f65af9e8SRichard Marian Thomaiyar                     }
2482f65af9e8SRichard Marian Thomaiyar                     std::string sensorName = item.first.substr(lastPos + 1);
2483f65af9e8SRichard Marian Thomaiyar 
2484f65af9e8SRichard Marian Thomaiyar                     const auto& iterator = overrideMap.find(sensorName);
2485f65af9e8SRichard Marian Thomaiyar                     if (iterator == overrideMap.end())
2486f65af9e8SRichard Marian Thomaiyar                     {
2487f65af9e8SRichard Marian Thomaiyar                         BMCWEB_LOG_INFO << "Unable to find sensor object"
2488f65af9e8SRichard Marian Thomaiyar                                         << item.first << "\n";
2489f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2490413961deSRichard Marian Thomaiyar                         return;
2491413961deSRichard Marian Thomaiyar                     }
2492413961deSRichard Marian Thomaiyar                     crow::connections::systemBus->async_method_call(
2493f65af9e8SRichard Marian Thomaiyar                         [sensorAsyncResp](const boost::system::error_code ec) {
2494413961deSRichard Marian Thomaiyar                             if (ec)
2495413961deSRichard Marian Thomaiyar                             {
2496413961deSRichard Marian Thomaiyar                                 BMCWEB_LOG_DEBUG
2497f65af9e8SRichard Marian Thomaiyar                                     << "setOverrideValueStatus DBUS error: "
2498413961deSRichard Marian Thomaiyar                                     << ec;
2499413961deSRichard Marian Thomaiyar                                 messages::internalError(sensorAsyncResp->res);
2500413961deSRichard Marian Thomaiyar                                 return;
2501413961deSRichard Marian Thomaiyar                             }
2502413961deSRichard Marian Thomaiyar                         },
2503f65af9e8SRichard Marian Thomaiyar                         item.second, item.first,
2504413961deSRichard Marian Thomaiyar                         "org.freedesktop.DBus.Properties", "Set",
2505413961deSRichard Marian Thomaiyar                         "xyz.openbmc_project.Sensor.Value", "Value",
2506f65af9e8SRichard Marian Thomaiyar                         sdbusplus::message::variant<double>(
2507f65af9e8SRichard Marian Thomaiyar                             iterator->second.first));
2508f65af9e8SRichard Marian Thomaiyar                 }
2509413961deSRichard Marian Thomaiyar             };
2510413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2511413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2512413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2513413961deSRichard Marian Thomaiyar     };
2514413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2515413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2516413961deSRichard Marian Thomaiyar }
2517413961deSRichard Marian Thomaiyar 
251895a3ecadSAnthony Wilson class SensorCollection : public Node
251995a3ecadSAnthony Wilson {
252095a3ecadSAnthony Wilson   public:
252195a3ecadSAnthony Wilson     SensorCollection(CrowApp& app) :
252295a3ecadSAnthony Wilson         Node(app, "/redfish/v1/Chassis/<str>/Sensors", std::string())
252395a3ecadSAnthony Wilson     {
252495a3ecadSAnthony Wilson         entityPrivileges = {
252595a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
252695a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
252795a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
252895a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
252995a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
253095a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
253195a3ecadSAnthony Wilson     }
253295a3ecadSAnthony Wilson 
253395a3ecadSAnthony Wilson   private:
253495a3ecadSAnthony Wilson     std::vector<const char*> typeList = {
253595a3ecadSAnthony Wilson         "/xyz/openbmc_project/sensors/power",
253695a3ecadSAnthony Wilson         "/xyz/openbmc_project/sensors/current"};
253795a3ecadSAnthony Wilson     void doGet(crow::Response& res, const crow::Request& req,
253895a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
253995a3ecadSAnthony Wilson     {
254095a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
254195a3ecadSAnthony Wilson         if (params.size() != 1)
254295a3ecadSAnthony Wilson         {
254395a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1";
254495a3ecadSAnthony Wilson             messages::internalError(res);
254595a3ecadSAnthony Wilson             res.end();
254695a3ecadSAnthony Wilson             return;
254795a3ecadSAnthony Wilson         }
254895a3ecadSAnthony Wilson 
254995a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
255095a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
255195a3ecadSAnthony Wilson             std::make_shared<SensorsAsyncResp>(res, chassisId, typeList,
255295a3ecadSAnthony Wilson                                                "Sensors");
255395a3ecadSAnthony Wilson 
255495a3ecadSAnthony Wilson         auto getChassisCb =
255595a3ecadSAnthony Wilson             [asyncResp](std::shared_ptr<boost::container::flat_set<std::string>>
255695a3ecadSAnthony Wilson                             sensorNames) {
255795a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb enter";
255895a3ecadSAnthony Wilson 
255995a3ecadSAnthony Wilson                 nlohmann::json& entriesArray =
256095a3ecadSAnthony Wilson                     asyncResp->res.jsonValue["Members"];
256195a3ecadSAnthony Wilson                 for (auto& sensor : *sensorNames)
256295a3ecadSAnthony Wilson                 {
256395a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
256495a3ecadSAnthony Wilson 
256595a3ecadSAnthony Wilson                     std::size_t lastPos = sensor.rfind("/");
256695a3ecadSAnthony Wilson                     if (lastPos == std::string::npos ||
256795a3ecadSAnthony Wilson                         lastPos + 1 >= sensor.size())
256895a3ecadSAnthony Wilson                     {
256995a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
257095a3ecadSAnthony Wilson                         messages::internalError(asyncResp->res);
257195a3ecadSAnthony Wilson                         return;
257295a3ecadSAnthony Wilson                     }
257395a3ecadSAnthony Wilson                     std::string sensorName = sensor.substr(lastPos + 1);
257495a3ecadSAnthony Wilson                     entriesArray.push_back(
257595a3ecadSAnthony Wilson                         {{"@odata.id",
257695a3ecadSAnthony Wilson                           "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
257795a3ecadSAnthony Wilson                               asyncResp->chassisSubNode + "/" + sensorName}});
257895a3ecadSAnthony Wilson                 }
257995a3ecadSAnthony Wilson 
258095a3ecadSAnthony Wilson                 asyncResp->res.jsonValue["Members@odata.count"] =
258195a3ecadSAnthony Wilson                     entriesArray.size();
258295a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb exit";
258395a3ecadSAnthony Wilson             };
258495a3ecadSAnthony Wilson 
258595a3ecadSAnthony Wilson         // Get set of sensors in chassis
258695a3ecadSAnthony Wilson         getChassis(asyncResp, std::move(getChassisCb));
258795a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
258895a3ecadSAnthony Wilson     }
258995a3ecadSAnthony Wilson };
259095a3ecadSAnthony Wilson 
259195a3ecadSAnthony Wilson class Sensor : public Node
259295a3ecadSAnthony Wilson {
259395a3ecadSAnthony Wilson   public:
259495a3ecadSAnthony Wilson     Sensor(CrowApp& app) :
259595a3ecadSAnthony Wilson         Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(),
259695a3ecadSAnthony Wilson              std::string())
259795a3ecadSAnthony Wilson     {
259895a3ecadSAnthony Wilson         entityPrivileges = {
259995a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
260095a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
260195a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
260295a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
260395a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
260495a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
260595a3ecadSAnthony Wilson     }
260695a3ecadSAnthony Wilson 
260795a3ecadSAnthony Wilson   private:
260895a3ecadSAnthony Wilson     void doGet(crow::Response& res, const crow::Request& req,
260995a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
261095a3ecadSAnthony Wilson     {
261195a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "Sensor doGet enter";
261295a3ecadSAnthony Wilson         if (params.size() != 2)
261395a3ecadSAnthony Wilson         {
261495a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2";
261595a3ecadSAnthony Wilson             messages::internalError(res);
261695a3ecadSAnthony Wilson             res.end();
261795a3ecadSAnthony Wilson             return;
261895a3ecadSAnthony Wilson         }
261995a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
262095a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
262195a3ecadSAnthony Wilson             std::make_shared<SensorsAsyncResp>(
262295a3ecadSAnthony Wilson                 res, chassisId, std::vector<const char*>(), "Sensors");
262395a3ecadSAnthony Wilson 
262495a3ecadSAnthony Wilson         const std::string& sensorName = params[1];
262595a3ecadSAnthony Wilson         const std::array<const char*, 1> interfaces = {
262695a3ecadSAnthony Wilson             "xyz.openbmc_project.Sensor.Value"};
262795a3ecadSAnthony Wilson 
262895a3ecadSAnthony Wilson         // Get a list of all of the sensors that implement Sensor.Value
262995a3ecadSAnthony Wilson         // and get the path and service name associated with the sensor
263095a3ecadSAnthony Wilson         crow::connections::systemBus->async_method_call(
263195a3ecadSAnthony Wilson             [asyncResp, sensorName](const boost::system::error_code ec,
263295a3ecadSAnthony Wilson                                     const GetSubTreeType& subtree) {
263395a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 enter";
263495a3ecadSAnthony Wilson                 if (ec)
263595a3ecadSAnthony Wilson                 {
263695a3ecadSAnthony Wilson                     messages::internalError(asyncResp->res);
263795a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
263895a3ecadSAnthony Wilson                                      << "Dbus error " << ec;
263995a3ecadSAnthony Wilson                     return;
264095a3ecadSAnthony Wilson                 }
264195a3ecadSAnthony Wilson 
264295a3ecadSAnthony Wilson                 GetSubTreeType::const_iterator it = std::find_if(
264395a3ecadSAnthony Wilson                     subtree.begin(), subtree.end(),
264495a3ecadSAnthony Wilson                     [sensorName](
264595a3ecadSAnthony Wilson                         const std::pair<
264695a3ecadSAnthony Wilson                             std::string,
264795a3ecadSAnthony Wilson                             std::vector<std::pair<std::string,
264895a3ecadSAnthony Wilson                                                   std::vector<std::string>>>>&
264995a3ecadSAnthony Wilson                             object) {
265095a3ecadSAnthony Wilson                         std::string_view sensor = object.first;
265195a3ecadSAnthony Wilson                         std::size_t lastPos = sensor.rfind("/");
265295a3ecadSAnthony Wilson                         if (lastPos == std::string::npos ||
265395a3ecadSAnthony Wilson                             lastPos + 1 >= sensor.size())
265495a3ecadSAnthony Wilson                         {
265595a3ecadSAnthony Wilson                             BMCWEB_LOG_ERROR << "Invalid sensor path: "
265695a3ecadSAnthony Wilson                                              << sensor;
265795a3ecadSAnthony Wilson                             return false;
265895a3ecadSAnthony Wilson                         }
265995a3ecadSAnthony Wilson                         std::string_view name = sensor.substr(lastPos + 1);
266095a3ecadSAnthony Wilson 
266195a3ecadSAnthony Wilson                         return name == sensorName;
266295a3ecadSAnthony Wilson                     });
266395a3ecadSAnthony Wilson 
266495a3ecadSAnthony Wilson                 if (it == subtree.end())
266595a3ecadSAnthony Wilson                 {
266695a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Could not find path for sensor: "
266795a3ecadSAnthony Wilson                                      << sensorName;
266895a3ecadSAnthony Wilson                     messages::resourceNotFound(asyncResp->res, "Sensor",
266995a3ecadSAnthony Wilson                                                sensorName);
267095a3ecadSAnthony Wilson                     return;
267195a3ecadSAnthony Wilson                 }
267295a3ecadSAnthony Wilson                 std::string_view sensorPath = (*it).first;
267395a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
267495a3ecadSAnthony Wilson                                  << sensorName << "': " << sensorPath;
267595a3ecadSAnthony Wilson 
267695a3ecadSAnthony Wilson                 const std::shared_ptr<boost::container::flat_set<std::string>>
267795a3ecadSAnthony Wilson                     sensorList = std::make_shared<
267895a3ecadSAnthony Wilson                         boost::container::flat_set<std::string>>();
267995a3ecadSAnthony Wilson 
268095a3ecadSAnthony Wilson                 sensorList->emplace(sensorPath);
268195a3ecadSAnthony Wilson                 processSensorList(asyncResp, sensorList);
268295a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 exit";
268395a3ecadSAnthony Wilson             },
268495a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper",
268595a3ecadSAnthony Wilson             "/xyz/openbmc_project/object_mapper",
268695a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
268795a3ecadSAnthony Wilson             "/xyz/openbmc_project/sensors", 2, interfaces);
268895a3ecadSAnthony Wilson     }
268995a3ecadSAnthony Wilson };
269095a3ecadSAnthony Wilson 
269108777fb0SLewanczyk, Dawid } // namespace redfish
2692