xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 42cbe53889b5f2d358d1174245df51a23efcb3f8)
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),
96*42cbe538SGunnar Mills         isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
97*42cbe538SGunnar 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;
113*42cbe538SGunnar 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["@odata.context"] =
38895a3ecadSAnthony Wilson                 "/redfish/v1/$metadata#SensorCollection.SensorCollection";
38995a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Description"] =
39095a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
39195a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members"] =
39295a3ecadSAnthony Wilson                 nlohmann::json::array();
39395a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0;
39495a3ecadSAnthony Wilson         }
39595a3ecadSAnthony Wilson 
39695a3ecadSAnthony Wilson         if (chassisSubNode != "Sensors")
39795a3ecadSAnthony Wilson         {
39895a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode;
39995a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["@odata.context"] =
40095a3ecadSAnthony Wilson                 "/redfish/v1/$metadata#" + chassisSubNode + "." +
40195a3ecadSAnthony Wilson                 chassisSubNode;
40295a3ecadSAnthony Wilson         }
40395a3ecadSAnthony Wilson 
40449c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["@odata.id"] =
40549c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
40649c53ac9SJohnathan Mantey             chassisSubNode;
40749c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode;
40849c53ac9SJohnathan Mantey 
4098fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
4108fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
41155c7b7a2SEd Tanous         crow::connections::systemBus->async_method_call(
41249c53ac9SJohnathan Mantey             [sensorsAsyncResp, callback{std::move(callback)}](
413271584abSEd Tanous                 const boost::system::error_code& e,
41449c53ac9SJohnathan Mantey                 const std::variant<std::vector<std::string>>&
41549c53ac9SJohnathan Mantey                     variantEndpoints) {
416271584abSEd Tanous                 if (e)
41749c53ac9SJohnathan Mantey                 {
418271584abSEd Tanous                     if (e.value() != EBADR)
41949c53ac9SJohnathan Mantey                     {
42049c53ac9SJohnathan Mantey                         messages::internalError(sensorsAsyncResp->res);
42149c53ac9SJohnathan Mantey                         return;
42249c53ac9SJohnathan Mantey                     }
42349c53ac9SJohnathan Mantey                 }
42449c53ac9SJohnathan Mantey                 const std::vector<std::string>* nodeSensorList =
42549c53ac9SJohnathan Mantey                     std::get_if<std::vector<std::string>>(&(variantEndpoints));
42649c53ac9SJohnathan Mantey                 if (nodeSensorList == nullptr)
42749c53ac9SJohnathan Mantey                 {
42849c53ac9SJohnathan Mantey                     messages::resourceNotFound(
42949c53ac9SJohnathan Mantey                         sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
43049c53ac9SJohnathan Mantey                         sensorsAsyncResp->chassisSubNode == "Thermal"
43149c53ac9SJohnathan Mantey                             ? "Temperatures"
43295a3ecadSAnthony Wilson                             : sensorsAsyncResp->chassisSubNode == "Power"
43395a3ecadSAnthony Wilson                                   ? "Voltages"
43495a3ecadSAnthony Wilson                                   : "Sensors");
43549c53ac9SJohnathan Mantey                     return;
43649c53ac9SJohnathan Mantey                 }
43749c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
43849c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
43949c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
44049c53ac9SJohnathan Mantey                 reduceSensorList(sensorsAsyncResp, nodeSensorList,
44149c53ac9SJohnathan Mantey                                  culledSensorList);
44249c53ac9SJohnathan Mantey                 callback(culledSensorList);
44349c53ac9SJohnathan Mantey             },
44449c53ac9SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", sensorPath,
44549c53ac9SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Get",
44649c53ac9SJohnathan Mantey             "xyz.openbmc_project.Association", "endpoints");
44749c53ac9SJohnathan Mantey     };
44849c53ac9SJohnathan Mantey 
44949c53ac9SJohnathan Mantey     // Get the Chassis Collection
45049c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
45149c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
45249c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
45349c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
454271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
45555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
45608777fb0SLewanczyk, Dawid }
45708777fb0SLewanczyk, Dawid 
45808777fb0SLewanczyk, Dawid /**
459de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
460de629b6eSShawn McCarney  *
461de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
462de629b6eSShawn McCarney  *
463de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
464de629b6eSShawn McCarney  * been obtained.
465de629b6eSShawn McCarney  *
466de629b6eSShawn McCarney  * The callback must have the following signature:
467de629b6eSShawn McCarney  *   @code
4688fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
4698fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
470de629b6eSShawn McCarney  *   @endcode
471de629b6eSShawn McCarney  *
47249c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
473de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
474de629b6eSShawn McCarney  */
475de629b6eSShawn McCarney template <typename Callback>
476de629b6eSShawn McCarney void getObjectManagerPaths(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
477de629b6eSShawn McCarney                            Callback&& callback)
478de629b6eSShawn McCarney {
479de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
480de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
481de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
482de629b6eSShawn McCarney 
483de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
484de629b6eSShawn McCarney     auto respHandler = [callback{std::move(callback)},
485de629b6eSShawn McCarney                         SensorsAsyncResp](const boost::system::error_code ec,
486de629b6eSShawn McCarney                                           const GetSubTreeType& subtree) {
487de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
488de629b6eSShawn McCarney         if (ec)
489de629b6eSShawn McCarney         {
490de629b6eSShawn McCarney             messages::internalError(SensorsAsyncResp->res);
491de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
492de629b6eSShawn McCarney                              << ec;
493de629b6eSShawn McCarney             return;
494de629b6eSShawn McCarney         }
495de629b6eSShawn McCarney 
496de629b6eSShawn McCarney         // Loop over returned object paths
4978fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
4988fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
4998fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
500de629b6eSShawn McCarney         for (const std::pair<
501de629b6eSShawn McCarney                  std::string,
502de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
503de629b6eSShawn McCarney                  object : subtree)
504de629b6eSShawn McCarney         {
505de629b6eSShawn McCarney             // Loop over connections for current object path
506de629b6eSShawn McCarney             const std::string& objectPath = object.first;
507de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
508de629b6eSShawn McCarney                      objData : object.second)
509de629b6eSShawn McCarney             {
510de629b6eSShawn McCarney                 // Add mapping from connection to object path
511de629b6eSShawn McCarney                 const std::string& connection = objData.first;
5128fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
513de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
514de629b6eSShawn McCarney                                  << objectPath;
515de629b6eSShawn McCarney             }
516de629b6eSShawn McCarney         }
5178fb49dd6SShawn McCarney         callback(objectMgrPaths);
518de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
519de629b6eSShawn McCarney     };
520de629b6eSShawn McCarney 
521de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
522de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
523de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
524de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
525271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
526de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
527de629b6eSShawn McCarney }
528de629b6eSShawn McCarney 
529de629b6eSShawn McCarney /**
530adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
531adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
532adc4f0dbSShawn McCarney  * @return State value for inventory item.
53334dd179eSJames Feist  */
534adc4f0dbSShawn McCarney static std::string getState(const InventoryItem* inventoryItem)
535adc4f0dbSShawn McCarney {
536adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
537adc4f0dbSShawn McCarney     {
538adc4f0dbSShawn McCarney         return "Absent";
539adc4f0dbSShawn McCarney     }
54034dd179eSJames Feist 
541adc4f0dbSShawn McCarney     return "Enabled";
542adc4f0dbSShawn McCarney }
543adc4f0dbSShawn McCarney 
544adc4f0dbSShawn McCarney /**
545adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
546adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
547adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
548adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
549adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
550adc4f0dbSShawn McCarney  * @return Health value for sensor.
551adc4f0dbSShawn McCarney  */
55234dd179eSJames Feist static std::string getHealth(
553adc4f0dbSShawn McCarney     nlohmann::json& sensorJson,
55434dd179eSJames Feist     const boost::container::flat_map<
55534dd179eSJames Feist         std::string, boost::container::flat_map<std::string, SensorVariant>>&
556adc4f0dbSShawn McCarney         interfacesDict,
557adc4f0dbSShawn McCarney     const InventoryItem* inventoryItem)
55834dd179eSJames Feist {
559adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
560adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
561adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
562adc4f0dbSShawn McCarney     std::string currentHealth;
563adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
564adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
565adc4f0dbSShawn McCarney     {
566adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
567adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
568adc4f0dbSShawn McCarney         {
569adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
570adc4f0dbSShawn McCarney             if (health != nullptr)
571adc4f0dbSShawn McCarney             {
572adc4f0dbSShawn McCarney                 currentHealth = *health;
573adc4f0dbSShawn McCarney             }
574adc4f0dbSShawn McCarney         }
575adc4f0dbSShawn McCarney     }
576adc4f0dbSShawn McCarney 
577adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
578adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
579adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
580adc4f0dbSShawn McCarney     {
581adc4f0dbSShawn McCarney         return "Critical";
582adc4f0dbSShawn McCarney     }
583adc4f0dbSShawn McCarney 
584adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
58534dd179eSJames Feist     auto criticalThresholdIt =
58634dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
58734dd179eSJames Feist     if (criticalThresholdIt != interfacesDict.end())
58834dd179eSJames Feist     {
58934dd179eSJames Feist         auto thresholdHighIt =
59034dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmHigh");
59134dd179eSJames Feist         auto thresholdLowIt =
59234dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmLow");
59334dd179eSJames Feist         if (thresholdHighIt != criticalThresholdIt->second.end())
59434dd179eSJames Feist         {
59534dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
59634dd179eSJames Feist             if (asserted == nullptr)
59734dd179eSJames Feist             {
59834dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
59934dd179eSJames Feist             }
60034dd179eSJames Feist             else if (*asserted)
60134dd179eSJames Feist             {
60234dd179eSJames Feist                 return "Critical";
60334dd179eSJames Feist             }
60434dd179eSJames Feist         }
60534dd179eSJames Feist         if (thresholdLowIt != criticalThresholdIt->second.end())
60634dd179eSJames Feist         {
60734dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
60834dd179eSJames Feist             if (asserted == nullptr)
60934dd179eSJames Feist             {
61034dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
61134dd179eSJames Feist             }
61234dd179eSJames Feist             else if (*asserted)
61334dd179eSJames Feist             {
61434dd179eSJames Feist                 return "Critical";
61534dd179eSJames Feist             }
61634dd179eSJames Feist         }
61734dd179eSJames Feist     }
61834dd179eSJames Feist 
619adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
620adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
621adc4f0dbSShawn McCarney     {
622adc4f0dbSShawn McCarney         return "Critical";
623adc4f0dbSShawn McCarney     }
624adc4f0dbSShawn McCarney 
625adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
626adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
627adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
628adc4f0dbSShawn McCarney     {
629adc4f0dbSShawn McCarney         return "Warning";
630adc4f0dbSShawn McCarney     }
631adc4f0dbSShawn McCarney 
632adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
63334dd179eSJames Feist     auto warningThresholdIt =
63434dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
63534dd179eSJames Feist     if (warningThresholdIt != interfacesDict.end())
63634dd179eSJames Feist     {
63734dd179eSJames Feist         auto thresholdHighIt =
63834dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmHigh");
63934dd179eSJames Feist         auto thresholdLowIt =
64034dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmLow");
64134dd179eSJames Feist         if (thresholdHighIt != warningThresholdIt->second.end())
64234dd179eSJames Feist         {
64334dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
64434dd179eSJames Feist             if (asserted == nullptr)
64534dd179eSJames Feist             {
64634dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
64734dd179eSJames Feist             }
64834dd179eSJames Feist             else if (*asserted)
64934dd179eSJames Feist             {
65034dd179eSJames Feist                 return "Warning";
65134dd179eSJames Feist             }
65234dd179eSJames Feist         }
65334dd179eSJames Feist         if (thresholdLowIt != warningThresholdIt->second.end())
65434dd179eSJames Feist         {
65534dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
65634dd179eSJames Feist             if (asserted == nullptr)
65734dd179eSJames Feist             {
65834dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
65934dd179eSJames Feist             }
66034dd179eSJames Feist             else if (*asserted)
66134dd179eSJames Feist             {
66234dd179eSJames Feist                 return "Warning";
66334dd179eSJames Feist             }
66434dd179eSJames Feist         }
66534dd179eSJames Feist     }
666adc4f0dbSShawn McCarney 
66734dd179eSJames Feist     return "OK";
66834dd179eSJames Feist }
66934dd179eSJames Feist 
670d500549bSAnthony Wilson static void setLedState(nlohmann::json& sensorJson,
671d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
672d500549bSAnthony Wilson {
673d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
674d500549bSAnthony Wilson     {
675d500549bSAnthony Wilson         switch (inventoryItem->ledState)
676d500549bSAnthony Wilson         {
677d500549bSAnthony Wilson             case LedState::OFF:
678d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
679d500549bSAnthony Wilson                 break;
680d500549bSAnthony Wilson             case LedState::ON:
681d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
682d500549bSAnthony Wilson                 break;
683d500549bSAnthony Wilson             case LedState::BLINK:
684d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
685d500549bSAnthony Wilson                 break;
686d500549bSAnthony Wilson             default:
687d500549bSAnthony Wilson                 break;
688d500549bSAnthony Wilson         }
689d500549bSAnthony Wilson     }
690d500549bSAnthony Wilson }
691d500549bSAnthony Wilson 
69234dd179eSJames Feist /**
69308777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
69408777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
695274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
69608777fb0SLewanczyk, Dawid  * build
69795a3ecadSAnthony Wilson  * @param sensorSchema  The schema (Power, Thermal, etc) being associated with
69895a3ecadSAnthony Wilson  * the sensor to build
69908777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
70008777fb0SLewanczyk, Dawid  * interfaces to be built from
70108777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
702adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
703adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
70408777fb0SLewanczyk, Dawid  */
70508777fb0SLewanczyk, Dawid void objectInterfacesToJson(
70608777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
70795a3ecadSAnthony Wilson     const std::string& sensorSchema,
70808777fb0SLewanczyk, Dawid     const boost::container::flat_map<
709aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>&
71008777fb0SLewanczyk, Dawid         interfacesDict,
711adc4f0dbSShawn McCarney     nlohmann::json& sensor_json, InventoryItem* inventoryItem)
7121abe55efSEd Tanous {
71308777fb0SLewanczyk, Dawid     // We need a value interface before we can do anything with it
71455c7b7a2SEd Tanous     auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
7151abe55efSEd Tanous     if (valueIt == interfacesDict.end())
7161abe55efSEd Tanous     {
71755c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
71808777fb0SLewanczyk, Dawid         return;
71908777fb0SLewanczyk, Dawid     }
72008777fb0SLewanczyk, Dawid 
72108777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
72208777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
72308777fb0SLewanczyk, Dawid 
72455c7b7a2SEd Tanous     auto scaleIt = valueIt->second.find("Scale");
72508777fb0SLewanczyk, Dawid     // If a scale exists, pull value as int64, and use the scaling.
7261abe55efSEd Tanous     if (scaleIt != valueIt->second.end())
7271abe55efSEd Tanous     {
728abf2add6SEd Tanous         const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
7291abe55efSEd Tanous         if (int64Value != nullptr)
7301abe55efSEd Tanous         {
73108777fb0SLewanczyk, Dawid             scaleMultiplier = *int64Value;
73208777fb0SLewanczyk, Dawid         }
73308777fb0SLewanczyk, Dawid     }
73408777fb0SLewanczyk, Dawid 
73595a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
736adc4f0dbSShawn McCarney     {
73795a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
73895a3ecadSAnthony Wilson         // including power sensors.
73995a3ecadSAnthony Wilson         sensor_json["Id"] = sensorName;
74095a3ecadSAnthony Wilson         sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
74195a3ecadSAnthony Wilson     }
74295a3ecadSAnthony Wilson     else if (sensorType != "power")
74395a3ecadSAnthony Wilson     {
74495a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
74595a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
74695a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
74708777fb0SLewanczyk, Dawid         sensor_json["MemberId"] = sensorName;
748e742b6ccSEd Tanous         sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
749adc4f0dbSShawn McCarney     }
750e742b6ccSEd Tanous 
751adc4f0dbSShawn McCarney     sensor_json["Status"]["State"] = getState(inventoryItem);
752adc4f0dbSShawn McCarney     sensor_json["Status"]["Health"] =
753adc4f0dbSShawn McCarney         getHealth(sensor_json, interfacesDict, inventoryItem);
75408777fb0SLewanczyk, Dawid 
75508777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
75608777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
75708777fb0SLewanczyk, Dawid     // that require integers, not floats.
75808777fb0SLewanczyk, Dawid     bool forceToInt = false;
75908777fb0SLewanczyk, Dawid 
7603929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
76195a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
76295a3ecadSAnthony Wilson     {
76395a3ecadSAnthony Wilson         sensor_json["@odata.type"] = "#Sensor.v1_0_0.Sensor";
76495a3ecadSAnthony Wilson         sensor_json["@odata.context"] = "/redfish/v1/$metadata#Sensor.Sensor";
76595a3ecadSAnthony Wilson         if (sensorType == "power")
76695a3ecadSAnthony Wilson         {
76795a3ecadSAnthony Wilson             sensor_json["ReadingUnits"] = "Watts";
76895a3ecadSAnthony Wilson         }
76995a3ecadSAnthony Wilson         else if (sensorType == "current")
77095a3ecadSAnthony Wilson         {
77195a3ecadSAnthony Wilson             sensor_json["ReadingUnits"] = "Amperes";
77295a3ecadSAnthony Wilson         }
77395a3ecadSAnthony Wilson     }
77495a3ecadSAnthony Wilson     else if (sensorType == "temperature")
7751abe55efSEd Tanous     {
7763929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
7777885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature";
77808777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
77908777fb0SLewanczyk, Dawid         // implementation seems to implement fan
7801abe55efSEd Tanous     }
7811abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
7821abe55efSEd Tanous     {
7833929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
78408777fb0SLewanczyk, Dawid         sensor_json["ReadingUnits"] = "RPM";
7857885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
786d500549bSAnthony Wilson         setLedState(sensor_json, inventoryItem);
78708777fb0SLewanczyk, Dawid         forceToInt = true;
7881abe55efSEd Tanous     }
7896f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
7906f6d0d32SEd Tanous     {
7913929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
7926f6d0d32SEd Tanous         sensor_json["ReadingUnits"] = "Percent";
7936f6d0d32SEd Tanous         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
794d500549bSAnthony Wilson         setLedState(sensor_json, inventoryItem);
7956f6d0d32SEd Tanous         forceToInt = true;
7966f6d0d32SEd Tanous     }
7971abe55efSEd Tanous     else if (sensorType == "voltage")
7981abe55efSEd Tanous     {
7993929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
8007885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage";
8011abe55efSEd Tanous     }
8022474adfaSEd Tanous     else if (sensorType == "power")
8032474adfaSEd Tanous     {
80449c53ac9SJohnathan Mantey         std::string sensorNameLower =
80549c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
80649c53ac9SJohnathan Mantey 
807028f7ebcSEddie James         if (!sensorName.compare("total_power"))
808028f7ebcSEddie James         {
8097ab06f49SGunnar Mills             sensor_json["@odata.type"] = "#Power.v1_0_0.PowerControl";
8107ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
8117ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
8127ab06f49SGunnar Mills             sensor_json["MemberId"] = "0";
8137ab06f49SGunnar Mills             sensor_json["Name"] = "Chassis Power Control";
8143929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
815028f7ebcSEddie James         }
816028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
81749c53ac9SJohnathan Mantey         {
8183929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
81949c53ac9SJohnathan Mantey         }
82049c53ac9SJohnathan Mantey         else
82149c53ac9SJohnathan Mantey         {
8223929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
82349c53ac9SJohnathan Mantey         }
8242474adfaSEd Tanous     }
8251abe55efSEd Tanous     else
8261abe55efSEd Tanous     {
82755c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
82808777fb0SLewanczyk, Dawid         return;
82908777fb0SLewanczyk, Dawid     }
83008777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
8313929aca1SAnthony Wilson     std::vector<
8323929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
8333929aca1SAnthony Wilson         properties;
83408777fb0SLewanczyk, Dawid     properties.reserve(7);
83508777fb0SLewanczyk, Dawid 
83608777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
837de629b6eSShawn McCarney 
8383929aca1SAnthony Wilson     if (sensorSchema == "Sensors")
8393929aca1SAnthony Wilson     {
8403929aca1SAnthony Wilson         properties.emplace_back(
8413929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
8423929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
8433929aca1SAnthony Wilson         properties.emplace_back(
8443929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
8453929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
8463929aca1SAnthony Wilson         properties.emplace_back(
8473929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
8483929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
8493929aca1SAnthony Wilson         properties.emplace_back(
8503929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
8513929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
8523929aca1SAnthony Wilson     }
8533929aca1SAnthony Wilson     else if (sensorType != "power")
854de629b6eSShawn McCarney     {
85508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8563929aca1SAnthony Wilson                                 "WarningHigh",
8573929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
85808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
8593929aca1SAnthony Wilson                                 "WarningLow",
8603929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
86108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8623929aca1SAnthony Wilson                                 "CriticalHigh",
8633929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
86408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
8653929aca1SAnthony Wilson                                 "CriticalLow",
8663929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
867de629b6eSShawn McCarney     }
86808777fb0SLewanczyk, Dawid 
8692474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
8702474adfaSEd Tanous 
87195a3ecadSAnthony Wilson     if (sensorSchema == "Sensors")
87295a3ecadSAnthony Wilson     {
87395a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8743929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
87595a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8763929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
87795a3ecadSAnthony Wilson     }
87895a3ecadSAnthony Wilson     else if (sensorType == "temperature")
8791abe55efSEd Tanous     {
88008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8813929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
88208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8833929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
8841abe55efSEd Tanous     }
885adc4f0dbSShawn McCarney     else if (sensorType != "power")
8861abe55efSEd Tanous     {
88708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
8883929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
88908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
8903929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
89108777fb0SLewanczyk, Dawid     }
89208777fb0SLewanczyk, Dawid 
8933929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
8943929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
8951abe55efSEd Tanous     {
89608777fb0SLewanczyk, Dawid         auto interfaceProperties = interfacesDict.find(std::get<0>(p));
8971abe55efSEd Tanous         if (interfaceProperties != interfacesDict.end())
8981abe55efSEd Tanous         {
899271584abSEd Tanous             auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
900271584abSEd Tanous             if (thisValueIt != interfaceProperties->second.end())
9011abe55efSEd Tanous             {
902271584abSEd Tanous                 const SensorVariant& valueVariant = thisValueIt->second;
9033929aca1SAnthony Wilson 
9043929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
9053929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
9063929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
9073929aca1SAnthony Wilson 
90808777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
909abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
91008777fb0SLewanczyk, Dawid 
911abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
912028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
9136f6d0d32SEd Tanous                 double temp = 0.0;
9146f6d0d32SEd Tanous                 if (int64Value != nullptr)
9151abe55efSEd Tanous                 {
916271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
9176f6d0d32SEd Tanous                 }
9186f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
9191abe55efSEd Tanous                 {
9206f6d0d32SEd Tanous                     temp = *doubleValue;
9211abe55efSEd Tanous                 }
922028f7ebcSEddie James                 else if (uValue != nullptr)
923028f7ebcSEddie James                 {
924028f7ebcSEddie James                     temp = *uValue;
925028f7ebcSEddie James                 }
9261abe55efSEd Tanous                 else
9271abe55efSEd Tanous                 {
9286f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
9296f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
9306f6d0d32SEd Tanous                     continue;
93108777fb0SLewanczyk, Dawid                 }
9326f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
9336f6d0d32SEd Tanous                 if (forceToInt)
9346f6d0d32SEd Tanous                 {
9353929aca1SAnthony Wilson                     sensor_json[key] = static_cast<int64_t>(temp);
9366f6d0d32SEd Tanous                 }
9376f6d0d32SEd Tanous                 else
9386f6d0d32SEd Tanous                 {
9393929aca1SAnthony Wilson                     sensor_json[key] = temp;
94008777fb0SLewanczyk, Dawid                 }
94108777fb0SLewanczyk, Dawid             }
94208777fb0SLewanczyk, Dawid         }
94308777fb0SLewanczyk, Dawid     }
94455c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
94508777fb0SLewanczyk, Dawid }
94608777fb0SLewanczyk, Dawid 
9478bd25ccdSJames Feist static void
9488bd25ccdSJames Feist     populateFanRedundancy(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp)
9498bd25ccdSJames Feist {
9508bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
9518bd25ccdSJames Feist         [sensorsAsyncResp](const boost::system::error_code ec,
9528bd25ccdSJames Feist                            const GetSubTreeType& resp) {
9538bd25ccdSJames Feist             if (ec)
9548bd25ccdSJames Feist             {
9558bd25ccdSJames Feist                 return; // don't have to have this interface
9568bd25ccdSJames Feist             }
957e278c18fSEd Tanous             for (const std::pair<std::string,
958e278c18fSEd Tanous                                  std::vector<std::pair<
959e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
960e278c18fSEd Tanous                      pathPair : resp)
9618bd25ccdSJames Feist             {
962e278c18fSEd Tanous                 const std::string& path = pathPair.first;
963e278c18fSEd Tanous                 const std::vector<
964e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
965e278c18fSEd Tanous                     pathPair.second;
9668bd25ccdSJames Feist                 if (objDict.empty())
9678bd25ccdSJames Feist                 {
9688bd25ccdSJames Feist                     continue; // this should be impossible
9698bd25ccdSJames Feist                 }
9708bd25ccdSJames Feist 
9718bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
9728bd25ccdSJames Feist                 crow::connections::systemBus->async_method_call(
9738bd25ccdSJames Feist                     [path, owner,
974271584abSEd Tanous                      sensorsAsyncResp](const boost::system::error_code e,
9758bd25ccdSJames Feist                                        std::variant<std::vector<std::string>>
9768bd25ccdSJames Feist                                            variantEndpoints) {
977271584abSEd Tanous                         if (e)
9788bd25ccdSJames Feist                         {
9798bd25ccdSJames Feist                             return; // if they don't have an association we
9808bd25ccdSJames Feist                                     // can't tell what chassis is
9818bd25ccdSJames Feist                         }
9828bd25ccdSJames Feist                         // verify part of the right chassis
9838bd25ccdSJames Feist                         auto endpoints = std::get_if<std::vector<std::string>>(
9848bd25ccdSJames Feist                             &variantEndpoints);
9858bd25ccdSJames Feist 
9868bd25ccdSJames Feist                         if (endpoints == nullptr)
9878bd25ccdSJames Feist                         {
9888bd25ccdSJames Feist                             BMCWEB_LOG_ERROR << "Invalid association interface";
9898bd25ccdSJames Feist                             messages::internalError(sensorsAsyncResp->res);
9908bd25ccdSJames Feist                             return;
9918bd25ccdSJames Feist                         }
9928bd25ccdSJames Feist 
9938bd25ccdSJames Feist                         auto found = std::find_if(
9948bd25ccdSJames Feist                             endpoints->begin(), endpoints->end(),
9958bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
9968bd25ccdSJames Feist                                 return entry.find(
9978bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
9988bd25ccdSJames Feist                                        std::string::npos;
9998bd25ccdSJames Feist                             });
10008bd25ccdSJames Feist 
10018bd25ccdSJames Feist                         if (found == endpoints->end())
10028bd25ccdSJames Feist                         {
10038bd25ccdSJames Feist                             return;
10048bd25ccdSJames Feist                         }
10058bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
10068bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1007271584abSEd Tanous                                 const boost::system::error_code& err,
10088bd25ccdSJames Feist                                 const boost::container::flat_map<
10098bd25ccdSJames Feist                                     std::string,
10108bd25ccdSJames Feist                                     std::variant<uint8_t,
10118bd25ccdSJames Feist                                                  std::vector<std::string>,
10128bd25ccdSJames Feist                                                  std::string>>& ret) {
1013271584abSEd Tanous                                 if (err)
10148bd25ccdSJames Feist                                 {
10158bd25ccdSJames Feist                                     return; // don't have to have this
10168bd25ccdSJames Feist                                             // interface
10178bd25ccdSJames Feist                                 }
10188bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
10198bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
10208bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
10218bd25ccdSJames Feist 
10228bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
10238bd25ccdSJames Feist                                     findCollection == ret.end() ||
10248bd25ccdSJames Feist                                     findStatus == ret.end())
10258bd25ccdSJames Feist                                 {
10268bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
10278bd25ccdSJames Feist                                         << "Invalid redundancy interface";
10288bd25ccdSJames Feist                                     messages::internalError(
10298bd25ccdSJames Feist                                         sensorsAsyncResp->res);
10308bd25ccdSJames Feist                                     return;
10318bd25ccdSJames Feist                                 }
10328bd25ccdSJames Feist 
10338bd25ccdSJames Feist                                 auto allowedFailures = std::get_if<uint8_t>(
10348bd25ccdSJames Feist                                     &(findFailures->second));
10358bd25ccdSJames Feist                                 auto collection =
10368bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
10378bd25ccdSJames Feist                                         &(findCollection->second));
10388bd25ccdSJames Feist                                 auto status = std::get_if<std::string>(
10398bd25ccdSJames Feist                                     &(findStatus->second));
10408bd25ccdSJames Feist 
10418bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
10428bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
10438bd25ccdSJames Feist                                 {
10448bd25ccdSJames Feist 
10458bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
10468bd25ccdSJames Feist                                         << "Invalid redundancy interface "
10478bd25ccdSJames Feist                                            "types";
10488bd25ccdSJames Feist                                     messages::internalError(
10498bd25ccdSJames Feist                                         sensorsAsyncResp->res);
10508bd25ccdSJames Feist                                     return;
10518bd25ccdSJames Feist                                 }
10528bd25ccdSJames Feist                                 size_t lastSlash = path.rfind("/");
10538bd25ccdSJames Feist                                 if (lastSlash == std::string::npos)
10548bd25ccdSJames Feist                                 {
10558bd25ccdSJames Feist                                     // this should be impossible
10568bd25ccdSJames Feist                                     messages::internalError(
10578bd25ccdSJames Feist                                         sensorsAsyncResp->res);
10588bd25ccdSJames Feist                                     return;
10598bd25ccdSJames Feist                                 }
10608bd25ccdSJames Feist                                 std::string name = path.substr(lastSlash + 1);
10618bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
10628bd25ccdSJames Feist                                              ' ');
10638bd25ccdSJames Feist 
10648bd25ccdSJames Feist                                 std::string health;
10658bd25ccdSJames Feist 
10668bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
10678bd25ccdSJames Feist                                 {
10688bd25ccdSJames Feist                                     health = "OK";
10698bd25ccdSJames Feist                                 }
10708bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
10718bd25ccdSJames Feist                                 {
10728bd25ccdSJames Feist                                     health = "Warning";
10738bd25ccdSJames Feist                                 }
10748bd25ccdSJames Feist                                 else
10758bd25ccdSJames Feist                                 {
10768bd25ccdSJames Feist                                     health = "Critical";
10778bd25ccdSJames Feist                                 }
10788bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
10798bd25ccdSJames Feist                                 const auto& fanRedfish =
10808bd25ccdSJames Feist                                     sensorsAsyncResp->res.jsonValue["Fans"];
10818bd25ccdSJames Feist                                 for (const std::string& item : *collection)
10828bd25ccdSJames Feist                                 {
10838bd25ccdSJames Feist                                     lastSlash = item.rfind("/");
10848bd25ccdSJames Feist                                     // make a copy as collection is const
10858bd25ccdSJames Feist                                     std::string itemName =
10868bd25ccdSJames Feist                                         item.substr(lastSlash + 1);
10878bd25ccdSJames Feist                                     /*
10888bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
10898bd25ccdSJames Feist                                     std::replace(itemName.begin(),
10908bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
10918bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
10928bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
10938bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
10948bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
10958bd25ccdSJames Feist                                         });
10968bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
10978bd25ccdSJames Feist                                     {
10988bd25ccdSJames Feist                                         redfishCollection.push_back(
10998bd25ccdSJames Feist                                             {{"@odata.id",
11008bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
11018bd25ccdSJames Feist                                     }
11028bd25ccdSJames Feist                                     else
11038bd25ccdSJames Feist                                     {
11048bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
11058bd25ccdSJames Feist                                             << "failed to find fan in schema";
11068bd25ccdSJames Feist                                         messages::internalError(
11078bd25ccdSJames Feist                                             sensorsAsyncResp->res);
11088bd25ccdSJames Feist                                         return;
11098bd25ccdSJames Feist                                     }
11108bd25ccdSJames Feist                                 }
11118bd25ccdSJames Feist 
1112271584abSEd Tanous                                 nlohmann::json& jResp =
1113271584abSEd Tanous                                     sensorsAsyncResp->res
11148bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1115271584abSEd Tanous                                 jResp.push_back(
11168bd25ccdSJames Feist                                     {{"@odata.id",
1117717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
11188bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
11198bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
11208bd25ccdSJames Feist                                           "#/Redundancy/" +
1121271584abSEd Tanous                                           std::to_string(jResp.size())},
11228bd25ccdSJames Feist                                      {"@odata.type",
11238bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
11248bd25ccdSJames Feist                                      {"MinNumNeeded",
11258bd25ccdSJames Feist                                       collection->size() - *allowedFailures},
11268bd25ccdSJames Feist                                      {"MemberId", name},
11278bd25ccdSJames Feist                                      {"Mode", "N+m"},
11288bd25ccdSJames Feist                                      {"Name", name},
11298bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
11308bd25ccdSJames Feist                                      {"Status",
11318bd25ccdSJames Feist                                       {{"Health", health},
11328bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
11338bd25ccdSJames Feist                             },
11348bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
11358bd25ccdSJames Feist                             "GetAll",
11368bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
11378bd25ccdSJames Feist                     },
113802e92e32SJames Feist                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11398bd25ccdSJames Feist                     "org.freedesktop.DBus.Properties", "Get",
11408bd25ccdSJames Feist                     "xyz.openbmc_project.Association", "endpoints");
11418bd25ccdSJames Feist             }
11428bd25ccdSJames Feist         },
11438bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
11448bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
11458bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
11468bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
11478bd25ccdSJames Feist         std::array<const char*, 1>{
11488bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
11498bd25ccdSJames Feist }
11508bd25ccdSJames Feist 
115149c53ac9SJohnathan Mantey void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
115249c53ac9SJohnathan Mantey {
115349c53ac9SJohnathan Mantey     nlohmann::json& response = SensorsAsyncResp->res.jsonValue;
115449c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
115549c53ac9SJohnathan Mantey     if (SensorsAsyncResp->chassisSubNode == "Power")
115649c53ac9SJohnathan Mantey     {
115749c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
115849c53ac9SJohnathan Mantey     }
115949c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
116049c53ac9SJohnathan Mantey     {
116149c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
116249c53ac9SJohnathan Mantey         if (entry != response.end())
116349c53ac9SJohnathan Mantey         {
116449c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
116549c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
116649c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
116749c53ac9SJohnathan Mantey                       });
116849c53ac9SJohnathan Mantey 
116949c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
117049c53ac9SJohnathan Mantey             size_t count = 0;
117149c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
117249c53ac9SJohnathan Mantey             {
117349c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
117449c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
117549c53ac9SJohnathan Mantey                 {
117649c53ac9SJohnathan Mantey                     continue;
117749c53ac9SJohnathan Mantey                 }
117849c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
117949c53ac9SJohnathan Mantey                 if (value != nullptr)
118049c53ac9SJohnathan Mantey                 {
118149c53ac9SJohnathan Mantey                     *value += std::to_string(count);
118249c53ac9SJohnathan Mantey                     count++;
118349c53ac9SJohnathan Mantey                 }
118449c53ac9SJohnathan Mantey             }
118549c53ac9SJohnathan Mantey         }
118649c53ac9SJohnathan Mantey     }
118749c53ac9SJohnathan Mantey }
118849c53ac9SJohnathan Mantey 
118908777fb0SLewanczyk, Dawid /**
1190adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1191adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1192adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1193adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
11948fb49dd6SShawn McCarney  */
1195adc4f0dbSShawn McCarney static InventoryItem* findInventoryItem(
1196adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1197adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
11988fb49dd6SShawn McCarney {
1199adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
12008fb49dd6SShawn McCarney     {
1201adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
12028fb49dd6SShawn McCarney         {
1203adc4f0dbSShawn McCarney             return &inventoryItem;
12048fb49dd6SShawn McCarney         }
12058fb49dd6SShawn McCarney     }
12068fb49dd6SShawn McCarney     return nullptr;
12078fb49dd6SShawn McCarney }
12088fb49dd6SShawn McCarney 
12098fb49dd6SShawn McCarney /**
1210adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1211adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1212adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1213adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12148fb49dd6SShawn McCarney  */
1215adc4f0dbSShawn McCarney static InventoryItem* findInventoryItemForSensor(
1216adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1217adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1218adc4f0dbSShawn McCarney {
1219adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1220adc4f0dbSShawn McCarney     {
1221adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1222adc4f0dbSShawn McCarney         {
1223adc4f0dbSShawn McCarney             return &inventoryItem;
1224adc4f0dbSShawn McCarney         }
1225adc4f0dbSShawn McCarney     }
1226adc4f0dbSShawn McCarney     return nullptr;
1227adc4f0dbSShawn McCarney }
1228adc4f0dbSShawn McCarney 
1229adc4f0dbSShawn McCarney /**
1230d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1231d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1232d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1233d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1234d500549bSAnthony Wilson  */
1235d500549bSAnthony Wilson inline InventoryItem*
1236d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1237d500549bSAnthony Wilson                             const std::string& ledObjPath)
1238d500549bSAnthony Wilson {
1239d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1240d500549bSAnthony Wilson     {
1241d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1242d500549bSAnthony Wilson         {
1243d500549bSAnthony Wilson             return &inventoryItem;
1244d500549bSAnthony Wilson         }
1245d500549bSAnthony Wilson     }
1246d500549bSAnthony Wilson     return nullptr;
1247d500549bSAnthony Wilson }
1248d500549bSAnthony Wilson 
1249d500549bSAnthony Wilson /**
1250adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1251adc4f0dbSShawn McCarney  *
1252adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1253adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1254adc4f0dbSShawn McCarney  * added to the vector.
1255adc4f0dbSShawn McCarney  *
1256adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1257adc4f0dbSShawn McCarney  * InventoryItem.
1258adc4f0dbSShawn McCarney  *
1259adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1260adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1261adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1262adc4f0dbSShawn McCarney  */
1263adc4f0dbSShawn McCarney static void
1264adc4f0dbSShawn McCarney     addInventoryItem(std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1265adc4f0dbSShawn McCarney                      const std::string& invItemObjPath,
1266adc4f0dbSShawn McCarney                      const std::string& sensorObjPath)
1267adc4f0dbSShawn McCarney {
1268adc4f0dbSShawn McCarney     // Look for inventory item in vector
1269adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1270adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1271adc4f0dbSShawn McCarney 
1272adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1273adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1274adc4f0dbSShawn McCarney     {
1275adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1276adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1277adc4f0dbSShawn McCarney     }
1278adc4f0dbSShawn McCarney 
1279adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1280adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1281adc4f0dbSShawn McCarney }
1282adc4f0dbSShawn McCarney 
1283adc4f0dbSShawn McCarney /**
1284adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1285adc4f0dbSShawn McCarney  *
1286adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1287adc4f0dbSShawn McCarney  * specified InventoryItem.
1288adc4f0dbSShawn McCarney  *
1289adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1290adc4f0dbSShawn McCarney  * response.
1291adc4f0dbSShawn McCarney  *
1292adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1293adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1294adc4f0dbSShawn McCarney  * for the specified inventory item.
1295adc4f0dbSShawn McCarney  */
1296adc4f0dbSShawn McCarney static void storeInventoryItemData(
1297adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
12988fb49dd6SShawn McCarney     const boost::container::flat_map<
12998fb49dd6SShawn McCarney         std::string, boost::container::flat_map<std::string, SensorVariant>>&
13008fb49dd6SShawn McCarney         interfacesDict)
13018fb49dd6SShawn McCarney {
1302adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1303adc4f0dbSShawn McCarney     auto interfaceIt =
1304adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1305adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
13068fb49dd6SShawn McCarney     {
1307adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Present");
1308adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
13098fb49dd6SShawn McCarney         {
1310adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1311adc4f0dbSShawn McCarney             if (value != nullptr)
13128fb49dd6SShawn McCarney             {
1313adc4f0dbSShawn McCarney                 inventoryItem.isPresent = *value;
13148fb49dd6SShawn McCarney             }
13158fb49dd6SShawn McCarney         }
13168fb49dd6SShawn McCarney     }
13178fb49dd6SShawn McCarney 
1318adc4f0dbSShawn McCarney     // Check if Inventory.Item.PowerSupply interface is present
1319adc4f0dbSShawn McCarney     interfaceIt =
1320adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1321adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
13228fb49dd6SShawn McCarney     {
1323adc4f0dbSShawn McCarney         inventoryItem.isPowerSupply = true;
13248fb49dd6SShawn McCarney     }
1325adc4f0dbSShawn McCarney 
1326adc4f0dbSShawn McCarney     // Get properties from Inventory.Decorator.Asset interface
1327adc4f0dbSShawn McCarney     interfaceIt =
1328adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1329adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1330adc4f0dbSShawn McCarney     {
1331adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Manufacturer");
1332adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1333adc4f0dbSShawn McCarney         {
1334adc4f0dbSShawn McCarney             const std::string* value =
1335adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1336adc4f0dbSShawn McCarney             if (value != nullptr)
1337adc4f0dbSShawn McCarney             {
1338adc4f0dbSShawn McCarney                 inventoryItem.manufacturer = *value;
1339adc4f0dbSShawn McCarney             }
1340adc4f0dbSShawn McCarney         }
1341adc4f0dbSShawn McCarney 
1342adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("Model");
1343adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1344adc4f0dbSShawn McCarney         {
1345adc4f0dbSShawn McCarney             const std::string* value =
1346adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1347adc4f0dbSShawn McCarney             if (value != nullptr)
1348adc4f0dbSShawn McCarney             {
1349adc4f0dbSShawn McCarney                 inventoryItem.model = *value;
1350adc4f0dbSShawn McCarney             }
1351adc4f0dbSShawn McCarney         }
1352adc4f0dbSShawn McCarney 
1353adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("PartNumber");
1354adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1355adc4f0dbSShawn McCarney         {
1356adc4f0dbSShawn McCarney             const std::string* value =
1357adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1358adc4f0dbSShawn McCarney             if (value != nullptr)
1359adc4f0dbSShawn McCarney             {
1360adc4f0dbSShawn McCarney                 inventoryItem.partNumber = *value;
1361adc4f0dbSShawn McCarney             }
1362adc4f0dbSShawn McCarney         }
1363adc4f0dbSShawn McCarney 
1364adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("SerialNumber");
1365adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1366adc4f0dbSShawn McCarney         {
1367adc4f0dbSShawn McCarney             const std::string* value =
1368adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1369adc4f0dbSShawn McCarney             if (value != nullptr)
1370adc4f0dbSShawn McCarney             {
1371adc4f0dbSShawn McCarney                 inventoryItem.serialNumber = *value;
1372adc4f0dbSShawn McCarney             }
1373adc4f0dbSShawn McCarney         }
1374adc4f0dbSShawn McCarney     }
1375adc4f0dbSShawn McCarney 
1376adc4f0dbSShawn McCarney     // Get properties from State.Decorator.OperationalStatus interface
1377adc4f0dbSShawn McCarney     interfaceIt = interfacesDict.find(
1378adc4f0dbSShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus");
1379adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1380adc4f0dbSShawn McCarney     {
1381adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Functional");
1382adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1383adc4f0dbSShawn McCarney         {
1384adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1385adc4f0dbSShawn McCarney             if (value != nullptr)
1386adc4f0dbSShawn McCarney             {
1387adc4f0dbSShawn McCarney                 inventoryItem.isFunctional = *value;
13888fb49dd6SShawn McCarney             }
13898fb49dd6SShawn McCarney         }
13908fb49dd6SShawn McCarney     }
13918fb49dd6SShawn McCarney }
13928fb49dd6SShawn McCarney 
13938fb49dd6SShawn McCarney /**
1394adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
13958fb49dd6SShawn McCarney  *
1396adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1397adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1398adc4f0dbSShawn McCarney  * inventoryItems vector.
13998fb49dd6SShawn McCarney  *
1400adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1401adc4f0dbSShawn McCarney  * response.
1402adc4f0dbSShawn McCarney  *
1403adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1404adc4f0dbSShawn McCarney  * been obtained.
1405adc4f0dbSShawn McCarney  *
1406adc4f0dbSShawn McCarney  * The callback must have the following signature:
1407adc4f0dbSShawn McCarney  *   @code
1408d500549bSAnthony Wilson  *   callback(void)
1409adc4f0dbSShawn McCarney  *   @endcode
1410adc4f0dbSShawn McCarney  *
1411adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1412adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1413adc4f0dbSShawn McCarney  * last asynchronous function has completed.
14148fb49dd6SShawn McCarney  *
14158fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1416adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1417adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
14188fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
14198fb49dd6SShawn McCarney  * implements ObjectManager.
1420adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1421adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1422adc4f0dbSShawn McCarney  * in recursive calls to this function.
14238fb49dd6SShawn McCarney  */
1424adc4f0dbSShawn McCarney template <typename Callback>
1425adc4f0dbSShawn McCarney static void getInventoryItemsData(
14268fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1427adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
14288fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
14298fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1430adc4f0dbSShawn McCarney         objectMgrPaths,
1431271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
14328fb49dd6SShawn McCarney {
1433adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
14348fb49dd6SShawn McCarney 
1435adc4f0dbSShawn McCarney     // If no more connections left, call callback
1436adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
14378fb49dd6SShawn McCarney     {
1438d500549bSAnthony Wilson         callback();
1439adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1440adc4f0dbSShawn McCarney         return;
1441adc4f0dbSShawn McCarney     }
1442adc4f0dbSShawn McCarney 
1443adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1444adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1445adc4f0dbSShawn McCarney     if (it != invConnections->end())
1446adc4f0dbSShawn McCarney     {
1447adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1448adc4f0dbSShawn McCarney 
14498fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1450adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1451adc4f0dbSShawn McCarney                             objectMgrPaths, callback{std::move(callback)},
1452adc4f0dbSShawn McCarney                             invConnectionsIndex](
1453adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
14548fb49dd6SShawn McCarney                                ManagedObjectsVectorType& resp) {
1455adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
14568fb49dd6SShawn McCarney             if (ec)
14578fb49dd6SShawn McCarney             {
14588fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1459adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
14608fb49dd6SShawn McCarney                 messages::internalError(sensorsAsyncResp->res);
14618fb49dd6SShawn McCarney                 return;
14628fb49dd6SShawn McCarney             }
14638fb49dd6SShawn McCarney 
14648fb49dd6SShawn McCarney             // Loop through returned object paths
14658fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
14668fb49dd6SShawn McCarney             {
14678fb49dd6SShawn McCarney                 const std::string& objPath =
14688fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
14698fb49dd6SShawn McCarney 
1470adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1471adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1472adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1473adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
14748fb49dd6SShawn McCarney                 {
1475adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1476adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
14778fb49dd6SShawn McCarney                 }
14788fb49dd6SShawn McCarney             }
14798fb49dd6SShawn McCarney 
1480adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1481adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1482adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1483adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1484adc4f0dbSShawn McCarney 
1485adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
14868fb49dd6SShawn McCarney         };
14878fb49dd6SShawn McCarney 
14888fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
14898fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
14908fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
14918fb49dd6SShawn McCarney         const std::string& objectMgrPath =
14928fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
14938fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
14948fb49dd6SShawn McCarney                          << objectMgrPath;
14958fb49dd6SShawn McCarney 
14968fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
14978fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
14988fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
14998fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15008fb49dd6SShawn McCarney     }
15018fb49dd6SShawn McCarney 
1502adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
15038fb49dd6SShawn McCarney }
15048fb49dd6SShawn McCarney 
15058fb49dd6SShawn McCarney /**
1506adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
15078fb49dd6SShawn McCarney  *
1508adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1509adc4f0dbSShawn McCarney  * items that are associated with sensors.
15108fb49dd6SShawn McCarney  *
15118fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
15128fb49dd6SShawn McCarney  * been obtained.
15138fb49dd6SShawn McCarney  *
15148fb49dd6SShawn McCarney  * The callback must have the following signature:
15158fb49dd6SShawn McCarney  *   @code
15168fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
15178fb49dd6SShawn McCarney  *            invConnections)
15188fb49dd6SShawn McCarney  *   @endcode
15198fb49dd6SShawn McCarney  *
15208fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1521adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
15228fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
15238fb49dd6SShawn McCarney  */
15248fb49dd6SShawn McCarney template <typename Callback>
15258fb49dd6SShawn McCarney static void getInventoryItemsConnections(
15268fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1527adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
15288fb49dd6SShawn McCarney     Callback&& callback)
15298fb49dd6SShawn McCarney {
15308fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
15318fb49dd6SShawn McCarney 
15328fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1533adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
15348fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1535adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1536adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
15378fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
15388fb49dd6SShawn McCarney 
15398fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
15408fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1541adc4f0dbSShawn McCarney                         inventoryItems](const boost::system::error_code ec,
15428fb49dd6SShawn McCarney                                         const GetSubTreeType& subtree) {
15438fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
15448fb49dd6SShawn McCarney         if (ec)
15458fb49dd6SShawn McCarney         {
15468fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
15478fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
15488fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
15498fb49dd6SShawn McCarney             return;
15508fb49dd6SShawn McCarney         }
15518fb49dd6SShawn McCarney 
15528fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
15538fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
15548fb49dd6SShawn McCarney             invConnections =
15558fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
15568fb49dd6SShawn McCarney         invConnections->reserve(8);
15578fb49dd6SShawn McCarney 
15588fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
15598fb49dd6SShawn McCarney         for (const std::pair<
15608fb49dd6SShawn McCarney                  std::string,
15618fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
15628fb49dd6SShawn McCarney                  object : subtree)
15638fb49dd6SShawn McCarney         {
1564adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
15658fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1566adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
15678fb49dd6SShawn McCarney             {
15688fb49dd6SShawn McCarney                 // Store all connections to inventory item
15698fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
15708fb49dd6SShawn McCarney                          objData : object.second)
15718fb49dd6SShawn McCarney                 {
15728fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
15738fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
15748fb49dd6SShawn McCarney                 }
15758fb49dd6SShawn McCarney             }
15768fb49dd6SShawn McCarney         }
1577d500549bSAnthony Wilson 
15788fb49dd6SShawn McCarney         callback(invConnections);
15798fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
15808fb49dd6SShawn McCarney     };
15818fb49dd6SShawn McCarney 
15828fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
15838fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
15848fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
15858fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
15868fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
15878fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
15888fb49dd6SShawn McCarney }
15898fb49dd6SShawn McCarney 
15908fb49dd6SShawn McCarney /**
1591adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
15928fb49dd6SShawn McCarney  *
15938fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1594d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1595d500549bSAnthony Wilson  * their LEDs, if any.
15968fb49dd6SShawn McCarney  *
15978fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
15988fb49dd6SShawn McCarney  * has been obtained.
15998fb49dd6SShawn McCarney  *
16008fb49dd6SShawn McCarney  * The callback must have the following signature:
16018fb49dd6SShawn McCarney  *   @code
1602adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
16038fb49dd6SShawn McCarney  *   @endcode
16048fb49dd6SShawn McCarney  *
16058fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
16068fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
16078fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
16088fb49dd6SShawn McCarney  * implements ObjectManager.
16098fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
16108fb49dd6SShawn McCarney  */
16118fb49dd6SShawn McCarney template <typename Callback>
1612adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
16138fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
16148fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
16158fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
16168fb49dd6SShawn McCarney         objectMgrPaths,
16178fb49dd6SShawn McCarney     Callback&& callback)
16188fb49dd6SShawn McCarney {
1619adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
16208fb49dd6SShawn McCarney 
16218fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
16228fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
16238fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
16248fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1625adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
16268fb49dd6SShawn McCarney         if (ec)
16278fb49dd6SShawn McCarney         {
1628adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1629adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
16308fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
16318fb49dd6SShawn McCarney             return;
16328fb49dd6SShawn McCarney         }
16338fb49dd6SShawn McCarney 
1634adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1635adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1636adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1637adc4f0dbSShawn McCarney 
16388fb49dd6SShawn McCarney         // Loop through returned object paths
16398fb49dd6SShawn McCarney         std::string sensorAssocPath;
16408fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
16418fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
16428fb49dd6SShawn McCarney         {
16438fb49dd6SShawn McCarney             const std::string& objPath =
16448fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
16458fb49dd6SShawn McCarney             const boost::container::flat_map<
16468fb49dd6SShawn McCarney                 std::string, boost::container::flat_map<
16478fb49dd6SShawn McCarney                                  std::string, dbus::utility::DbusVariantType>>&
16488fb49dd6SShawn McCarney                 interfacesDict = objDictEntry.second;
16498fb49dd6SShawn McCarney 
16508fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
16518fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
16528fb49dd6SShawn McCarney             {
16538fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
16548fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
16558fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
16568fb49dd6SShawn McCarney                 {
16578fb49dd6SShawn McCarney                     // Get Association interface for object path
16588fb49dd6SShawn McCarney                     auto assocIt =
16598fb49dd6SShawn McCarney                         interfacesDict.find("xyz.openbmc_project.Association");
16608fb49dd6SShawn McCarney                     if (assocIt != interfacesDict.end())
16618fb49dd6SShawn McCarney                     {
16628fb49dd6SShawn McCarney                         // Get inventory item from end point
16638fb49dd6SShawn McCarney                         auto endpointsIt = assocIt->second.find("endpoints");
16648fb49dd6SShawn McCarney                         if (endpointsIt != assocIt->second.end())
16658fb49dd6SShawn McCarney                         {
16668fb49dd6SShawn McCarney                             const std::vector<std::string>* endpoints =
16678fb49dd6SShawn McCarney                                 std::get_if<std::vector<std::string>>(
16688fb49dd6SShawn McCarney                                     &endpointsIt->second);
16698fb49dd6SShawn McCarney                             if ((endpoints != nullptr) && !endpoints->empty())
16708fb49dd6SShawn McCarney                             {
1671adc4f0dbSShawn McCarney                                 // Add inventory item to vector
1672adc4f0dbSShawn McCarney                                 const std::string& invItemPath =
1673adc4f0dbSShawn McCarney                                     endpoints->front();
1674adc4f0dbSShawn McCarney                                 addInventoryItem(inventoryItems, invItemPath,
1675adc4f0dbSShawn McCarney                                                  sensorName);
16768fb49dd6SShawn McCarney                             }
16778fb49dd6SShawn McCarney                         }
16788fb49dd6SShawn McCarney                     }
16798fb49dd6SShawn McCarney                     break;
16808fb49dd6SShawn McCarney                 }
16818fb49dd6SShawn McCarney             }
16828fb49dd6SShawn McCarney         }
16838fb49dd6SShawn McCarney 
1684d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1685d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1686d500549bSAnthony Wilson         std::string inventoryAssocPath;
1687d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1688d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1689d500549bSAnthony Wilson         {
1690d500549bSAnthony Wilson             const std::string& objPath =
1691d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1692d500549bSAnthony Wilson             const boost::container::flat_map<
1693d500549bSAnthony Wilson                 std::string, boost::container::flat_map<
1694d500549bSAnthony Wilson                                  std::string, dbus::utility::DbusVariantType>>&
1695d500549bSAnthony Wilson                 interfacesDict = objDictEntry.second;
1696d500549bSAnthony Wilson 
1697d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1698d500549bSAnthony Wilson             {
1699d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1700d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1701d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1702d500549bSAnthony Wilson                 {
1703d500549bSAnthony Wilson                     // Get Association interface for object path
1704d500549bSAnthony Wilson                     auto assocIt =
1705d500549bSAnthony Wilson                         interfacesDict.find("xyz.openbmc_project.Association");
1706d500549bSAnthony Wilson                     if (assocIt != interfacesDict.end())
1707d500549bSAnthony Wilson                     {
1708d500549bSAnthony Wilson                         // Get inventory item from end point
1709d500549bSAnthony Wilson                         auto endpointsIt = assocIt->second.find("endpoints");
1710d500549bSAnthony Wilson                         if (endpointsIt != assocIt->second.end())
1711d500549bSAnthony Wilson                         {
1712d500549bSAnthony Wilson                             const std::vector<std::string>* endpoints =
1713d500549bSAnthony Wilson                                 std::get_if<std::vector<std::string>>(
1714d500549bSAnthony Wilson                                     &endpointsIt->second);
1715d500549bSAnthony Wilson                             if ((endpoints != nullptr) && !endpoints->empty())
1716d500549bSAnthony Wilson                             {
1717d500549bSAnthony Wilson                                 // Store LED path in inventory item
1718d500549bSAnthony Wilson                                 const std::string& ledPath = endpoints->front();
1719d500549bSAnthony Wilson                                 inventoryItem.ledObjectPath = ledPath;
1720d500549bSAnthony Wilson                             }
1721d500549bSAnthony Wilson                         }
1722d500549bSAnthony Wilson                     }
1723d500549bSAnthony Wilson                     break;
1724d500549bSAnthony Wilson                 }
1725d500549bSAnthony Wilson             }
1726d500549bSAnthony Wilson         }
1727adc4f0dbSShawn McCarney         callback(inventoryItems);
1728adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
17298fb49dd6SShawn McCarney     };
17308fb49dd6SShawn McCarney 
17318fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
17328fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
17338fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
17348fb49dd6SShawn McCarney     const std::string& objectMgrPath =
17358fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
17368fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
17378fb49dd6SShawn McCarney                      << objectMgrPath;
17388fb49dd6SShawn McCarney 
17398fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
17408fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17418fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
17428fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17438fb49dd6SShawn McCarney 
1744adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
17458fb49dd6SShawn McCarney }
17468fb49dd6SShawn McCarney 
17478fb49dd6SShawn McCarney /**
1748d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1749d500549bSAnthony Wilson  *
1750d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1751d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1752d500549bSAnthony Wilson  * inventoryItems vector.
1753d500549bSAnthony Wilson  *
1754d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1755d500549bSAnthony Wilson  * response.
1756d500549bSAnthony Wilson  *
1757d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1758d500549bSAnthony Wilson  * has been obtained.
1759d500549bSAnthony Wilson  *
1760d500549bSAnthony Wilson  * The callback must have the following signature:
1761d500549bSAnthony Wilson  *   @code
1762*42cbe538SGunnar Mills  *   callback()
1763d500549bSAnthony Wilson  *   @endcode
1764d500549bSAnthony Wilson  *
1765d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1766d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1767d500549bSAnthony Wilson  * last asynchronous function has completed.
1768d500549bSAnthony Wilson  *
1769d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1770d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1771d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1772d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1773d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1774d500549bSAnthony Wilson  * in recursive calls to this function.
1775d500549bSAnthony Wilson  */
1776d500549bSAnthony Wilson template <typename Callback>
1777d500549bSAnthony Wilson void getInventoryLedData(
1778d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1779d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1780d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1781d500549bSAnthony Wilson         ledConnections,
1782d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1783d500549bSAnthony Wilson {
1784d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1785d500549bSAnthony Wilson 
1786d500549bSAnthony Wilson     // If no more connections left, call callback
1787d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1788d500549bSAnthony Wilson     {
1789*42cbe538SGunnar Mills         callback();
1790d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1791d500549bSAnthony Wilson         return;
1792d500549bSAnthony Wilson     }
1793d500549bSAnthony Wilson 
1794d500549bSAnthony Wilson     // Get inventory item data from current connection
1795d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1796d500549bSAnthony Wilson     if (it != ledConnections->end())
1797d500549bSAnthony Wilson     {
1798d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1799d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1800d500549bSAnthony Wilson         // Response handler for Get State property
1801d500549bSAnthony Wilson         auto respHandler =
1802d500549bSAnthony Wilson             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1803d500549bSAnthony Wilson              callback{std::move(callback)},
1804d500549bSAnthony Wilson              ledConnectionsIndex](const boost::system::error_code ec,
1805d500549bSAnthony Wilson                                   const std::variant<std::string>& ledState) {
1806d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1807d500549bSAnthony Wilson                 if (ec)
1808d500549bSAnthony Wilson                 {
1809d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
1810d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
1811d500549bSAnthony Wilson                     messages::internalError(sensorsAsyncResp->res);
1812d500549bSAnthony Wilson                     return;
1813d500549bSAnthony Wilson                 }
1814d500549bSAnthony Wilson 
1815d500549bSAnthony Wilson                 const std::string* state = std::get_if<std::string>(&ledState);
1816d500549bSAnthony Wilson                 if (state != nullptr)
1817d500549bSAnthony Wilson                 {
1818d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Led state: " << *state;
1819d500549bSAnthony Wilson                     // Find inventory item with this LED object path
1820d500549bSAnthony Wilson                     InventoryItem* inventoryItem =
1821d500549bSAnthony Wilson                         findInventoryItemForLed(*inventoryItems, ledPath);
1822d500549bSAnthony Wilson                     if (inventoryItem != nullptr)
1823d500549bSAnthony Wilson                     {
1824d500549bSAnthony Wilson                         // Store LED state in InventoryItem
1825d500549bSAnthony Wilson                         if (boost::ends_with(*state, "On"))
1826d500549bSAnthony Wilson                         {
1827d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::ON;
1828d500549bSAnthony Wilson                         }
1829d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Blink"))
1830d500549bSAnthony Wilson                         {
1831d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::BLINK;
1832d500549bSAnthony Wilson                         }
1833d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Off"))
1834d500549bSAnthony Wilson                         {
1835d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::OFF;
1836d500549bSAnthony Wilson                         }
1837d500549bSAnthony Wilson                         else
1838d500549bSAnthony Wilson                         {
1839d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::UNKNOWN;
1840d500549bSAnthony Wilson                         }
1841d500549bSAnthony Wilson                     }
1842d500549bSAnthony Wilson                 }
1843d500549bSAnthony Wilson                 else
1844d500549bSAnthony Wilson                 {
1845d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
1846d500549bSAnthony Wilson                                      << ledPath;
1847d500549bSAnthony Wilson                 }
1848d500549bSAnthony Wilson 
1849d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1850d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1851d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1852d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1853d500549bSAnthony Wilson 
1854d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1855d500549bSAnthony Wilson             };
1856d500549bSAnthony Wilson 
1857d500549bSAnthony Wilson         // Get the State property for the current LED
1858d500549bSAnthony Wilson         crow::connections::systemBus->async_method_call(
1859d500549bSAnthony Wilson             std::move(respHandler), ledConnection, ledPath,
1860d500549bSAnthony Wilson             "org.freedesktop.DBus.Properties", "Get",
1861d500549bSAnthony Wilson             "xyz.openbmc_project.Led.Physical", "State");
1862d500549bSAnthony Wilson     }
1863d500549bSAnthony Wilson 
1864d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1865d500549bSAnthony Wilson }
1866d500549bSAnthony Wilson 
1867d500549bSAnthony Wilson /**
1868d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1869d500549bSAnthony Wilson  *
1870d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1871d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1872d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1873d500549bSAnthony Wilson  *
1874d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1875d500549bSAnthony Wilson  * response.
1876d500549bSAnthony Wilson  *
1877d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1878d500549bSAnthony Wilson  * been obtained.
1879d500549bSAnthony Wilson  *
1880d500549bSAnthony Wilson  * The callback must have the following signature:
1881d500549bSAnthony Wilson  *   @code
1882*42cbe538SGunnar Mills  *   callback()
1883d500549bSAnthony Wilson  *   @endcode
1884d500549bSAnthony Wilson  *
1885d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1886d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1887d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1888d500549bSAnthony Wilson  */
1889d500549bSAnthony Wilson template <typename Callback>
1890d500549bSAnthony Wilson void getInventoryLeds(
1891d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1892d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1893d500549bSAnthony Wilson     Callback&& callback)
1894d500549bSAnthony Wilson {
1895d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1896d500549bSAnthony Wilson 
1897d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1898d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
1899d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1900d500549bSAnthony Wilson 
1901d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
1902d500549bSAnthony Wilson     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1903d500549bSAnthony Wilson                         inventoryItems](const boost::system::error_code ec,
1904d500549bSAnthony Wilson                                         const GetSubTreeType& subtree) {
1905d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1906d500549bSAnthony Wilson         if (ec)
1907d500549bSAnthony Wilson         {
1908d500549bSAnthony Wilson             messages::internalError(sensorsAsyncResp->res);
1909d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1910d500549bSAnthony Wilson                              << ec;
1911d500549bSAnthony Wilson             return;
1912d500549bSAnthony Wilson         }
1913d500549bSAnthony Wilson 
1914d500549bSAnthony Wilson         // Build map of LED object paths to connections
1915d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1916d500549bSAnthony Wilson             ledConnections = std::make_shared<
1917d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
1918d500549bSAnthony Wilson 
1919d500549bSAnthony Wilson         // Loop through objects from GetSubTree
1920d500549bSAnthony Wilson         for (const std::pair<
1921d500549bSAnthony Wilson                  std::string,
1922d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1923d500549bSAnthony Wilson                  object : subtree)
1924d500549bSAnthony Wilson         {
1925d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
1926d500549bSAnthony Wilson             // items
1927d500549bSAnthony Wilson             const std::string& ledPath = object.first;
1928d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1929d500549bSAnthony Wilson             {
1930d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
1931d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
1932d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
1933d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1934d500549bSAnthony Wilson                                  << connection;
1935d500549bSAnthony Wilson             }
1936d500549bSAnthony Wilson         }
1937d500549bSAnthony Wilson 
1938d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1939d500549bSAnthony Wilson                             std::move(callback));
1940d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1941d500549bSAnthony Wilson     };
1942d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
1943d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
1944d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1945d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
1946d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1947d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1948d500549bSAnthony Wilson }
1949d500549bSAnthony Wilson 
1950d500549bSAnthony Wilson /**
1951*42cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1952*42cbe538SGunnar Mills  *
1953*42cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
1954*42cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1955*42cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1956*42cbe538SGunnar Mills  *
1957*42cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
1958*42cbe538SGunnar Mills  * response.
1959*42cbe538SGunnar Mills  *
1960*42cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
1961*42cbe538SGunnar Mills  * when data has been obtained.
1962*42cbe538SGunnar Mills  *
1963*42cbe538SGunnar Mills  * The callback must have the following signature:
1964*42cbe538SGunnar Mills  *   @code
1965*42cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1966*42cbe538SGunnar Mills  *   @endcode
1967*42cbe538SGunnar Mills  *
1968*42cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
1969*42cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
1970*42cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
1971*42cbe538SGunnar Mills  *        Supply Attributes
1972*42cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
1973*42cbe538SGunnar Mills  */
1974*42cbe538SGunnar Mills template <typename Callback>
1975*42cbe538SGunnar Mills void getPowerSupplyAttributesData(
1976*42cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1977*42cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1978*42cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
1979*42cbe538SGunnar Mills         psAttributesConnections,
1980*42cbe538SGunnar Mills     Callback&& callback)
1981*42cbe538SGunnar Mills {
1982*42cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1983*42cbe538SGunnar Mills 
1984*42cbe538SGunnar Mills     if (psAttributesConnections.empty())
1985*42cbe538SGunnar Mills     {
1986*42cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1987*42cbe538SGunnar Mills         callback(inventoryItems);
1988*42cbe538SGunnar Mills         return;
1989*42cbe538SGunnar Mills     }
1990*42cbe538SGunnar Mills 
1991*42cbe538SGunnar Mills     // Assuming just one connection (service) for now
1992*42cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
1993*42cbe538SGunnar Mills 
1994*42cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
1995*42cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
1996*42cbe538SGunnar Mills 
1997*42cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
1998*42cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
1999*42cbe538SGunnar Mills                         callback{std::move(callback)}](
2000*42cbe538SGunnar Mills                            const boost::system::error_code ec,
2001*42cbe538SGunnar Mills                            const std::variant<uint32_t>& deratingFactor) {
2002*42cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2003*42cbe538SGunnar Mills         if (ec)
2004*42cbe538SGunnar Mills         {
2005*42cbe538SGunnar Mills             BMCWEB_LOG_ERROR
2006*42cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
2007*42cbe538SGunnar Mills             messages::internalError(sensorsAsyncResp->res);
2008*42cbe538SGunnar Mills             return;
2009*42cbe538SGunnar Mills         }
2010*42cbe538SGunnar Mills 
2011*42cbe538SGunnar Mills         const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
2012*42cbe538SGunnar Mills         if (value != nullptr)
2013*42cbe538SGunnar Mills         {
2014*42cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
2015*42cbe538SGunnar Mills             // Store value in Power Supply Inventory Items
2016*42cbe538SGunnar Mills             for (InventoryItem& inventoryItem : *inventoryItems)
2017*42cbe538SGunnar Mills             {
2018*42cbe538SGunnar Mills                 if (inventoryItem.isPowerSupply == true)
2019*42cbe538SGunnar Mills                 {
2020*42cbe538SGunnar Mills                     inventoryItem.powerSupplyEfficiencyPercent =
2021*42cbe538SGunnar Mills                         static_cast<int>(*value);
2022*42cbe538SGunnar Mills                 }
2023*42cbe538SGunnar Mills             }
2024*42cbe538SGunnar Mills         }
2025*42cbe538SGunnar Mills         else
2026*42cbe538SGunnar Mills         {
2027*42cbe538SGunnar Mills             BMCWEB_LOG_DEBUG
2028*42cbe538SGunnar Mills                 << "Failed to find EfficiencyPercent value for PowerSupplies";
2029*42cbe538SGunnar Mills         }
2030*42cbe538SGunnar Mills 
2031*42cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2032*42cbe538SGunnar Mills         callback(inventoryItems);
2033*42cbe538SGunnar Mills     };
2034*42cbe538SGunnar Mills 
2035*42cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
2036*42cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
2037*42cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
2038*42cbe538SGunnar Mills         std::move(respHandler), psAttributesConnection, psAttributesPath,
2039*42cbe538SGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
2040*42cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
2041*42cbe538SGunnar Mills 
2042*42cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2043*42cbe538SGunnar Mills }
2044*42cbe538SGunnar Mills 
2045*42cbe538SGunnar Mills /**
2046*42cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2047*42cbe538SGunnar Mills  *
2048*42cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
2049*42cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
2050*42cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
2051*42cbe538SGunnar Mills  * item.
2052*42cbe538SGunnar Mills  *
2053*42cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
2054*42cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2055*42cbe538SGunnar Mills  *
2056*42cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
2057*42cbe538SGunnar Mills  * when information has been obtained.
2058*42cbe538SGunnar Mills  *
2059*42cbe538SGunnar Mills  * The callback must have the following signature:
2060*42cbe538SGunnar Mills  *   @code
2061*42cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2062*42cbe538SGunnar Mills  *   @endcode
2063*42cbe538SGunnar Mills  *
2064*42cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
2065*42cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
2066*42cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
2067*42cbe538SGunnar Mills  */
2068*42cbe538SGunnar Mills template <typename Callback>
2069*42cbe538SGunnar Mills void getPowerSupplyAttributes(
2070*42cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2071*42cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2072*42cbe538SGunnar Mills     Callback&& callback)
2073*42cbe538SGunnar Mills {
2074*42cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2075*42cbe538SGunnar Mills 
2076*42cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2077*42cbe538SGunnar Mills     if (sensorsAsyncResp->chassisSubNode != "Power")
2078*42cbe538SGunnar Mills     {
2079*42cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2080*42cbe538SGunnar Mills         callback(inventoryItems);
2081*42cbe538SGunnar Mills         return;
2082*42cbe538SGunnar Mills     }
2083*42cbe538SGunnar Mills 
2084*42cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
2085*42cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2086*42cbe538SGunnar Mills 
2087*42cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2088*42cbe538SGunnar Mills     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2089*42cbe538SGunnar Mills                         inventoryItems](const boost::system::error_code ec,
2090*42cbe538SGunnar Mills                                         const GetSubTreeType& subtree) {
2091*42cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2092*42cbe538SGunnar Mills         if (ec)
2093*42cbe538SGunnar Mills         {
2094*42cbe538SGunnar Mills             messages::internalError(sensorsAsyncResp->res);
2095*42cbe538SGunnar Mills             BMCWEB_LOG_ERROR
2096*42cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2097*42cbe538SGunnar Mills             return;
2098*42cbe538SGunnar Mills         }
2099*42cbe538SGunnar Mills         if (subtree.size() == 0)
2100*42cbe538SGunnar Mills         {
2101*42cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2102*42cbe538SGunnar Mills             callback(inventoryItems);
2103*42cbe538SGunnar Mills             return;
2104*42cbe538SGunnar Mills         }
2105*42cbe538SGunnar Mills 
2106*42cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
2107*42cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
2108*42cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
2109*42cbe538SGunnar Mills         boost::container::flat_map<std::string, std::string>
2110*42cbe538SGunnar Mills             psAttributesConnections;
2111*42cbe538SGunnar Mills 
2112*42cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
2113*42cbe538SGunnar Mills         {
2114*42cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2115*42cbe538SGunnar Mills             callback(inventoryItems);
2116*42cbe538SGunnar Mills             return;
2117*42cbe538SGunnar Mills         }
2118*42cbe538SGunnar Mills 
2119*42cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
2120*42cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
2121*42cbe538SGunnar Mills 
2122*42cbe538SGunnar Mills         if (connection.empty())
2123*42cbe538SGunnar Mills         {
2124*42cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2125*42cbe538SGunnar Mills             callback(inventoryItems);
2126*42cbe538SGunnar Mills             return;
2127*42cbe538SGunnar Mills         }
2128*42cbe538SGunnar Mills 
2129*42cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
2130*42cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2131*42cbe538SGunnar Mills                          << connection;
2132*42cbe538SGunnar Mills 
2133*42cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2134*42cbe538SGunnar Mills                                      psAttributesConnections,
2135*42cbe538SGunnar Mills                                      std::move(callback));
2136*42cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2137*42cbe538SGunnar Mills     };
2138*42cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
2139*42cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
2140*42cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2141*42cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
2142*42cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2143*42cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
2144*42cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2145*42cbe538SGunnar Mills }
2146*42cbe538SGunnar Mills 
2147*42cbe538SGunnar Mills /**
2148adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
21498fb49dd6SShawn McCarney  *
21508fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2151adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
21528fb49dd6SShawn McCarney  *
2153adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2154adc4f0dbSShawn McCarney  * response.
21558fb49dd6SShawn McCarney  *
2156adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2157adc4f0dbSShawn McCarney  * inventory items have been obtained.
2158adc4f0dbSShawn McCarney  *
2159adc4f0dbSShawn McCarney  * The callback must have the following signature:
2160adc4f0dbSShawn McCarney  *   @code
2161adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2162adc4f0dbSShawn McCarney  *   @endcode
21638fb49dd6SShawn McCarney  *
21648fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
21658fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
21668fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
21678fb49dd6SShawn McCarney  * implements ObjectManager.
2168adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
21698fb49dd6SShawn McCarney  */
2170adc4f0dbSShawn McCarney template <typename Callback>
2171adc4f0dbSShawn McCarney static void getInventoryItems(
21728fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
21738fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
21748fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2175adc4f0dbSShawn McCarney         objectMgrPaths,
2176adc4f0dbSShawn McCarney     Callback&& callback)
21778fb49dd6SShawn McCarney {
2178adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2179adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2180adc4f0dbSShawn McCarney         [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2181adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2182adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
21838fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2184adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2185adc4f0dbSShawn McCarney                  callback{std::move(callback)}](
21868fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
21878fb49dd6SShawn McCarney                         invConnections) {
21888fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2189d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2190d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2191d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2192d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
2193*42cbe538SGunnar Mills 
2194*42cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
2195*42cbe538SGunnar Mills                                                        inventoryItems,
2196*42cbe538SGunnar Mills                                                        callback{std::move(
2197*42cbe538SGunnar Mills                                                            callback)}]() {
2198*42cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2199*42cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
2200*42cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
2201*42cbe538SGunnar Mills                                                          inventoryItems,
2202*42cbe538SGunnar Mills                                                          std::move(callback));
2203*42cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2204*42cbe538SGunnar Mills                             };
2205*42cbe538SGunnar Mills 
2206d500549bSAnthony Wilson                             // Find led connections and get the data
2207d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
2208*42cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2209d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2210d500549bSAnthony Wilson                         };
22118fb49dd6SShawn McCarney 
2212adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2213adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2214adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2215d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
22168fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
22178fb49dd6SShawn McCarney                 };
22188fb49dd6SShawn McCarney 
2219adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
22208fb49dd6SShawn McCarney             getInventoryItemsConnections(
2221adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
22228fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2223adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
22248fb49dd6SShawn McCarney         };
22258fb49dd6SShawn McCarney 
2226adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2227adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2228adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2229adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2230adc4f0dbSShawn McCarney }
2231adc4f0dbSShawn McCarney 
2232adc4f0dbSShawn McCarney /**
2233adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2234adc4f0dbSShawn McCarney  *
2235adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2236adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2237adc4f0dbSShawn McCarney  * array.
2238adc4f0dbSShawn McCarney  *
2239adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2240adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2241adc4f0dbSShawn McCarney  * object.
2242adc4f0dbSShawn McCarney  *
2243adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2244adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2245adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2246adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2247adc4f0dbSShawn McCarney  */
2248adc4f0dbSShawn McCarney static nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2249adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2250adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2251adc4f0dbSShawn McCarney {
2252adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2253adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2254adc4f0dbSShawn McCarney     {
2255adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2256adc4f0dbSShawn McCarney         {
2257adc4f0dbSShawn McCarney             return powerSupply;
2258adc4f0dbSShawn McCarney         }
2259adc4f0dbSShawn McCarney     }
2260adc4f0dbSShawn McCarney 
2261adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2262adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2263adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2264adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2265adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2266adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2267adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2268adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2269adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2270adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2271adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2272d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2273adc4f0dbSShawn McCarney 
2274*42cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2275*42cbe538SGunnar Mills     {
2276*42cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
2277*42cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
2278*42cbe538SGunnar Mills     }
2279*42cbe538SGunnar Mills 
2280*42cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2281adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2282adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2283adc4f0dbSShawn McCarney 
2284adc4f0dbSShawn McCarney     return powerSupply;
22858fb49dd6SShawn McCarney }
22868fb49dd6SShawn McCarney 
22878fb49dd6SShawn McCarney /**
2288de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2289de629b6eSShawn McCarney  *
2290de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2291de629b6eSShawn McCarney  *
2292de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2293de629b6eSShawn McCarney  * information has been obtained.
2294de629b6eSShawn McCarney  *
2295adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2296de629b6eSShawn McCarney  *
2297de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2298de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2299de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2300de629b6eSShawn McCarney  *
2301de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2302de629b6eSShawn McCarney  *
2303de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2304de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2305de629b6eSShawn McCarney  *
2306adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2307adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2308adc4f0dbSShawn McCarney  *
2309de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2310adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2311de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2312de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2313de629b6eSShawn McCarney  * implements ObjectManager.
2314adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2315de629b6eSShawn McCarney  */
2316de629b6eSShawn McCarney void getSensorData(
2317de629b6eSShawn McCarney     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
231849c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2319de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
23208fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2321adc4f0dbSShawn McCarney         objectMgrPaths,
2322adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2323de629b6eSShawn McCarney {
2324de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2325de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2326de629b6eSShawn McCarney     for (const std::string& connection : connections)
2327de629b6eSShawn McCarney     {
2328de629b6eSShawn McCarney         // Response handler to process managed objects
23298fb49dd6SShawn McCarney         auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames,
2330adc4f0dbSShawn McCarney                                     inventoryItems](
2331de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2332de629b6eSShawn McCarney                                        ManagedObjectsVectorType& resp) {
2333de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2334de629b6eSShawn McCarney             if (ec)
2335de629b6eSShawn McCarney             {
2336de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
2337de629b6eSShawn McCarney                 messages::internalError(SensorsAsyncResp->res);
2338de629b6eSShawn McCarney                 return;
2339de629b6eSShawn McCarney             }
2340de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2341de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2342de629b6eSShawn McCarney             {
2343de629b6eSShawn McCarney                 const std::string& objPath =
2344de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2345de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2346de629b6eSShawn McCarney                                  << objPath;
2347de629b6eSShawn McCarney 
2348de629b6eSShawn McCarney                 std::vector<std::string> split;
2349de629b6eSShawn McCarney                 // Reserve space for
2350de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2351de629b6eSShawn McCarney                 split.reserve(6);
2352de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2353de629b6eSShawn McCarney                 if (split.size() < 6)
2354de629b6eSShawn McCarney                 {
2355de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2356de629b6eSShawn McCarney                                      << objPath;
2357de629b6eSShawn McCarney                     continue;
2358de629b6eSShawn McCarney                 }
2359de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2360de629b6eSShawn McCarney                 // string at the beginning
2361de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2362de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2363de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2364de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
236549c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2366de629b6eSShawn McCarney                 {
2367de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2368de629b6eSShawn McCarney                     continue;
2369de629b6eSShawn McCarney                 }
2370de629b6eSShawn McCarney 
2371adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2372adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2373adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2374adc4f0dbSShawn McCarney 
237595a3ecadSAnthony Wilson                 const std::string& sensorSchema =
237695a3ecadSAnthony Wilson                     SensorsAsyncResp->chassisSubNode;
237795a3ecadSAnthony Wilson 
237895a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
237995a3ecadSAnthony Wilson 
238095a3ecadSAnthony Wilson                 if (sensorSchema == "Sensors")
238195a3ecadSAnthony Wilson                 {
238295a3ecadSAnthony Wilson                     SensorsAsyncResp->res.jsonValue["@odata.id"] =
238395a3ecadSAnthony Wilson                         "/redfish/v1/Chassis/" + SensorsAsyncResp->chassisId +
238495a3ecadSAnthony Wilson                         "/" + SensorsAsyncResp->chassisSubNode + "/" +
238595a3ecadSAnthony Wilson                         sensorName;
238695a3ecadSAnthony Wilson                     sensorJson = &(SensorsAsyncResp->res.jsonValue);
238795a3ecadSAnthony Wilson                 }
238895a3ecadSAnthony Wilson                 else
238995a3ecadSAnthony Wilson                 {
2390271584abSEd Tanous                     std::string fieldName;
2391de629b6eSShawn McCarney                     if (sensorType == "temperature")
2392de629b6eSShawn McCarney                     {
2393de629b6eSShawn McCarney                         fieldName = "Temperatures";
2394de629b6eSShawn McCarney                     }
2395de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2396de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2397de629b6eSShawn McCarney                     {
2398de629b6eSShawn McCarney                         fieldName = "Fans";
2399de629b6eSShawn McCarney                     }
2400de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2401de629b6eSShawn McCarney                     {
2402de629b6eSShawn McCarney                         fieldName = "Voltages";
2403de629b6eSShawn McCarney                     }
2404de629b6eSShawn McCarney                     else if (sensorType == "power")
2405de629b6eSShawn McCarney                     {
2406028f7ebcSEddie James                         if (!sensorName.compare("total_power"))
2407028f7ebcSEddie James                         {
2408028f7ebcSEddie James                             fieldName = "PowerControl";
2409028f7ebcSEddie James                         }
2410adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2411adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2412028f7ebcSEddie James                         {
2413de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2414de629b6eSShawn McCarney                         }
2415adc4f0dbSShawn McCarney                         else
2416adc4f0dbSShawn McCarney                         {
2417adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2418adc4f0dbSShawn McCarney                             continue;
2419adc4f0dbSShawn McCarney                         }
2420028f7ebcSEddie James                     }
2421de629b6eSShawn McCarney                     else
2422de629b6eSShawn McCarney                     {
2423de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2424de629b6eSShawn McCarney                                          << sensorType;
2425de629b6eSShawn McCarney                         continue;
2426de629b6eSShawn McCarney                     }
2427de629b6eSShawn McCarney 
2428de629b6eSShawn McCarney                     nlohmann::json& tempArray =
2429de629b6eSShawn McCarney                         SensorsAsyncResp->res.jsonValue[fieldName];
2430adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
243149c53ac9SJohnathan Mantey                     {
2432adc4f0dbSShawn McCarney                         if (tempArray.empty())
24337ab06f49SGunnar Mills                         {
243495a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
243595a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
243695a3ecadSAnthony Wilson                             // naming in power.hpp.
24377ab06f49SGunnar Mills                             tempArray.push_back(
2438adc4f0dbSShawn McCarney                                 {{"@odata.id",
2439adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
24407ab06f49SGunnar Mills                                       SensorsAsyncResp->chassisId + "/" +
2441adc4f0dbSShawn McCarney                                       SensorsAsyncResp->chassisSubNode + "#/" +
2442adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2443adc4f0dbSShawn McCarney                         }
2444adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2445adc4f0dbSShawn McCarney                     }
2446adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2447adc4f0dbSShawn McCarney                     {
2448adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2449adc4f0dbSShawn McCarney                         {
2450adc4f0dbSShawn McCarney                             sensorJson =
2451adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
2452adc4f0dbSShawn McCarney                                                  SensorsAsyncResp->chassisId));
2453adc4f0dbSShawn McCarney                         }
245449c53ac9SJohnathan Mantey                     }
245549c53ac9SJohnathan Mantey                     else
245649c53ac9SJohnathan Mantey                     {
2457de629b6eSShawn McCarney                         tempArray.push_back(
245895a3ecadSAnthony Wilson                             {{"@odata.id",
245995a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
246049c53ac9SJohnathan Mantey                                   SensorsAsyncResp->chassisId + "/" +
246195a3ecadSAnthony Wilson                                   SensorsAsyncResp->chassisSubNode + "#/" +
246295a3ecadSAnthony Wilson                                   fieldName + "/"}});
2463adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
246449c53ac9SJohnathan Mantey                     }
246595a3ecadSAnthony Wilson                 }
2466de629b6eSShawn McCarney 
2467adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2468adc4f0dbSShawn McCarney                 {
2469de629b6eSShawn McCarney                     objectInterfacesToJson(sensorName, sensorType,
247095a3ecadSAnthony Wilson                                            SensorsAsyncResp->chassisSubNode,
2471adc4f0dbSShawn McCarney                                            objDictEntry.second, *sensorJson,
2472adc4f0dbSShawn McCarney                                            inventoryItem);
2473adc4f0dbSShawn McCarney                 }
2474de629b6eSShawn McCarney             }
247549c53ac9SJohnathan Mantey             if (SensorsAsyncResp.use_count() == 1)
247649c53ac9SJohnathan Mantey             {
247749c53ac9SJohnathan Mantey                 sortJSONResponse(SensorsAsyncResp);
247849c53ac9SJohnathan Mantey                 if (SensorsAsyncResp->chassisSubNode == "Thermal")
24798bd25ccdSJames Feist                 {
24808bd25ccdSJames Feist                     populateFanRedundancy(SensorsAsyncResp);
24818bd25ccdSJames Feist                 }
248249c53ac9SJohnathan Mantey             }
2483de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2484de629b6eSShawn McCarney         };
2485de629b6eSShawn McCarney 
2486de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2487de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
24888fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2489de629b6eSShawn McCarney         const std::string& objectMgrPath =
24908fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2491de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2492de629b6eSShawn McCarney                          << objectMgrPath;
2493de629b6eSShawn McCarney 
2494de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2495de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2496de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
2497de629b6eSShawn McCarney     };
2498de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2499de629b6eSShawn McCarney }
2500de629b6eSShawn McCarney 
250195a3ecadSAnthony Wilson void processSensorList(
250295a3ecadSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
250395a3ecadSAnthony Wilson     std::shared_ptr<boost::container::flat_set<std::string>> sensorNames)
25041abe55efSEd Tanous {
250595a3ecadSAnthony Wilson     auto getConnectionCb =
250695a3ecadSAnthony Wilson         [SensorsAsyncResp, sensorNames](
250795a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
250855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2509de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
251049c53ac9SJohnathan Mantey                 [SensorsAsyncResp, sensorNames, connections](
251195a3ecadSAnthony Wilson                     std::shared_ptr<
251295a3ecadSAnthony Wilson                         boost::container::flat_map<std::string, std::string>>
25138fb49dd6SShawn McCarney                         objectMgrPaths) {
2514de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2515adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
2516adc4f0dbSShawn McCarney                         [SensorsAsyncResp, sensorNames, connections,
2517adc4f0dbSShawn McCarney                          objectMgrPaths](
2518adc4f0dbSShawn McCarney                             std::shared_ptr<std::vector<InventoryItem>>
2519adc4f0dbSShawn McCarney                                 inventoryItems) {
2520adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
252149c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
2522de629b6eSShawn McCarney                             getSensorData(SensorsAsyncResp, sensorNames,
2523adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2524adc4f0dbSShawn McCarney                                           inventoryItems);
2525adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2526adc4f0dbSShawn McCarney                         };
2527adc4f0dbSShawn McCarney 
2528adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
2529adc4f0dbSShawn McCarney                     getInventoryItems(SensorsAsyncResp, sensorNames,
2530adc4f0dbSShawn McCarney                                       objectMgrPaths,
2531adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2532adc4f0dbSShawn McCarney 
2533de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
253408777fb0SLewanczyk, Dawid                 };
2535de629b6eSShawn McCarney 
253649c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
253749c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
2538de629b6eSShawn McCarney             getObjectManagerPaths(SensorsAsyncResp,
2539de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
254055c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
254108777fb0SLewanczyk, Dawid         };
2542de629b6eSShawn McCarney 
2543de629b6eSShawn McCarney     // Get set of connections that provide sensor values
254495a3ecadSAnthony Wilson     getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb));
254595a3ecadSAnthony Wilson }
254695a3ecadSAnthony Wilson 
254795a3ecadSAnthony Wilson /**
254895a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
254995a3ecadSAnthony Wilson  *        chassis.
255095a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
255195a3ecadSAnthony Wilson  */
255295a3ecadSAnthony Wilson void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
255395a3ecadSAnthony Wilson {
255495a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
255595a3ecadSAnthony Wilson     auto getChassisCb =
255695a3ecadSAnthony Wilson         [SensorsAsyncResp](
255795a3ecadSAnthony Wilson             std::shared_ptr<boost::container::flat_set<std::string>>
255895a3ecadSAnthony Wilson                 sensorNames) {
255995a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
256095a3ecadSAnthony Wilson             processSensorList(SensorsAsyncResp, sensorNames);
256155c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
256208777fb0SLewanczyk, Dawid         };
25634f9a2130SJennifer Lee     SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
256408777fb0SLewanczyk, Dawid 
256526f03899SShawn McCarney     // Get set of sensors in chassis
2566588c3f0dSKowalski, Kamil     getChassis(SensorsAsyncResp, std::move(getChassisCb));
256755c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2568271584abSEd Tanous }
256908777fb0SLewanczyk, Dawid 
2570413961deSRichard Marian Thomaiyar /**
257149c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
257249c53ac9SJohnathan Mantey  * the chassis node
257349c53ac9SJohnathan Mantey  *
257449c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
257549c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
257649c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
257749c53ac9SJohnathan Mantey  *                         repeated calls to this function
257849c53ac9SJohnathan Mantey  */
257949c53ac9SJohnathan Mantey bool findSensorNameUsingSensorPath(
25800a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
258149c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
258249c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
258349c53ac9SJohnathan Mantey {
25840a86febdSRichard Marian Thomaiyar     for (std::string_view chassisSensor : sensorsList)
258549c53ac9SJohnathan Mantey     {
25860a86febdSRichard Marian Thomaiyar         std::size_t pos = chassisSensor.rfind("/");
25870a86febdSRichard Marian Thomaiyar         if (pos >= (chassisSensor.size() - 1))
258849c53ac9SJohnathan Mantey         {
258949c53ac9SJohnathan Mantey             continue;
259049c53ac9SJohnathan Mantey         }
25910a86febdSRichard Marian Thomaiyar         std::string_view thisSensorName = chassisSensor.substr(pos + 1);
259249c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
259349c53ac9SJohnathan Mantey         {
259449c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
259549c53ac9SJohnathan Mantey             return true;
259649c53ac9SJohnathan Mantey         }
259749c53ac9SJohnathan Mantey     }
259849c53ac9SJohnathan Mantey     return false;
259949c53ac9SJohnathan Mantey }
260049c53ac9SJohnathan Mantey 
260149c53ac9SJohnathan Mantey /**
2602413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2603413961deSRichard Marian Thomaiyar  *
2604413961deSRichard Marian Thomaiyar  * @param res   response object
26054bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2606413961deSRichard Marian Thomaiyar  * @param typeList   TypeList of sensors for the resource queried
2607413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2608413961deSRichard Marian Thomaiyar  */
26094bb3dc34SCarol Wang void setSensorOverride(
26104bb3dc34SCarol Wang     std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
26114bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
26124bb3dc34SCarol Wang         allCollections,
26134bb3dc34SCarol Wang     const std::string& chassisName, const std::vector<const char*> typeList)
2614413961deSRichard Marian Thomaiyar {
26154bb3dc34SCarol Wang     BMCWEB_LOG_INFO << "setSensorOverride for subNode"
26164bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2617413961deSRichard Marian Thomaiyar 
2618f65af9e8SRichard Marian Thomaiyar     const char* propertyValueName;
2619f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2620413961deSRichard Marian Thomaiyar     std::string memberId;
2621413961deSRichard Marian Thomaiyar     double value;
2622f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2623f65af9e8SRichard Marian Thomaiyar     {
2624f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2625f65af9e8SRichard Marian Thomaiyar         {
2626f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2627f65af9e8SRichard Marian Thomaiyar         }
2628f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2629f65af9e8SRichard Marian Thomaiyar         {
2630f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2631f65af9e8SRichard Marian Thomaiyar         }
2632f65af9e8SRichard Marian Thomaiyar         else
2633f65af9e8SRichard Marian Thomaiyar         {
2634f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2635f65af9e8SRichard Marian Thomaiyar         }
2636f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2637f65af9e8SRichard Marian Thomaiyar         {
26384bb3dc34SCarol Wang             if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId",
26394bb3dc34SCarol Wang                                      memberId, propertyValueName, value))
2640413961deSRichard Marian Thomaiyar             {
2641413961deSRichard Marian Thomaiyar                 return;
2642413961deSRichard Marian Thomaiyar             }
2643f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2644f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2645f65af9e8SRichard Marian Thomaiyar         }
2646f65af9e8SRichard Marian Thomaiyar     }
26474bb3dc34SCarol Wang 
264849c53ac9SJohnathan Mantey     auto getChassisSensorListCb = [sensorAsyncResp,
264949c53ac9SJohnathan Mantey                                    overrideMap](const std::shared_ptr<
265049c53ac9SJohnathan Mantey                                                 boost::container::flat_set<
265149c53ac9SJohnathan Mantey                                                     std::string>>
265249c53ac9SJohnathan Mantey                                                     sensorsList) {
265349c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
265449c53ac9SJohnathan Mantey         // chassis node
265549c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
265649c53ac9SJohnathan Mantey             sensorNames =
265749c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2658f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2659413961deSRichard Marian Thomaiyar         {
2660f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
266149c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
266249c53ac9SJohnathan Mantey                                                *sensorNames))
2663f65af9e8SRichard Marian Thomaiyar             {
2664f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
2665413961deSRichard Marian Thomaiyar                 messages::resourceNotFound(sensorAsyncResp->res,
2666f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2667413961deSRichard Marian Thomaiyar                 return;
2668413961deSRichard Marian Thomaiyar             }
2669f65af9e8SRichard Marian Thomaiyar         }
2670413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2671413961deSRichard Marian Thomaiyar         auto getObjectsWithConnectionCb =
2672f65af9e8SRichard Marian Thomaiyar             [sensorAsyncResp, overrideMap](
2673413961deSRichard Marian Thomaiyar                 const boost::container::flat_set<std::string>& connections,
2674413961deSRichard Marian Thomaiyar                 const std::set<std::pair<std::string, std::string>>&
2675413961deSRichard Marian Thomaiyar                     objectsWithConnection) {
2676f65af9e8SRichard Marian Thomaiyar                 if (objectsWithConnection.size() != overrideMap.size())
2677413961deSRichard Marian Thomaiyar                 {
2678413961deSRichard Marian Thomaiyar                     BMCWEB_LOG_INFO
2679f65af9e8SRichard Marian Thomaiyar                         << "Unable to find all objects with proper connection "
2680f65af9e8SRichard Marian Thomaiyar                         << objectsWithConnection.size() << " requested "
2681f65af9e8SRichard Marian Thomaiyar                         << overrideMap.size() << "\n";
2682413961deSRichard Marian Thomaiyar                     messages::resourceNotFound(
2683413961deSRichard Marian Thomaiyar                         sensorAsyncResp->res,
2684413961deSRichard Marian Thomaiyar                         sensorAsyncResp->chassisSubNode == "Thermal"
2685413961deSRichard Marian Thomaiyar                             ? "Temperatures"
2686413961deSRichard Marian Thomaiyar                             : "Voltages",
2687f65af9e8SRichard Marian Thomaiyar                         "Count");
2688f65af9e8SRichard Marian Thomaiyar                     return;
2689f65af9e8SRichard Marian Thomaiyar                 }
2690f65af9e8SRichard Marian Thomaiyar                 for (const auto& item : objectsWithConnection)
2691f65af9e8SRichard Marian Thomaiyar                 {
2692f65af9e8SRichard Marian Thomaiyar 
2693f65af9e8SRichard Marian Thomaiyar                     auto lastPos = item.first.rfind('/');
2694f65af9e8SRichard Marian Thomaiyar                     if (lastPos == std::string::npos)
2695f65af9e8SRichard Marian Thomaiyar                     {
2696f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2697f65af9e8SRichard Marian Thomaiyar                         return;
2698f65af9e8SRichard Marian Thomaiyar                     }
2699f65af9e8SRichard Marian Thomaiyar                     std::string sensorName = item.first.substr(lastPos + 1);
2700f65af9e8SRichard Marian Thomaiyar 
2701f65af9e8SRichard Marian Thomaiyar                     const auto& iterator = overrideMap.find(sensorName);
2702f65af9e8SRichard Marian Thomaiyar                     if (iterator == overrideMap.end())
2703f65af9e8SRichard Marian Thomaiyar                     {
2704f65af9e8SRichard Marian Thomaiyar                         BMCWEB_LOG_INFO << "Unable to find sensor object"
2705f65af9e8SRichard Marian Thomaiyar                                         << item.first << "\n";
2706f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2707413961deSRichard Marian Thomaiyar                         return;
2708413961deSRichard Marian Thomaiyar                     }
2709413961deSRichard Marian Thomaiyar                     crow::connections::systemBus->async_method_call(
2710f65af9e8SRichard Marian Thomaiyar                         [sensorAsyncResp](const boost::system::error_code ec) {
2711413961deSRichard Marian Thomaiyar                             if (ec)
2712413961deSRichard Marian Thomaiyar                             {
2713413961deSRichard Marian Thomaiyar                                 BMCWEB_LOG_DEBUG
2714f65af9e8SRichard Marian Thomaiyar                                     << "setOverrideValueStatus DBUS error: "
2715413961deSRichard Marian Thomaiyar                                     << ec;
2716413961deSRichard Marian Thomaiyar                                 messages::internalError(sensorAsyncResp->res);
2717413961deSRichard Marian Thomaiyar                                 return;
2718413961deSRichard Marian Thomaiyar                             }
2719413961deSRichard Marian Thomaiyar                         },
2720f65af9e8SRichard Marian Thomaiyar                         item.second, item.first,
2721413961deSRichard Marian Thomaiyar                         "org.freedesktop.DBus.Properties", "Set",
2722413961deSRichard Marian Thomaiyar                         "xyz.openbmc_project.Sensor.Value", "Value",
2723f65af9e8SRichard Marian Thomaiyar                         sdbusplus::message::variant<double>(
2724f65af9e8SRichard Marian Thomaiyar                             iterator->second.first));
2725f65af9e8SRichard Marian Thomaiyar                 }
2726413961deSRichard Marian Thomaiyar             };
2727413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2728413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2729413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2730413961deSRichard Marian Thomaiyar     };
2731413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2732413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2733413961deSRichard Marian Thomaiyar }
2734413961deSRichard Marian Thomaiyar 
273595a3ecadSAnthony Wilson class SensorCollection : public Node
273695a3ecadSAnthony Wilson {
273795a3ecadSAnthony Wilson   public:
273895a3ecadSAnthony Wilson     SensorCollection(CrowApp& app) :
273995a3ecadSAnthony Wilson         Node(app, "/redfish/v1/Chassis/<str>/Sensors", std::string())
274095a3ecadSAnthony Wilson     {
274195a3ecadSAnthony Wilson         entityPrivileges = {
274295a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
274395a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
274495a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
274595a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
274695a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
274795a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
274895a3ecadSAnthony Wilson     }
274995a3ecadSAnthony Wilson 
275095a3ecadSAnthony Wilson   private:
275195a3ecadSAnthony Wilson     std::vector<const char*> typeList = {
275295a3ecadSAnthony Wilson         "/xyz/openbmc_project/sensors/power",
275395a3ecadSAnthony Wilson         "/xyz/openbmc_project/sensors/current"};
275495a3ecadSAnthony Wilson     void doGet(crow::Response& res, const crow::Request& req,
275595a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
275695a3ecadSAnthony Wilson     {
275795a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
275895a3ecadSAnthony Wilson         if (params.size() != 1)
275995a3ecadSAnthony Wilson         {
276095a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1";
276195a3ecadSAnthony Wilson             messages::internalError(res);
276295a3ecadSAnthony Wilson             res.end();
276395a3ecadSAnthony Wilson             return;
276495a3ecadSAnthony Wilson         }
276595a3ecadSAnthony Wilson 
276695a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
276795a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
276895a3ecadSAnthony Wilson             std::make_shared<SensorsAsyncResp>(res, chassisId, typeList,
276995a3ecadSAnthony Wilson                                                "Sensors");
277095a3ecadSAnthony Wilson 
277195a3ecadSAnthony Wilson         auto getChassisCb =
277295a3ecadSAnthony Wilson             [asyncResp](std::shared_ptr<boost::container::flat_set<std::string>>
277395a3ecadSAnthony Wilson                             sensorNames) {
277495a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb enter";
277595a3ecadSAnthony Wilson 
277695a3ecadSAnthony Wilson                 nlohmann::json& entriesArray =
277795a3ecadSAnthony Wilson                     asyncResp->res.jsonValue["Members"];
277895a3ecadSAnthony Wilson                 for (auto& sensor : *sensorNames)
277995a3ecadSAnthony Wilson                 {
278095a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
278195a3ecadSAnthony Wilson 
278295a3ecadSAnthony Wilson                     std::size_t lastPos = sensor.rfind("/");
278395a3ecadSAnthony Wilson                     if (lastPos == std::string::npos ||
278495a3ecadSAnthony Wilson                         lastPos + 1 >= sensor.size())
278595a3ecadSAnthony Wilson                     {
278695a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
278795a3ecadSAnthony Wilson                         messages::internalError(asyncResp->res);
278895a3ecadSAnthony Wilson                         return;
278995a3ecadSAnthony Wilson                     }
279095a3ecadSAnthony Wilson                     std::string sensorName = sensor.substr(lastPos + 1);
279195a3ecadSAnthony Wilson                     entriesArray.push_back(
279295a3ecadSAnthony Wilson                         {{"@odata.id",
279395a3ecadSAnthony Wilson                           "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
279495a3ecadSAnthony Wilson                               asyncResp->chassisSubNode + "/" + sensorName}});
279595a3ecadSAnthony Wilson                 }
279695a3ecadSAnthony Wilson 
279795a3ecadSAnthony Wilson                 asyncResp->res.jsonValue["Members@odata.count"] =
279895a3ecadSAnthony Wilson                     entriesArray.size();
279995a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb exit";
280095a3ecadSAnthony Wilson             };
280195a3ecadSAnthony Wilson 
280295a3ecadSAnthony Wilson         // Get set of sensors in chassis
280395a3ecadSAnthony Wilson         getChassis(asyncResp, std::move(getChassisCb));
280495a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
280595a3ecadSAnthony Wilson     }
280695a3ecadSAnthony Wilson };
280795a3ecadSAnthony Wilson 
280895a3ecadSAnthony Wilson class Sensor : public Node
280995a3ecadSAnthony Wilson {
281095a3ecadSAnthony Wilson   public:
281195a3ecadSAnthony Wilson     Sensor(CrowApp& app) :
281295a3ecadSAnthony Wilson         Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(),
281395a3ecadSAnthony Wilson              std::string())
281495a3ecadSAnthony Wilson     {
281595a3ecadSAnthony Wilson         entityPrivileges = {
281695a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
281795a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
281895a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
281995a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
282095a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
282195a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
282295a3ecadSAnthony Wilson     }
282395a3ecadSAnthony Wilson 
282495a3ecadSAnthony Wilson   private:
282595a3ecadSAnthony Wilson     void doGet(crow::Response& res, const crow::Request& req,
282695a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
282795a3ecadSAnthony Wilson     {
282895a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "Sensor doGet enter";
282995a3ecadSAnthony Wilson         if (params.size() != 2)
283095a3ecadSAnthony Wilson         {
283195a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2";
283295a3ecadSAnthony Wilson             messages::internalError(res);
283395a3ecadSAnthony Wilson             res.end();
283495a3ecadSAnthony Wilson             return;
283595a3ecadSAnthony Wilson         }
283695a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
283795a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
283895a3ecadSAnthony Wilson             std::make_shared<SensorsAsyncResp>(
283995a3ecadSAnthony Wilson                 res, chassisId, std::vector<const char*>(), "Sensors");
284095a3ecadSAnthony Wilson 
284195a3ecadSAnthony Wilson         const std::string& sensorName = params[1];
284295a3ecadSAnthony Wilson         const std::array<const char*, 1> interfaces = {
284395a3ecadSAnthony Wilson             "xyz.openbmc_project.Sensor.Value"};
284495a3ecadSAnthony Wilson 
284595a3ecadSAnthony Wilson         // Get a list of all of the sensors that implement Sensor.Value
284695a3ecadSAnthony Wilson         // and get the path and service name associated with the sensor
284795a3ecadSAnthony Wilson         crow::connections::systemBus->async_method_call(
284895a3ecadSAnthony Wilson             [asyncResp, sensorName](const boost::system::error_code ec,
284995a3ecadSAnthony Wilson                                     const GetSubTreeType& subtree) {
285095a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 enter";
285195a3ecadSAnthony Wilson                 if (ec)
285295a3ecadSAnthony Wilson                 {
285395a3ecadSAnthony Wilson                     messages::internalError(asyncResp->res);
285495a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
285595a3ecadSAnthony Wilson                                      << "Dbus error " << ec;
285695a3ecadSAnthony Wilson                     return;
285795a3ecadSAnthony Wilson                 }
285895a3ecadSAnthony Wilson 
285995a3ecadSAnthony Wilson                 GetSubTreeType::const_iterator it = std::find_if(
286095a3ecadSAnthony Wilson                     subtree.begin(), subtree.end(),
286195a3ecadSAnthony Wilson                     [sensorName](
286295a3ecadSAnthony Wilson                         const std::pair<
286395a3ecadSAnthony Wilson                             std::string,
286495a3ecadSAnthony Wilson                             std::vector<std::pair<std::string,
286595a3ecadSAnthony Wilson                                                   std::vector<std::string>>>>&
286695a3ecadSAnthony Wilson                             object) {
286795a3ecadSAnthony Wilson                         std::string_view sensor = object.first;
286895a3ecadSAnthony Wilson                         std::size_t lastPos = sensor.rfind("/");
286995a3ecadSAnthony Wilson                         if (lastPos == std::string::npos ||
287095a3ecadSAnthony Wilson                             lastPos + 1 >= sensor.size())
287195a3ecadSAnthony Wilson                         {
287295a3ecadSAnthony Wilson                             BMCWEB_LOG_ERROR << "Invalid sensor path: "
287395a3ecadSAnthony Wilson                                              << sensor;
287495a3ecadSAnthony Wilson                             return false;
287595a3ecadSAnthony Wilson                         }
287695a3ecadSAnthony Wilson                         std::string_view name = sensor.substr(lastPos + 1);
287795a3ecadSAnthony Wilson 
287895a3ecadSAnthony Wilson                         return name == sensorName;
287995a3ecadSAnthony Wilson                     });
288095a3ecadSAnthony Wilson 
288195a3ecadSAnthony Wilson                 if (it == subtree.end())
288295a3ecadSAnthony Wilson                 {
288395a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Could not find path for sensor: "
288495a3ecadSAnthony Wilson                                      << sensorName;
288595a3ecadSAnthony Wilson                     messages::resourceNotFound(asyncResp->res, "Sensor",
288695a3ecadSAnthony Wilson                                                sensorName);
288795a3ecadSAnthony Wilson                     return;
288895a3ecadSAnthony Wilson                 }
288995a3ecadSAnthony Wilson                 std::string_view sensorPath = (*it).first;
289095a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
289195a3ecadSAnthony Wilson                                  << sensorName << "': " << sensorPath;
289295a3ecadSAnthony Wilson 
289395a3ecadSAnthony Wilson                 const std::shared_ptr<boost::container::flat_set<std::string>>
289495a3ecadSAnthony Wilson                     sensorList = std::make_shared<
289595a3ecadSAnthony Wilson                         boost::container::flat_set<std::string>>();
289695a3ecadSAnthony Wilson 
289795a3ecadSAnthony Wilson                 sensorList->emplace(sensorPath);
289895a3ecadSAnthony Wilson                 processSensorList(asyncResp, sensorList);
289995a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 exit";
290095a3ecadSAnthony Wilson             },
290195a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper",
290295a3ecadSAnthony Wilson             "/xyz/openbmc_project/object_mapper",
290395a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
290495a3ecadSAnthony Wilson             "/xyz/openbmc_project/sensors", 2, interfaces);
290595a3ecadSAnthony Wilson     }
290695a3ecadSAnthony Wilson };
290795a3ecadSAnthony Wilson 
290808777fb0SLewanczyk, Dawid } // namespace redfish
2909