xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision f8ede15e7ffc7ecc70429fa1ef53c13c95181910)
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 /**
78d500549bSAnthony Wilson  * Possible states for physical inventory leds
79d500549bSAnthony Wilson  */
80d500549bSAnthony Wilson enum class LedState
81d500549bSAnthony Wilson {
82d500549bSAnthony Wilson     OFF,
83d500549bSAnthony Wilson     ON,
84d500549bSAnthony Wilson     BLINK,
85d500549bSAnthony Wilson     UNKNOWN
86d500549bSAnthony Wilson };
87d500549bSAnthony Wilson 
88d500549bSAnthony 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),
9642cbe538SGunnar Mills         isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
9742cbe538SGunnar Mills         model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""),
98d500549bSAnthony 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;
11342cbe538SGunnar Mills     int powerSupplyEfficiencyPercent;
114adc4f0dbSShawn McCarney     std::string manufacturer;
115adc4f0dbSShawn McCarney     std::string model;
116adc4f0dbSShawn McCarney     std::string partNumber;
117adc4f0dbSShawn McCarney     std::string serialNumber;
118adc4f0dbSShawn McCarney     std::set<std::string> sensors;
119d500549bSAnthony Wilson     std::string ledObjectPath;
120d500549bSAnthony Wilson     LedState ledState;
121adc4f0dbSShawn McCarney };
122adc4f0dbSShawn McCarney 
123adc4f0dbSShawn McCarney /**
124413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
125588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
12608777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
12708777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
12808777fb0SLewanczyk, Dawid  */
12908777fb0SLewanczyk, Dawid template <typename Callback>
130413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
131413961deSRichard Marian Thomaiyar     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
13249c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
1331abe55efSEd Tanous     Callback&& callback)
1341abe55efSEd Tanous {
135413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
13603b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
13708777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
13808777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
13908777fb0SLewanczyk, Dawid 
14008777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
1411abe55efSEd Tanous     auto respHandler = [callback{std::move(callback)}, SensorsAsyncResp,
1421abe55efSEd Tanous                         sensorNames](const boost::system::error_code ec,
1431abe55efSEd Tanous                                      const GetSubTreeType& subtree) {
144413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
1451abe55efSEd Tanous         if (ec)
1461abe55efSEd Tanous         {
1475f7d88c4SEd Tanous             messages::internalError(SensorsAsyncResp->res);
148413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
149413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
15008777fb0SLewanczyk, Dawid             return;
15108777fb0SLewanczyk, Dawid         }
15208777fb0SLewanczyk, Dawid 
15355c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
15408777fb0SLewanczyk, Dawid 
15508777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
15608777fb0SLewanczyk, Dawid         // found in the chassis
15708777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
158413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
1591abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
1601abe55efSEd Tanous         // producers
16108777fb0SLewanczyk, Dawid         connections.reserve(8);
16208777fb0SLewanczyk, Dawid 
16349c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
16449c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
1651abe55efSEd Tanous         {
16655c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
16708777fb0SLewanczyk, Dawid         }
16808777fb0SLewanczyk, Dawid 
16908777fb0SLewanczyk, Dawid         for (const std::pair<
17008777fb0SLewanczyk, Dawid                  std::string,
17108777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1721abe55efSEd Tanous                  object : subtree)
1731abe55efSEd Tanous         {
17449c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
1751abe55efSEd Tanous             {
17649c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
1771abe55efSEd Tanous                          objData : object.second)
1781abe55efSEd Tanous                 {
17949c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
18008777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
181de629b6eSShawn McCarney                     objectsWithConnection.insert(
182de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
18308777fb0SLewanczyk, Dawid                 }
18408777fb0SLewanczyk, Dawid             }
18508777fb0SLewanczyk, Dawid         }
18655c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
187413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
188413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
18908777fb0SLewanczyk, Dawid     };
19008777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
19155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
19255c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1931abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
1941abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
195413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
196413961deSRichard Marian Thomaiyar }
197413961deSRichard Marian Thomaiyar 
198413961deSRichard Marian Thomaiyar /**
199413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
200413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
201413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
202413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
203413961deSRichard Marian Thomaiyar  */
204413961deSRichard Marian Thomaiyar template <typename Callback>
20549c53ac9SJohnathan Mantey void getConnections(
20649c53ac9SJohnathan Mantey     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
20749c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
208413961deSRichard Marian Thomaiyar     Callback&& callback)
209413961deSRichard Marian Thomaiyar {
210413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
211413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
212413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
213413961deSRichard Marian Thomaiyar                        objectsWithConnection) {
214413961deSRichard Marian Thomaiyar             callback(std::move(connections));
215413961deSRichard Marian Thomaiyar         };
216413961deSRichard Marian Thomaiyar     getObjectsWithConnection(SensorsAsyncResp, sensorNames,
217413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
21808777fb0SLewanczyk, Dawid }
21908777fb0SLewanczyk, Dawid 
22008777fb0SLewanczyk, Dawid /**
22149c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
22249c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
22349c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
22449c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
22549c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
22649c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
22749c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
22849c53ac9SJohnathan Mantey  */
22949c53ac9SJohnathan Mantey void reduceSensorList(
23049c53ac9SJohnathan Mantey     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
23149c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
23249c53ac9SJohnathan Mantey     std::shared_ptr<boost::container::flat_set<std::string>> activeSensors)
23349c53ac9SJohnathan Mantey {
23449c53ac9SJohnathan Mantey     if (SensorsAsyncResp == nullptr)
23549c53ac9SJohnathan Mantey     {
23649c53ac9SJohnathan Mantey         return;
23749c53ac9SJohnathan Mantey     }
23849c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
23949c53ac9SJohnathan Mantey     {
24049c53ac9SJohnathan Mantey         messages::resourceNotFound(
24149c53ac9SJohnathan Mantey             SensorsAsyncResp->res, SensorsAsyncResp->chassisSubNode,
24249c53ac9SJohnathan Mantey             SensorsAsyncResp->chassisSubNode == "Thermal" ? "Temperatures"
24349c53ac9SJohnathan Mantey                                                           : "Voltages");
24449c53ac9SJohnathan Mantey 
24549c53ac9SJohnathan Mantey         return;
24649c53ac9SJohnathan Mantey     }
24749c53ac9SJohnathan Mantey     if (allSensors->empty())
24849c53ac9SJohnathan Mantey     {
24949c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
25049c53ac9SJohnathan Mantey         return;
25149c53ac9SJohnathan Mantey     }
25249c53ac9SJohnathan Mantey 
25349c53ac9SJohnathan Mantey     for (const char* type : SensorsAsyncResp->types)
25449c53ac9SJohnathan Mantey     {
25549c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
25649c53ac9SJohnathan Mantey         {
25749c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
25849c53ac9SJohnathan Mantey             {
25949c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
26049c53ac9SJohnathan Mantey             }
26149c53ac9SJohnathan Mantey         }
26249c53ac9SJohnathan Mantey     }
26349c53ac9SJohnathan Mantey }
26449c53ac9SJohnathan Mantey 
26549c53ac9SJohnathan Mantey /**
2664bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
2674bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
2684bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
2694bb3dc34SCarol Wang  */
2704bb3dc34SCarol Wang template <typename Callback>
2714bb3dc34SCarol Wang void getValidChassisPath(std::shared_ptr<SensorsAsyncResp> asyncResp,
2724bb3dc34SCarol Wang                          Callback&& callback)
2734bb3dc34SCarol Wang {
2744bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
2754bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
2764bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
2774bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
2784bb3dc34SCarol Wang 
2794bb3dc34SCarol Wang     auto respHandler =
2804bb3dc34SCarol Wang         [callback{std::move(callback)},
2814bb3dc34SCarol Wang          asyncResp](const boost::system::error_code ec,
2824bb3dc34SCarol Wang                     const std::vector<std::string>& chassisPaths) mutable {
2834bb3dc34SCarol Wang             BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
2844bb3dc34SCarol Wang             if (ec)
2854bb3dc34SCarol Wang             {
2864bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR
2874bb3dc34SCarol Wang                     << "getValidChassisPath respHandler DBUS error: " << ec;
2884bb3dc34SCarol Wang                 messages::internalError(asyncResp->res);
2894bb3dc34SCarol Wang                 return;
2904bb3dc34SCarol Wang             }
2914bb3dc34SCarol Wang 
2924bb3dc34SCarol Wang             std::optional<std::string> chassisPath;
2934bb3dc34SCarol Wang             std::string chassisName;
2944bb3dc34SCarol Wang             for (const std::string& chassis : chassisPaths)
2954bb3dc34SCarol Wang             {
2964bb3dc34SCarol Wang                 std::size_t lastPos = chassis.rfind("/");
2974bb3dc34SCarol Wang                 if (lastPos == std::string::npos)
2984bb3dc34SCarol Wang                 {
2994bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
3004bb3dc34SCarol Wang                     continue;
3014bb3dc34SCarol Wang                 }
3024bb3dc34SCarol Wang                 chassisName = chassis.substr(lastPos + 1);
3034bb3dc34SCarol Wang                 if (chassisName == asyncResp->chassisId)
3044bb3dc34SCarol Wang                 {
3054bb3dc34SCarol Wang                     chassisPath = chassis;
3064bb3dc34SCarol Wang                     break;
3074bb3dc34SCarol Wang                 }
3084bb3dc34SCarol Wang             }
3094bb3dc34SCarol Wang             callback(chassisPath);
3104bb3dc34SCarol Wang         };
3114bb3dc34SCarol Wang 
3124bb3dc34SCarol Wang     // Get the Chassis Collection
3134bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
3144bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
3154bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
3164bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
3174bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
3184bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
3194bb3dc34SCarol Wang }
3204bb3dc34SCarol Wang 
3214bb3dc34SCarol Wang /**
32208777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
323588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
32408777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
32508777fb0SLewanczyk, Dawid  */
32608777fb0SLewanczyk, Dawid template <typename Callback>
32749c53ac9SJohnathan Mantey void getChassis(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
3281abe55efSEd Tanous                 Callback&& callback)
3291abe55efSEd Tanous {
33055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
331adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
33249c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
333adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
33449c53ac9SJohnathan Mantey     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
33549c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
33649c53ac9SJohnathan Mantey                            const std::vector<std::string>& chassisPaths) {
33755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
3381abe55efSEd Tanous         if (ec)
3391abe55efSEd Tanous         {
34055c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
34149c53ac9SJohnathan Mantey             messages::internalError(sensorsAsyncResp->res);
34208777fb0SLewanczyk, Dawid             return;
34308777fb0SLewanczyk, Dawid         }
34408777fb0SLewanczyk, Dawid 
34549c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
34649c53ac9SJohnathan Mantey         std::string chassisName;
34749c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
3481abe55efSEd Tanous         {
34949c53ac9SJohnathan Mantey             std::size_t lastPos = chassis.rfind("/");
35049c53ac9SJohnathan Mantey             if (lastPos == std::string::npos)
3511abe55efSEd Tanous             {
35249c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
353daf36e2eSEd Tanous                 continue;
354daf36e2eSEd Tanous             }
35549c53ac9SJohnathan Mantey             chassisName = chassis.substr(lastPos + 1);
35649c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
3571abe55efSEd Tanous             {
35849c53ac9SJohnathan Mantey                 chassisPath = &chassis;
35949c53ac9SJohnathan Mantey                 break;
360daf36e2eSEd Tanous             }
36149c53ac9SJohnathan Mantey         }
36249c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
3631abe55efSEd Tanous         {
36449c53ac9SJohnathan Mantey             messages::resourceNotFound(sensorsAsyncResp->res, "Chassis",
36549c53ac9SJohnathan Mantey                                        sensorsAsyncResp->chassisId);
36649c53ac9SJohnathan Mantey             return;
3671abe55efSEd Tanous         }
36808777fb0SLewanczyk, Dawid 
36949c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
37049c53ac9SJohnathan Mantey         if (chassisSubNode == "Power")
37149c53ac9SJohnathan Mantey         {
37249c53ac9SJohnathan Mantey             sensorsAsyncResp->res.jsonValue["@odata.type"] =
37349c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
37449c53ac9SJohnathan Mantey         }
37549c53ac9SJohnathan Mantey         else if (chassisSubNode == "Thermal")
37649c53ac9SJohnathan Mantey         {
37749c53ac9SJohnathan Mantey             sensorsAsyncResp->res.jsonValue["@odata.type"] =
37849c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
3794f9a2130SJennifer Lee             sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array();
3804f9a2130SJennifer Lee             sensorsAsyncResp->res.jsonValue["Temperatures"] =
3814f9a2130SJennifer Lee                 nlohmann::json::array();
38249c53ac9SJohnathan Mantey         }
38395a3ecadSAnthony Wilson         else if (chassisSubNode == "Sensors")
38495a3ecadSAnthony Wilson         {
38595a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["@odata.type"] =
38695a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
38795a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Description"] =
38895a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
38995a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members"] =
39095a3ecadSAnthony Wilson                 nlohmann::json::array();
39195a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0;
39295a3ecadSAnthony Wilson         }
39395a3ecadSAnthony Wilson 
39495a3ecadSAnthony Wilson         if (chassisSubNode != "Sensors")
39595a3ecadSAnthony Wilson         {
39695a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode;
39795a3ecadSAnthony Wilson         }
39895a3ecadSAnthony Wilson 
39949c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["@odata.id"] =
40049c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
40149c53ac9SJohnathan Mantey             chassisSubNode;
40249c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode;
40349c53ac9SJohnathan Mantey 
4048fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
4058fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
40655c7b7a2SEd Tanous         crow::connections::systemBus->async_method_call(
40749c53ac9SJohnathan Mantey             [sensorsAsyncResp, callback{std::move(callback)}](
408271584abSEd Tanous                 const boost::system::error_code& e,
40949c53ac9SJohnathan Mantey                 const std::variant<std::vector<std::string>>&
41049c53ac9SJohnathan Mantey                     variantEndpoints) {
411271584abSEd Tanous                 if (e)
41249c53ac9SJohnathan Mantey                 {
413271584abSEd Tanous                     if (e.value() != EBADR)
41449c53ac9SJohnathan Mantey                     {
41549c53ac9SJohnathan Mantey                         messages::internalError(sensorsAsyncResp->res);
41649c53ac9SJohnathan Mantey                         return;
41749c53ac9SJohnathan Mantey                     }
41849c53ac9SJohnathan Mantey                 }
41949c53ac9SJohnathan Mantey                 const std::vector<std::string>* nodeSensorList =
42049c53ac9SJohnathan Mantey                     std::get_if<std::vector<std::string>>(&(variantEndpoints));
42149c53ac9SJohnathan Mantey                 if (nodeSensorList == nullptr)
42249c53ac9SJohnathan Mantey                 {
42349c53ac9SJohnathan Mantey                     messages::resourceNotFound(
42449c53ac9SJohnathan Mantey                         sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
42549c53ac9SJohnathan Mantey                         sensorsAsyncResp->chassisSubNode == "Thermal"
42649c53ac9SJohnathan Mantey                             ? "Temperatures"
42795a3ecadSAnthony Wilson                             : sensorsAsyncResp->chassisSubNode == "Power"
42895a3ecadSAnthony Wilson                                   ? "Voltages"
42995a3ecadSAnthony Wilson                                   : "Sensors");
43049c53ac9SJohnathan Mantey                     return;
43149c53ac9SJohnathan Mantey                 }
43249c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
43349c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
43449c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
43549c53ac9SJohnathan Mantey                 reduceSensorList(sensorsAsyncResp, nodeSensorList,
43649c53ac9SJohnathan Mantey                                  culledSensorList);
43749c53ac9SJohnathan Mantey                 callback(culledSensorList);
43849c53ac9SJohnathan Mantey             },
43949c53ac9SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", sensorPath,
44049c53ac9SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Get",
44149c53ac9SJohnathan Mantey             "xyz.openbmc_project.Association", "endpoints");
44249c53ac9SJohnathan Mantey     };
44349c53ac9SJohnathan Mantey 
44449c53ac9SJohnathan Mantey     // Get the Chassis Collection
44549c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
44649c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
44749c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
44849c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
449271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
45055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
45108777fb0SLewanczyk, Dawid }
45208777fb0SLewanczyk, Dawid 
45308777fb0SLewanczyk, Dawid /**
454de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
455de629b6eSShawn McCarney  *
456de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
457de629b6eSShawn McCarney  *
458de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
459de629b6eSShawn McCarney  * been obtained.
460de629b6eSShawn McCarney  *
461de629b6eSShawn McCarney  * The callback must have the following signature:
462de629b6eSShawn McCarney  *   @code
4638fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
4648fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
465de629b6eSShawn McCarney  *   @endcode
466de629b6eSShawn McCarney  *
46749c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
468de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
469de629b6eSShawn McCarney  */
470de629b6eSShawn McCarney template <typename Callback>
471de629b6eSShawn McCarney void getObjectManagerPaths(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
472de629b6eSShawn McCarney                            Callback&& callback)
473de629b6eSShawn McCarney {
474de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
475de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
476de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
477de629b6eSShawn McCarney 
478de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
479de629b6eSShawn McCarney     auto respHandler = [callback{std::move(callback)},
480de629b6eSShawn McCarney                         SensorsAsyncResp](const boost::system::error_code ec,
481de629b6eSShawn McCarney                                           const GetSubTreeType& subtree) {
482de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
483de629b6eSShawn McCarney         if (ec)
484de629b6eSShawn McCarney         {
485de629b6eSShawn McCarney             messages::internalError(SensorsAsyncResp->res);
486de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
487de629b6eSShawn McCarney                              << ec;
488de629b6eSShawn McCarney             return;
489de629b6eSShawn McCarney         }
490de629b6eSShawn McCarney 
491de629b6eSShawn McCarney         // Loop over returned object paths
4928fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
4938fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
4948fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
495de629b6eSShawn McCarney         for (const std::pair<
496de629b6eSShawn McCarney                  std::string,
497de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
498de629b6eSShawn McCarney                  object : subtree)
499de629b6eSShawn McCarney         {
500de629b6eSShawn McCarney             // Loop over connections for current object path
501de629b6eSShawn McCarney             const std::string& objectPath = object.first;
502de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
503de629b6eSShawn McCarney                      objData : object.second)
504de629b6eSShawn McCarney             {
505de629b6eSShawn McCarney                 // Add mapping from connection to object path
506de629b6eSShawn McCarney                 const std::string& connection = objData.first;
5078fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
508de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
509de629b6eSShawn McCarney                                  << objectPath;
510de629b6eSShawn McCarney             }
511de629b6eSShawn McCarney         }
5128fb49dd6SShawn McCarney         callback(objectMgrPaths);
513de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
514de629b6eSShawn McCarney     };
515de629b6eSShawn McCarney 
516de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
517de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
518de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
519de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
520271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
521de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
522de629b6eSShawn McCarney }
523de629b6eSShawn McCarney 
524de629b6eSShawn McCarney /**
525adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
526adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
527adc4f0dbSShawn McCarney  * @return State value for inventory item.
52834dd179eSJames Feist  */
529adc4f0dbSShawn McCarney static std::string getState(const InventoryItem* inventoryItem)
530adc4f0dbSShawn McCarney {
531adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
532adc4f0dbSShawn McCarney     {
533adc4f0dbSShawn McCarney         return "Absent";
534adc4f0dbSShawn McCarney     }
53534dd179eSJames Feist 
536adc4f0dbSShawn McCarney     return "Enabled";
537adc4f0dbSShawn McCarney }
538adc4f0dbSShawn McCarney 
539adc4f0dbSShawn McCarney /**
540adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
541adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
542adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
543adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
544adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
545adc4f0dbSShawn McCarney  * @return Health value for sensor.
546adc4f0dbSShawn McCarney  */
54734dd179eSJames Feist static std::string getHealth(
548adc4f0dbSShawn McCarney     nlohmann::json& sensorJson,
54934dd179eSJames Feist     const boost::container::flat_map<
55034dd179eSJames Feist         std::string, boost::container::flat_map<std::string, SensorVariant>>&
551adc4f0dbSShawn McCarney         interfacesDict,
552adc4f0dbSShawn McCarney     const InventoryItem* inventoryItem)
55334dd179eSJames Feist {
554adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
555adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
556adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
557adc4f0dbSShawn McCarney     std::string currentHealth;
558adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
559adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
560adc4f0dbSShawn McCarney     {
561adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
562adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
563adc4f0dbSShawn McCarney         {
564adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
565adc4f0dbSShawn McCarney             if (health != nullptr)
566adc4f0dbSShawn McCarney             {
567adc4f0dbSShawn McCarney                 currentHealth = *health;
568adc4f0dbSShawn McCarney             }
569adc4f0dbSShawn McCarney         }
570adc4f0dbSShawn McCarney     }
571adc4f0dbSShawn McCarney 
572adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
573adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
574adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
575adc4f0dbSShawn McCarney     {
576adc4f0dbSShawn McCarney         return "Critical";
577adc4f0dbSShawn McCarney     }
578adc4f0dbSShawn McCarney 
579adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
58034dd179eSJames Feist     auto criticalThresholdIt =
58134dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
58234dd179eSJames Feist     if (criticalThresholdIt != interfacesDict.end())
58334dd179eSJames Feist     {
58434dd179eSJames Feist         auto thresholdHighIt =
58534dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmHigh");
58634dd179eSJames Feist         auto thresholdLowIt =
58734dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmLow");
58834dd179eSJames Feist         if (thresholdHighIt != criticalThresholdIt->second.end())
58934dd179eSJames Feist         {
59034dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
59134dd179eSJames Feist             if (asserted == nullptr)
59234dd179eSJames Feist             {
59334dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
59434dd179eSJames Feist             }
59534dd179eSJames Feist             else if (*asserted)
59634dd179eSJames Feist             {
59734dd179eSJames Feist                 return "Critical";
59834dd179eSJames Feist             }
59934dd179eSJames Feist         }
60034dd179eSJames Feist         if (thresholdLowIt != criticalThresholdIt->second.end())
60134dd179eSJames Feist         {
60234dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
60334dd179eSJames Feist             if (asserted == nullptr)
60434dd179eSJames Feist             {
60534dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
60634dd179eSJames Feist             }
60734dd179eSJames Feist             else if (*asserted)
60834dd179eSJames Feist             {
60934dd179eSJames Feist                 return "Critical";
61034dd179eSJames Feist             }
61134dd179eSJames Feist         }
61234dd179eSJames Feist     }
61334dd179eSJames Feist 
614adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
615adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
616adc4f0dbSShawn McCarney     {
617adc4f0dbSShawn McCarney         return "Critical";
618adc4f0dbSShawn McCarney     }
619adc4f0dbSShawn McCarney 
620adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
621adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
622adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
623adc4f0dbSShawn McCarney     {
624adc4f0dbSShawn McCarney         return "Warning";
625adc4f0dbSShawn McCarney     }
626adc4f0dbSShawn McCarney 
627adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
62834dd179eSJames Feist     auto warningThresholdIt =
62934dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
63034dd179eSJames Feist     if (warningThresholdIt != interfacesDict.end())
63134dd179eSJames Feist     {
63234dd179eSJames Feist         auto thresholdHighIt =
63334dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmHigh");
63434dd179eSJames Feist         auto thresholdLowIt =
63534dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmLow");
63634dd179eSJames Feist         if (thresholdHighIt != warningThresholdIt->second.end())
63734dd179eSJames Feist         {
63834dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
63934dd179eSJames Feist             if (asserted == nullptr)
64034dd179eSJames Feist             {
64134dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
64234dd179eSJames Feist             }
64334dd179eSJames Feist             else if (*asserted)
64434dd179eSJames Feist             {
64534dd179eSJames Feist                 return "Warning";
64634dd179eSJames Feist             }
64734dd179eSJames Feist         }
64834dd179eSJames Feist         if (thresholdLowIt != warningThresholdIt->second.end())
64934dd179eSJames Feist         {
65034dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
65134dd179eSJames Feist             if (asserted == nullptr)
65234dd179eSJames Feist             {
65334dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
65434dd179eSJames Feist             }
65534dd179eSJames Feist             else if (*asserted)
65634dd179eSJames Feist             {
65734dd179eSJames Feist                 return "Warning";
65834dd179eSJames Feist             }
65934dd179eSJames Feist         }
66034dd179eSJames Feist     }
661adc4f0dbSShawn McCarney 
66234dd179eSJames Feist     return "OK";
66334dd179eSJames Feist }
66434dd179eSJames Feist 
665d500549bSAnthony Wilson static void setLedState(nlohmann::json& sensorJson,
666d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
667d500549bSAnthony Wilson {
668d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
669d500549bSAnthony Wilson     {
670d500549bSAnthony Wilson         switch (inventoryItem->ledState)
671d500549bSAnthony Wilson         {
672d500549bSAnthony Wilson             case LedState::OFF:
673d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
674d500549bSAnthony Wilson                 break;
675d500549bSAnthony Wilson             case LedState::ON:
676d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
677d500549bSAnthony Wilson                 break;
678d500549bSAnthony Wilson             case LedState::BLINK:
679d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
680d500549bSAnthony Wilson                 break;
681d500549bSAnthony Wilson             default:
682d500549bSAnthony Wilson                 break;
683d500549bSAnthony Wilson         }
684d500549bSAnthony Wilson     }
685d500549bSAnthony Wilson }
686d500549bSAnthony Wilson 
68734dd179eSJames Feist /**
68808777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
68908777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
690274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
69108777fb0SLewanczyk, Dawid  * build
69295a3ecadSAnthony Wilson  * @param sensorSchema  The schema (Power, Thermal, etc) being associated with
69395a3ecadSAnthony Wilson  * the sensor to build
69408777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
69508777fb0SLewanczyk, Dawid  * interfaces to be built from
69608777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
697adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
698adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
69908777fb0SLewanczyk, Dawid  */
70008777fb0SLewanczyk, Dawid void objectInterfacesToJson(
70108777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
70295a3ecadSAnthony Wilson     const std::string& sensorSchema,
70308777fb0SLewanczyk, Dawid     const boost::container::flat_map<
704aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>&
70508777fb0SLewanczyk, Dawid         interfacesDict,
706adc4f0dbSShawn McCarney     nlohmann::json& sensor_json, InventoryItem* inventoryItem)
7071abe55efSEd Tanous {
70808777fb0SLewanczyk, Dawid     // We need a value interface before we can do anything with it
70955c7b7a2SEd Tanous     auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
7101abe55efSEd Tanous     if (valueIt == interfacesDict.end())
7111abe55efSEd Tanous     {
71255c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
71308777fb0SLewanczyk, Dawid         return;
71408777fb0SLewanczyk, Dawid     }
71508777fb0SLewanczyk, Dawid 
71608777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
71708777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
71808777fb0SLewanczyk, Dawid 
71955c7b7a2SEd Tanous     auto scaleIt = valueIt->second.find("Scale");
72008777fb0SLewanczyk, Dawid     // If a scale exists, pull value as int64, and use the scaling.
7211abe55efSEd Tanous     if (scaleIt != valueIt->second.end())
7221abe55efSEd Tanous     {
723abf2add6SEd Tanous         const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
7241abe55efSEd Tanous         if (int64Value != nullptr)
7251abe55efSEd Tanous         {
72608777fb0SLewanczyk, Dawid             scaleMultiplier = *int64Value;
72708777fb0SLewanczyk, Dawid         }
72808777fb0SLewanczyk, Dawid     }
72908777fb0SLewanczyk, Dawid 
73095a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
731adc4f0dbSShawn McCarney     {
73295a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
73395a3ecadSAnthony Wilson         // including power sensors.
73495a3ecadSAnthony Wilson         sensor_json["Id"] = sensorName;
73595a3ecadSAnthony Wilson         sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
73695a3ecadSAnthony Wilson     }
73795a3ecadSAnthony Wilson     else if (sensorType != "power")
73895a3ecadSAnthony Wilson     {
73995a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
74095a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
74195a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
74208777fb0SLewanczyk, Dawid         sensor_json["MemberId"] = sensorName;
743e742b6ccSEd Tanous         sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
744adc4f0dbSShawn McCarney     }
745e742b6ccSEd Tanous 
746adc4f0dbSShawn McCarney     sensor_json["Status"]["State"] = getState(inventoryItem);
747adc4f0dbSShawn McCarney     sensor_json["Status"]["Health"] =
748adc4f0dbSShawn McCarney         getHealth(sensor_json, interfacesDict, inventoryItem);
74908777fb0SLewanczyk, Dawid 
75008777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
75108777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
75208777fb0SLewanczyk, Dawid     // that require integers, not floats.
75308777fb0SLewanczyk, Dawid     bool forceToInt = false;
75408777fb0SLewanczyk, Dawid 
7553929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
75695a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
75795a3ecadSAnthony Wilson     {
75895a3ecadSAnthony Wilson         sensor_json["@odata.type"] = "#Sensor.v1_0_0.Sensor";
75995a3ecadSAnthony Wilson         if (sensorType == "power")
76095a3ecadSAnthony Wilson         {
76195a3ecadSAnthony Wilson             sensor_json["ReadingUnits"] = "Watts";
76295a3ecadSAnthony Wilson         }
76395a3ecadSAnthony Wilson         else if (sensorType == "current")
76495a3ecadSAnthony Wilson         {
76595a3ecadSAnthony Wilson             sensor_json["ReadingUnits"] = "Amperes";
76695a3ecadSAnthony Wilson         }
767*f8ede15eSAdrian Ambrożewicz         else if (sensorType == "utilization")
768*f8ede15eSAdrian Ambrożewicz         {
769*f8ede15eSAdrian Ambrożewicz             sensor_json["ReadingUnits"] = "Percent";
770*f8ede15eSAdrian Ambrożewicz         }
77195a3ecadSAnthony Wilson     }
77295a3ecadSAnthony Wilson     else if (sensorType == "temperature")
7731abe55efSEd Tanous     {
7743929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
7757885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature";
77608777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
77708777fb0SLewanczyk, Dawid         // implementation seems to implement fan
7781abe55efSEd Tanous     }
7791abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
7801abe55efSEd Tanous     {
7813929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
78208777fb0SLewanczyk, Dawid         sensor_json["ReadingUnits"] = "RPM";
7837885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
784d500549bSAnthony Wilson         setLedState(sensor_json, inventoryItem);
78508777fb0SLewanczyk, Dawid         forceToInt = true;
7861abe55efSEd Tanous     }
7876f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
7886f6d0d32SEd Tanous     {
7893929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
7906f6d0d32SEd Tanous         sensor_json["ReadingUnits"] = "Percent";
7916f6d0d32SEd Tanous         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
792d500549bSAnthony Wilson         setLedState(sensor_json, inventoryItem);
7936f6d0d32SEd Tanous         forceToInt = true;
7946f6d0d32SEd Tanous     }
7951abe55efSEd Tanous     else if (sensorType == "voltage")
7961abe55efSEd Tanous     {
7973929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
7987885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage";
7991abe55efSEd Tanous     }
8002474adfaSEd Tanous     else if (sensorType == "power")
8012474adfaSEd Tanous     {
80249c53ac9SJohnathan Mantey         std::string sensorNameLower =
80349c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
80449c53ac9SJohnathan Mantey 
805028f7ebcSEddie James         if (!sensorName.compare("total_power"))
806028f7ebcSEddie James         {
8077ab06f49SGunnar Mills             sensor_json["@odata.type"] = "#Power.v1_0_0.PowerControl";
8087ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
8097ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
8107ab06f49SGunnar Mills             sensor_json["MemberId"] = "0";
8117ab06f49SGunnar Mills             sensor_json["Name"] = "Chassis Power Control";
8123929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
813028f7ebcSEddie James         }
814028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
81549c53ac9SJohnathan Mantey         {
8163929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
81749c53ac9SJohnathan Mantey         }
81849c53ac9SJohnathan Mantey         else
81949c53ac9SJohnathan Mantey         {
8203929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
82149c53ac9SJohnathan Mantey         }
8222474adfaSEd Tanous     }
8231abe55efSEd Tanous     else
8241abe55efSEd Tanous     {
82555c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
82608777fb0SLewanczyk, Dawid         return;
82708777fb0SLewanczyk, Dawid     }
82808777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
8293929aca1SAnthony Wilson     std::vector<
8303929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
8313929aca1SAnthony Wilson         properties;
83208777fb0SLewanczyk, Dawid     properties.reserve(7);
83308777fb0SLewanczyk, Dawid 
83408777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
835de629b6eSShawn McCarney 
8363929aca1SAnthony Wilson     if (sensorSchema == "Sensors")
8373929aca1SAnthony Wilson     {
8383929aca1SAnthony Wilson         properties.emplace_back(
8393929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
8403929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
8413929aca1SAnthony Wilson         properties.emplace_back(
8423929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
8433929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
8443929aca1SAnthony Wilson         properties.emplace_back(
8453929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
8463929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
8473929aca1SAnthony Wilson         properties.emplace_back(
8483929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
8493929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
8503929aca1SAnthony Wilson     }
8513929aca1SAnthony Wilson     else if (sensorType != "power")
852de629b6eSShawn McCarney     {
85308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8543929aca1SAnthony Wilson                                 "WarningHigh",
8553929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
85608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8573929aca1SAnthony Wilson                                 "WarningLow",
8583929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
85908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8603929aca1SAnthony Wilson                                 "CriticalHigh",
8613929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
86208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8633929aca1SAnthony Wilson                                 "CriticalLow",
8643929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
865de629b6eSShawn McCarney     }
86608777fb0SLewanczyk, Dawid 
8672474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
8682474adfaSEd Tanous 
86995a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
87095a3ecadSAnthony Wilson     {
87195a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8723929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
87395a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8743929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
87595a3ecadSAnthony Wilson     }
87695a3ecadSAnthony Wilson     else if (sensorType == "temperature")
8771abe55efSEd Tanous     {
87808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8793929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
88008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8813929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
8821abe55efSEd Tanous     }
883adc4f0dbSShawn McCarney     else if (sensorType != "power")
8841abe55efSEd Tanous     {
88508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8863929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
88708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8883929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
88908777fb0SLewanczyk, Dawid     }
89008777fb0SLewanczyk, Dawid 
8913929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
8923929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
8931abe55efSEd Tanous     {
89408777fb0SLewanczyk, Dawid         auto interfaceProperties = interfacesDict.find(std::get<0>(p));
8951abe55efSEd Tanous         if (interfaceProperties != interfacesDict.end())
8961abe55efSEd Tanous         {
897271584abSEd Tanous             auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
898271584abSEd Tanous             if (thisValueIt != interfaceProperties->second.end())
8991abe55efSEd Tanous             {
900271584abSEd Tanous                 const SensorVariant& valueVariant = thisValueIt->second;
9013929aca1SAnthony Wilson 
9023929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
9033929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
9043929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
9053929aca1SAnthony Wilson 
90608777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
907abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
90808777fb0SLewanczyk, Dawid 
909abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
910028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
9116f6d0d32SEd Tanous                 double temp = 0.0;
9126f6d0d32SEd Tanous                 if (int64Value != nullptr)
9131abe55efSEd Tanous                 {
914271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
9156f6d0d32SEd Tanous                 }
9166f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
9171abe55efSEd Tanous                 {
9186f6d0d32SEd Tanous                     temp = *doubleValue;
9191abe55efSEd Tanous                 }
920028f7ebcSEddie James                 else if (uValue != nullptr)
921028f7ebcSEddie James                 {
922028f7ebcSEddie James                     temp = *uValue;
923028f7ebcSEddie James                 }
9241abe55efSEd Tanous                 else
9251abe55efSEd Tanous                 {
9266f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
9276f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
9286f6d0d32SEd Tanous                     continue;
92908777fb0SLewanczyk, Dawid                 }
9306f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
9316f6d0d32SEd Tanous                 if (forceToInt)
9326f6d0d32SEd Tanous                 {
9333929aca1SAnthony Wilson                     sensor_json[key] = static_cast<int64_t>(temp);
9346f6d0d32SEd Tanous                 }
9356f6d0d32SEd Tanous                 else
9366f6d0d32SEd Tanous                 {
9373929aca1SAnthony Wilson                     sensor_json[key] = temp;
93808777fb0SLewanczyk, Dawid                 }
93908777fb0SLewanczyk, Dawid             }
94008777fb0SLewanczyk, Dawid         }
94108777fb0SLewanczyk, Dawid     }
94255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
94308777fb0SLewanczyk, Dawid }
94408777fb0SLewanczyk, Dawid 
9458bd25ccdSJames Feist static void
9468bd25ccdSJames Feist     populateFanRedundancy(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp)
9478bd25ccdSJames Feist {
9488bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
9498bd25ccdSJames Feist         [sensorsAsyncResp](const boost::system::error_code ec,
9508bd25ccdSJames Feist                            const GetSubTreeType& resp) {
9518bd25ccdSJames Feist             if (ec)
9528bd25ccdSJames Feist             {
9538bd25ccdSJames Feist                 return; // don't have to have this interface
9548bd25ccdSJames Feist             }
955e278c18fSEd Tanous             for (const std::pair<std::string,
956e278c18fSEd Tanous                                  std::vector<std::pair<
957e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
958e278c18fSEd Tanous                      pathPair : resp)
9598bd25ccdSJames Feist             {
960e278c18fSEd Tanous                 const std::string& path = pathPair.first;
961e278c18fSEd Tanous                 const std::vector<
962e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
963e278c18fSEd Tanous                     pathPair.second;
9648bd25ccdSJames Feist                 if (objDict.empty())
9658bd25ccdSJames Feist                 {
9668bd25ccdSJames Feist                     continue; // this should be impossible
9678bd25ccdSJames Feist                 }
9688bd25ccdSJames Feist 
9698bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
9708bd25ccdSJames Feist                 crow::connections::systemBus->async_method_call(
9718bd25ccdSJames Feist                     [path, owner,
972271584abSEd Tanous                      sensorsAsyncResp](const boost::system::error_code e,
9738bd25ccdSJames Feist                                        std::variant<std::vector<std::string>>
9748bd25ccdSJames Feist                                            variantEndpoints) {
975271584abSEd Tanous                         if (e)
9768bd25ccdSJames Feist                         {
9778bd25ccdSJames Feist                             return; // if they don't have an association we
9788bd25ccdSJames Feist                                     // can't tell what chassis is
9798bd25ccdSJames Feist                         }
9808bd25ccdSJames Feist                         // verify part of the right chassis
9818bd25ccdSJames Feist                         auto endpoints = std::get_if<std::vector<std::string>>(
9828bd25ccdSJames Feist                             &variantEndpoints);
9838bd25ccdSJames Feist 
9848bd25ccdSJames Feist                         if (endpoints == nullptr)
9858bd25ccdSJames Feist                         {
9868bd25ccdSJames Feist                             BMCWEB_LOG_ERROR << "Invalid association interface";
9878bd25ccdSJames Feist                             messages::internalError(sensorsAsyncResp->res);
9888bd25ccdSJames Feist                             return;
9898bd25ccdSJames Feist                         }
9908bd25ccdSJames Feist 
9918bd25ccdSJames Feist                         auto found = std::find_if(
9928bd25ccdSJames Feist                             endpoints->begin(), endpoints->end(),
9938bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
9948bd25ccdSJames Feist                                 return entry.find(
9958bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
9968bd25ccdSJames Feist                                        std::string::npos;
9978bd25ccdSJames Feist                             });
9988bd25ccdSJames Feist 
9998bd25ccdSJames Feist                         if (found == endpoints->end())
10008bd25ccdSJames Feist                         {
10018bd25ccdSJames Feist                             return;
10028bd25ccdSJames Feist                         }
10038bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
10048bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1005271584abSEd Tanous                                 const boost::system::error_code& err,
10068bd25ccdSJames Feist                                 const boost::container::flat_map<
10078bd25ccdSJames Feist                                     std::string,
10088bd25ccdSJames Feist                                     std::variant<uint8_t,
10098bd25ccdSJames Feist                                                  std::vector<std::string>,
10108bd25ccdSJames Feist                                                  std::string>>& ret) {
1011271584abSEd Tanous                                 if (err)
10128bd25ccdSJames Feist                                 {
10138bd25ccdSJames Feist                                     return; // don't have to have this
10148bd25ccdSJames Feist                                             // interface
10158bd25ccdSJames Feist                                 }
10168bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
10178bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
10188bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
10198bd25ccdSJames Feist 
10208bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
10218bd25ccdSJames Feist                                     findCollection == ret.end() ||
10228bd25ccdSJames Feist                                     findStatus == ret.end())
10238bd25ccdSJames Feist                                 {
10248bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
10258bd25ccdSJames Feist                                         << "Invalid redundancy interface";
10268bd25ccdSJames Feist                                     messages::internalError(
10278bd25ccdSJames Feist                                         sensorsAsyncResp->res);
10288bd25ccdSJames Feist                                     return;
10298bd25ccdSJames Feist                                 }
10308bd25ccdSJames Feist 
10318bd25ccdSJames Feist                                 auto allowedFailures = std::get_if<uint8_t>(
10328bd25ccdSJames Feist                                     &(findFailures->second));
10338bd25ccdSJames Feist                                 auto collection =
10348bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
10358bd25ccdSJames Feist                                         &(findCollection->second));
10368bd25ccdSJames Feist                                 auto status = std::get_if<std::string>(
10378bd25ccdSJames Feist                                     &(findStatus->second));
10388bd25ccdSJames Feist 
10398bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
10408bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
10418bd25ccdSJames Feist                                 {
10428bd25ccdSJames Feist 
10438bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
10448bd25ccdSJames Feist                                         << "Invalid redundancy interface "
10458bd25ccdSJames Feist                                            "types";
10468bd25ccdSJames Feist                                     messages::internalError(
10478bd25ccdSJames Feist                                         sensorsAsyncResp->res);
10488bd25ccdSJames Feist                                     return;
10498bd25ccdSJames Feist                                 }
10508bd25ccdSJames Feist                                 size_t lastSlash = path.rfind("/");
10518bd25ccdSJames Feist                                 if (lastSlash == std::string::npos)
10528bd25ccdSJames Feist                                 {
10538bd25ccdSJames Feist                                     // this should be impossible
10548bd25ccdSJames Feist                                     messages::internalError(
10558bd25ccdSJames Feist                                         sensorsAsyncResp->res);
10568bd25ccdSJames Feist                                     return;
10578bd25ccdSJames Feist                                 }
10588bd25ccdSJames Feist                                 std::string name = path.substr(lastSlash + 1);
10598bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
10608bd25ccdSJames Feist                                              ' ');
10618bd25ccdSJames Feist 
10628bd25ccdSJames Feist                                 std::string health;
10638bd25ccdSJames Feist 
10648bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
10658bd25ccdSJames Feist                                 {
10668bd25ccdSJames Feist                                     health = "OK";
10678bd25ccdSJames Feist                                 }
10688bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
10698bd25ccdSJames Feist                                 {
10708bd25ccdSJames Feist                                     health = "Warning";
10718bd25ccdSJames Feist                                 }
10728bd25ccdSJames Feist                                 else
10738bd25ccdSJames Feist                                 {
10748bd25ccdSJames Feist                                     health = "Critical";
10758bd25ccdSJames Feist                                 }
10768bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
10778bd25ccdSJames Feist                                 const auto& fanRedfish =
10788bd25ccdSJames Feist                                     sensorsAsyncResp->res.jsonValue["Fans"];
10798bd25ccdSJames Feist                                 for (const std::string& item : *collection)
10808bd25ccdSJames Feist                                 {
10818bd25ccdSJames Feist                                     lastSlash = item.rfind("/");
10828bd25ccdSJames Feist                                     // make a copy as collection is const
10838bd25ccdSJames Feist                                     std::string itemName =
10848bd25ccdSJames Feist                                         item.substr(lastSlash + 1);
10858bd25ccdSJames Feist                                     /*
10868bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
10878bd25ccdSJames Feist                                     std::replace(itemName.begin(),
10888bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
10898bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
10908bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
10918bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
10928bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
10938bd25ccdSJames Feist                                         });
10948bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
10958bd25ccdSJames Feist                                     {
10968bd25ccdSJames Feist                                         redfishCollection.push_back(
10978bd25ccdSJames Feist                                             {{"@odata.id",
10988bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
10998bd25ccdSJames Feist                                     }
11008bd25ccdSJames Feist                                     else
11018bd25ccdSJames Feist                                     {
11028bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
11038bd25ccdSJames Feist                                             << "failed to find fan in schema";
11048bd25ccdSJames Feist                                         messages::internalError(
11058bd25ccdSJames Feist                                             sensorsAsyncResp->res);
11068bd25ccdSJames Feist                                         return;
11078bd25ccdSJames Feist                                     }
11088bd25ccdSJames Feist                                 }
11098bd25ccdSJames Feist 
1110271584abSEd Tanous                                 nlohmann::json& jResp =
1111271584abSEd Tanous                                     sensorsAsyncResp->res
11128bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1113271584abSEd Tanous                                 jResp.push_back(
11148bd25ccdSJames Feist                                     {{"@odata.id",
1115717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
11168bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
11178bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
11188bd25ccdSJames Feist                                           "#/Redundancy/" +
1119271584abSEd Tanous                                           std::to_string(jResp.size())},
11208bd25ccdSJames Feist                                      {"@odata.type",
11218bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
11228bd25ccdSJames Feist                                      {"MinNumNeeded",
11238bd25ccdSJames Feist                                       collection->size() - *allowedFailures},
11248bd25ccdSJames Feist                                      {"MemberId", name},
11258bd25ccdSJames Feist                                      {"Mode", "N+m"},
11268bd25ccdSJames Feist                                      {"Name", name},
11278bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
11288bd25ccdSJames Feist                                      {"Status",
11298bd25ccdSJames Feist                                       {{"Health", health},
11308bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
11318bd25ccdSJames Feist                             },
11328bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
11338bd25ccdSJames Feist                             "GetAll",
11348bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
11358bd25ccdSJames Feist                     },
113602e92e32SJames Feist                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11378bd25ccdSJames Feist                     "org.freedesktop.DBus.Properties", "Get",
11388bd25ccdSJames Feist                     "xyz.openbmc_project.Association", "endpoints");
11398bd25ccdSJames Feist             }
11408bd25ccdSJames Feist         },
11418bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
11428bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
11438bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
11448bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
11458bd25ccdSJames Feist         std::array<const char*, 1>{
11468bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
11478bd25ccdSJames Feist }
11488bd25ccdSJames Feist 
114949c53ac9SJohnathan Mantey void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
115049c53ac9SJohnathan Mantey {
115149c53ac9SJohnathan Mantey     nlohmann::json& response = SensorsAsyncResp->res.jsonValue;
115249c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
115349c53ac9SJohnathan Mantey     if (SensorsAsyncResp->chassisSubNode == "Power")
115449c53ac9SJohnathan Mantey     {
115549c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
115649c53ac9SJohnathan Mantey     }
115749c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
115849c53ac9SJohnathan Mantey     {
115949c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
116049c53ac9SJohnathan Mantey         if (entry != response.end())
116149c53ac9SJohnathan Mantey         {
116249c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
116349c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
116449c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
116549c53ac9SJohnathan Mantey                       });
116649c53ac9SJohnathan Mantey 
116749c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
116849c53ac9SJohnathan Mantey             size_t count = 0;
116949c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
117049c53ac9SJohnathan Mantey             {
117149c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
117249c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
117349c53ac9SJohnathan Mantey                 {
117449c53ac9SJohnathan Mantey                     continue;
117549c53ac9SJohnathan Mantey                 }
117649c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
117749c53ac9SJohnathan Mantey                 if (value != nullptr)
117849c53ac9SJohnathan Mantey                 {
117949c53ac9SJohnathan Mantey                     *value += std::to_string(count);
118049c53ac9SJohnathan Mantey                     count++;
118149c53ac9SJohnathan Mantey                 }
118249c53ac9SJohnathan Mantey             }
118349c53ac9SJohnathan Mantey         }
118449c53ac9SJohnathan Mantey     }
118549c53ac9SJohnathan Mantey }
118649c53ac9SJohnathan Mantey 
118708777fb0SLewanczyk, Dawid /**
1188adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1189adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1190adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1191adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
11928fb49dd6SShawn McCarney  */
1193adc4f0dbSShawn McCarney static InventoryItem* findInventoryItem(
1194adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1195adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
11968fb49dd6SShawn McCarney {
1197adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
11988fb49dd6SShawn McCarney     {
1199adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
12008fb49dd6SShawn McCarney         {
1201adc4f0dbSShawn McCarney             return &inventoryItem;
12028fb49dd6SShawn McCarney         }
12038fb49dd6SShawn McCarney     }
12048fb49dd6SShawn McCarney     return nullptr;
12058fb49dd6SShawn McCarney }
12068fb49dd6SShawn McCarney 
12078fb49dd6SShawn McCarney /**
1208adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1209adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1210adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1211adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12128fb49dd6SShawn McCarney  */
1213adc4f0dbSShawn McCarney static InventoryItem* findInventoryItemForSensor(
1214adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1215adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1216adc4f0dbSShawn McCarney {
1217adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1218adc4f0dbSShawn McCarney     {
1219adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1220adc4f0dbSShawn McCarney         {
1221adc4f0dbSShawn McCarney             return &inventoryItem;
1222adc4f0dbSShawn McCarney         }
1223adc4f0dbSShawn McCarney     }
1224adc4f0dbSShawn McCarney     return nullptr;
1225adc4f0dbSShawn McCarney }
1226adc4f0dbSShawn McCarney 
1227adc4f0dbSShawn McCarney /**
1228d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1229d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1230d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1231d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1232d500549bSAnthony Wilson  */
1233d500549bSAnthony Wilson inline InventoryItem*
1234d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1235d500549bSAnthony Wilson                             const std::string& ledObjPath)
1236d500549bSAnthony Wilson {
1237d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1238d500549bSAnthony Wilson     {
1239d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1240d500549bSAnthony Wilson         {
1241d500549bSAnthony Wilson             return &inventoryItem;
1242d500549bSAnthony Wilson         }
1243d500549bSAnthony Wilson     }
1244d500549bSAnthony Wilson     return nullptr;
1245d500549bSAnthony Wilson }
1246d500549bSAnthony Wilson 
1247d500549bSAnthony Wilson /**
1248adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1249adc4f0dbSShawn McCarney  *
1250adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1251adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1252adc4f0dbSShawn McCarney  * added to the vector.
1253adc4f0dbSShawn McCarney  *
1254adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1255adc4f0dbSShawn McCarney  * InventoryItem.
1256adc4f0dbSShawn McCarney  *
1257adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1258adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1259adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1260adc4f0dbSShawn McCarney  */
1261adc4f0dbSShawn McCarney static void
1262adc4f0dbSShawn McCarney     addInventoryItem(std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1263adc4f0dbSShawn McCarney                      const std::string& invItemObjPath,
1264adc4f0dbSShawn McCarney                      const std::string& sensorObjPath)
1265adc4f0dbSShawn McCarney {
1266adc4f0dbSShawn McCarney     // Look for inventory item in vector
1267adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1268adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1269adc4f0dbSShawn McCarney 
1270adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1271adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1272adc4f0dbSShawn McCarney     {
1273adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1274adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1275adc4f0dbSShawn McCarney     }
1276adc4f0dbSShawn McCarney 
1277adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1278adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1279adc4f0dbSShawn McCarney }
1280adc4f0dbSShawn McCarney 
1281adc4f0dbSShawn McCarney /**
1282adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1283adc4f0dbSShawn McCarney  *
1284adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1285adc4f0dbSShawn McCarney  * specified InventoryItem.
1286adc4f0dbSShawn McCarney  *
1287adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1288adc4f0dbSShawn McCarney  * response.
1289adc4f0dbSShawn McCarney  *
1290adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1291adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1292adc4f0dbSShawn McCarney  * for the specified inventory item.
1293adc4f0dbSShawn McCarney  */
1294adc4f0dbSShawn McCarney static void storeInventoryItemData(
1295adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
12968fb49dd6SShawn McCarney     const boost::container::flat_map<
12978fb49dd6SShawn McCarney         std::string, boost::container::flat_map<std::string, SensorVariant>>&
12988fb49dd6SShawn McCarney         interfacesDict)
12998fb49dd6SShawn McCarney {
1300adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1301adc4f0dbSShawn McCarney     auto interfaceIt =
1302adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1303adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
13048fb49dd6SShawn McCarney     {
1305adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Present");
1306adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
13078fb49dd6SShawn McCarney         {
1308adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1309adc4f0dbSShawn McCarney             if (value != nullptr)
13108fb49dd6SShawn McCarney             {
1311adc4f0dbSShawn McCarney                 inventoryItem.isPresent = *value;
13128fb49dd6SShawn McCarney             }
13138fb49dd6SShawn McCarney         }
13148fb49dd6SShawn McCarney     }
13158fb49dd6SShawn McCarney 
1316adc4f0dbSShawn McCarney     // Check if Inventory.Item.PowerSupply interface is present
1317adc4f0dbSShawn McCarney     interfaceIt =
1318adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1319adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
13208fb49dd6SShawn McCarney     {
1321adc4f0dbSShawn McCarney         inventoryItem.isPowerSupply = true;
13228fb49dd6SShawn McCarney     }
1323adc4f0dbSShawn McCarney 
1324adc4f0dbSShawn McCarney     // Get properties from Inventory.Decorator.Asset interface
1325adc4f0dbSShawn McCarney     interfaceIt =
1326adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1327adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1328adc4f0dbSShawn McCarney     {
1329adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Manufacturer");
1330adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1331adc4f0dbSShawn McCarney         {
1332adc4f0dbSShawn McCarney             const std::string* value =
1333adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1334adc4f0dbSShawn McCarney             if (value != nullptr)
1335adc4f0dbSShawn McCarney             {
1336adc4f0dbSShawn McCarney                 inventoryItem.manufacturer = *value;
1337adc4f0dbSShawn McCarney             }
1338adc4f0dbSShawn McCarney         }
1339adc4f0dbSShawn McCarney 
1340adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("Model");
1341adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1342adc4f0dbSShawn McCarney         {
1343adc4f0dbSShawn McCarney             const std::string* value =
1344adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1345adc4f0dbSShawn McCarney             if (value != nullptr)
1346adc4f0dbSShawn McCarney             {
1347adc4f0dbSShawn McCarney                 inventoryItem.model = *value;
1348adc4f0dbSShawn McCarney             }
1349adc4f0dbSShawn McCarney         }
1350adc4f0dbSShawn McCarney 
1351adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("PartNumber");
1352adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1353adc4f0dbSShawn McCarney         {
1354adc4f0dbSShawn McCarney             const std::string* value =
1355adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1356adc4f0dbSShawn McCarney             if (value != nullptr)
1357adc4f0dbSShawn McCarney             {
1358adc4f0dbSShawn McCarney                 inventoryItem.partNumber = *value;
1359adc4f0dbSShawn McCarney             }
1360adc4f0dbSShawn McCarney         }
1361adc4f0dbSShawn McCarney 
1362adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("SerialNumber");
1363adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1364adc4f0dbSShawn McCarney         {
1365adc4f0dbSShawn McCarney             const std::string* value =
1366adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1367adc4f0dbSShawn McCarney             if (value != nullptr)
1368adc4f0dbSShawn McCarney             {
1369adc4f0dbSShawn McCarney                 inventoryItem.serialNumber = *value;
1370adc4f0dbSShawn McCarney             }
1371adc4f0dbSShawn McCarney         }
1372adc4f0dbSShawn McCarney     }
1373adc4f0dbSShawn McCarney 
1374adc4f0dbSShawn McCarney     // Get properties from State.Decorator.OperationalStatus interface
1375adc4f0dbSShawn McCarney     interfaceIt = interfacesDict.find(
1376adc4f0dbSShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus");
1377adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1378adc4f0dbSShawn McCarney     {
1379adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Functional");
1380adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1381adc4f0dbSShawn McCarney         {
1382adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1383adc4f0dbSShawn McCarney             if (value != nullptr)
1384adc4f0dbSShawn McCarney             {
1385adc4f0dbSShawn McCarney                 inventoryItem.isFunctional = *value;
13868fb49dd6SShawn McCarney             }
13878fb49dd6SShawn McCarney         }
13888fb49dd6SShawn McCarney     }
13898fb49dd6SShawn McCarney }
13908fb49dd6SShawn McCarney 
13918fb49dd6SShawn McCarney /**
1392adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
13938fb49dd6SShawn McCarney  *
1394adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1395adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1396adc4f0dbSShawn McCarney  * inventoryItems vector.
13978fb49dd6SShawn McCarney  *
1398adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1399adc4f0dbSShawn McCarney  * response.
1400adc4f0dbSShawn McCarney  *
1401adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1402adc4f0dbSShawn McCarney  * been obtained.
1403adc4f0dbSShawn McCarney  *
1404adc4f0dbSShawn McCarney  * The callback must have the following signature:
1405adc4f0dbSShawn McCarney  *   @code
1406d500549bSAnthony Wilson  *   callback(void)
1407adc4f0dbSShawn McCarney  *   @endcode
1408adc4f0dbSShawn McCarney  *
1409adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1410adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1411adc4f0dbSShawn McCarney  * last asynchronous function has completed.
14128fb49dd6SShawn McCarney  *
14138fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1414adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1415adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
14168fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
14178fb49dd6SShawn McCarney  * implements ObjectManager.
1418adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1419adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1420adc4f0dbSShawn McCarney  * in recursive calls to this function.
14218fb49dd6SShawn McCarney  */
1422adc4f0dbSShawn McCarney template <typename Callback>
1423adc4f0dbSShawn McCarney static void getInventoryItemsData(
14248fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1425adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
14268fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
14278fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1428adc4f0dbSShawn McCarney         objectMgrPaths,
1429271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
14308fb49dd6SShawn McCarney {
1431adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
14328fb49dd6SShawn McCarney 
1433adc4f0dbSShawn McCarney     // If no more connections left, call callback
1434adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
14358fb49dd6SShawn McCarney     {
1436d500549bSAnthony Wilson         callback();
1437adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1438adc4f0dbSShawn McCarney         return;
1439adc4f0dbSShawn McCarney     }
1440adc4f0dbSShawn McCarney 
1441adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1442adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1443adc4f0dbSShawn McCarney     if (it != invConnections->end())
1444adc4f0dbSShawn McCarney     {
1445adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1446adc4f0dbSShawn McCarney 
14478fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1448adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1449adc4f0dbSShawn McCarney                             objectMgrPaths, callback{std::move(callback)},
1450adc4f0dbSShawn McCarney                             invConnectionsIndex](
1451adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
14528fb49dd6SShawn McCarney                                ManagedObjectsVectorType& resp) {
1453adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
14548fb49dd6SShawn McCarney             if (ec)
14558fb49dd6SShawn McCarney             {
14568fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1457adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
14588fb49dd6SShawn McCarney                 messages::internalError(sensorsAsyncResp->res);
14598fb49dd6SShawn McCarney                 return;
14608fb49dd6SShawn McCarney             }
14618fb49dd6SShawn McCarney 
14628fb49dd6SShawn McCarney             // Loop through returned object paths
14638fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
14648fb49dd6SShawn McCarney             {
14658fb49dd6SShawn McCarney                 const std::string& objPath =
14668fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
14678fb49dd6SShawn McCarney 
1468adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1469adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1470adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1471adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
14728fb49dd6SShawn McCarney                 {
1473adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1474adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
14758fb49dd6SShawn McCarney                 }
14768fb49dd6SShawn McCarney             }
14778fb49dd6SShawn McCarney 
1478adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1479adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1480adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1481adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1482adc4f0dbSShawn McCarney 
1483adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
14848fb49dd6SShawn McCarney         };
14858fb49dd6SShawn McCarney 
14868fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
14878fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
14888fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
14898fb49dd6SShawn McCarney         const std::string& objectMgrPath =
14908fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
14918fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
14928fb49dd6SShawn McCarney                          << objectMgrPath;
14938fb49dd6SShawn McCarney 
14948fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
14958fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
14968fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
14978fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14988fb49dd6SShawn McCarney     }
14998fb49dd6SShawn McCarney 
1500adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
15018fb49dd6SShawn McCarney }
15028fb49dd6SShawn McCarney 
15038fb49dd6SShawn McCarney /**
1504adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
15058fb49dd6SShawn McCarney  *
1506adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1507adc4f0dbSShawn McCarney  * items that are associated with sensors.
15088fb49dd6SShawn McCarney  *
15098fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
15108fb49dd6SShawn McCarney  * been obtained.
15118fb49dd6SShawn McCarney  *
15128fb49dd6SShawn McCarney  * The callback must have the following signature:
15138fb49dd6SShawn McCarney  *   @code
15148fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
15158fb49dd6SShawn McCarney  *            invConnections)
15168fb49dd6SShawn McCarney  *   @endcode
15178fb49dd6SShawn McCarney  *
15188fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1519adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
15208fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
15218fb49dd6SShawn McCarney  */
15228fb49dd6SShawn McCarney template <typename Callback>
15238fb49dd6SShawn McCarney static void getInventoryItemsConnections(
15248fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1525adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
15268fb49dd6SShawn McCarney     Callback&& callback)
15278fb49dd6SShawn McCarney {
15288fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
15298fb49dd6SShawn McCarney 
15308fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1531adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
15328fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1533adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1534adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
15358fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
15368fb49dd6SShawn McCarney 
15378fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
15388fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1539adc4f0dbSShawn McCarney                         inventoryItems](const boost::system::error_code ec,
15408fb49dd6SShawn McCarney                                         const GetSubTreeType& subtree) {
15418fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
15428fb49dd6SShawn McCarney         if (ec)
15438fb49dd6SShawn McCarney         {
15448fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
15458fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
15468fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
15478fb49dd6SShawn McCarney             return;
15488fb49dd6SShawn McCarney         }
15498fb49dd6SShawn McCarney 
15508fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
15518fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
15528fb49dd6SShawn McCarney             invConnections =
15538fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
15548fb49dd6SShawn McCarney         invConnections->reserve(8);
15558fb49dd6SShawn McCarney 
15568fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
15578fb49dd6SShawn McCarney         for (const std::pair<
15588fb49dd6SShawn McCarney                  std::string,
15598fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
15608fb49dd6SShawn McCarney                  object : subtree)
15618fb49dd6SShawn McCarney         {
1562adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
15638fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1564adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
15658fb49dd6SShawn McCarney             {
15668fb49dd6SShawn McCarney                 // Store all connections to inventory item
15678fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
15688fb49dd6SShawn McCarney                          objData : object.second)
15698fb49dd6SShawn McCarney                 {
15708fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
15718fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
15728fb49dd6SShawn McCarney                 }
15738fb49dd6SShawn McCarney             }
15748fb49dd6SShawn McCarney         }
1575d500549bSAnthony Wilson 
15768fb49dd6SShawn McCarney         callback(invConnections);
15778fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
15788fb49dd6SShawn McCarney     };
15798fb49dd6SShawn McCarney 
15808fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
15818fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
15828fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
15838fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
15848fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
15858fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
15868fb49dd6SShawn McCarney }
15878fb49dd6SShawn McCarney 
15888fb49dd6SShawn McCarney /**
1589adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
15908fb49dd6SShawn McCarney  *
15918fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1592d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1593d500549bSAnthony Wilson  * their LEDs, if any.
15948fb49dd6SShawn McCarney  *
15958fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
15968fb49dd6SShawn McCarney  * has been obtained.
15978fb49dd6SShawn McCarney  *
15988fb49dd6SShawn McCarney  * The callback must have the following signature:
15998fb49dd6SShawn McCarney  *   @code
1600adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
16018fb49dd6SShawn McCarney  *   @endcode
16028fb49dd6SShawn McCarney  *
16038fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
16048fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
16058fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
16068fb49dd6SShawn McCarney  * implements ObjectManager.
16078fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
16088fb49dd6SShawn McCarney  */
16098fb49dd6SShawn McCarney template <typename Callback>
1610adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
16118fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
16128fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
16138fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
16148fb49dd6SShawn McCarney         objectMgrPaths,
16158fb49dd6SShawn McCarney     Callback&& callback)
16168fb49dd6SShawn McCarney {
1617adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
16188fb49dd6SShawn McCarney 
16198fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
16208fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
16218fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
16228fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1623adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
16248fb49dd6SShawn McCarney         if (ec)
16258fb49dd6SShawn McCarney         {
1626adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1627adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
16288fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
16298fb49dd6SShawn McCarney             return;
16308fb49dd6SShawn McCarney         }
16318fb49dd6SShawn McCarney 
1632adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1633adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1634adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1635adc4f0dbSShawn McCarney 
16368fb49dd6SShawn McCarney         // Loop through returned object paths
16378fb49dd6SShawn McCarney         std::string sensorAssocPath;
16388fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
16398fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
16408fb49dd6SShawn McCarney         {
16418fb49dd6SShawn McCarney             const std::string& objPath =
16428fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
16438fb49dd6SShawn McCarney             const boost::container::flat_map<
16448fb49dd6SShawn McCarney                 std::string, boost::container::flat_map<
16458fb49dd6SShawn McCarney                                  std::string, dbus::utility::DbusVariantType>>&
16468fb49dd6SShawn McCarney                 interfacesDict = objDictEntry.second;
16478fb49dd6SShawn McCarney 
16488fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
16498fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
16508fb49dd6SShawn McCarney             {
16518fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
16528fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
16538fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
16548fb49dd6SShawn McCarney                 {
16558fb49dd6SShawn McCarney                     // Get Association interface for object path
16568fb49dd6SShawn McCarney                     auto assocIt =
16578fb49dd6SShawn McCarney                         interfacesDict.find("xyz.openbmc_project.Association");
16588fb49dd6SShawn McCarney                     if (assocIt != interfacesDict.end())
16598fb49dd6SShawn McCarney                     {
16608fb49dd6SShawn McCarney                         // Get inventory item from end point
16618fb49dd6SShawn McCarney                         auto endpointsIt = assocIt->second.find("endpoints");
16628fb49dd6SShawn McCarney                         if (endpointsIt != assocIt->second.end())
16638fb49dd6SShawn McCarney                         {
16648fb49dd6SShawn McCarney                             const std::vector<std::string>* endpoints =
16658fb49dd6SShawn McCarney                                 std::get_if<std::vector<std::string>>(
16668fb49dd6SShawn McCarney                                     &endpointsIt->second);
16678fb49dd6SShawn McCarney                             if ((endpoints != nullptr) && !endpoints->empty())
16688fb49dd6SShawn McCarney                             {
1669adc4f0dbSShawn McCarney                                 // Add inventory item to vector
1670adc4f0dbSShawn McCarney                                 const std::string& invItemPath =
1671adc4f0dbSShawn McCarney                                     endpoints->front();
1672adc4f0dbSShawn McCarney                                 addInventoryItem(inventoryItems, invItemPath,
1673adc4f0dbSShawn McCarney                                                  sensorName);
16748fb49dd6SShawn McCarney                             }
16758fb49dd6SShawn McCarney                         }
16768fb49dd6SShawn McCarney                     }
16778fb49dd6SShawn McCarney                     break;
16788fb49dd6SShawn McCarney                 }
16798fb49dd6SShawn McCarney             }
16808fb49dd6SShawn McCarney         }
16818fb49dd6SShawn McCarney 
1682d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1683d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1684d500549bSAnthony Wilson         std::string inventoryAssocPath;
1685d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1686d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1687d500549bSAnthony Wilson         {
1688d500549bSAnthony Wilson             const std::string& objPath =
1689d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1690d500549bSAnthony Wilson             const boost::container::flat_map<
1691d500549bSAnthony Wilson                 std::string, boost::container::flat_map<
1692d500549bSAnthony Wilson                                  std::string, dbus::utility::DbusVariantType>>&
1693d500549bSAnthony Wilson                 interfacesDict = objDictEntry.second;
1694d500549bSAnthony Wilson 
1695d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1696d500549bSAnthony Wilson             {
1697d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1698d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1699d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1700d500549bSAnthony Wilson                 {
1701d500549bSAnthony Wilson                     // Get Association interface for object path
1702d500549bSAnthony Wilson                     auto assocIt =
1703d500549bSAnthony Wilson                         interfacesDict.find("xyz.openbmc_project.Association");
1704d500549bSAnthony Wilson                     if (assocIt != interfacesDict.end())
1705d500549bSAnthony Wilson                     {
1706d500549bSAnthony Wilson                         // Get inventory item from end point
1707d500549bSAnthony Wilson                         auto endpointsIt = assocIt->second.find("endpoints");
1708d500549bSAnthony Wilson                         if (endpointsIt != assocIt->second.end())
1709d500549bSAnthony Wilson                         {
1710d500549bSAnthony Wilson                             const std::vector<std::string>* endpoints =
1711d500549bSAnthony Wilson                                 std::get_if<std::vector<std::string>>(
1712d500549bSAnthony Wilson                                     &endpointsIt->second);
1713d500549bSAnthony Wilson                             if ((endpoints != nullptr) && !endpoints->empty())
1714d500549bSAnthony Wilson                             {
1715d500549bSAnthony Wilson                                 // Store LED path in inventory item
1716d500549bSAnthony Wilson                                 const std::string& ledPath = endpoints->front();
1717d500549bSAnthony Wilson                                 inventoryItem.ledObjectPath = ledPath;
1718d500549bSAnthony Wilson                             }
1719d500549bSAnthony Wilson                         }
1720d500549bSAnthony Wilson                     }
1721d500549bSAnthony Wilson                     break;
1722d500549bSAnthony Wilson                 }
1723d500549bSAnthony Wilson             }
1724d500549bSAnthony Wilson         }
1725adc4f0dbSShawn McCarney         callback(inventoryItems);
1726adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
17278fb49dd6SShawn McCarney     };
17288fb49dd6SShawn McCarney 
17298fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
17308fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
17318fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
17328fb49dd6SShawn McCarney     const std::string& objectMgrPath =
17338fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
17348fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
17358fb49dd6SShawn McCarney                      << objectMgrPath;
17368fb49dd6SShawn McCarney 
17378fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
17388fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17398fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
17408fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17418fb49dd6SShawn McCarney 
1742adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
17438fb49dd6SShawn McCarney }
17448fb49dd6SShawn McCarney 
17458fb49dd6SShawn McCarney /**
1746d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1747d500549bSAnthony Wilson  *
1748d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1749d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1750d500549bSAnthony Wilson  * inventoryItems vector.
1751d500549bSAnthony Wilson  *
1752d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1753d500549bSAnthony Wilson  * response.
1754d500549bSAnthony Wilson  *
1755d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1756d500549bSAnthony Wilson  * has been obtained.
1757d500549bSAnthony Wilson  *
1758d500549bSAnthony Wilson  * The callback must have the following signature:
1759d500549bSAnthony Wilson  *   @code
176042cbe538SGunnar Mills  *   callback()
1761d500549bSAnthony Wilson  *   @endcode
1762d500549bSAnthony Wilson  *
1763d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1764d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1765d500549bSAnthony Wilson  * last asynchronous function has completed.
1766d500549bSAnthony Wilson  *
1767d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1768d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1769d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1770d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1771d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1772d500549bSAnthony Wilson  * in recursive calls to this function.
1773d500549bSAnthony Wilson  */
1774d500549bSAnthony Wilson template <typename Callback>
1775d500549bSAnthony Wilson void getInventoryLedData(
1776d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1777d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1778d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1779d500549bSAnthony Wilson         ledConnections,
1780d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1781d500549bSAnthony Wilson {
1782d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1783d500549bSAnthony Wilson 
1784d500549bSAnthony Wilson     // If no more connections left, call callback
1785d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1786d500549bSAnthony Wilson     {
178742cbe538SGunnar Mills         callback();
1788d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1789d500549bSAnthony Wilson         return;
1790d500549bSAnthony Wilson     }
1791d500549bSAnthony Wilson 
1792d500549bSAnthony Wilson     // Get inventory item data from current connection
1793d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1794d500549bSAnthony Wilson     if (it != ledConnections->end())
1795d500549bSAnthony Wilson     {
1796d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1797d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1798d500549bSAnthony Wilson         // Response handler for Get State property
1799d500549bSAnthony Wilson         auto respHandler =
1800d500549bSAnthony Wilson             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1801d500549bSAnthony Wilson              callback{std::move(callback)},
1802d500549bSAnthony Wilson              ledConnectionsIndex](const boost::system::error_code ec,
1803d500549bSAnthony Wilson                                   const std::variant<std::string>& ledState) {
1804d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1805d500549bSAnthony Wilson                 if (ec)
1806d500549bSAnthony Wilson                 {
1807d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
1808d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
1809d500549bSAnthony Wilson                     messages::internalError(sensorsAsyncResp->res);
1810d500549bSAnthony Wilson                     return;
1811d500549bSAnthony Wilson                 }
1812d500549bSAnthony Wilson 
1813d500549bSAnthony Wilson                 const std::string* state = std::get_if<std::string>(&ledState);
1814d500549bSAnthony Wilson                 if (state != nullptr)
1815d500549bSAnthony Wilson                 {
1816d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Led state: " << *state;
1817d500549bSAnthony Wilson                     // Find inventory item with this LED object path
1818d500549bSAnthony Wilson                     InventoryItem* inventoryItem =
1819d500549bSAnthony Wilson                         findInventoryItemForLed(*inventoryItems, ledPath);
1820d500549bSAnthony Wilson                     if (inventoryItem != nullptr)
1821d500549bSAnthony Wilson                     {
1822d500549bSAnthony Wilson                         // Store LED state in InventoryItem
1823d500549bSAnthony Wilson                         if (boost::ends_with(*state, "On"))
1824d500549bSAnthony Wilson                         {
1825d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::ON;
1826d500549bSAnthony Wilson                         }
1827d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Blink"))
1828d500549bSAnthony Wilson                         {
1829d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::BLINK;
1830d500549bSAnthony Wilson                         }
1831d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Off"))
1832d500549bSAnthony Wilson                         {
1833d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::OFF;
1834d500549bSAnthony Wilson                         }
1835d500549bSAnthony Wilson                         else
1836d500549bSAnthony Wilson                         {
1837d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::UNKNOWN;
1838d500549bSAnthony Wilson                         }
1839d500549bSAnthony Wilson                     }
1840d500549bSAnthony Wilson                 }
1841d500549bSAnthony Wilson                 else
1842d500549bSAnthony Wilson                 {
1843d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
1844d500549bSAnthony Wilson                                      << ledPath;
1845d500549bSAnthony Wilson                 }
1846d500549bSAnthony Wilson 
1847d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1848d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1849d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1850d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1851d500549bSAnthony Wilson 
1852d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1853d500549bSAnthony Wilson             };
1854d500549bSAnthony Wilson 
1855d500549bSAnthony Wilson         // Get the State property for the current LED
1856d500549bSAnthony Wilson         crow::connections::systemBus->async_method_call(
1857d500549bSAnthony Wilson             std::move(respHandler), ledConnection, ledPath,
1858d500549bSAnthony Wilson             "org.freedesktop.DBus.Properties", "Get",
1859d500549bSAnthony Wilson             "xyz.openbmc_project.Led.Physical", "State");
1860d500549bSAnthony Wilson     }
1861d500549bSAnthony Wilson 
1862d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1863d500549bSAnthony Wilson }
1864d500549bSAnthony Wilson 
1865d500549bSAnthony Wilson /**
1866d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1867d500549bSAnthony Wilson  *
1868d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1869d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1870d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1871d500549bSAnthony Wilson  *
1872d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1873d500549bSAnthony Wilson  * response.
1874d500549bSAnthony Wilson  *
1875d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1876d500549bSAnthony Wilson  * been obtained.
1877d500549bSAnthony Wilson  *
1878d500549bSAnthony Wilson  * The callback must have the following signature:
1879d500549bSAnthony Wilson  *   @code
188042cbe538SGunnar Mills  *   callback()
1881d500549bSAnthony Wilson  *   @endcode
1882d500549bSAnthony Wilson  *
1883d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1884d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1885d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1886d500549bSAnthony Wilson  */
1887d500549bSAnthony Wilson template <typename Callback>
1888d500549bSAnthony Wilson void getInventoryLeds(
1889d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1890d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1891d500549bSAnthony Wilson     Callback&& callback)
1892d500549bSAnthony Wilson {
1893d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1894d500549bSAnthony Wilson 
1895d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1896d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
1897d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1898d500549bSAnthony Wilson 
1899d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
1900d500549bSAnthony Wilson     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1901d500549bSAnthony Wilson                         inventoryItems](const boost::system::error_code ec,
1902d500549bSAnthony Wilson                                         const GetSubTreeType& subtree) {
1903d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1904d500549bSAnthony Wilson         if (ec)
1905d500549bSAnthony Wilson         {
1906d500549bSAnthony Wilson             messages::internalError(sensorsAsyncResp->res);
1907d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1908d500549bSAnthony Wilson                              << ec;
1909d500549bSAnthony Wilson             return;
1910d500549bSAnthony Wilson         }
1911d500549bSAnthony Wilson 
1912d500549bSAnthony Wilson         // Build map of LED object paths to connections
1913d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1914d500549bSAnthony Wilson             ledConnections = std::make_shared<
1915d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
1916d500549bSAnthony Wilson 
1917d500549bSAnthony Wilson         // Loop through objects from GetSubTree
1918d500549bSAnthony Wilson         for (const std::pair<
1919d500549bSAnthony Wilson                  std::string,
1920d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1921d500549bSAnthony Wilson                  object : subtree)
1922d500549bSAnthony Wilson         {
1923d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
1924d500549bSAnthony Wilson             // items
1925d500549bSAnthony Wilson             const std::string& ledPath = object.first;
1926d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1927d500549bSAnthony Wilson             {
1928d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
1929d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
1930d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
1931d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1932d500549bSAnthony Wilson                                  << connection;
1933d500549bSAnthony Wilson             }
1934d500549bSAnthony Wilson         }
1935d500549bSAnthony Wilson 
1936d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1937d500549bSAnthony Wilson                             std::move(callback));
1938d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1939d500549bSAnthony Wilson     };
1940d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
1941d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
1942d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1943d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
1944d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1945d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1946d500549bSAnthony Wilson }
1947d500549bSAnthony Wilson 
1948d500549bSAnthony Wilson /**
194942cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
195042cbe538SGunnar Mills  *
195142cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
195242cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
195342cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
195442cbe538SGunnar Mills  *
195542cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
195642cbe538SGunnar Mills  * response.
195742cbe538SGunnar Mills  *
195842cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
195942cbe538SGunnar Mills  * when data has been obtained.
196042cbe538SGunnar Mills  *
196142cbe538SGunnar Mills  * The callback must have the following signature:
196242cbe538SGunnar Mills  *   @code
196342cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
196442cbe538SGunnar Mills  *   @endcode
196542cbe538SGunnar Mills  *
196642cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
196742cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
196842cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
196942cbe538SGunnar Mills  *        Supply Attributes
197042cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
197142cbe538SGunnar Mills  */
197242cbe538SGunnar Mills template <typename Callback>
197342cbe538SGunnar Mills void getPowerSupplyAttributesData(
197442cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
197542cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
197642cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
197742cbe538SGunnar Mills         psAttributesConnections,
197842cbe538SGunnar Mills     Callback&& callback)
197942cbe538SGunnar Mills {
198042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
198142cbe538SGunnar Mills 
198242cbe538SGunnar Mills     if (psAttributesConnections.empty())
198342cbe538SGunnar Mills     {
198442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
198542cbe538SGunnar Mills         callback(inventoryItems);
198642cbe538SGunnar Mills         return;
198742cbe538SGunnar Mills     }
198842cbe538SGunnar Mills 
198942cbe538SGunnar Mills     // Assuming just one connection (service) for now
199042cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
199142cbe538SGunnar Mills 
199242cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
199342cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
199442cbe538SGunnar Mills 
199542cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
199642cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
199742cbe538SGunnar Mills                         callback{std::move(callback)}](
199842cbe538SGunnar Mills                            const boost::system::error_code ec,
199942cbe538SGunnar Mills                            const std::variant<uint32_t>& deratingFactor) {
200042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
200142cbe538SGunnar Mills         if (ec)
200242cbe538SGunnar Mills         {
200342cbe538SGunnar Mills             BMCWEB_LOG_ERROR
200442cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
200542cbe538SGunnar Mills             messages::internalError(sensorsAsyncResp->res);
200642cbe538SGunnar Mills             return;
200742cbe538SGunnar Mills         }
200842cbe538SGunnar Mills 
200942cbe538SGunnar Mills         const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
201042cbe538SGunnar Mills         if (value != nullptr)
201142cbe538SGunnar Mills         {
201242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
201342cbe538SGunnar Mills             // Store value in Power Supply Inventory Items
201442cbe538SGunnar Mills             for (InventoryItem& inventoryItem : *inventoryItems)
201542cbe538SGunnar Mills             {
201642cbe538SGunnar Mills                 if (inventoryItem.isPowerSupply == true)
201742cbe538SGunnar Mills                 {
201842cbe538SGunnar Mills                     inventoryItem.powerSupplyEfficiencyPercent =
201942cbe538SGunnar Mills                         static_cast<int>(*value);
202042cbe538SGunnar Mills                 }
202142cbe538SGunnar Mills             }
202242cbe538SGunnar Mills         }
202342cbe538SGunnar Mills         else
202442cbe538SGunnar Mills         {
202542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG
202642cbe538SGunnar Mills                 << "Failed to find EfficiencyPercent value for PowerSupplies";
202742cbe538SGunnar Mills         }
202842cbe538SGunnar Mills 
202942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
203042cbe538SGunnar Mills         callback(inventoryItems);
203142cbe538SGunnar Mills     };
203242cbe538SGunnar Mills 
203342cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
203442cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
203542cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
203642cbe538SGunnar Mills         std::move(respHandler), psAttributesConnection, psAttributesPath,
203742cbe538SGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
203842cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
203942cbe538SGunnar Mills 
204042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
204142cbe538SGunnar Mills }
204242cbe538SGunnar Mills 
204342cbe538SGunnar Mills /**
204442cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
204542cbe538SGunnar Mills  *
204642cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
204742cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
204842cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
204942cbe538SGunnar Mills  * item.
205042cbe538SGunnar Mills  *
205142cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
205242cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
205342cbe538SGunnar Mills  *
205442cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
205542cbe538SGunnar Mills  * when information has been obtained.
205642cbe538SGunnar Mills  *
205742cbe538SGunnar Mills  * The callback must have the following signature:
205842cbe538SGunnar Mills  *   @code
205942cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
206042cbe538SGunnar Mills  *   @endcode
206142cbe538SGunnar Mills  *
206242cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
206342cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
206442cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
206542cbe538SGunnar Mills  */
206642cbe538SGunnar Mills template <typename Callback>
206742cbe538SGunnar Mills void getPowerSupplyAttributes(
206842cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
206942cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
207042cbe538SGunnar Mills     Callback&& callback)
207142cbe538SGunnar Mills {
207242cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
207342cbe538SGunnar Mills 
207442cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
207542cbe538SGunnar Mills     if (sensorsAsyncResp->chassisSubNode != "Power")
207642cbe538SGunnar Mills     {
207742cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
207842cbe538SGunnar Mills         callback(inventoryItems);
207942cbe538SGunnar Mills         return;
208042cbe538SGunnar Mills     }
208142cbe538SGunnar Mills 
208242cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
208342cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
208442cbe538SGunnar Mills 
208542cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
208642cbe538SGunnar Mills     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
208742cbe538SGunnar Mills                         inventoryItems](const boost::system::error_code ec,
208842cbe538SGunnar Mills                                         const GetSubTreeType& subtree) {
208942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
209042cbe538SGunnar Mills         if (ec)
209142cbe538SGunnar Mills         {
209242cbe538SGunnar Mills             messages::internalError(sensorsAsyncResp->res);
209342cbe538SGunnar Mills             BMCWEB_LOG_ERROR
209442cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
209542cbe538SGunnar Mills             return;
209642cbe538SGunnar Mills         }
209742cbe538SGunnar Mills         if (subtree.size() == 0)
209842cbe538SGunnar Mills         {
209942cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
210042cbe538SGunnar Mills             callback(inventoryItems);
210142cbe538SGunnar Mills             return;
210242cbe538SGunnar Mills         }
210342cbe538SGunnar Mills 
210442cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
210542cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
210642cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
210742cbe538SGunnar Mills         boost::container::flat_map<std::string, std::string>
210842cbe538SGunnar Mills             psAttributesConnections;
210942cbe538SGunnar Mills 
211042cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
211142cbe538SGunnar Mills         {
211242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
211342cbe538SGunnar Mills             callback(inventoryItems);
211442cbe538SGunnar Mills             return;
211542cbe538SGunnar Mills         }
211642cbe538SGunnar Mills 
211742cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
211842cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
211942cbe538SGunnar Mills 
212042cbe538SGunnar Mills         if (connection.empty())
212142cbe538SGunnar Mills         {
212242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
212342cbe538SGunnar Mills             callback(inventoryItems);
212442cbe538SGunnar Mills             return;
212542cbe538SGunnar Mills         }
212642cbe538SGunnar Mills 
212742cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
212842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
212942cbe538SGunnar Mills                          << connection;
213042cbe538SGunnar Mills 
213142cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
213242cbe538SGunnar Mills                                      psAttributesConnections,
213342cbe538SGunnar Mills                                      std::move(callback));
213442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
213542cbe538SGunnar Mills     };
213642cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
213742cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
213842cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
213942cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
214042cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
214142cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
214242cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
214342cbe538SGunnar Mills }
214442cbe538SGunnar Mills 
214542cbe538SGunnar Mills /**
2146adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
21478fb49dd6SShawn McCarney  *
21488fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2149adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
21508fb49dd6SShawn McCarney  *
2151adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2152adc4f0dbSShawn McCarney  * response.
21538fb49dd6SShawn McCarney  *
2154adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2155adc4f0dbSShawn McCarney  * inventory items have been obtained.
2156adc4f0dbSShawn McCarney  *
2157adc4f0dbSShawn McCarney  * The callback must have the following signature:
2158adc4f0dbSShawn McCarney  *   @code
2159adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2160adc4f0dbSShawn McCarney  *   @endcode
21618fb49dd6SShawn McCarney  *
21628fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
21638fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
21648fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
21658fb49dd6SShawn McCarney  * implements ObjectManager.
2166adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
21678fb49dd6SShawn McCarney  */
2168adc4f0dbSShawn McCarney template <typename Callback>
2169adc4f0dbSShawn McCarney static void getInventoryItems(
21708fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
21718fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
21728fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2173adc4f0dbSShawn McCarney         objectMgrPaths,
2174adc4f0dbSShawn McCarney     Callback&& callback)
21758fb49dd6SShawn McCarney {
2176adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2177adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2178adc4f0dbSShawn McCarney         [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2179adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2180adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
21818fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2182adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2183adc4f0dbSShawn McCarney                  callback{std::move(callback)}](
21848fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
21858fb49dd6SShawn McCarney                         invConnections) {
21868fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2187d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2188d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2189d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2190d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
219142cbe538SGunnar Mills 
219242cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
219342cbe538SGunnar Mills                                                        inventoryItems,
219442cbe538SGunnar Mills                                                        callback{std::move(
219542cbe538SGunnar Mills                                                            callback)}]() {
219642cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
219742cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
219842cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
219942cbe538SGunnar Mills                                                          inventoryItems,
220042cbe538SGunnar Mills                                                          std::move(callback));
220142cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
220242cbe538SGunnar Mills                             };
220342cbe538SGunnar Mills 
2204d500549bSAnthony Wilson                             // Find led connections and get the data
2205d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
220642cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2207d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2208d500549bSAnthony Wilson                         };
22098fb49dd6SShawn McCarney 
2210adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2211adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2212adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2213d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
22148fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
22158fb49dd6SShawn McCarney                 };
22168fb49dd6SShawn McCarney 
2217adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
22188fb49dd6SShawn McCarney             getInventoryItemsConnections(
2219adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
22208fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2221adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
22228fb49dd6SShawn McCarney         };
22238fb49dd6SShawn McCarney 
2224adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2225adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2226adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2227adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2228adc4f0dbSShawn McCarney }
2229adc4f0dbSShawn McCarney 
2230adc4f0dbSShawn McCarney /**
2231adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2232adc4f0dbSShawn McCarney  *
2233adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2234adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2235adc4f0dbSShawn McCarney  * array.
2236adc4f0dbSShawn McCarney  *
2237adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2238adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2239adc4f0dbSShawn McCarney  * object.
2240adc4f0dbSShawn McCarney  *
2241adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2242adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2243adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2244adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2245adc4f0dbSShawn McCarney  */
2246adc4f0dbSShawn McCarney static nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2247adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2248adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2249adc4f0dbSShawn McCarney {
2250adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2251adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2252adc4f0dbSShawn McCarney     {
2253adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2254adc4f0dbSShawn McCarney         {
2255adc4f0dbSShawn McCarney             return powerSupply;
2256adc4f0dbSShawn McCarney         }
2257adc4f0dbSShawn McCarney     }
2258adc4f0dbSShawn McCarney 
2259adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2260adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2261adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2262adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2263adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2264adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2265adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2266adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2267adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2268adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2269adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2270d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2271adc4f0dbSShawn McCarney 
227242cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
227342cbe538SGunnar Mills     {
227442cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
227542cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
227642cbe538SGunnar Mills     }
227742cbe538SGunnar Mills 
227842cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2279adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2280adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2281adc4f0dbSShawn McCarney 
2282adc4f0dbSShawn McCarney     return powerSupply;
22838fb49dd6SShawn McCarney }
22848fb49dd6SShawn McCarney 
22858fb49dd6SShawn McCarney /**
2286de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2287de629b6eSShawn McCarney  *
2288de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2289de629b6eSShawn McCarney  *
2290de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2291de629b6eSShawn McCarney  * information has been obtained.
2292de629b6eSShawn McCarney  *
2293adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2294de629b6eSShawn McCarney  *
2295de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2296de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2297de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2298de629b6eSShawn McCarney  *
2299de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2300de629b6eSShawn McCarney  *
2301de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2302de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2303de629b6eSShawn McCarney  *
2304adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2305adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2306adc4f0dbSShawn McCarney  *
2307de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2308adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2309de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2310de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2311de629b6eSShawn McCarney  * implements ObjectManager.
2312adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2313de629b6eSShawn McCarney  */
2314de629b6eSShawn McCarney void getSensorData(
2315de629b6eSShawn McCarney     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
231649c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2317de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
23188fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2319adc4f0dbSShawn McCarney         objectMgrPaths,
2320adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2321de629b6eSShawn McCarney {
2322de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2323de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2324de629b6eSShawn McCarney     for (const std::string& connection : connections)
2325de629b6eSShawn McCarney     {
2326de629b6eSShawn McCarney         // Response handler to process managed objects
23278fb49dd6SShawn McCarney         auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames,
2328adc4f0dbSShawn McCarney                                     inventoryItems](
2329de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2330de629b6eSShawn McCarney                                        ManagedObjectsVectorType& resp) {
2331de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2332de629b6eSShawn McCarney             if (ec)
2333de629b6eSShawn McCarney             {
2334de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
2335de629b6eSShawn McCarney                 messages::internalError(SensorsAsyncResp->res);
2336de629b6eSShawn McCarney                 return;
2337de629b6eSShawn McCarney             }
2338de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2339de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2340de629b6eSShawn McCarney             {
2341de629b6eSShawn McCarney                 const std::string& objPath =
2342de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2343de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2344de629b6eSShawn McCarney                                  << objPath;
2345de629b6eSShawn McCarney 
2346de629b6eSShawn McCarney                 std::vector<std::string> split;
2347de629b6eSShawn McCarney                 // Reserve space for
2348de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2349de629b6eSShawn McCarney                 split.reserve(6);
2350de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2351de629b6eSShawn McCarney                 if (split.size() < 6)
2352de629b6eSShawn McCarney                 {
2353de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2354de629b6eSShawn McCarney                                      << objPath;
2355de629b6eSShawn McCarney                     continue;
2356de629b6eSShawn McCarney                 }
2357de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2358de629b6eSShawn McCarney                 // string at the beginning
2359de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2360de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2361de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2362de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
236349c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2364de629b6eSShawn McCarney                 {
2365de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2366de629b6eSShawn McCarney                     continue;
2367de629b6eSShawn McCarney                 }
2368de629b6eSShawn McCarney 
2369adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2370adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2371adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2372adc4f0dbSShawn McCarney 
237395a3ecadSAnthony Wilson                 const std::string& sensorSchema =
237495a3ecadSAnthony Wilson                     SensorsAsyncResp->chassisSubNode;
237595a3ecadSAnthony Wilson 
237695a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
237795a3ecadSAnthony Wilson 
237895a3ecadSAnthony Wilson                 if (sensorSchema == "Sensors")
237995a3ecadSAnthony Wilson                 {
238095a3ecadSAnthony Wilson                     SensorsAsyncResp->res.jsonValue["@odata.id"] =
238195a3ecadSAnthony Wilson                         "/redfish/v1/Chassis/" + SensorsAsyncResp->chassisId +
238295a3ecadSAnthony Wilson                         "/" + SensorsAsyncResp->chassisSubNode + "/" +
238395a3ecadSAnthony Wilson                         sensorName;
238495a3ecadSAnthony Wilson                     sensorJson = &(SensorsAsyncResp->res.jsonValue);
238595a3ecadSAnthony Wilson                 }
238695a3ecadSAnthony Wilson                 else
238795a3ecadSAnthony Wilson                 {
2388271584abSEd Tanous                     std::string fieldName;
2389de629b6eSShawn McCarney                     if (sensorType == "temperature")
2390de629b6eSShawn McCarney                     {
2391de629b6eSShawn McCarney                         fieldName = "Temperatures";
2392de629b6eSShawn McCarney                     }
2393de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2394de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2395de629b6eSShawn McCarney                     {
2396de629b6eSShawn McCarney                         fieldName = "Fans";
2397de629b6eSShawn McCarney                     }
2398de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2399de629b6eSShawn McCarney                     {
2400de629b6eSShawn McCarney                         fieldName = "Voltages";
2401de629b6eSShawn McCarney                     }
2402de629b6eSShawn McCarney                     else if (sensorType == "power")
2403de629b6eSShawn McCarney                     {
2404028f7ebcSEddie James                         if (!sensorName.compare("total_power"))
2405028f7ebcSEddie James                         {
2406028f7ebcSEddie James                             fieldName = "PowerControl";
2407028f7ebcSEddie James                         }
2408adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2409adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2410028f7ebcSEddie James                         {
2411de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2412de629b6eSShawn McCarney                         }
2413adc4f0dbSShawn McCarney                         else
2414adc4f0dbSShawn McCarney                         {
2415adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2416adc4f0dbSShawn McCarney                             continue;
2417adc4f0dbSShawn McCarney                         }
2418028f7ebcSEddie James                     }
2419de629b6eSShawn McCarney                     else
2420de629b6eSShawn McCarney                     {
2421de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2422de629b6eSShawn McCarney                                          << sensorType;
2423de629b6eSShawn McCarney                         continue;
2424de629b6eSShawn McCarney                     }
2425de629b6eSShawn McCarney 
2426de629b6eSShawn McCarney                     nlohmann::json& tempArray =
2427de629b6eSShawn McCarney                         SensorsAsyncResp->res.jsonValue[fieldName];
2428adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
242949c53ac9SJohnathan Mantey                     {
2430adc4f0dbSShawn McCarney                         if (tempArray.empty())
24317ab06f49SGunnar Mills                         {
243295a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
243395a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
243495a3ecadSAnthony Wilson                             // naming in power.hpp.
24357ab06f49SGunnar Mills                             tempArray.push_back(
2436adc4f0dbSShawn McCarney                                 {{"@odata.id",
2437adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
24387ab06f49SGunnar Mills                                       SensorsAsyncResp->chassisId + "/" +
2439adc4f0dbSShawn McCarney                                       SensorsAsyncResp->chassisSubNode + "#/" +
2440adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2441adc4f0dbSShawn McCarney                         }
2442adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2443adc4f0dbSShawn McCarney                     }
2444adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2445adc4f0dbSShawn McCarney                     {
2446adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2447adc4f0dbSShawn McCarney                         {
2448adc4f0dbSShawn McCarney                             sensorJson =
2449adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
2450adc4f0dbSShawn McCarney                                                  SensorsAsyncResp->chassisId));
2451adc4f0dbSShawn McCarney                         }
245249c53ac9SJohnathan Mantey                     }
245349c53ac9SJohnathan Mantey                     else
245449c53ac9SJohnathan Mantey                     {
2455de629b6eSShawn McCarney                         tempArray.push_back(
245695a3ecadSAnthony Wilson                             {{"@odata.id",
245795a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
245849c53ac9SJohnathan Mantey                                   SensorsAsyncResp->chassisId + "/" +
245995a3ecadSAnthony Wilson                                   SensorsAsyncResp->chassisSubNode + "#/" +
246095a3ecadSAnthony Wilson                                   fieldName + "/"}});
2461adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
246249c53ac9SJohnathan Mantey                     }
246395a3ecadSAnthony Wilson                 }
2464de629b6eSShawn McCarney 
2465adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2466adc4f0dbSShawn McCarney                 {
2467de629b6eSShawn McCarney                     objectInterfacesToJson(sensorName, sensorType,
246895a3ecadSAnthony Wilson                                            SensorsAsyncResp->chassisSubNode,
2469adc4f0dbSShawn McCarney                                            objDictEntry.second, *sensorJson,
2470adc4f0dbSShawn McCarney                                            inventoryItem);
2471adc4f0dbSShawn McCarney                 }
2472de629b6eSShawn McCarney             }
247349c53ac9SJohnathan Mantey             if (SensorsAsyncResp.use_count() == 1)
247449c53ac9SJohnathan Mantey             {
247549c53ac9SJohnathan Mantey                 sortJSONResponse(SensorsAsyncResp);
247649c53ac9SJohnathan Mantey                 if (SensorsAsyncResp->chassisSubNode == "Thermal")
24778bd25ccdSJames Feist                 {
24788bd25ccdSJames Feist                     populateFanRedundancy(SensorsAsyncResp);
24798bd25ccdSJames Feist                 }
248049c53ac9SJohnathan Mantey             }
2481de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2482de629b6eSShawn McCarney         };
2483de629b6eSShawn McCarney 
2484de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2485de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
24868fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2487de629b6eSShawn McCarney         const std::string& objectMgrPath =
24888fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2489de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2490de629b6eSShawn McCarney                          << objectMgrPath;
2491de629b6eSShawn McCarney 
2492de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2493de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2494de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
2495de629b6eSShawn McCarney     };
2496de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2497de629b6eSShawn McCarney }
2498de629b6eSShawn McCarney 
249995a3ecadSAnthony Wilson void processSensorList(
250095a3ecadSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
250195a3ecadSAnthony Wilson     std::shared_ptr<boost::container::flat_set<std::string>> sensorNames)
25021abe55efSEd Tanous {
250395a3ecadSAnthony Wilson     auto getConnectionCb =
250495a3ecadSAnthony Wilson         [SensorsAsyncResp, sensorNames](
250595a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
250655c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2507de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
250849c53ac9SJohnathan Mantey                 [SensorsAsyncResp, sensorNames, connections](
250995a3ecadSAnthony Wilson                     std::shared_ptr<
251095a3ecadSAnthony Wilson                         boost::container::flat_map<std::string, std::string>>
25118fb49dd6SShawn McCarney                         objectMgrPaths) {
2512de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2513adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
2514adc4f0dbSShawn McCarney                         [SensorsAsyncResp, sensorNames, connections,
2515adc4f0dbSShawn McCarney                          objectMgrPaths](
2516adc4f0dbSShawn McCarney                             std::shared_ptr<std::vector<InventoryItem>>
2517adc4f0dbSShawn McCarney                                 inventoryItems) {
2518adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
251949c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
2520de629b6eSShawn McCarney                             getSensorData(SensorsAsyncResp, sensorNames,
2521adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2522adc4f0dbSShawn McCarney                                           inventoryItems);
2523adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2524adc4f0dbSShawn McCarney                         };
2525adc4f0dbSShawn McCarney 
2526adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
2527adc4f0dbSShawn McCarney                     getInventoryItems(SensorsAsyncResp, sensorNames,
2528adc4f0dbSShawn McCarney                                       objectMgrPaths,
2529adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2530adc4f0dbSShawn McCarney 
2531de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
253208777fb0SLewanczyk, Dawid                 };
2533de629b6eSShawn McCarney 
253449c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
253549c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
2536de629b6eSShawn McCarney             getObjectManagerPaths(SensorsAsyncResp,
2537de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
253855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
253908777fb0SLewanczyk, Dawid         };
2540de629b6eSShawn McCarney 
2541de629b6eSShawn McCarney     // Get set of connections that provide sensor values
254295a3ecadSAnthony Wilson     getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb));
254395a3ecadSAnthony Wilson }
254495a3ecadSAnthony Wilson 
254595a3ecadSAnthony Wilson /**
254695a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
254795a3ecadSAnthony Wilson  *        chassis.
254895a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
254995a3ecadSAnthony Wilson  */
255095a3ecadSAnthony Wilson void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
255195a3ecadSAnthony Wilson {
255295a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
255395a3ecadSAnthony Wilson     auto getChassisCb =
255495a3ecadSAnthony Wilson         [SensorsAsyncResp](
255595a3ecadSAnthony Wilson             std::shared_ptr<boost::container::flat_set<std::string>>
255695a3ecadSAnthony Wilson                 sensorNames) {
255795a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
255895a3ecadSAnthony Wilson             processSensorList(SensorsAsyncResp, sensorNames);
255955c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
256008777fb0SLewanczyk, Dawid         };
25614f9a2130SJennifer Lee     SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
256208777fb0SLewanczyk, Dawid 
256326f03899SShawn McCarney     // Get set of sensors in chassis
2564588c3f0dSKowalski, Kamil     getChassis(SensorsAsyncResp, std::move(getChassisCb));
256555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2566271584abSEd Tanous }
256708777fb0SLewanczyk, Dawid 
2568413961deSRichard Marian Thomaiyar /**
256949c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
257049c53ac9SJohnathan Mantey  * the chassis node
257149c53ac9SJohnathan Mantey  *
257249c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
257349c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
257449c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
257549c53ac9SJohnathan Mantey  *                         repeated calls to this function
257649c53ac9SJohnathan Mantey  */
257749c53ac9SJohnathan Mantey bool findSensorNameUsingSensorPath(
25780a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
257949c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
258049c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
258149c53ac9SJohnathan Mantey {
25820a86febdSRichard Marian Thomaiyar     for (std::string_view chassisSensor : sensorsList)
258349c53ac9SJohnathan Mantey     {
25840a86febdSRichard Marian Thomaiyar         std::size_t pos = chassisSensor.rfind("/");
25850a86febdSRichard Marian Thomaiyar         if (pos >= (chassisSensor.size() - 1))
258649c53ac9SJohnathan Mantey         {
258749c53ac9SJohnathan Mantey             continue;
258849c53ac9SJohnathan Mantey         }
25890a86febdSRichard Marian Thomaiyar         std::string_view thisSensorName = chassisSensor.substr(pos + 1);
259049c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
259149c53ac9SJohnathan Mantey         {
259249c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
259349c53ac9SJohnathan Mantey             return true;
259449c53ac9SJohnathan Mantey         }
259549c53ac9SJohnathan Mantey     }
259649c53ac9SJohnathan Mantey     return false;
259749c53ac9SJohnathan Mantey }
259849c53ac9SJohnathan Mantey 
259949c53ac9SJohnathan Mantey /**
2600413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2601413961deSRichard Marian Thomaiyar  *
2602413961deSRichard Marian Thomaiyar  * @param res   response object
26034bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2604413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2605413961deSRichard Marian Thomaiyar  */
260670d1d0aaSjayaprakash Mutyala void setSensorsOverride(
26074bb3dc34SCarol Wang     std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
26084bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2609397fd61fSjayaprakash Mutyala         allCollections)
2610413961deSRichard Marian Thomaiyar {
261170d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
26124bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2613413961deSRichard Marian Thomaiyar 
2614f65af9e8SRichard Marian Thomaiyar     const char* propertyValueName;
2615f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2616413961deSRichard Marian Thomaiyar     std::string memberId;
2617413961deSRichard Marian Thomaiyar     double value;
2618f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2619f65af9e8SRichard Marian Thomaiyar     {
2620f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2621f65af9e8SRichard Marian Thomaiyar         {
2622f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2623f65af9e8SRichard Marian Thomaiyar         }
2624f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2625f65af9e8SRichard Marian Thomaiyar         {
2626f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2627f65af9e8SRichard Marian Thomaiyar         }
2628f65af9e8SRichard Marian Thomaiyar         else
2629f65af9e8SRichard Marian Thomaiyar         {
2630f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2631f65af9e8SRichard Marian Thomaiyar         }
2632f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2633f65af9e8SRichard Marian Thomaiyar         {
26344bb3dc34SCarol Wang             if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId",
26354bb3dc34SCarol Wang                                      memberId, propertyValueName, value))
2636413961deSRichard Marian Thomaiyar             {
2637413961deSRichard Marian Thomaiyar                 return;
2638413961deSRichard Marian Thomaiyar             }
2639f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2640f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2641f65af9e8SRichard Marian Thomaiyar         }
2642f65af9e8SRichard Marian Thomaiyar     }
26434bb3dc34SCarol Wang 
264449c53ac9SJohnathan Mantey     auto getChassisSensorListCb = [sensorAsyncResp,
264549c53ac9SJohnathan Mantey                                    overrideMap](const std::shared_ptr<
264649c53ac9SJohnathan Mantey                                                 boost::container::flat_set<
264749c53ac9SJohnathan Mantey                                                     std::string>>
264849c53ac9SJohnathan Mantey                                                     sensorsList) {
264949c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
265049c53ac9SJohnathan Mantey         // chassis node
265149c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
265249c53ac9SJohnathan Mantey             sensorNames =
265349c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2654f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2655413961deSRichard Marian Thomaiyar         {
2656f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
265749c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
265849c53ac9SJohnathan Mantey                                                *sensorNames))
2659f65af9e8SRichard Marian Thomaiyar             {
2660f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
2661413961deSRichard Marian Thomaiyar                 messages::resourceNotFound(sensorAsyncResp->res,
2662f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2663413961deSRichard Marian Thomaiyar                 return;
2664413961deSRichard Marian Thomaiyar             }
2665f65af9e8SRichard Marian Thomaiyar         }
2666413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2667413961deSRichard Marian Thomaiyar         auto getObjectsWithConnectionCb =
2668f65af9e8SRichard Marian Thomaiyar             [sensorAsyncResp, overrideMap](
2669413961deSRichard Marian Thomaiyar                 const boost::container::flat_set<std::string>& connections,
2670413961deSRichard Marian Thomaiyar                 const std::set<std::pair<std::string, std::string>>&
2671413961deSRichard Marian Thomaiyar                     objectsWithConnection) {
2672f65af9e8SRichard Marian Thomaiyar                 if (objectsWithConnection.size() != overrideMap.size())
2673413961deSRichard Marian Thomaiyar                 {
2674413961deSRichard Marian Thomaiyar                     BMCWEB_LOG_INFO
2675f65af9e8SRichard Marian Thomaiyar                         << "Unable to find all objects with proper connection "
2676f65af9e8SRichard Marian Thomaiyar                         << objectsWithConnection.size() << " requested "
2677f65af9e8SRichard Marian Thomaiyar                         << overrideMap.size() << "\n";
2678413961deSRichard Marian Thomaiyar                     messages::resourceNotFound(
2679413961deSRichard Marian Thomaiyar                         sensorAsyncResp->res,
2680413961deSRichard Marian Thomaiyar                         sensorAsyncResp->chassisSubNode == "Thermal"
2681413961deSRichard Marian Thomaiyar                             ? "Temperatures"
2682413961deSRichard Marian Thomaiyar                             : "Voltages",
2683f65af9e8SRichard Marian Thomaiyar                         "Count");
2684f65af9e8SRichard Marian Thomaiyar                     return;
2685f65af9e8SRichard Marian Thomaiyar                 }
2686f65af9e8SRichard Marian Thomaiyar                 for (const auto& item : objectsWithConnection)
2687f65af9e8SRichard Marian Thomaiyar                 {
2688f65af9e8SRichard Marian Thomaiyar 
2689f65af9e8SRichard Marian Thomaiyar                     auto lastPos = item.first.rfind('/');
2690f65af9e8SRichard Marian Thomaiyar                     if (lastPos == std::string::npos)
2691f65af9e8SRichard Marian Thomaiyar                     {
2692f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2693f65af9e8SRichard Marian Thomaiyar                         return;
2694f65af9e8SRichard Marian Thomaiyar                     }
2695f65af9e8SRichard Marian Thomaiyar                     std::string sensorName = item.first.substr(lastPos + 1);
2696f65af9e8SRichard Marian Thomaiyar 
2697f65af9e8SRichard Marian Thomaiyar                     const auto& iterator = overrideMap.find(sensorName);
2698f65af9e8SRichard Marian Thomaiyar                     if (iterator == overrideMap.end())
2699f65af9e8SRichard Marian Thomaiyar                     {
2700f65af9e8SRichard Marian Thomaiyar                         BMCWEB_LOG_INFO << "Unable to find sensor object"
2701f65af9e8SRichard Marian Thomaiyar                                         << item.first << "\n";
2702f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2703413961deSRichard Marian Thomaiyar                         return;
2704413961deSRichard Marian Thomaiyar                     }
2705413961deSRichard Marian Thomaiyar                     crow::connections::systemBus->async_method_call(
2706f65af9e8SRichard Marian Thomaiyar                         [sensorAsyncResp](const boost::system::error_code ec) {
2707413961deSRichard Marian Thomaiyar                             if (ec)
2708413961deSRichard Marian Thomaiyar                             {
2709413961deSRichard Marian Thomaiyar                                 BMCWEB_LOG_DEBUG
2710f65af9e8SRichard Marian Thomaiyar                                     << "setOverrideValueStatus DBUS error: "
2711413961deSRichard Marian Thomaiyar                                     << ec;
2712413961deSRichard Marian Thomaiyar                                 messages::internalError(sensorAsyncResp->res);
2713413961deSRichard Marian Thomaiyar                                 return;
2714413961deSRichard Marian Thomaiyar                             }
2715413961deSRichard Marian Thomaiyar                         },
2716f65af9e8SRichard Marian Thomaiyar                         item.second, item.first,
2717413961deSRichard Marian Thomaiyar                         "org.freedesktop.DBus.Properties", "Set",
2718413961deSRichard Marian Thomaiyar                         "xyz.openbmc_project.Sensor.Value", "Value",
271919bd78d9SPatrick Williams                         std::variant<double>(iterator->second.first));
2720f65af9e8SRichard Marian Thomaiyar                 }
2721413961deSRichard Marian Thomaiyar             };
2722413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2723413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2724413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2725413961deSRichard Marian Thomaiyar     };
2726413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2727413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2728413961deSRichard Marian Thomaiyar }
2729413961deSRichard Marian Thomaiyar 
273070d1d0aaSjayaprakash Mutyala bool isOverridingAllowed(const std::string& manufacturingModeStatus)
273170d1d0aaSjayaprakash Mutyala {
273270d1d0aaSjayaprakash Mutyala     if (manufacturingModeStatus ==
273370d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.Control.Security.SpecialMode.Modes.Manufacturing")
273470d1d0aaSjayaprakash Mutyala     {
273570d1d0aaSjayaprakash Mutyala         return true;
273670d1d0aaSjayaprakash Mutyala     }
273770d1d0aaSjayaprakash Mutyala 
273870d1d0aaSjayaprakash Mutyala #ifdef BMCWEB_ENABLE_VALIDATION_UNSECURE_FEATURE
273970d1d0aaSjayaprakash Mutyala     if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
274070d1d0aaSjayaprakash Mutyala                                    "SpecialMode.Modes.ValidationUnsecure")
274170d1d0aaSjayaprakash Mutyala     {
274270d1d0aaSjayaprakash Mutyala         return true;
274370d1d0aaSjayaprakash Mutyala     }
274470d1d0aaSjayaprakash Mutyala 
274570d1d0aaSjayaprakash Mutyala #endif
274670d1d0aaSjayaprakash Mutyala 
274770d1d0aaSjayaprakash Mutyala     return false;
274870d1d0aaSjayaprakash Mutyala }
274970d1d0aaSjayaprakash Mutyala 
275070d1d0aaSjayaprakash Mutyala /**
275170d1d0aaSjayaprakash Mutyala  * @brief Entry point for Checking the manufacturing mode before doing sensor
275270d1d0aaSjayaprakash Mutyala  * override values of given sensor
275370d1d0aaSjayaprakash Mutyala  *
275470d1d0aaSjayaprakash Mutyala  * @param res   response object
275570d1d0aaSjayaprakash Mutyala  * @param allCollections   Collections extract from sensors' request patch info
275670d1d0aaSjayaprakash Mutyala  * @param chassisSubNode   Chassis Node for which the query has to happen
275770d1d0aaSjayaprakash Mutyala  */
275870d1d0aaSjayaprakash Mutyala void checkAndDoSensorsOverride(
275970d1d0aaSjayaprakash Mutyala     std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
2760397fd61fSjayaprakash Mutyala     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2761397fd61fSjayaprakash Mutyala         allCollections)
276270d1d0aaSjayaprakash Mutyala {
276370d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "checkAndDoSensorsOverride for subnode"
276470d1d0aaSjayaprakash Mutyala                     << sensorAsyncResp->chassisSubNode << "\n";
276570d1d0aaSjayaprakash Mutyala 
276670d1d0aaSjayaprakash Mutyala     const std::array<std::string, 1> interfaces = {
276770d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.Security.SpecialMode"};
276870d1d0aaSjayaprakash Mutyala 
276970d1d0aaSjayaprakash Mutyala     crow::connections::systemBus->async_method_call(
2770397fd61fSjayaprakash Mutyala         [sensorAsyncResp, allCollections](const boost::system::error_code ec,
277170d1d0aaSjayaprakash Mutyala                                           const GetSubTreeType& resp) mutable {
277270d1d0aaSjayaprakash Mutyala             if (ec)
277370d1d0aaSjayaprakash Mutyala             {
277470d1d0aaSjayaprakash Mutyala                 BMCWEB_LOG_DEBUG
277570d1d0aaSjayaprakash Mutyala                     << "Error in querying GetSubTree with Object Mapper. "
277670d1d0aaSjayaprakash Mutyala                     << ec;
277770d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
277870d1d0aaSjayaprakash Mutyala                 return;
277970d1d0aaSjayaprakash Mutyala             }
278091e130a3Sjayaprakash Mutyala #ifdef BMCWEB_INSECURE_UNRESTRICTED_SENSOR_OVERRIDE
278191e130a3Sjayaprakash Mutyala             // Proceed with sensor override
2782397fd61fSjayaprakash Mutyala             setSensorsOverride(sensorAsyncResp, allCollections);
278370d1d0aaSjayaprakash Mutyala             return;
278491e130a3Sjayaprakash Mutyala #endif
278570d1d0aaSjayaprakash Mutyala 
278670d1d0aaSjayaprakash Mutyala             if (resp.size() != 1)
278770d1d0aaSjayaprakash Mutyala             {
278891e130a3Sjayaprakash Mutyala                 BMCWEB_LOG_WARNING
278991e130a3Sjayaprakash Mutyala                     << "Overriding sensor value is not allowed - Internal "
279091e130a3Sjayaprakash Mutyala                        "error in querying SpecialMode property.";
279170d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
279270d1d0aaSjayaprakash Mutyala                 return;
279370d1d0aaSjayaprakash Mutyala             }
279470d1d0aaSjayaprakash Mutyala             const std::string& path = resp[0].first;
279570d1d0aaSjayaprakash Mutyala             const std::string& serviceName = resp[0].second.begin()->first;
279670d1d0aaSjayaprakash Mutyala 
279770d1d0aaSjayaprakash Mutyala             if (path.empty() || serviceName.empty())
279870d1d0aaSjayaprakash Mutyala             {
279970d1d0aaSjayaprakash Mutyala                 BMCWEB_LOG_DEBUG
280070d1d0aaSjayaprakash Mutyala                     << "Path or service name is returned as empty. ";
280170d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
280270d1d0aaSjayaprakash Mutyala                 return;
280370d1d0aaSjayaprakash Mutyala             }
280470d1d0aaSjayaprakash Mutyala 
280570d1d0aaSjayaprakash Mutyala             // Sensor override is allowed only in manufacturing mode or
280670d1d0aaSjayaprakash Mutyala             // validation unsecure mode .
280770d1d0aaSjayaprakash Mutyala             crow::connections::systemBus->async_method_call(
2808397fd61fSjayaprakash Mutyala                 [sensorAsyncResp, allCollections,
280970d1d0aaSjayaprakash Mutyala                  path](const boost::system::error_code ec,
281070d1d0aaSjayaprakash Mutyala                        std::variant<std::string>& getManufactMode) mutable {
281170d1d0aaSjayaprakash Mutyala                     if (ec)
281270d1d0aaSjayaprakash Mutyala                     {
281370d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_DEBUG
281470d1d0aaSjayaprakash Mutyala                             << "Error in querying Special mode property " << ec;
281570d1d0aaSjayaprakash Mutyala                         messages::internalError(sensorAsyncResp->res);
281670d1d0aaSjayaprakash Mutyala                         return;
281770d1d0aaSjayaprakash Mutyala                     }
281870d1d0aaSjayaprakash Mutyala 
281970d1d0aaSjayaprakash Mutyala                     const std::string* manufacturingModeStatus =
282070d1d0aaSjayaprakash Mutyala                         std::get_if<std::string>(&getManufactMode);
282170d1d0aaSjayaprakash Mutyala 
282270d1d0aaSjayaprakash Mutyala                     if (nullptr == manufacturingModeStatus)
282370d1d0aaSjayaprakash Mutyala                     {
282470d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_DEBUG << "Sensor override mode is not "
282570d1d0aaSjayaprakash Mutyala                                             "Enabled. Returning ... ";
282670d1d0aaSjayaprakash Mutyala                         messages::internalError(sensorAsyncResp->res);
282770d1d0aaSjayaprakash Mutyala                         return;
282870d1d0aaSjayaprakash Mutyala                     }
282970d1d0aaSjayaprakash Mutyala 
283070d1d0aaSjayaprakash Mutyala                     if (isOverridingAllowed(*manufacturingModeStatus))
283170d1d0aaSjayaprakash Mutyala                     {
283270d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_INFO << "Manufacturing mode is Enabled. "
283370d1d0aaSjayaprakash Mutyala                                            "Proceeding further... ";
2834397fd61fSjayaprakash Mutyala                         setSensorsOverride(sensorAsyncResp, allCollections);
283570d1d0aaSjayaprakash Mutyala                     }
283670d1d0aaSjayaprakash Mutyala                     else
283770d1d0aaSjayaprakash Mutyala                     {
283870d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_WARNING
283970d1d0aaSjayaprakash Mutyala                             << "Manufacturing mode is not Enabled...can't "
284070d1d0aaSjayaprakash Mutyala                                "Override the sensor value. ";
284170d1d0aaSjayaprakash Mutyala 
284270d1d0aaSjayaprakash Mutyala                         messages::actionNotSupported(
284370d1d0aaSjayaprakash Mutyala                             sensorAsyncResp->res,
284470d1d0aaSjayaprakash Mutyala                             "Overriding of Sensor Value for non "
284570d1d0aaSjayaprakash Mutyala                             "manufacturing mode");
284670d1d0aaSjayaprakash Mutyala                         return;
284770d1d0aaSjayaprakash Mutyala                     }
284870d1d0aaSjayaprakash Mutyala                 },
284970d1d0aaSjayaprakash Mutyala                 serviceName, path, "org.freedesktop.DBus.Properties", "Get",
285070d1d0aaSjayaprakash Mutyala                 "xyz.openbmc_project.Security.SpecialMode", "SpecialMode");
285170d1d0aaSjayaprakash Mutyala         },
285270d1d0aaSjayaprakash Mutyala 
285370d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.ObjectMapper",
285470d1d0aaSjayaprakash Mutyala         "/xyz/openbmc_project/object_mapper",
285570d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 5, interfaces);
285670d1d0aaSjayaprakash Mutyala }
285770d1d0aaSjayaprakash Mutyala 
285895a3ecadSAnthony Wilson class SensorCollection : public Node
285995a3ecadSAnthony Wilson {
286095a3ecadSAnthony Wilson   public:
286195a3ecadSAnthony Wilson     SensorCollection(CrowApp& app) :
2862f99c379dSGunnar Mills         Node(app, "/redfish/v1/Chassis/<str>/Sensors/", std::string())
286395a3ecadSAnthony Wilson     {
286495a3ecadSAnthony Wilson         entityPrivileges = {
286595a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
286695a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
286795a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
286895a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
286995a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
287095a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
287195a3ecadSAnthony Wilson     }
287295a3ecadSAnthony Wilson 
287395a3ecadSAnthony Wilson   private:
287495a3ecadSAnthony Wilson     std::vector<const char*> typeList = {
287595a3ecadSAnthony Wilson         "/xyz/openbmc_project/sensors/power",
2876*f8ede15eSAdrian Ambrożewicz         "/xyz/openbmc_project/sensors/current",
2877*f8ede15eSAdrian Ambrożewicz         "/xyz/openbmc_project/sensors/utilization"};
287895a3ecadSAnthony Wilson     void doGet(crow::Response& res, const crow::Request& req,
287995a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
288095a3ecadSAnthony Wilson     {
288195a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
288295a3ecadSAnthony Wilson         if (params.size() != 1)
288395a3ecadSAnthony Wilson         {
288495a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1";
288595a3ecadSAnthony Wilson             messages::internalError(res);
288695a3ecadSAnthony Wilson             res.end();
288795a3ecadSAnthony Wilson             return;
288895a3ecadSAnthony Wilson         }
288995a3ecadSAnthony Wilson 
289095a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
289195a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
289295a3ecadSAnthony Wilson             std::make_shared<SensorsAsyncResp>(res, chassisId, typeList,
289395a3ecadSAnthony Wilson                                                "Sensors");
289495a3ecadSAnthony Wilson 
289595a3ecadSAnthony Wilson         auto getChassisCb =
289695a3ecadSAnthony Wilson             [asyncResp](std::shared_ptr<boost::container::flat_set<std::string>>
289795a3ecadSAnthony Wilson                             sensorNames) {
289895a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb enter";
289995a3ecadSAnthony Wilson 
290095a3ecadSAnthony Wilson                 nlohmann::json& entriesArray =
290195a3ecadSAnthony Wilson                     asyncResp->res.jsonValue["Members"];
290295a3ecadSAnthony Wilson                 for (auto& sensor : *sensorNames)
290395a3ecadSAnthony Wilson                 {
290495a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
290595a3ecadSAnthony Wilson 
290695a3ecadSAnthony Wilson                     std::size_t lastPos = sensor.rfind("/");
290795a3ecadSAnthony Wilson                     if (lastPos == std::string::npos ||
290895a3ecadSAnthony Wilson                         lastPos + 1 >= sensor.size())
290995a3ecadSAnthony Wilson                     {
291095a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
291195a3ecadSAnthony Wilson                         messages::internalError(asyncResp->res);
291295a3ecadSAnthony Wilson                         return;
291395a3ecadSAnthony Wilson                     }
291495a3ecadSAnthony Wilson                     std::string sensorName = sensor.substr(lastPos + 1);
291595a3ecadSAnthony Wilson                     entriesArray.push_back(
291695a3ecadSAnthony Wilson                         {{"@odata.id",
291795a3ecadSAnthony Wilson                           "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
291895a3ecadSAnthony Wilson                               asyncResp->chassisSubNode + "/" + sensorName}});
291995a3ecadSAnthony Wilson                 }
292095a3ecadSAnthony Wilson 
292195a3ecadSAnthony Wilson                 asyncResp->res.jsonValue["Members@odata.count"] =
292295a3ecadSAnthony Wilson                     entriesArray.size();
292395a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb exit";
292495a3ecadSAnthony Wilson             };
292595a3ecadSAnthony Wilson 
292695a3ecadSAnthony Wilson         // Get set of sensors in chassis
292795a3ecadSAnthony Wilson         getChassis(asyncResp, std::move(getChassisCb));
292895a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
292995a3ecadSAnthony Wilson     }
293095a3ecadSAnthony Wilson };
293195a3ecadSAnthony Wilson 
293295a3ecadSAnthony Wilson class Sensor : public Node
293395a3ecadSAnthony Wilson {
293495a3ecadSAnthony Wilson   public:
293595a3ecadSAnthony Wilson     Sensor(CrowApp& app) :
293695a3ecadSAnthony Wilson         Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(),
293795a3ecadSAnthony Wilson              std::string())
293895a3ecadSAnthony Wilson     {
293995a3ecadSAnthony Wilson         entityPrivileges = {
294095a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
294195a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
294295a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
294395a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
294495a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
294595a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
294695a3ecadSAnthony Wilson     }
294795a3ecadSAnthony Wilson 
294895a3ecadSAnthony Wilson   private:
294995a3ecadSAnthony Wilson     void doGet(crow::Response& res, const crow::Request& req,
295095a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
295195a3ecadSAnthony Wilson     {
295295a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "Sensor doGet enter";
295395a3ecadSAnthony Wilson         if (params.size() != 2)
295495a3ecadSAnthony Wilson         {
295595a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2";
295695a3ecadSAnthony Wilson             messages::internalError(res);
295795a3ecadSAnthony Wilson             res.end();
295895a3ecadSAnthony Wilson             return;
295995a3ecadSAnthony Wilson         }
296095a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
296195a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
296295a3ecadSAnthony Wilson             std::make_shared<SensorsAsyncResp>(
296395a3ecadSAnthony Wilson                 res, chassisId, std::vector<const char*>(), "Sensors");
296495a3ecadSAnthony Wilson 
296595a3ecadSAnthony Wilson         const std::string& sensorName = params[1];
296695a3ecadSAnthony Wilson         const std::array<const char*, 1> interfaces = {
296795a3ecadSAnthony Wilson             "xyz.openbmc_project.Sensor.Value"};
296895a3ecadSAnthony Wilson 
296995a3ecadSAnthony Wilson         // Get a list of all of the sensors that implement Sensor.Value
297095a3ecadSAnthony Wilson         // and get the path and service name associated with the sensor
297195a3ecadSAnthony Wilson         crow::connections::systemBus->async_method_call(
297295a3ecadSAnthony Wilson             [asyncResp, sensorName](const boost::system::error_code ec,
297395a3ecadSAnthony Wilson                                     const GetSubTreeType& subtree) {
297495a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 enter";
297595a3ecadSAnthony Wilson                 if (ec)
297695a3ecadSAnthony Wilson                 {
297795a3ecadSAnthony Wilson                     messages::internalError(asyncResp->res);
297895a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
297995a3ecadSAnthony Wilson                                      << "Dbus error " << ec;
298095a3ecadSAnthony Wilson                     return;
298195a3ecadSAnthony Wilson                 }
298295a3ecadSAnthony Wilson 
298395a3ecadSAnthony Wilson                 GetSubTreeType::const_iterator it = std::find_if(
298495a3ecadSAnthony Wilson                     subtree.begin(), subtree.end(),
298595a3ecadSAnthony Wilson                     [sensorName](
298695a3ecadSAnthony Wilson                         const std::pair<
298795a3ecadSAnthony Wilson                             std::string,
298895a3ecadSAnthony Wilson                             std::vector<std::pair<std::string,
298995a3ecadSAnthony Wilson                                                   std::vector<std::string>>>>&
299095a3ecadSAnthony Wilson                             object) {
299195a3ecadSAnthony Wilson                         std::string_view sensor = object.first;
299295a3ecadSAnthony Wilson                         std::size_t lastPos = sensor.rfind("/");
299395a3ecadSAnthony Wilson                         if (lastPos == std::string::npos ||
299495a3ecadSAnthony Wilson                             lastPos + 1 >= sensor.size())
299595a3ecadSAnthony Wilson                         {
299695a3ecadSAnthony Wilson                             BMCWEB_LOG_ERROR << "Invalid sensor path: "
299795a3ecadSAnthony Wilson                                              << sensor;
299895a3ecadSAnthony Wilson                             return false;
299995a3ecadSAnthony Wilson                         }
300095a3ecadSAnthony Wilson                         std::string_view name = sensor.substr(lastPos + 1);
300195a3ecadSAnthony Wilson 
300295a3ecadSAnthony Wilson                         return name == sensorName;
300395a3ecadSAnthony Wilson                     });
300495a3ecadSAnthony Wilson 
300595a3ecadSAnthony Wilson                 if (it == subtree.end())
300695a3ecadSAnthony Wilson                 {
300795a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Could not find path for sensor: "
300895a3ecadSAnthony Wilson                                      << sensorName;
300995a3ecadSAnthony Wilson                     messages::resourceNotFound(asyncResp->res, "Sensor",
301095a3ecadSAnthony Wilson                                                sensorName);
301195a3ecadSAnthony Wilson                     return;
301295a3ecadSAnthony Wilson                 }
301395a3ecadSAnthony Wilson                 std::string_view sensorPath = (*it).first;
301495a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
301595a3ecadSAnthony Wilson                                  << sensorName << "': " << sensorPath;
301695a3ecadSAnthony Wilson 
301795a3ecadSAnthony Wilson                 const std::shared_ptr<boost::container::flat_set<std::string>>
301895a3ecadSAnthony Wilson                     sensorList = std::make_shared<
301995a3ecadSAnthony Wilson                         boost::container::flat_set<std::string>>();
302095a3ecadSAnthony Wilson 
302195a3ecadSAnthony Wilson                 sensorList->emplace(sensorPath);
302295a3ecadSAnthony Wilson                 processSensorList(asyncResp, sensorList);
302395a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 exit";
302495a3ecadSAnthony Wilson             },
302595a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper",
302695a3ecadSAnthony Wilson             "/xyz/openbmc_project/object_mapper",
302795a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
302895a3ecadSAnthony Wilson             "/xyz/openbmc_project/sensors", 2, interfaces);
302995a3ecadSAnthony Wilson     }
303095a3ecadSAnthony Wilson };
303195a3ecadSAnthony Wilson 
303208777fb0SLewanczyk, Dawid } // namespace redfish
3033