xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 3174e4dfd3185c131a07371b4b5a5b40cf0e0bdb)
108777fb0SLewanczyk, Dawid /*
208777fb0SLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
308777fb0SLewanczyk, Dawid //
408777fb0SLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
508777fb0SLewanczyk, Dawid // you may not use this file except in compliance with the License.
608777fb0SLewanczyk, Dawid // You may obtain a copy of the License at
708777fb0SLewanczyk, Dawid //
808777fb0SLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
908777fb0SLewanczyk, Dawid //
1008777fb0SLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1108777fb0SLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1208777fb0SLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1308777fb0SLewanczyk, Dawid // See the License for the specific language governing permissions and
1408777fb0SLewanczyk, Dawid // limitations under the License.
1508777fb0SLewanczyk, Dawid */
1608777fb0SLewanczyk, Dawid #pragma once
1708777fb0SLewanczyk, Dawid 
1895a3ecadSAnthony Wilson #include "node.hpp"
1995a3ecadSAnthony Wilson 
2008777fb0SLewanczyk, Dawid #include <boost/algorithm/string/predicate.hpp>
2108777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2208777fb0SLewanczyk, Dawid #include <boost/container/flat_map.hpp>
2308777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
241abe55efSEd Tanous #include <dbus_singleton.hpp>
25413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
261214b7e7SGunnar Mills 
271214b7e7SGunnar Mills #include <cmath>
28abf2add6SEd Tanous #include <variant>
2908777fb0SLewanczyk, Dawid 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
3208777fb0SLewanczyk, Dawid 
3308777fb0SLewanczyk, Dawid using GetSubTreeType = std::vector<
3408777fb0SLewanczyk, Dawid     std::pair<std::string,
3508777fb0SLewanczyk, Dawid               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
3608777fb0SLewanczyk, Dawid 
37adc4f0dbSShawn McCarney using SensorVariant =
38adc4f0dbSShawn McCarney     std::variant<int64_t, double, uint32_t, bool, std::string>;
39aa2e59c1SEd Tanous 
4008777fb0SLewanczyk, Dawid using ManagedObjectsVectorType = std::vector<std::pair<
41aa2e59c1SEd Tanous     sdbusplus::message::object_path,
4208777fb0SLewanczyk, Dawid     boost::container::flat_map<
43aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
4408777fb0SLewanczyk, Dawid 
45a0ec28b6SAdrian Ambrożewicz namespace sensors
46a0ec28b6SAdrian Ambrożewicz {
47a0ec28b6SAdrian Ambrożewicz namespace node
48a0ec28b6SAdrian Ambrożewicz {
49a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
50a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
51a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
52a0ec28b6SAdrian Ambrożewicz } // namespace node
53a0ec28b6SAdrian Ambrożewicz 
54a0ec28b6SAdrian Ambrożewicz namespace dbus
55a0ec28b6SAdrian Ambrożewicz {
56a0ec28b6SAdrian Ambrożewicz static const boost::container::flat_map<std::string_view,
57a0ec28b6SAdrian Ambrożewicz                                         std::vector<const char*>>
58a0ec28b6SAdrian Ambrożewicz     types = {{node::power,
59a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/voltage",
60a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/power"}},
61a0ec28b6SAdrian Ambrożewicz              {node::sensors,
62a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/power",
63a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/current",
64a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/utilization"}},
65a0ec28b6SAdrian Ambrożewicz              {node::thermal,
66a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/fan_tach",
67a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/temperature",
68a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/fan_pwm"}}};
69a0ec28b6SAdrian Ambrożewicz }
70a0ec28b6SAdrian Ambrożewicz } // namespace sensors
71a0ec28b6SAdrian Ambrożewicz 
7208777fb0SLewanczyk, Dawid /**
73588c3f0dSKowalski, Kamil  * SensorsAsyncResp
7408777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
7508777fb0SLewanczyk, Dawid  */
761abe55efSEd Tanous class SensorsAsyncResp
771abe55efSEd Tanous {
7808777fb0SLewanczyk, Dawid   public:
79a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
80a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
81a0ec28b6SAdrian Ambrożewicz         const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
82a0ec28b6SAdrian Ambrożewicz 
83a0ec28b6SAdrian Ambrożewicz     struct SensorData
84a0ec28b6SAdrian Ambrożewicz     {
85a0ec28b6SAdrian Ambrożewicz         const std::string name;
86a0ec28b6SAdrian Ambrożewicz         std::string uri;
87a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
88a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
89a0ec28b6SAdrian Ambrożewicz     };
90a0ec28b6SAdrian Ambrożewicz 
91271584abSEd Tanous     SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
92271584abSEd Tanous                      const std::vector<const char*> typesIn,
93a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode) :
9443b761d0SEd Tanous         res(response),
95271584abSEd Tanous         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
961214b7e7SGunnar Mills     {}
9708777fb0SLewanczyk, Dawid 
98a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
99a0ec28b6SAdrian Ambrożewicz     SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
100a0ec28b6SAdrian Ambrożewicz                      const std::vector<const char*> typesIn,
101a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode,
102a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
103a0ec28b6SAdrian Ambrożewicz         res(response),
104a0ec28b6SAdrian Ambrożewicz         chassisId(chassisIdIn), types(typesIn),
105a0ec28b6SAdrian Ambrożewicz         chassisSubNode(subNode), metadata{std::vector<SensorData>()},
106a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
107a0ec28b6SAdrian Ambrożewicz     {}
108a0ec28b6SAdrian Ambrożewicz 
1091abe55efSEd Tanous     ~SensorsAsyncResp()
1101abe55efSEd Tanous     {
1111abe55efSEd Tanous         if (res.result() == boost::beast::http::status::internal_server_error)
1121abe55efSEd Tanous         {
1131abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
1141abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
1151abe55efSEd Tanous             // proper code
11655c7b7a2SEd Tanous             res.jsonValue = nlohmann::json::object();
11708777fb0SLewanczyk, Dawid         }
118a0ec28b6SAdrian Ambrożewicz 
119a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
120a0ec28b6SAdrian Ambrożewicz         {
121a0ec28b6SAdrian Ambrożewicz             boost::container::flat_map<std::string, std::string> map;
122a0ec28b6SAdrian Ambrożewicz             if (res.result() == boost::beast::http::status::ok)
123a0ec28b6SAdrian Ambrożewicz             {
124a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
125a0ec28b6SAdrian Ambrożewicz                 {
126a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
127a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
128a0ec28b6SAdrian Ambrożewicz                 }
129a0ec28b6SAdrian Ambrożewicz             }
130a0ec28b6SAdrian Ambrożewicz             dataComplete(res.result(), map);
131a0ec28b6SAdrian Ambrożewicz         }
132a0ec28b6SAdrian Ambrożewicz 
13308777fb0SLewanczyk, Dawid         res.end();
13408777fb0SLewanczyk, Dawid     }
135588c3f0dSKowalski, Kamil 
136a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
137a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
138a0ec28b6SAdrian Ambrożewicz     {
139a0ec28b6SAdrian Ambrożewicz         if (metadata)
140a0ec28b6SAdrian Ambrożewicz         {
141a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
142a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
143a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
144a0ec28b6SAdrian Ambrożewicz         }
145a0ec28b6SAdrian Ambrożewicz     }
146a0ec28b6SAdrian Ambrożewicz 
147a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
148a0ec28b6SAdrian Ambrożewicz     {
149a0ec28b6SAdrian Ambrożewicz         if (metadata)
150a0ec28b6SAdrian Ambrożewicz         {
151a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
152a0ec28b6SAdrian Ambrożewicz             {
153a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
154a0ec28b6SAdrian Ambrożewicz                 {
155a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
156a0ec28b6SAdrian Ambrożewicz                 }
157a0ec28b6SAdrian Ambrożewicz             }
158a0ec28b6SAdrian Ambrożewicz         }
159a0ec28b6SAdrian Ambrożewicz     }
160a0ec28b6SAdrian Ambrożewicz 
16155c7b7a2SEd Tanous     crow::Response& res;
162a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
16308777fb0SLewanczyk, Dawid     const std::vector<const char*> types;
164a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
165a0ec28b6SAdrian Ambrożewicz 
166a0ec28b6SAdrian Ambrożewicz   private:
167a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
168a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
16908777fb0SLewanczyk, Dawid };
17008777fb0SLewanczyk, Dawid 
17108777fb0SLewanczyk, Dawid /**
172d500549bSAnthony Wilson  * Possible states for physical inventory leds
173d500549bSAnthony Wilson  */
174d500549bSAnthony Wilson enum class LedState
175d500549bSAnthony Wilson {
176d500549bSAnthony Wilson     OFF,
177d500549bSAnthony Wilson     ON,
178d500549bSAnthony Wilson     BLINK,
179d500549bSAnthony Wilson     UNKNOWN
180d500549bSAnthony Wilson };
181d500549bSAnthony Wilson 
182d500549bSAnthony Wilson /**
183adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
184adc4f0dbSShawn McCarney  */
185adc4f0dbSShawn McCarney class InventoryItem
186adc4f0dbSShawn McCarney {
187adc4f0dbSShawn McCarney   public:
188adc4f0dbSShawn McCarney     InventoryItem(const std::string& objPath) :
189adc4f0dbSShawn McCarney         objectPath(objPath), name(), isPresent(true), isFunctional(true),
19042cbe538SGunnar Mills         isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
19142cbe538SGunnar Mills         model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""),
192d500549bSAnthony Wilson         ledState(LedState::UNKNOWN)
193adc4f0dbSShawn McCarney     {
194adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
195adc4f0dbSShawn McCarney         auto pos = objectPath.rfind('/');
196adc4f0dbSShawn McCarney         if ((pos != std::string::npos) && ((pos + 1) < objectPath.size()))
197adc4f0dbSShawn McCarney         {
198adc4f0dbSShawn McCarney             name = objectPath.substr(pos + 1);
199adc4f0dbSShawn McCarney         }
200adc4f0dbSShawn McCarney     }
201adc4f0dbSShawn McCarney 
202adc4f0dbSShawn McCarney     std::string objectPath;
203adc4f0dbSShawn McCarney     std::string name;
204adc4f0dbSShawn McCarney     bool isPresent;
205adc4f0dbSShawn McCarney     bool isFunctional;
206adc4f0dbSShawn McCarney     bool isPowerSupply;
20742cbe538SGunnar Mills     int powerSupplyEfficiencyPercent;
208adc4f0dbSShawn McCarney     std::string manufacturer;
209adc4f0dbSShawn McCarney     std::string model;
210adc4f0dbSShawn McCarney     std::string partNumber;
211adc4f0dbSShawn McCarney     std::string serialNumber;
212adc4f0dbSShawn McCarney     std::set<std::string> sensors;
213d500549bSAnthony Wilson     std::string ledObjectPath;
214d500549bSAnthony Wilson     LedState ledState;
215adc4f0dbSShawn McCarney };
216adc4f0dbSShawn McCarney 
217adc4f0dbSShawn McCarney /**
218413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
219588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
22008777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
22108777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
22208777fb0SLewanczyk, Dawid  */
22308777fb0SLewanczyk, Dawid template <typename Callback>
224413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
225413961deSRichard Marian Thomaiyar     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
22649c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2271abe55efSEd Tanous     Callback&& callback)
2281abe55efSEd Tanous {
229413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
23003b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
23108777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
23208777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
23308777fb0SLewanczyk, Dawid 
23408777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
2351abe55efSEd Tanous     auto respHandler = [callback{std::move(callback)}, SensorsAsyncResp,
2361abe55efSEd Tanous                         sensorNames](const boost::system::error_code ec,
2371abe55efSEd Tanous                                      const GetSubTreeType& subtree) {
238413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
2391abe55efSEd Tanous         if (ec)
2401abe55efSEd Tanous         {
2415f7d88c4SEd Tanous             messages::internalError(SensorsAsyncResp->res);
242413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
243413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
24408777fb0SLewanczyk, Dawid             return;
24508777fb0SLewanczyk, Dawid         }
24608777fb0SLewanczyk, Dawid 
24755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
24808777fb0SLewanczyk, Dawid 
24908777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
25008777fb0SLewanczyk, Dawid         // found in the chassis
25108777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
252413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
2531abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
2541abe55efSEd Tanous         // producers
25508777fb0SLewanczyk, Dawid         connections.reserve(8);
25608777fb0SLewanczyk, Dawid 
25749c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
25849c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
2591abe55efSEd Tanous         {
26055c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
26108777fb0SLewanczyk, Dawid         }
26208777fb0SLewanczyk, Dawid 
26308777fb0SLewanczyk, Dawid         for (const std::pair<
26408777fb0SLewanczyk, Dawid                  std::string,
26508777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2661abe55efSEd Tanous                  object : subtree)
2671abe55efSEd Tanous         {
26849c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
2691abe55efSEd Tanous             {
27049c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
2711abe55efSEd Tanous                          objData : object.second)
2721abe55efSEd Tanous                 {
27349c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
27408777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
275de629b6eSShawn McCarney                     objectsWithConnection.insert(
276de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
27708777fb0SLewanczyk, Dawid                 }
27808777fb0SLewanczyk, Dawid             }
27908777fb0SLewanczyk, Dawid         }
28055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
281413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
282413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
28308777fb0SLewanczyk, Dawid     };
28408777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
28555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
28655c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2871abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
2881abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
289413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
290413961deSRichard Marian Thomaiyar }
291413961deSRichard Marian Thomaiyar 
292413961deSRichard Marian Thomaiyar /**
293413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
294413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
295413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
296413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
297413961deSRichard Marian Thomaiyar  */
298413961deSRichard Marian Thomaiyar template <typename Callback>
29949c53ac9SJohnathan Mantey void getConnections(
30049c53ac9SJohnathan Mantey     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
30149c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
302413961deSRichard Marian Thomaiyar     Callback&& callback)
303413961deSRichard Marian Thomaiyar {
304413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
305413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
306413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
307*3174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
308413961deSRichard Marian Thomaiyar     getObjectsWithConnection(SensorsAsyncResp, sensorNames,
309413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
31008777fb0SLewanczyk, Dawid }
31108777fb0SLewanczyk, Dawid 
31208777fb0SLewanczyk, Dawid /**
31349c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
31449c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
31549c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
31649c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
31749c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
31849c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
31949c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
32049c53ac9SJohnathan Mantey  */
32123a21a1cSEd Tanous inline void reduceSensorList(
32249c53ac9SJohnathan Mantey     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
32349c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
32449c53ac9SJohnathan Mantey     std::shared_ptr<boost::container::flat_set<std::string>> activeSensors)
32549c53ac9SJohnathan Mantey {
32649c53ac9SJohnathan Mantey     if (SensorsAsyncResp == nullptr)
32749c53ac9SJohnathan Mantey     {
32849c53ac9SJohnathan Mantey         return;
32949c53ac9SJohnathan Mantey     }
33049c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
33149c53ac9SJohnathan Mantey     {
33249c53ac9SJohnathan Mantey         messages::resourceNotFound(
33349c53ac9SJohnathan Mantey             SensorsAsyncResp->res, SensorsAsyncResp->chassisSubNode,
334a0ec28b6SAdrian Ambrożewicz             SensorsAsyncResp->chassisSubNode == sensors::node::thermal
335a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
33649c53ac9SJohnathan Mantey                 : "Voltages");
33749c53ac9SJohnathan Mantey 
33849c53ac9SJohnathan Mantey         return;
33949c53ac9SJohnathan Mantey     }
34049c53ac9SJohnathan Mantey     if (allSensors->empty())
34149c53ac9SJohnathan Mantey     {
34249c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
34349c53ac9SJohnathan Mantey         return;
34449c53ac9SJohnathan Mantey     }
34549c53ac9SJohnathan Mantey 
34649c53ac9SJohnathan Mantey     for (const char* type : SensorsAsyncResp->types)
34749c53ac9SJohnathan Mantey     {
34849c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
34949c53ac9SJohnathan Mantey         {
35049c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
35149c53ac9SJohnathan Mantey             {
35249c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
35349c53ac9SJohnathan Mantey             }
35449c53ac9SJohnathan Mantey         }
35549c53ac9SJohnathan Mantey     }
35649c53ac9SJohnathan Mantey }
35749c53ac9SJohnathan Mantey 
35849c53ac9SJohnathan Mantey /**
3594bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
3604bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
3614bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
3624bb3dc34SCarol Wang  */
3634bb3dc34SCarol Wang template <typename Callback>
3644bb3dc34SCarol Wang void getValidChassisPath(std::shared_ptr<SensorsAsyncResp> asyncResp,
3654bb3dc34SCarol Wang                          Callback&& callback)
3664bb3dc34SCarol Wang {
3674bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
3684bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
3694bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
3704bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
3714bb3dc34SCarol Wang 
3724bb3dc34SCarol Wang     auto respHandler =
3734bb3dc34SCarol Wang         [callback{std::move(callback)},
3744bb3dc34SCarol Wang          asyncResp](const boost::system::error_code ec,
3754bb3dc34SCarol Wang                     const std::vector<std::string>& chassisPaths) mutable {
3764bb3dc34SCarol Wang             BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
3774bb3dc34SCarol Wang             if (ec)
3784bb3dc34SCarol Wang             {
3794bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR
3804bb3dc34SCarol Wang                     << "getValidChassisPath respHandler DBUS error: " << ec;
3814bb3dc34SCarol Wang                 messages::internalError(asyncResp->res);
3824bb3dc34SCarol Wang                 return;
3834bb3dc34SCarol Wang             }
3844bb3dc34SCarol Wang 
3854bb3dc34SCarol Wang             std::optional<std::string> chassisPath;
3864bb3dc34SCarol Wang             std::string chassisName;
3874bb3dc34SCarol Wang             for (const std::string& chassis : chassisPaths)
3884bb3dc34SCarol Wang             {
3894bb3dc34SCarol Wang                 std::size_t lastPos = chassis.rfind("/");
3904bb3dc34SCarol Wang                 if (lastPos == std::string::npos)
3914bb3dc34SCarol Wang                 {
3924bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
3934bb3dc34SCarol Wang                     continue;
3944bb3dc34SCarol Wang                 }
3954bb3dc34SCarol Wang                 chassisName = chassis.substr(lastPos + 1);
3964bb3dc34SCarol Wang                 if (chassisName == asyncResp->chassisId)
3974bb3dc34SCarol Wang                 {
3984bb3dc34SCarol Wang                     chassisPath = chassis;
3994bb3dc34SCarol Wang                     break;
4004bb3dc34SCarol Wang                 }
4014bb3dc34SCarol Wang             }
4024bb3dc34SCarol Wang             callback(chassisPath);
4034bb3dc34SCarol Wang         };
4044bb3dc34SCarol Wang 
4054bb3dc34SCarol Wang     // Get the Chassis Collection
4064bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
4074bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
4084bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
4094bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
4104bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
4114bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
4124bb3dc34SCarol Wang }
4134bb3dc34SCarol Wang 
4144bb3dc34SCarol Wang /**
41508777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
416588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
41708777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
41808777fb0SLewanczyk, Dawid  */
41908777fb0SLewanczyk, Dawid template <typename Callback>
42049c53ac9SJohnathan Mantey void getChassis(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
4211abe55efSEd Tanous                 Callback&& callback)
4221abe55efSEd Tanous {
42355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
424adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
42549c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
426adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
42749c53ac9SJohnathan Mantey     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
42849c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
42949c53ac9SJohnathan Mantey                            const std::vector<std::string>& chassisPaths) {
43055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
4311abe55efSEd Tanous         if (ec)
4321abe55efSEd Tanous         {
43355c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
43449c53ac9SJohnathan Mantey             messages::internalError(sensorsAsyncResp->res);
43508777fb0SLewanczyk, Dawid             return;
43608777fb0SLewanczyk, Dawid         }
43708777fb0SLewanczyk, Dawid 
43849c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
43949c53ac9SJohnathan Mantey         std::string chassisName;
44049c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
4411abe55efSEd Tanous         {
44249c53ac9SJohnathan Mantey             std::size_t lastPos = chassis.rfind("/");
44349c53ac9SJohnathan Mantey             if (lastPos == std::string::npos)
4441abe55efSEd Tanous             {
44549c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
446daf36e2eSEd Tanous                 continue;
447daf36e2eSEd Tanous             }
44849c53ac9SJohnathan Mantey             chassisName = chassis.substr(lastPos + 1);
44949c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
4501abe55efSEd Tanous             {
45149c53ac9SJohnathan Mantey                 chassisPath = &chassis;
45249c53ac9SJohnathan Mantey                 break;
453daf36e2eSEd Tanous             }
45449c53ac9SJohnathan Mantey         }
45549c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
4561abe55efSEd Tanous         {
45749c53ac9SJohnathan Mantey             messages::resourceNotFound(sensorsAsyncResp->res, "Chassis",
45849c53ac9SJohnathan Mantey                                        sensorsAsyncResp->chassisId);
45949c53ac9SJohnathan Mantey             return;
4601abe55efSEd Tanous         }
46108777fb0SLewanczyk, Dawid 
46249c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
463a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
46449c53ac9SJohnathan Mantey         {
46549c53ac9SJohnathan Mantey             sensorsAsyncResp->res.jsonValue["@odata.type"] =
46649c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
46749c53ac9SJohnathan Mantey         }
468a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
46949c53ac9SJohnathan Mantey         {
47049c53ac9SJohnathan Mantey             sensorsAsyncResp->res.jsonValue["@odata.type"] =
47149c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
4724f9a2130SJennifer Lee             sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array();
4734f9a2130SJennifer Lee             sensorsAsyncResp->res.jsonValue["Temperatures"] =
4744f9a2130SJennifer Lee                 nlohmann::json::array();
47549c53ac9SJohnathan Mantey         }
476a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
47795a3ecadSAnthony Wilson         {
47895a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["@odata.type"] =
47995a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
48095a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Description"] =
48195a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
48295a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members"] =
48395a3ecadSAnthony Wilson                 nlohmann::json::array();
48495a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0;
48595a3ecadSAnthony Wilson         }
48695a3ecadSAnthony Wilson 
487a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
48895a3ecadSAnthony Wilson         {
48995a3ecadSAnthony Wilson             sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode;
49095a3ecadSAnthony Wilson         }
49195a3ecadSAnthony Wilson 
49249c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["@odata.id"] =
49349c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
49449c53ac9SJohnathan Mantey             chassisSubNode;
49549c53ac9SJohnathan Mantey         sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode;
49649c53ac9SJohnathan Mantey 
4978fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
4988fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
49955c7b7a2SEd Tanous         crow::connections::systemBus->async_method_call(
50049c53ac9SJohnathan Mantey             [sensorsAsyncResp, callback{std::move(callback)}](
501271584abSEd Tanous                 const boost::system::error_code& e,
50249c53ac9SJohnathan Mantey                 const std::variant<std::vector<std::string>>&
50349c53ac9SJohnathan Mantey                     variantEndpoints) {
504271584abSEd Tanous                 if (e)
50549c53ac9SJohnathan Mantey                 {
506271584abSEd Tanous                     if (e.value() != EBADR)
50749c53ac9SJohnathan Mantey                     {
50849c53ac9SJohnathan Mantey                         messages::internalError(sensorsAsyncResp->res);
50949c53ac9SJohnathan Mantey                         return;
51049c53ac9SJohnathan Mantey                     }
51149c53ac9SJohnathan Mantey                 }
51249c53ac9SJohnathan Mantey                 const std::vector<std::string>* nodeSensorList =
51349c53ac9SJohnathan Mantey                     std::get_if<std::vector<std::string>>(&(variantEndpoints));
51449c53ac9SJohnathan Mantey                 if (nodeSensorList == nullptr)
51549c53ac9SJohnathan Mantey                 {
51649c53ac9SJohnathan Mantey                     messages::resourceNotFound(
51749c53ac9SJohnathan Mantey                         sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
518a0ec28b6SAdrian Ambrożewicz                         sensorsAsyncResp->chassisSubNode ==
519a0ec28b6SAdrian Ambrożewicz                                 sensors::node::thermal
52049c53ac9SJohnathan Mantey                             ? "Temperatures"
521a0ec28b6SAdrian Ambrożewicz                             : sensorsAsyncResp->chassisSubNode ==
522a0ec28b6SAdrian Ambrożewicz                                       sensors::node::power
52395a3ecadSAnthony Wilson                                   ? "Voltages"
52495a3ecadSAnthony Wilson                                   : "Sensors");
52549c53ac9SJohnathan Mantey                     return;
52649c53ac9SJohnathan Mantey                 }
52749c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
52849c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
52949c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
53049c53ac9SJohnathan Mantey                 reduceSensorList(sensorsAsyncResp, nodeSensorList,
53149c53ac9SJohnathan Mantey                                  culledSensorList);
53249c53ac9SJohnathan Mantey                 callback(culledSensorList);
53349c53ac9SJohnathan Mantey             },
53449c53ac9SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", sensorPath,
53549c53ac9SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Get",
53649c53ac9SJohnathan Mantey             "xyz.openbmc_project.Association", "endpoints");
53749c53ac9SJohnathan Mantey     };
53849c53ac9SJohnathan Mantey 
53949c53ac9SJohnathan Mantey     // Get the Chassis Collection
54049c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
54149c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
54249c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
54349c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
544271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
54555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
54608777fb0SLewanczyk, Dawid }
54708777fb0SLewanczyk, Dawid 
54808777fb0SLewanczyk, Dawid /**
549de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
550de629b6eSShawn McCarney  *
551de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
552de629b6eSShawn McCarney  *
553de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
554de629b6eSShawn McCarney  * been obtained.
555de629b6eSShawn McCarney  *
556de629b6eSShawn McCarney  * The callback must have the following signature:
557de629b6eSShawn McCarney  *   @code
5588fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
5598fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
560de629b6eSShawn McCarney  *   @endcode
561de629b6eSShawn McCarney  *
56249c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
563de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
564de629b6eSShawn McCarney  */
565de629b6eSShawn McCarney template <typename Callback>
566de629b6eSShawn McCarney void getObjectManagerPaths(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
567de629b6eSShawn McCarney                            Callback&& callback)
568de629b6eSShawn McCarney {
569de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
570de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
571de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
572de629b6eSShawn McCarney 
573de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
574de629b6eSShawn McCarney     auto respHandler = [callback{std::move(callback)},
575de629b6eSShawn McCarney                         SensorsAsyncResp](const boost::system::error_code ec,
576de629b6eSShawn McCarney                                           const GetSubTreeType& subtree) {
577de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
578de629b6eSShawn McCarney         if (ec)
579de629b6eSShawn McCarney         {
580de629b6eSShawn McCarney             messages::internalError(SensorsAsyncResp->res);
581de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
582de629b6eSShawn McCarney                              << ec;
583de629b6eSShawn McCarney             return;
584de629b6eSShawn McCarney         }
585de629b6eSShawn McCarney 
586de629b6eSShawn McCarney         // Loop over returned object paths
5878fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
5888fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
5898fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
590de629b6eSShawn McCarney         for (const std::pair<
591de629b6eSShawn McCarney                  std::string,
592de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
593de629b6eSShawn McCarney                  object : subtree)
594de629b6eSShawn McCarney         {
595de629b6eSShawn McCarney             // Loop over connections for current object path
596de629b6eSShawn McCarney             const std::string& objectPath = object.first;
597de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
598de629b6eSShawn McCarney                      objData : object.second)
599de629b6eSShawn McCarney             {
600de629b6eSShawn McCarney                 // Add mapping from connection to object path
601de629b6eSShawn McCarney                 const std::string& connection = objData.first;
6028fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
603de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
604de629b6eSShawn McCarney                                  << objectPath;
605de629b6eSShawn McCarney             }
606de629b6eSShawn McCarney         }
6078fb49dd6SShawn McCarney         callback(objectMgrPaths);
608de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
609de629b6eSShawn McCarney     };
610de629b6eSShawn McCarney 
611de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
612de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
613de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
614de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
615271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
616de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
617de629b6eSShawn McCarney }
618de629b6eSShawn McCarney 
619de629b6eSShawn McCarney /**
620adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
621adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
622adc4f0dbSShawn McCarney  * @return State value for inventory item.
62334dd179eSJames Feist  */
62423a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
625adc4f0dbSShawn McCarney {
626adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
627adc4f0dbSShawn McCarney     {
628adc4f0dbSShawn McCarney         return "Absent";
629adc4f0dbSShawn McCarney     }
63034dd179eSJames Feist 
631adc4f0dbSShawn McCarney     return "Enabled";
632adc4f0dbSShawn McCarney }
633adc4f0dbSShawn McCarney 
634adc4f0dbSShawn McCarney /**
635adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
636adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
637adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
638adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
639adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
640adc4f0dbSShawn McCarney  * @return Health value for sensor.
641adc4f0dbSShawn McCarney  */
64223a21a1cSEd Tanous inline std::string getHealth(
643adc4f0dbSShawn McCarney     nlohmann::json& sensorJson,
64434dd179eSJames Feist     const boost::container::flat_map<
64534dd179eSJames Feist         std::string, boost::container::flat_map<std::string, SensorVariant>>&
646adc4f0dbSShawn McCarney         interfacesDict,
647adc4f0dbSShawn McCarney     const InventoryItem* inventoryItem)
64834dd179eSJames Feist {
649adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
650adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
651adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
652adc4f0dbSShawn McCarney     std::string currentHealth;
653adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
654adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
655adc4f0dbSShawn McCarney     {
656adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
657adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
658adc4f0dbSShawn McCarney         {
659adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
660adc4f0dbSShawn McCarney             if (health != nullptr)
661adc4f0dbSShawn McCarney             {
662adc4f0dbSShawn McCarney                 currentHealth = *health;
663adc4f0dbSShawn McCarney             }
664adc4f0dbSShawn McCarney         }
665adc4f0dbSShawn McCarney     }
666adc4f0dbSShawn McCarney 
667adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
668adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
669adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
670adc4f0dbSShawn McCarney     {
671adc4f0dbSShawn McCarney         return "Critical";
672adc4f0dbSShawn McCarney     }
673adc4f0dbSShawn McCarney 
674adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
67534dd179eSJames Feist     auto criticalThresholdIt =
67634dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
67734dd179eSJames Feist     if (criticalThresholdIt != interfacesDict.end())
67834dd179eSJames Feist     {
67934dd179eSJames Feist         auto thresholdHighIt =
68034dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmHigh");
68134dd179eSJames Feist         auto thresholdLowIt =
68234dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmLow");
68334dd179eSJames Feist         if (thresholdHighIt != criticalThresholdIt->second.end())
68434dd179eSJames Feist         {
68534dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
68634dd179eSJames Feist             if (asserted == nullptr)
68734dd179eSJames Feist             {
68834dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
68934dd179eSJames Feist             }
69034dd179eSJames Feist             else if (*asserted)
69134dd179eSJames Feist             {
69234dd179eSJames Feist                 return "Critical";
69334dd179eSJames Feist             }
69434dd179eSJames Feist         }
69534dd179eSJames Feist         if (thresholdLowIt != criticalThresholdIt->second.end())
69634dd179eSJames Feist         {
69734dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
69834dd179eSJames Feist             if (asserted == nullptr)
69934dd179eSJames Feist             {
70034dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
70134dd179eSJames Feist             }
70234dd179eSJames Feist             else if (*asserted)
70334dd179eSJames Feist             {
70434dd179eSJames Feist                 return "Critical";
70534dd179eSJames Feist             }
70634dd179eSJames Feist         }
70734dd179eSJames Feist     }
70834dd179eSJames Feist 
709adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
710adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
711adc4f0dbSShawn McCarney     {
712adc4f0dbSShawn McCarney         return "Critical";
713adc4f0dbSShawn McCarney     }
714adc4f0dbSShawn McCarney 
715adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
716adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
717adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
718adc4f0dbSShawn McCarney     {
719adc4f0dbSShawn McCarney         return "Warning";
720adc4f0dbSShawn McCarney     }
721adc4f0dbSShawn McCarney 
722adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
72334dd179eSJames Feist     auto warningThresholdIt =
72434dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
72534dd179eSJames Feist     if (warningThresholdIt != interfacesDict.end())
72634dd179eSJames Feist     {
72734dd179eSJames Feist         auto thresholdHighIt =
72834dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmHigh");
72934dd179eSJames Feist         auto thresholdLowIt =
73034dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmLow");
73134dd179eSJames Feist         if (thresholdHighIt != warningThresholdIt->second.end())
73234dd179eSJames Feist         {
73334dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
73434dd179eSJames Feist             if (asserted == nullptr)
73534dd179eSJames Feist             {
73634dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
73734dd179eSJames Feist             }
73834dd179eSJames Feist             else if (*asserted)
73934dd179eSJames Feist             {
74034dd179eSJames Feist                 return "Warning";
74134dd179eSJames Feist             }
74234dd179eSJames Feist         }
74334dd179eSJames Feist         if (thresholdLowIt != warningThresholdIt->second.end())
74434dd179eSJames Feist         {
74534dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
74634dd179eSJames Feist             if (asserted == nullptr)
74734dd179eSJames Feist             {
74834dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
74934dd179eSJames Feist             }
75034dd179eSJames Feist             else if (*asserted)
75134dd179eSJames Feist             {
75234dd179eSJames Feist                 return "Warning";
75334dd179eSJames Feist             }
75434dd179eSJames Feist         }
75534dd179eSJames Feist     }
756adc4f0dbSShawn McCarney 
75734dd179eSJames Feist     return "OK";
75834dd179eSJames Feist }
75934dd179eSJames Feist 
76023a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
761d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
762d500549bSAnthony Wilson {
763d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
764d500549bSAnthony Wilson     {
765d500549bSAnthony Wilson         switch (inventoryItem->ledState)
766d500549bSAnthony Wilson         {
767d500549bSAnthony Wilson             case LedState::OFF:
768d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
769d500549bSAnthony Wilson                 break;
770d500549bSAnthony Wilson             case LedState::ON:
771d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
772d500549bSAnthony Wilson                 break;
773d500549bSAnthony Wilson             case LedState::BLINK:
774d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
775d500549bSAnthony Wilson                 break;
77623a21a1cSEd Tanous             case LedState::UNKNOWN:
777d500549bSAnthony Wilson                 break;
778d500549bSAnthony Wilson         }
779d500549bSAnthony Wilson     }
780d500549bSAnthony Wilson }
781d500549bSAnthony Wilson 
78234dd179eSJames Feist /**
78308777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
78408777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
785274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
78608777fb0SLewanczyk, Dawid  * build
787a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
78808777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
78908777fb0SLewanczyk, Dawid  * interfaces to be built from
79008777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
791adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
792adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
79308777fb0SLewanczyk, Dawid  */
79423a21a1cSEd Tanous inline void objectInterfacesToJson(
79508777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
796a0ec28b6SAdrian Ambrożewicz     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
79708777fb0SLewanczyk, Dawid     const boost::container::flat_map<
798aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>&
79908777fb0SLewanczyk, Dawid         interfacesDict,
800adc4f0dbSShawn McCarney     nlohmann::json& sensor_json, InventoryItem* inventoryItem)
8011abe55efSEd Tanous {
80208777fb0SLewanczyk, Dawid     // We need a value interface before we can do anything with it
80355c7b7a2SEd Tanous     auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
8041abe55efSEd Tanous     if (valueIt == interfacesDict.end())
8051abe55efSEd Tanous     {
80655c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
80708777fb0SLewanczyk, Dawid         return;
80808777fb0SLewanczyk, Dawid     }
80908777fb0SLewanczyk, Dawid 
81008777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
81108777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
81208777fb0SLewanczyk, Dawid 
81355c7b7a2SEd Tanous     auto scaleIt = valueIt->second.find("Scale");
81408777fb0SLewanczyk, Dawid     // If a scale exists, pull value as int64, and use the scaling.
8151abe55efSEd Tanous     if (scaleIt != valueIt->second.end())
8161abe55efSEd Tanous     {
817abf2add6SEd Tanous         const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
8181abe55efSEd Tanous         if (int64Value != nullptr)
8191abe55efSEd Tanous         {
82008777fb0SLewanczyk, Dawid             scaleMultiplier = *int64Value;
82108777fb0SLewanczyk, Dawid         }
82208777fb0SLewanczyk, Dawid     }
82308777fb0SLewanczyk, Dawid 
824a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
825adc4f0dbSShawn McCarney     {
82695a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
82795a3ecadSAnthony Wilson         // including power sensors.
82895a3ecadSAnthony Wilson         sensor_json["Id"] = sensorName;
82995a3ecadSAnthony Wilson         sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
83095a3ecadSAnthony Wilson     }
83195a3ecadSAnthony Wilson     else if (sensorType != "power")
83295a3ecadSAnthony Wilson     {
83395a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
83495a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
83595a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
83608777fb0SLewanczyk, Dawid         sensor_json["MemberId"] = sensorName;
837e742b6ccSEd Tanous         sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
838adc4f0dbSShawn McCarney     }
839e742b6ccSEd Tanous 
840adc4f0dbSShawn McCarney     sensor_json["Status"]["State"] = getState(inventoryItem);
841adc4f0dbSShawn McCarney     sensor_json["Status"]["Health"] =
842adc4f0dbSShawn McCarney         getHealth(sensor_json, interfacesDict, inventoryItem);
84308777fb0SLewanczyk, Dawid 
84408777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
84508777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
84608777fb0SLewanczyk, Dawid     // that require integers, not floats.
84708777fb0SLewanczyk, Dawid     bool forceToInt = false;
84808777fb0SLewanczyk, Dawid 
8493929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
850a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
85195a3ecadSAnthony Wilson     {
85295a3ecadSAnthony Wilson         sensor_json["@odata.type"] = "#Sensor.v1_0_0.Sensor";
85395a3ecadSAnthony Wilson         if (sensorType == "power")
85495a3ecadSAnthony Wilson         {
85595a3ecadSAnthony Wilson             sensor_json["ReadingUnits"] = "Watts";
85695a3ecadSAnthony Wilson         }
85795a3ecadSAnthony Wilson         else if (sensorType == "current")
85895a3ecadSAnthony Wilson         {
85995a3ecadSAnthony Wilson             sensor_json["ReadingUnits"] = "Amperes";
86095a3ecadSAnthony Wilson         }
861f8ede15eSAdrian Ambrożewicz         else if (sensorType == "utilization")
862f8ede15eSAdrian Ambrożewicz         {
863f8ede15eSAdrian Ambrożewicz             sensor_json["ReadingUnits"] = "Percent";
864f8ede15eSAdrian Ambrożewicz         }
86595a3ecadSAnthony Wilson     }
86695a3ecadSAnthony Wilson     else if (sensorType == "temperature")
8671abe55efSEd Tanous     {
8683929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
8697885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature";
87008777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
87108777fb0SLewanczyk, Dawid         // implementation seems to implement fan
8721abe55efSEd Tanous     }
8731abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
8741abe55efSEd Tanous     {
8753929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
87608777fb0SLewanczyk, Dawid         sensor_json["ReadingUnits"] = "RPM";
8777885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
878d500549bSAnthony Wilson         setLedState(sensor_json, inventoryItem);
87908777fb0SLewanczyk, Dawid         forceToInt = true;
8801abe55efSEd Tanous     }
8816f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
8826f6d0d32SEd Tanous     {
8833929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
8846f6d0d32SEd Tanous         sensor_json["ReadingUnits"] = "Percent";
8856f6d0d32SEd Tanous         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
886d500549bSAnthony Wilson         setLedState(sensor_json, inventoryItem);
8876f6d0d32SEd Tanous         forceToInt = true;
8886f6d0d32SEd Tanous     }
8891abe55efSEd Tanous     else if (sensorType == "voltage")
8901abe55efSEd Tanous     {
8913929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
8927885954aSLewanczyk, Dawid         sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage";
8931abe55efSEd Tanous     }
8942474adfaSEd Tanous     else if (sensorType == "power")
8952474adfaSEd Tanous     {
89649c53ac9SJohnathan Mantey         std::string sensorNameLower =
89749c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
89849c53ac9SJohnathan Mantey 
899028f7ebcSEddie James         if (!sensorName.compare("total_power"))
900028f7ebcSEddie James         {
9017ab06f49SGunnar Mills             sensor_json["@odata.type"] = "#Power.v1_0_0.PowerControl";
9027ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
9037ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
9047ab06f49SGunnar Mills             sensor_json["MemberId"] = "0";
9057ab06f49SGunnar Mills             sensor_json["Name"] = "Chassis Power Control";
9063929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
907028f7ebcSEddie James         }
908028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
90949c53ac9SJohnathan Mantey         {
9103929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
91149c53ac9SJohnathan Mantey         }
91249c53ac9SJohnathan Mantey         else
91349c53ac9SJohnathan Mantey         {
9143929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
91549c53ac9SJohnathan Mantey         }
9162474adfaSEd Tanous     }
9171abe55efSEd Tanous     else
9181abe55efSEd Tanous     {
91955c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
92008777fb0SLewanczyk, Dawid         return;
92108777fb0SLewanczyk, Dawid     }
92208777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
9233929aca1SAnthony Wilson     std::vector<
9243929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
9253929aca1SAnthony Wilson         properties;
92608777fb0SLewanczyk, Dawid     properties.reserve(7);
92708777fb0SLewanczyk, Dawid 
92808777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
929de629b6eSShawn McCarney 
930a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
9313929aca1SAnthony Wilson     {
9323929aca1SAnthony Wilson         properties.emplace_back(
9333929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
9343929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
9353929aca1SAnthony Wilson         properties.emplace_back(
9363929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
9373929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
9383929aca1SAnthony Wilson         properties.emplace_back(
9393929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
9403929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
9413929aca1SAnthony Wilson         properties.emplace_back(
9423929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
9433929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
9443929aca1SAnthony Wilson     }
9453929aca1SAnthony Wilson     else if (sensorType != "power")
946de629b6eSShawn McCarney     {
94708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
9483929aca1SAnthony Wilson                                 "WarningHigh",
9493929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
95008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
9513929aca1SAnthony Wilson                                 "WarningLow",
9523929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
95308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
9543929aca1SAnthony Wilson                                 "CriticalHigh",
9553929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
95608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
9573929aca1SAnthony Wilson                                 "CriticalLow",
9583929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
959de629b6eSShawn McCarney     }
96008777fb0SLewanczyk, Dawid 
9612474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
9622474adfaSEd Tanous 
963a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
96495a3ecadSAnthony Wilson     {
96595a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9663929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
96795a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9683929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
96995a3ecadSAnthony Wilson     }
97095a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9711abe55efSEd Tanous     {
97208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9733929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
97408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9753929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
9761abe55efSEd Tanous     }
977adc4f0dbSShawn McCarney     else if (sensorType != "power")
9781abe55efSEd Tanous     {
97908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
9803929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
98108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
9823929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
98308777fb0SLewanczyk, Dawid     }
98408777fb0SLewanczyk, Dawid 
9853929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
9863929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
9871abe55efSEd Tanous     {
98808777fb0SLewanczyk, Dawid         auto interfaceProperties = interfacesDict.find(std::get<0>(p));
9891abe55efSEd Tanous         if (interfaceProperties != interfacesDict.end())
9901abe55efSEd Tanous         {
991271584abSEd Tanous             auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
992271584abSEd Tanous             if (thisValueIt != interfaceProperties->second.end())
9931abe55efSEd Tanous             {
994271584abSEd Tanous                 const SensorVariant& valueVariant = thisValueIt->second;
9953929aca1SAnthony Wilson 
9963929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
9973929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
9983929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
9993929aca1SAnthony Wilson 
100008777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1001abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
100208777fb0SLewanczyk, Dawid 
1003abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1004028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
10056f6d0d32SEd Tanous                 double temp = 0.0;
10066f6d0d32SEd Tanous                 if (int64Value != nullptr)
10071abe55efSEd Tanous                 {
1008271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
10096f6d0d32SEd Tanous                 }
10106f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
10111abe55efSEd Tanous                 {
10126f6d0d32SEd Tanous                     temp = *doubleValue;
10131abe55efSEd Tanous                 }
1014028f7ebcSEddie James                 else if (uValue != nullptr)
1015028f7ebcSEddie James                 {
1016028f7ebcSEddie James                     temp = *uValue;
1017028f7ebcSEddie James                 }
10181abe55efSEd Tanous                 else
10191abe55efSEd Tanous                 {
10206f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
10216f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
10226f6d0d32SEd Tanous                     continue;
102308777fb0SLewanczyk, Dawid                 }
10246f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
10256f6d0d32SEd Tanous                 if (forceToInt)
10266f6d0d32SEd Tanous                 {
10273929aca1SAnthony Wilson                     sensor_json[key] = static_cast<int64_t>(temp);
10286f6d0d32SEd Tanous                 }
10296f6d0d32SEd Tanous                 else
10306f6d0d32SEd Tanous                 {
10313929aca1SAnthony Wilson                     sensor_json[key] = temp;
103208777fb0SLewanczyk, Dawid                 }
103308777fb0SLewanczyk, Dawid             }
103408777fb0SLewanczyk, Dawid         }
103508777fb0SLewanczyk, Dawid     }
1036a0ec28b6SAdrian Ambrożewicz 
1037a0ec28b6SAdrian Ambrożewicz     sensorsAsyncResp->addMetadata(sensor_json, unit.to_string(),
1038a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1039a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1040a0ec28b6SAdrian Ambrożewicz 
104155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
104208777fb0SLewanczyk, Dawid }
104308777fb0SLewanczyk, Dawid 
104423a21a1cSEd Tanous inline void
10458bd25ccdSJames Feist     populateFanRedundancy(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp)
10468bd25ccdSJames Feist {
10478bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
10488bd25ccdSJames Feist         [sensorsAsyncResp](const boost::system::error_code ec,
10498bd25ccdSJames Feist                            const GetSubTreeType& resp) {
10508bd25ccdSJames Feist             if (ec)
10518bd25ccdSJames Feist             {
10528bd25ccdSJames Feist                 return; // don't have to have this interface
10538bd25ccdSJames Feist             }
1054e278c18fSEd Tanous             for (const std::pair<std::string,
1055e278c18fSEd Tanous                                  std::vector<std::pair<
1056e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
1057e278c18fSEd Tanous                      pathPair : resp)
10588bd25ccdSJames Feist             {
1059e278c18fSEd Tanous                 const std::string& path = pathPair.first;
1060e278c18fSEd Tanous                 const std::vector<
1061e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
1062e278c18fSEd Tanous                     pathPair.second;
10638bd25ccdSJames Feist                 if (objDict.empty())
10648bd25ccdSJames Feist                 {
10658bd25ccdSJames Feist                     continue; // this should be impossible
10668bd25ccdSJames Feist                 }
10678bd25ccdSJames Feist 
10688bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
10698bd25ccdSJames Feist                 crow::connections::systemBus->async_method_call(
10708bd25ccdSJames Feist                     [path, owner,
1071271584abSEd Tanous                      sensorsAsyncResp](const boost::system::error_code e,
10728bd25ccdSJames Feist                                        std::variant<std::vector<std::string>>
10738bd25ccdSJames Feist                                            variantEndpoints) {
1074271584abSEd Tanous                         if (e)
10758bd25ccdSJames Feist                         {
10768bd25ccdSJames Feist                             return; // if they don't have an association we
10778bd25ccdSJames Feist                                     // can't tell what chassis is
10788bd25ccdSJames Feist                         }
10798bd25ccdSJames Feist                         // verify part of the right chassis
10808bd25ccdSJames Feist                         auto endpoints = std::get_if<std::vector<std::string>>(
10818bd25ccdSJames Feist                             &variantEndpoints);
10828bd25ccdSJames Feist 
10838bd25ccdSJames Feist                         if (endpoints == nullptr)
10848bd25ccdSJames Feist                         {
10858bd25ccdSJames Feist                             BMCWEB_LOG_ERROR << "Invalid association interface";
10868bd25ccdSJames Feist                             messages::internalError(sensorsAsyncResp->res);
10878bd25ccdSJames Feist                             return;
10888bd25ccdSJames Feist                         }
10898bd25ccdSJames Feist 
10908bd25ccdSJames Feist                         auto found = std::find_if(
10918bd25ccdSJames Feist                             endpoints->begin(), endpoints->end(),
10928bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
10938bd25ccdSJames Feist                                 return entry.find(
10948bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
10958bd25ccdSJames Feist                                        std::string::npos;
10968bd25ccdSJames Feist                             });
10978bd25ccdSJames Feist 
10988bd25ccdSJames Feist                         if (found == endpoints->end())
10998bd25ccdSJames Feist                         {
11008bd25ccdSJames Feist                             return;
11018bd25ccdSJames Feist                         }
11028bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
11038bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1104271584abSEd Tanous                                 const boost::system::error_code& err,
11058bd25ccdSJames Feist                                 const boost::container::flat_map<
11068bd25ccdSJames Feist                                     std::string,
11078bd25ccdSJames Feist                                     std::variant<uint8_t,
11088bd25ccdSJames Feist                                                  std::vector<std::string>,
11098bd25ccdSJames Feist                                                  std::string>>& ret) {
1110271584abSEd Tanous                                 if (err)
11118bd25ccdSJames Feist                                 {
11128bd25ccdSJames Feist                                     return; // don't have to have this
11138bd25ccdSJames Feist                                             // interface
11148bd25ccdSJames Feist                                 }
11158bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
11168bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
11178bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
11188bd25ccdSJames Feist 
11198bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
11208bd25ccdSJames Feist                                     findCollection == ret.end() ||
11218bd25ccdSJames Feist                                     findStatus == ret.end())
11228bd25ccdSJames Feist                                 {
11238bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
11248bd25ccdSJames Feist                                         << "Invalid redundancy interface";
11258bd25ccdSJames Feist                                     messages::internalError(
11268bd25ccdSJames Feist                                         sensorsAsyncResp->res);
11278bd25ccdSJames Feist                                     return;
11288bd25ccdSJames Feist                                 }
11298bd25ccdSJames Feist 
11308bd25ccdSJames Feist                                 auto allowedFailures = std::get_if<uint8_t>(
11318bd25ccdSJames Feist                                     &(findFailures->second));
11328bd25ccdSJames Feist                                 auto collection =
11338bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
11348bd25ccdSJames Feist                                         &(findCollection->second));
11358bd25ccdSJames Feist                                 auto status = std::get_if<std::string>(
11368bd25ccdSJames Feist                                     &(findStatus->second));
11378bd25ccdSJames Feist 
11388bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
11398bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
11408bd25ccdSJames Feist                                 {
11418bd25ccdSJames Feist 
11428bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
11438bd25ccdSJames Feist                                         << "Invalid redundancy interface "
11448bd25ccdSJames Feist                                            "types";
11458bd25ccdSJames Feist                                     messages::internalError(
11468bd25ccdSJames Feist                                         sensorsAsyncResp->res);
11478bd25ccdSJames Feist                                     return;
11488bd25ccdSJames Feist                                 }
11498bd25ccdSJames Feist                                 size_t lastSlash = path.rfind("/");
11508bd25ccdSJames Feist                                 if (lastSlash == std::string::npos)
11518bd25ccdSJames Feist                                 {
11528bd25ccdSJames Feist                                     // this should be impossible
11538bd25ccdSJames Feist                                     messages::internalError(
11548bd25ccdSJames Feist                                         sensorsAsyncResp->res);
11558bd25ccdSJames Feist                                     return;
11568bd25ccdSJames Feist                                 }
11578bd25ccdSJames Feist                                 std::string name = path.substr(lastSlash + 1);
11588bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
11598bd25ccdSJames Feist                                              ' ');
11608bd25ccdSJames Feist 
11618bd25ccdSJames Feist                                 std::string health;
11628bd25ccdSJames Feist 
11638bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
11648bd25ccdSJames Feist                                 {
11658bd25ccdSJames Feist                                     health = "OK";
11668bd25ccdSJames Feist                                 }
11678bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
11688bd25ccdSJames Feist                                 {
11698bd25ccdSJames Feist                                     health = "Warning";
11708bd25ccdSJames Feist                                 }
11718bd25ccdSJames Feist                                 else
11728bd25ccdSJames Feist                                 {
11738bd25ccdSJames Feist                                     health = "Critical";
11748bd25ccdSJames Feist                                 }
11758bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
11768bd25ccdSJames Feist                                 const auto& fanRedfish =
11778bd25ccdSJames Feist                                     sensorsAsyncResp->res.jsonValue["Fans"];
11788bd25ccdSJames Feist                                 for (const std::string& item : *collection)
11798bd25ccdSJames Feist                                 {
11808bd25ccdSJames Feist                                     lastSlash = item.rfind("/");
11818bd25ccdSJames Feist                                     // make a copy as collection is const
11828bd25ccdSJames Feist                                     std::string itemName =
11838bd25ccdSJames Feist                                         item.substr(lastSlash + 1);
11848bd25ccdSJames Feist                                     /*
11858bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
11868bd25ccdSJames Feist                                     std::replace(itemName.begin(),
11878bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
11888bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
11898bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
11908bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
11918bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
11928bd25ccdSJames Feist                                         });
11938bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
11948bd25ccdSJames Feist                                     {
11958bd25ccdSJames Feist                                         redfishCollection.push_back(
11968bd25ccdSJames Feist                                             {{"@odata.id",
11978bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
11988bd25ccdSJames Feist                                     }
11998bd25ccdSJames Feist                                     else
12008bd25ccdSJames Feist                                     {
12018bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
12028bd25ccdSJames Feist                                             << "failed to find fan in schema";
12038bd25ccdSJames Feist                                         messages::internalError(
12048bd25ccdSJames Feist                                             sensorsAsyncResp->res);
12058bd25ccdSJames Feist                                         return;
12068bd25ccdSJames Feist                                     }
12078bd25ccdSJames Feist                                 }
12088bd25ccdSJames Feist 
12093e9e72ebSKuiying Wang                                 size_t minNumNeeded =
12103e9e72ebSKuiying Wang                                     collection->size() > 0
12113e9e72ebSKuiying Wang                                         ? collection->size() - *allowedFailures
12123e9e72ebSKuiying Wang                                         : 0;
1213271584abSEd Tanous                                 nlohmann::json& jResp =
1214271584abSEd Tanous                                     sensorsAsyncResp->res
12158bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1216271584abSEd Tanous                                 jResp.push_back(
12178bd25ccdSJames Feist                                     {{"@odata.id",
1218717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
12198bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
12208bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
12218bd25ccdSJames Feist                                           "#/Redundancy/" +
1222271584abSEd Tanous                                           std::to_string(jResp.size())},
12238bd25ccdSJames Feist                                      {"@odata.type",
12248bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
12253e9e72ebSKuiying Wang                                      {"MinNumNeeded", minNumNeeded},
12268bd25ccdSJames Feist                                      {"MemberId", name},
12278bd25ccdSJames Feist                                      {"Mode", "N+m"},
12288bd25ccdSJames Feist                                      {"Name", name},
12298bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
12308bd25ccdSJames Feist                                      {"Status",
12318bd25ccdSJames Feist                                       {{"Health", health},
12328bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
12338bd25ccdSJames Feist                             },
12348bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
12358bd25ccdSJames Feist                             "GetAll",
12368bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
12378bd25ccdSJames Feist                     },
123802e92e32SJames Feist                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
12398bd25ccdSJames Feist                     "org.freedesktop.DBus.Properties", "Get",
12408bd25ccdSJames Feist                     "xyz.openbmc_project.Association", "endpoints");
12418bd25ccdSJames Feist             }
12428bd25ccdSJames Feist         },
12438bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
12448bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
12458bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
12468bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
12478bd25ccdSJames Feist         std::array<const char*, 1>{
12488bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
12498bd25ccdSJames Feist }
12508bd25ccdSJames Feist 
125123a21a1cSEd Tanous inline void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
125249c53ac9SJohnathan Mantey {
125349c53ac9SJohnathan Mantey     nlohmann::json& response = SensorsAsyncResp->res.jsonValue;
125449c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
1255a0ec28b6SAdrian Ambrożewicz     if (SensorsAsyncResp->chassisSubNode == sensors::node::power)
125649c53ac9SJohnathan Mantey     {
125749c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
125849c53ac9SJohnathan Mantey     }
125949c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
126049c53ac9SJohnathan Mantey     {
126149c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
126249c53ac9SJohnathan Mantey         if (entry != response.end())
126349c53ac9SJohnathan Mantey         {
126449c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
126549c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
126649c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
126749c53ac9SJohnathan Mantey                       });
126849c53ac9SJohnathan Mantey 
126949c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
127049c53ac9SJohnathan Mantey             size_t count = 0;
127149c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
127249c53ac9SJohnathan Mantey             {
127349c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
127449c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
127549c53ac9SJohnathan Mantey                 {
127649c53ac9SJohnathan Mantey                     continue;
127749c53ac9SJohnathan Mantey                 }
127849c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
127949c53ac9SJohnathan Mantey                 if (value != nullptr)
128049c53ac9SJohnathan Mantey                 {
128149c53ac9SJohnathan Mantey                     *value += std::to_string(count);
128249c53ac9SJohnathan Mantey                     count++;
1283a0ec28b6SAdrian Ambrożewicz                     SensorsAsyncResp->updateUri(sensorJson["Name"], *value);
128449c53ac9SJohnathan Mantey                 }
128549c53ac9SJohnathan Mantey             }
128649c53ac9SJohnathan Mantey         }
128749c53ac9SJohnathan Mantey     }
128849c53ac9SJohnathan Mantey }
128949c53ac9SJohnathan Mantey 
129008777fb0SLewanczyk, Dawid /**
1291adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1292adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1293adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1294adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
12958fb49dd6SShawn McCarney  */
129623a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1297adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1298adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
12998fb49dd6SShawn McCarney {
1300adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13018fb49dd6SShawn McCarney     {
1302adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13038fb49dd6SShawn McCarney         {
1304adc4f0dbSShawn McCarney             return &inventoryItem;
13058fb49dd6SShawn McCarney         }
13068fb49dd6SShawn McCarney     }
13078fb49dd6SShawn McCarney     return nullptr;
13088fb49dd6SShawn McCarney }
13098fb49dd6SShawn McCarney 
13108fb49dd6SShawn McCarney /**
1311adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1312adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1313adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1314adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13158fb49dd6SShawn McCarney  */
131623a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1317adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1318adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1319adc4f0dbSShawn McCarney {
1320adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1321adc4f0dbSShawn McCarney     {
1322adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1323adc4f0dbSShawn McCarney         {
1324adc4f0dbSShawn McCarney             return &inventoryItem;
1325adc4f0dbSShawn McCarney         }
1326adc4f0dbSShawn McCarney     }
1327adc4f0dbSShawn McCarney     return nullptr;
1328adc4f0dbSShawn McCarney }
1329adc4f0dbSShawn McCarney 
1330adc4f0dbSShawn McCarney /**
1331d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1332d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1333d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1334d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1335d500549bSAnthony Wilson  */
1336d500549bSAnthony Wilson inline InventoryItem*
1337d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1338d500549bSAnthony Wilson                             const std::string& ledObjPath)
1339d500549bSAnthony Wilson {
1340d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1341d500549bSAnthony Wilson     {
1342d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1343d500549bSAnthony Wilson         {
1344d500549bSAnthony Wilson             return &inventoryItem;
1345d500549bSAnthony Wilson         }
1346d500549bSAnthony Wilson     }
1347d500549bSAnthony Wilson     return nullptr;
1348d500549bSAnthony Wilson }
1349d500549bSAnthony Wilson 
1350d500549bSAnthony Wilson /**
1351adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1352adc4f0dbSShawn McCarney  *
1353adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1354adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1355adc4f0dbSShawn McCarney  * added to the vector.
1356adc4f0dbSShawn McCarney  *
1357adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1358adc4f0dbSShawn McCarney  * InventoryItem.
1359adc4f0dbSShawn McCarney  *
1360adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1361adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1362adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1363adc4f0dbSShawn McCarney  */
136423a21a1cSEd Tanous inline void
1365adc4f0dbSShawn McCarney     addInventoryItem(std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1366adc4f0dbSShawn McCarney                      const std::string& invItemObjPath,
1367adc4f0dbSShawn McCarney                      const std::string& sensorObjPath)
1368adc4f0dbSShawn McCarney {
1369adc4f0dbSShawn McCarney     // Look for inventory item in vector
1370adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1371adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1372adc4f0dbSShawn McCarney 
1373adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1374adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1375adc4f0dbSShawn McCarney     {
1376adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1377adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1378adc4f0dbSShawn McCarney     }
1379adc4f0dbSShawn McCarney 
1380adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1381adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1382adc4f0dbSShawn McCarney }
1383adc4f0dbSShawn McCarney 
1384adc4f0dbSShawn McCarney /**
1385adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1386adc4f0dbSShawn McCarney  *
1387adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1388adc4f0dbSShawn McCarney  * specified InventoryItem.
1389adc4f0dbSShawn McCarney  *
1390adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1391adc4f0dbSShawn McCarney  * response.
1392adc4f0dbSShawn McCarney  *
1393adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1394adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1395adc4f0dbSShawn McCarney  * for the specified inventory item.
1396adc4f0dbSShawn McCarney  */
139723a21a1cSEd Tanous inline void storeInventoryItemData(
1398adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
13998fb49dd6SShawn McCarney     const boost::container::flat_map<
14008fb49dd6SShawn McCarney         std::string, boost::container::flat_map<std::string, SensorVariant>>&
14018fb49dd6SShawn McCarney         interfacesDict)
14028fb49dd6SShawn McCarney {
1403adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1404adc4f0dbSShawn McCarney     auto interfaceIt =
1405adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1406adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
14078fb49dd6SShawn McCarney     {
1408adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Present");
1409adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
14108fb49dd6SShawn McCarney         {
1411adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1412adc4f0dbSShawn McCarney             if (value != nullptr)
14138fb49dd6SShawn McCarney             {
1414adc4f0dbSShawn McCarney                 inventoryItem.isPresent = *value;
14158fb49dd6SShawn McCarney             }
14168fb49dd6SShawn McCarney         }
14178fb49dd6SShawn McCarney     }
14188fb49dd6SShawn McCarney 
1419adc4f0dbSShawn McCarney     // Check if Inventory.Item.PowerSupply interface is present
1420adc4f0dbSShawn McCarney     interfaceIt =
1421adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1422adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
14238fb49dd6SShawn McCarney     {
1424adc4f0dbSShawn McCarney         inventoryItem.isPowerSupply = true;
14258fb49dd6SShawn McCarney     }
1426adc4f0dbSShawn McCarney 
1427adc4f0dbSShawn McCarney     // Get properties from Inventory.Decorator.Asset interface
1428adc4f0dbSShawn McCarney     interfaceIt =
1429adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1430adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1431adc4f0dbSShawn McCarney     {
1432adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Manufacturer");
1433adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1434adc4f0dbSShawn McCarney         {
1435adc4f0dbSShawn McCarney             const std::string* value =
1436adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1437adc4f0dbSShawn McCarney             if (value != nullptr)
1438adc4f0dbSShawn McCarney             {
1439adc4f0dbSShawn McCarney                 inventoryItem.manufacturer = *value;
1440adc4f0dbSShawn McCarney             }
1441adc4f0dbSShawn McCarney         }
1442adc4f0dbSShawn McCarney 
1443adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("Model");
1444adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1445adc4f0dbSShawn McCarney         {
1446adc4f0dbSShawn McCarney             const std::string* value =
1447adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1448adc4f0dbSShawn McCarney             if (value != nullptr)
1449adc4f0dbSShawn McCarney             {
1450adc4f0dbSShawn McCarney                 inventoryItem.model = *value;
1451adc4f0dbSShawn McCarney             }
1452adc4f0dbSShawn McCarney         }
1453adc4f0dbSShawn McCarney 
1454adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("PartNumber");
1455adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1456adc4f0dbSShawn McCarney         {
1457adc4f0dbSShawn McCarney             const std::string* value =
1458adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1459adc4f0dbSShawn McCarney             if (value != nullptr)
1460adc4f0dbSShawn McCarney             {
1461adc4f0dbSShawn McCarney                 inventoryItem.partNumber = *value;
1462adc4f0dbSShawn McCarney             }
1463adc4f0dbSShawn McCarney         }
1464adc4f0dbSShawn McCarney 
1465adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("SerialNumber");
1466adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1467adc4f0dbSShawn McCarney         {
1468adc4f0dbSShawn McCarney             const std::string* value =
1469adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1470adc4f0dbSShawn McCarney             if (value != nullptr)
1471adc4f0dbSShawn McCarney             {
1472adc4f0dbSShawn McCarney                 inventoryItem.serialNumber = *value;
1473adc4f0dbSShawn McCarney             }
1474adc4f0dbSShawn McCarney         }
1475adc4f0dbSShawn McCarney     }
1476adc4f0dbSShawn McCarney 
1477adc4f0dbSShawn McCarney     // Get properties from State.Decorator.OperationalStatus interface
1478adc4f0dbSShawn McCarney     interfaceIt = interfacesDict.find(
1479adc4f0dbSShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus");
1480adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1481adc4f0dbSShawn McCarney     {
1482adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Functional");
1483adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1484adc4f0dbSShawn McCarney         {
1485adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1486adc4f0dbSShawn McCarney             if (value != nullptr)
1487adc4f0dbSShawn McCarney             {
1488adc4f0dbSShawn McCarney                 inventoryItem.isFunctional = *value;
14898fb49dd6SShawn McCarney             }
14908fb49dd6SShawn McCarney         }
14918fb49dd6SShawn McCarney     }
14928fb49dd6SShawn McCarney }
14938fb49dd6SShawn McCarney 
14948fb49dd6SShawn McCarney /**
1495adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
14968fb49dd6SShawn McCarney  *
1497adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1498adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1499adc4f0dbSShawn McCarney  * inventoryItems vector.
15008fb49dd6SShawn McCarney  *
1501adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1502adc4f0dbSShawn McCarney  * response.
1503adc4f0dbSShawn McCarney  *
1504adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1505adc4f0dbSShawn McCarney  * been obtained.
1506adc4f0dbSShawn McCarney  *
1507adc4f0dbSShawn McCarney  * The callback must have the following signature:
1508adc4f0dbSShawn McCarney  *   @code
1509d500549bSAnthony Wilson  *   callback(void)
1510adc4f0dbSShawn McCarney  *   @endcode
1511adc4f0dbSShawn McCarney  *
1512adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1513adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1514adc4f0dbSShawn McCarney  * last asynchronous function has completed.
15158fb49dd6SShawn McCarney  *
15168fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1517adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1518adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
15198fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
15208fb49dd6SShawn McCarney  * implements ObjectManager.
1521adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1522adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1523adc4f0dbSShawn McCarney  * in recursive calls to this function.
15248fb49dd6SShawn McCarney  */
1525adc4f0dbSShawn McCarney template <typename Callback>
1526adc4f0dbSShawn McCarney static void getInventoryItemsData(
15278fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1528adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
15298fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
15308fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1531adc4f0dbSShawn McCarney         objectMgrPaths,
1532271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
15338fb49dd6SShawn McCarney {
1534adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
15358fb49dd6SShawn McCarney 
1536adc4f0dbSShawn McCarney     // If no more connections left, call callback
1537adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
15388fb49dd6SShawn McCarney     {
1539d500549bSAnthony Wilson         callback();
1540adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1541adc4f0dbSShawn McCarney         return;
1542adc4f0dbSShawn McCarney     }
1543adc4f0dbSShawn McCarney 
1544adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1545adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1546adc4f0dbSShawn McCarney     if (it != invConnections->end())
1547adc4f0dbSShawn McCarney     {
1548adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1549adc4f0dbSShawn McCarney 
15508fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1551adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1552adc4f0dbSShawn McCarney                             objectMgrPaths, callback{std::move(callback)},
1553adc4f0dbSShawn McCarney                             invConnectionsIndex](
1554adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
15558fb49dd6SShawn McCarney                                ManagedObjectsVectorType& resp) {
1556adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
15578fb49dd6SShawn McCarney             if (ec)
15588fb49dd6SShawn McCarney             {
15598fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1560adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
15618fb49dd6SShawn McCarney                 messages::internalError(sensorsAsyncResp->res);
15628fb49dd6SShawn McCarney                 return;
15638fb49dd6SShawn McCarney             }
15648fb49dd6SShawn McCarney 
15658fb49dd6SShawn McCarney             // Loop through returned object paths
15668fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
15678fb49dd6SShawn McCarney             {
15688fb49dd6SShawn McCarney                 const std::string& objPath =
15698fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
15708fb49dd6SShawn McCarney 
1571adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1572adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1573adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1574adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
15758fb49dd6SShawn McCarney                 {
1576adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1577adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
15788fb49dd6SShawn McCarney                 }
15798fb49dd6SShawn McCarney             }
15808fb49dd6SShawn McCarney 
1581adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1582adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1583adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1584adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1585adc4f0dbSShawn McCarney 
1586adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
15878fb49dd6SShawn McCarney         };
15888fb49dd6SShawn McCarney 
15898fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
15908fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
15918fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
15928fb49dd6SShawn McCarney         const std::string& objectMgrPath =
15938fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
15948fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
15958fb49dd6SShawn McCarney                          << objectMgrPath;
15968fb49dd6SShawn McCarney 
15978fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
15988fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
15998fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16008fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16018fb49dd6SShawn McCarney     }
16028fb49dd6SShawn McCarney 
1603adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16048fb49dd6SShawn McCarney }
16058fb49dd6SShawn McCarney 
16068fb49dd6SShawn McCarney /**
1607adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16088fb49dd6SShawn McCarney  *
1609adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1610adc4f0dbSShawn McCarney  * items that are associated with sensors.
16118fb49dd6SShawn McCarney  *
16128fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
16138fb49dd6SShawn McCarney  * been obtained.
16148fb49dd6SShawn McCarney  *
16158fb49dd6SShawn McCarney  * The callback must have the following signature:
16168fb49dd6SShawn McCarney  *   @code
16178fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
16188fb49dd6SShawn McCarney  *            invConnections)
16198fb49dd6SShawn McCarney  *   @endcode
16208fb49dd6SShawn McCarney  *
16218fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1622adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
16238fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
16248fb49dd6SShawn McCarney  */
16258fb49dd6SShawn McCarney template <typename Callback>
16268fb49dd6SShawn McCarney static void getInventoryItemsConnections(
16278fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1628adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
16298fb49dd6SShawn McCarney     Callback&& callback)
16308fb49dd6SShawn McCarney {
16318fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
16328fb49dd6SShawn McCarney 
16338fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1634adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
16358fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1636adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1637adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
16388fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
16398fb49dd6SShawn McCarney 
16408fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
16418fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1642adc4f0dbSShawn McCarney                         inventoryItems](const boost::system::error_code ec,
16438fb49dd6SShawn McCarney                                         const GetSubTreeType& subtree) {
16448fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
16458fb49dd6SShawn McCarney         if (ec)
16468fb49dd6SShawn McCarney         {
16478fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
16488fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
16498fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
16508fb49dd6SShawn McCarney             return;
16518fb49dd6SShawn McCarney         }
16528fb49dd6SShawn McCarney 
16538fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
16548fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
16558fb49dd6SShawn McCarney             invConnections =
16568fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
16578fb49dd6SShawn McCarney         invConnections->reserve(8);
16588fb49dd6SShawn McCarney 
16598fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
16608fb49dd6SShawn McCarney         for (const std::pair<
16618fb49dd6SShawn McCarney                  std::string,
16628fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
16638fb49dd6SShawn McCarney                  object : subtree)
16648fb49dd6SShawn McCarney         {
1665adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
16668fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1667adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
16688fb49dd6SShawn McCarney             {
16698fb49dd6SShawn McCarney                 // Store all connections to inventory item
16708fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
16718fb49dd6SShawn McCarney                          objData : object.second)
16728fb49dd6SShawn McCarney                 {
16738fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
16748fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
16758fb49dd6SShawn McCarney                 }
16768fb49dd6SShawn McCarney             }
16778fb49dd6SShawn McCarney         }
1678d500549bSAnthony Wilson 
16798fb49dd6SShawn McCarney         callback(invConnections);
16808fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
16818fb49dd6SShawn McCarney     };
16828fb49dd6SShawn McCarney 
16838fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
16848fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
16858fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
16868fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
16878fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
16888fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
16898fb49dd6SShawn McCarney }
16908fb49dd6SShawn McCarney 
16918fb49dd6SShawn McCarney /**
1692adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
16938fb49dd6SShawn McCarney  *
16948fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1695d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1696d500549bSAnthony Wilson  * their LEDs, if any.
16978fb49dd6SShawn McCarney  *
16988fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
16998fb49dd6SShawn McCarney  * has been obtained.
17008fb49dd6SShawn McCarney  *
17018fb49dd6SShawn McCarney  * The callback must have the following signature:
17028fb49dd6SShawn McCarney  *   @code
1703adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17048fb49dd6SShawn McCarney  *   @endcode
17058fb49dd6SShawn McCarney  *
17068fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17078fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17088fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
17098fb49dd6SShawn McCarney  * implements ObjectManager.
17108fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17118fb49dd6SShawn McCarney  */
17128fb49dd6SShawn McCarney template <typename Callback>
1713adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
17148fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
17158fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
17168fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
17178fb49dd6SShawn McCarney         objectMgrPaths,
17188fb49dd6SShawn McCarney     Callback&& callback)
17198fb49dd6SShawn McCarney {
1720adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
17218fb49dd6SShawn McCarney 
17228fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
17238fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
17248fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
17258fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1726adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
17278fb49dd6SShawn McCarney         if (ec)
17288fb49dd6SShawn McCarney         {
1729adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1730adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
17318fb49dd6SShawn McCarney             messages::internalError(sensorsAsyncResp->res);
17328fb49dd6SShawn McCarney             return;
17338fb49dd6SShawn McCarney         }
17348fb49dd6SShawn McCarney 
1735adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1736adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1737adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1738adc4f0dbSShawn McCarney 
17398fb49dd6SShawn McCarney         // Loop through returned object paths
17408fb49dd6SShawn McCarney         std::string sensorAssocPath;
17418fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
17428fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
17438fb49dd6SShawn McCarney         {
17448fb49dd6SShawn McCarney             const std::string& objPath =
17458fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
17468fb49dd6SShawn McCarney             const boost::container::flat_map<
17478fb49dd6SShawn McCarney                 std::string, boost::container::flat_map<
17488fb49dd6SShawn McCarney                                  std::string, dbus::utility::DbusVariantType>>&
17498fb49dd6SShawn McCarney                 interfacesDict = objDictEntry.second;
17508fb49dd6SShawn McCarney 
17518fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
17528fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
17538fb49dd6SShawn McCarney             {
17548fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
17558fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
17568fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
17578fb49dd6SShawn McCarney                 {
17588fb49dd6SShawn McCarney                     // Get Association interface for object path
17598fb49dd6SShawn McCarney                     auto assocIt =
17608fb49dd6SShawn McCarney                         interfacesDict.find("xyz.openbmc_project.Association");
17618fb49dd6SShawn McCarney                     if (assocIt != interfacesDict.end())
17628fb49dd6SShawn McCarney                     {
17638fb49dd6SShawn McCarney                         // Get inventory item from end point
17648fb49dd6SShawn McCarney                         auto endpointsIt = assocIt->second.find("endpoints");
17658fb49dd6SShawn McCarney                         if (endpointsIt != assocIt->second.end())
17668fb49dd6SShawn McCarney                         {
17678fb49dd6SShawn McCarney                             const std::vector<std::string>* endpoints =
17688fb49dd6SShawn McCarney                                 std::get_if<std::vector<std::string>>(
17698fb49dd6SShawn McCarney                                     &endpointsIt->second);
17708fb49dd6SShawn McCarney                             if ((endpoints != nullptr) && !endpoints->empty())
17718fb49dd6SShawn McCarney                             {
1772adc4f0dbSShawn McCarney                                 // Add inventory item to vector
1773adc4f0dbSShawn McCarney                                 const std::string& invItemPath =
1774adc4f0dbSShawn McCarney                                     endpoints->front();
1775adc4f0dbSShawn McCarney                                 addInventoryItem(inventoryItems, invItemPath,
1776adc4f0dbSShawn McCarney                                                  sensorName);
17778fb49dd6SShawn McCarney                             }
17788fb49dd6SShawn McCarney                         }
17798fb49dd6SShawn McCarney                     }
17808fb49dd6SShawn McCarney                     break;
17818fb49dd6SShawn McCarney                 }
17828fb49dd6SShawn McCarney             }
17838fb49dd6SShawn McCarney         }
17848fb49dd6SShawn McCarney 
1785d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1786d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1787d500549bSAnthony Wilson         std::string inventoryAssocPath;
1788d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1789d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1790d500549bSAnthony Wilson         {
1791d500549bSAnthony Wilson             const std::string& objPath =
1792d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1793d500549bSAnthony Wilson             const boost::container::flat_map<
1794d500549bSAnthony Wilson                 std::string, boost::container::flat_map<
1795d500549bSAnthony Wilson                                  std::string, dbus::utility::DbusVariantType>>&
1796d500549bSAnthony Wilson                 interfacesDict = objDictEntry.second;
1797d500549bSAnthony Wilson 
1798d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1799d500549bSAnthony Wilson             {
1800d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1801d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1802d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1803d500549bSAnthony Wilson                 {
1804d500549bSAnthony Wilson                     // Get Association interface for object path
1805d500549bSAnthony Wilson                     auto assocIt =
1806d500549bSAnthony Wilson                         interfacesDict.find("xyz.openbmc_project.Association");
1807d500549bSAnthony Wilson                     if (assocIt != interfacesDict.end())
1808d500549bSAnthony Wilson                     {
1809d500549bSAnthony Wilson                         // Get inventory item from end point
1810d500549bSAnthony Wilson                         auto endpointsIt = assocIt->second.find("endpoints");
1811d500549bSAnthony Wilson                         if (endpointsIt != assocIt->second.end())
1812d500549bSAnthony Wilson                         {
1813d500549bSAnthony Wilson                             const std::vector<std::string>* endpoints =
1814d500549bSAnthony Wilson                                 std::get_if<std::vector<std::string>>(
1815d500549bSAnthony Wilson                                     &endpointsIt->second);
1816d500549bSAnthony Wilson                             if ((endpoints != nullptr) && !endpoints->empty())
1817d500549bSAnthony Wilson                             {
1818d500549bSAnthony Wilson                                 // Store LED path in inventory item
1819d500549bSAnthony Wilson                                 const std::string& ledPath = endpoints->front();
1820d500549bSAnthony Wilson                                 inventoryItem.ledObjectPath = ledPath;
1821d500549bSAnthony Wilson                             }
1822d500549bSAnthony Wilson                         }
1823d500549bSAnthony Wilson                     }
1824d500549bSAnthony Wilson                     break;
1825d500549bSAnthony Wilson                 }
1826d500549bSAnthony Wilson             }
1827d500549bSAnthony Wilson         }
1828adc4f0dbSShawn McCarney         callback(inventoryItems);
1829adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
18308fb49dd6SShawn McCarney     };
18318fb49dd6SShawn McCarney 
18328fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
18338fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
18348fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
18358fb49dd6SShawn McCarney     const std::string& objectMgrPath =
18368fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
18378fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
18388fb49dd6SShawn McCarney                      << objectMgrPath;
18398fb49dd6SShawn McCarney 
18408fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
18418fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
18428fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
18438fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18448fb49dd6SShawn McCarney 
1845adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
18468fb49dd6SShawn McCarney }
18478fb49dd6SShawn McCarney 
18488fb49dd6SShawn McCarney /**
1849d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1850d500549bSAnthony Wilson  *
1851d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1852d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1853d500549bSAnthony Wilson  * inventoryItems vector.
1854d500549bSAnthony Wilson  *
1855d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1856d500549bSAnthony Wilson  * response.
1857d500549bSAnthony Wilson  *
1858d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1859d500549bSAnthony Wilson  * has been obtained.
1860d500549bSAnthony Wilson  *
1861d500549bSAnthony Wilson  * The callback must have the following signature:
1862d500549bSAnthony Wilson  *   @code
186342cbe538SGunnar Mills  *   callback()
1864d500549bSAnthony Wilson  *   @endcode
1865d500549bSAnthony Wilson  *
1866d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1867d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1868d500549bSAnthony Wilson  * last asynchronous function has completed.
1869d500549bSAnthony Wilson  *
1870d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1871d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1872d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1873d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1874d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1875d500549bSAnthony Wilson  * in recursive calls to this function.
1876d500549bSAnthony Wilson  */
1877d500549bSAnthony Wilson template <typename Callback>
1878d500549bSAnthony Wilson void getInventoryLedData(
1879d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1880d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1881d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1882d500549bSAnthony Wilson         ledConnections,
1883d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1884d500549bSAnthony Wilson {
1885d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1886d500549bSAnthony Wilson 
1887d500549bSAnthony Wilson     // If no more connections left, call callback
1888d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1889d500549bSAnthony Wilson     {
189042cbe538SGunnar Mills         callback();
1891d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1892d500549bSAnthony Wilson         return;
1893d500549bSAnthony Wilson     }
1894d500549bSAnthony Wilson 
1895d500549bSAnthony Wilson     // Get inventory item data from current connection
1896d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1897d500549bSAnthony Wilson     if (it != ledConnections->end())
1898d500549bSAnthony Wilson     {
1899d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1900d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1901d500549bSAnthony Wilson         // Response handler for Get State property
1902d500549bSAnthony Wilson         auto respHandler =
1903d500549bSAnthony Wilson             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1904d500549bSAnthony Wilson              callback{std::move(callback)},
1905d500549bSAnthony Wilson              ledConnectionsIndex](const boost::system::error_code ec,
1906d500549bSAnthony Wilson                                   const std::variant<std::string>& ledState) {
1907d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1908d500549bSAnthony Wilson                 if (ec)
1909d500549bSAnthony Wilson                 {
1910d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
1911d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
1912d500549bSAnthony Wilson                     messages::internalError(sensorsAsyncResp->res);
1913d500549bSAnthony Wilson                     return;
1914d500549bSAnthony Wilson                 }
1915d500549bSAnthony Wilson 
1916d500549bSAnthony Wilson                 const std::string* state = std::get_if<std::string>(&ledState);
1917d500549bSAnthony Wilson                 if (state != nullptr)
1918d500549bSAnthony Wilson                 {
1919d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Led state: " << *state;
1920d500549bSAnthony Wilson                     // Find inventory item with this LED object path
1921d500549bSAnthony Wilson                     InventoryItem* inventoryItem =
1922d500549bSAnthony Wilson                         findInventoryItemForLed(*inventoryItems, ledPath);
1923d500549bSAnthony Wilson                     if (inventoryItem != nullptr)
1924d500549bSAnthony Wilson                     {
1925d500549bSAnthony Wilson                         // Store LED state in InventoryItem
1926d500549bSAnthony Wilson                         if (boost::ends_with(*state, "On"))
1927d500549bSAnthony Wilson                         {
1928d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::ON;
1929d500549bSAnthony Wilson                         }
1930d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Blink"))
1931d500549bSAnthony Wilson                         {
1932d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::BLINK;
1933d500549bSAnthony Wilson                         }
1934d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Off"))
1935d500549bSAnthony Wilson                         {
1936d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::OFF;
1937d500549bSAnthony Wilson                         }
1938d500549bSAnthony Wilson                         else
1939d500549bSAnthony Wilson                         {
1940d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::UNKNOWN;
1941d500549bSAnthony Wilson                         }
1942d500549bSAnthony Wilson                     }
1943d500549bSAnthony Wilson                 }
1944d500549bSAnthony Wilson                 else
1945d500549bSAnthony Wilson                 {
1946d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
1947d500549bSAnthony Wilson                                      << ledPath;
1948d500549bSAnthony Wilson                 }
1949d500549bSAnthony Wilson 
1950d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1951d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1952d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1953d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1954d500549bSAnthony Wilson 
1955d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1956d500549bSAnthony Wilson             };
1957d500549bSAnthony Wilson 
1958d500549bSAnthony Wilson         // Get the State property for the current LED
1959d500549bSAnthony Wilson         crow::connections::systemBus->async_method_call(
1960d500549bSAnthony Wilson             std::move(respHandler), ledConnection, ledPath,
1961d500549bSAnthony Wilson             "org.freedesktop.DBus.Properties", "Get",
1962d500549bSAnthony Wilson             "xyz.openbmc_project.Led.Physical", "State");
1963d500549bSAnthony Wilson     }
1964d500549bSAnthony Wilson 
1965d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1966d500549bSAnthony Wilson }
1967d500549bSAnthony Wilson 
1968d500549bSAnthony Wilson /**
1969d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1970d500549bSAnthony Wilson  *
1971d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1972d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1973d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1974d500549bSAnthony Wilson  *
1975d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1976d500549bSAnthony Wilson  * response.
1977d500549bSAnthony Wilson  *
1978d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1979d500549bSAnthony Wilson  * been obtained.
1980d500549bSAnthony Wilson  *
1981d500549bSAnthony Wilson  * The callback must have the following signature:
1982d500549bSAnthony Wilson  *   @code
198342cbe538SGunnar Mills  *   callback()
1984d500549bSAnthony Wilson  *   @endcode
1985d500549bSAnthony Wilson  *
1986d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1987d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1988d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1989d500549bSAnthony Wilson  */
1990d500549bSAnthony Wilson template <typename Callback>
1991d500549bSAnthony Wilson void getInventoryLeds(
1992d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1993d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1994d500549bSAnthony Wilson     Callback&& callback)
1995d500549bSAnthony Wilson {
1996d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1997d500549bSAnthony Wilson 
1998d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1999d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2000d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2001d500549bSAnthony Wilson 
2002d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2003d500549bSAnthony Wilson     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2004d500549bSAnthony Wilson                         inventoryItems](const boost::system::error_code ec,
2005d500549bSAnthony Wilson                                         const GetSubTreeType& subtree) {
2006d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2007d500549bSAnthony Wilson         if (ec)
2008d500549bSAnthony Wilson         {
2009d500549bSAnthony Wilson             messages::internalError(sensorsAsyncResp->res);
2010d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2011d500549bSAnthony Wilson                              << ec;
2012d500549bSAnthony Wilson             return;
2013d500549bSAnthony Wilson         }
2014d500549bSAnthony Wilson 
2015d500549bSAnthony Wilson         // Build map of LED object paths to connections
2016d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2017d500549bSAnthony Wilson             ledConnections = std::make_shared<
2018d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2019d500549bSAnthony Wilson 
2020d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2021d500549bSAnthony Wilson         for (const std::pair<
2022d500549bSAnthony Wilson                  std::string,
2023d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2024d500549bSAnthony Wilson                  object : subtree)
2025d500549bSAnthony Wilson         {
2026d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2027d500549bSAnthony Wilson             // items
2028d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2029d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2030d500549bSAnthony Wilson             {
2031d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2032d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2033d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2034d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2035d500549bSAnthony Wilson                                  << connection;
2036d500549bSAnthony Wilson             }
2037d500549bSAnthony Wilson         }
2038d500549bSAnthony Wilson 
2039d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2040d500549bSAnthony Wilson                             std::move(callback));
2041d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2042d500549bSAnthony Wilson     };
2043d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2044d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2045d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2046d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2047d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2048d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2049d500549bSAnthony Wilson }
2050d500549bSAnthony Wilson 
2051d500549bSAnthony Wilson /**
205242cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
205342cbe538SGunnar Mills  *
205442cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
205542cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
205642cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
205742cbe538SGunnar Mills  *
205842cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
205942cbe538SGunnar Mills  * response.
206042cbe538SGunnar Mills  *
206142cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
206242cbe538SGunnar Mills  * when data has been obtained.
206342cbe538SGunnar Mills  *
206442cbe538SGunnar Mills  * The callback must have the following signature:
206542cbe538SGunnar Mills  *   @code
206642cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
206742cbe538SGunnar Mills  *   @endcode
206842cbe538SGunnar Mills  *
206942cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
207042cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
207142cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
207242cbe538SGunnar Mills  *        Supply Attributes
207342cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
207442cbe538SGunnar Mills  */
207542cbe538SGunnar Mills template <typename Callback>
207642cbe538SGunnar Mills void getPowerSupplyAttributesData(
207742cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
207842cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
207942cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
208042cbe538SGunnar Mills         psAttributesConnections,
208142cbe538SGunnar Mills     Callback&& callback)
208242cbe538SGunnar Mills {
208342cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
208442cbe538SGunnar Mills 
208542cbe538SGunnar Mills     if (psAttributesConnections.empty())
208642cbe538SGunnar Mills     {
208742cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
208842cbe538SGunnar Mills         callback(inventoryItems);
208942cbe538SGunnar Mills         return;
209042cbe538SGunnar Mills     }
209142cbe538SGunnar Mills 
209242cbe538SGunnar Mills     // Assuming just one connection (service) for now
209342cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
209442cbe538SGunnar Mills 
209542cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
209642cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
209742cbe538SGunnar Mills 
209842cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
209942cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
210042cbe538SGunnar Mills                         callback{std::move(callback)}](
210142cbe538SGunnar Mills                            const boost::system::error_code ec,
210242cbe538SGunnar Mills                            const std::variant<uint32_t>& deratingFactor) {
210342cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
210442cbe538SGunnar Mills         if (ec)
210542cbe538SGunnar Mills         {
210642cbe538SGunnar Mills             BMCWEB_LOG_ERROR
210742cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
210842cbe538SGunnar Mills             messages::internalError(sensorsAsyncResp->res);
210942cbe538SGunnar Mills             return;
211042cbe538SGunnar Mills         }
211142cbe538SGunnar Mills 
211242cbe538SGunnar Mills         const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
211342cbe538SGunnar Mills         if (value != nullptr)
211442cbe538SGunnar Mills         {
211542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
211642cbe538SGunnar Mills             // Store value in Power Supply Inventory Items
211742cbe538SGunnar Mills             for (InventoryItem& inventoryItem : *inventoryItems)
211842cbe538SGunnar Mills             {
211942cbe538SGunnar Mills                 if (inventoryItem.isPowerSupply == true)
212042cbe538SGunnar Mills                 {
212142cbe538SGunnar Mills                     inventoryItem.powerSupplyEfficiencyPercent =
212242cbe538SGunnar Mills                         static_cast<int>(*value);
212342cbe538SGunnar Mills                 }
212442cbe538SGunnar Mills             }
212542cbe538SGunnar Mills         }
212642cbe538SGunnar Mills         else
212742cbe538SGunnar Mills         {
212842cbe538SGunnar Mills             BMCWEB_LOG_DEBUG
212942cbe538SGunnar Mills                 << "Failed to find EfficiencyPercent value for PowerSupplies";
213042cbe538SGunnar Mills         }
213142cbe538SGunnar Mills 
213242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
213342cbe538SGunnar Mills         callback(inventoryItems);
213442cbe538SGunnar Mills     };
213542cbe538SGunnar Mills 
213642cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
213742cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
213842cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
213942cbe538SGunnar Mills         std::move(respHandler), psAttributesConnection, psAttributesPath,
214042cbe538SGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
214142cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
214242cbe538SGunnar Mills 
214342cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
214442cbe538SGunnar Mills }
214542cbe538SGunnar Mills 
214642cbe538SGunnar Mills /**
214742cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
214842cbe538SGunnar Mills  *
214942cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
215042cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
215142cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
215242cbe538SGunnar Mills  * item.
215342cbe538SGunnar Mills  *
215442cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
215542cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
215642cbe538SGunnar Mills  *
215742cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
215842cbe538SGunnar Mills  * when information has been obtained.
215942cbe538SGunnar Mills  *
216042cbe538SGunnar Mills  * The callback must have the following signature:
216142cbe538SGunnar Mills  *   @code
216242cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
216342cbe538SGunnar Mills  *   @endcode
216442cbe538SGunnar Mills  *
216542cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
216642cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
216742cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
216842cbe538SGunnar Mills  */
216942cbe538SGunnar Mills template <typename Callback>
217042cbe538SGunnar Mills void getPowerSupplyAttributes(
217142cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
217242cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
217342cbe538SGunnar Mills     Callback&& callback)
217442cbe538SGunnar Mills {
217542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
217642cbe538SGunnar Mills 
217742cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2178a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
217942cbe538SGunnar Mills     {
218042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
218142cbe538SGunnar Mills         callback(inventoryItems);
218242cbe538SGunnar Mills         return;
218342cbe538SGunnar Mills     }
218442cbe538SGunnar Mills 
218542cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
218642cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
218742cbe538SGunnar Mills 
218842cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
218942cbe538SGunnar Mills     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
219042cbe538SGunnar Mills                         inventoryItems](const boost::system::error_code ec,
219142cbe538SGunnar Mills                                         const GetSubTreeType& subtree) {
219242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
219342cbe538SGunnar Mills         if (ec)
219442cbe538SGunnar Mills         {
219542cbe538SGunnar Mills             messages::internalError(sensorsAsyncResp->res);
219642cbe538SGunnar Mills             BMCWEB_LOG_ERROR
219742cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
219842cbe538SGunnar Mills             return;
219942cbe538SGunnar Mills         }
220042cbe538SGunnar Mills         if (subtree.size() == 0)
220142cbe538SGunnar Mills         {
220242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
220342cbe538SGunnar Mills             callback(inventoryItems);
220442cbe538SGunnar Mills             return;
220542cbe538SGunnar Mills         }
220642cbe538SGunnar Mills 
220742cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
220842cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
220942cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
221042cbe538SGunnar Mills         boost::container::flat_map<std::string, std::string>
221142cbe538SGunnar Mills             psAttributesConnections;
221242cbe538SGunnar Mills 
221342cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
221442cbe538SGunnar Mills         {
221542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
221642cbe538SGunnar Mills             callback(inventoryItems);
221742cbe538SGunnar Mills             return;
221842cbe538SGunnar Mills         }
221942cbe538SGunnar Mills 
222042cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
222142cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
222242cbe538SGunnar Mills 
222342cbe538SGunnar Mills         if (connection.empty())
222442cbe538SGunnar Mills         {
222542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
222642cbe538SGunnar Mills             callback(inventoryItems);
222742cbe538SGunnar Mills             return;
222842cbe538SGunnar Mills         }
222942cbe538SGunnar Mills 
223042cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
223142cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
223242cbe538SGunnar Mills                          << connection;
223342cbe538SGunnar Mills 
223442cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
223542cbe538SGunnar Mills                                      psAttributesConnections,
223642cbe538SGunnar Mills                                      std::move(callback));
223742cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
223842cbe538SGunnar Mills     };
223942cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
224042cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
224142cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
224242cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
224342cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
224442cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
224542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
224642cbe538SGunnar Mills }
224742cbe538SGunnar Mills 
224842cbe538SGunnar Mills /**
2249adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
22508fb49dd6SShawn McCarney  *
22518fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2252adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
22538fb49dd6SShawn McCarney  *
2254adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2255adc4f0dbSShawn McCarney  * response.
22568fb49dd6SShawn McCarney  *
2257adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2258adc4f0dbSShawn McCarney  * inventory items have been obtained.
2259adc4f0dbSShawn McCarney  *
2260adc4f0dbSShawn McCarney  * The callback must have the following signature:
2261adc4f0dbSShawn McCarney  *   @code
2262adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2263adc4f0dbSShawn McCarney  *   @endcode
22648fb49dd6SShawn McCarney  *
22658fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
22668fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
22678fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
22688fb49dd6SShawn McCarney  * implements ObjectManager.
2269adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
22708fb49dd6SShawn McCarney  */
2271adc4f0dbSShawn McCarney template <typename Callback>
2272adc4f0dbSShawn McCarney static void getInventoryItems(
22738fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
22748fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
22758fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2276adc4f0dbSShawn McCarney         objectMgrPaths,
2277adc4f0dbSShawn McCarney     Callback&& callback)
22788fb49dd6SShawn McCarney {
2279adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2280adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2281adc4f0dbSShawn McCarney         [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2282adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2283adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
22848fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2285adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2286adc4f0dbSShawn McCarney                  callback{std::move(callback)}](
22878fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
22888fb49dd6SShawn McCarney                         invConnections) {
22898fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2290d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2291d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2292d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2293d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
229442cbe538SGunnar Mills 
229542cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
229642cbe538SGunnar Mills                                                        inventoryItems,
229742cbe538SGunnar Mills                                                        callback{std::move(
229842cbe538SGunnar Mills                                                            callback)}]() {
229942cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
230042cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
230142cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
230242cbe538SGunnar Mills                                                          inventoryItems,
230342cbe538SGunnar Mills                                                          std::move(callback));
230442cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
230542cbe538SGunnar Mills                             };
230642cbe538SGunnar Mills 
2307d500549bSAnthony Wilson                             // Find led connections and get the data
2308d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
230942cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2310d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2311d500549bSAnthony Wilson                         };
23128fb49dd6SShawn McCarney 
2313adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2314adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2315adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2316d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
23178fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
23188fb49dd6SShawn McCarney                 };
23198fb49dd6SShawn McCarney 
2320adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
23218fb49dd6SShawn McCarney             getInventoryItemsConnections(
2322adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
23238fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2324adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
23258fb49dd6SShawn McCarney         };
23268fb49dd6SShawn McCarney 
2327adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2328adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2329adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2330adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2331adc4f0dbSShawn McCarney }
2332adc4f0dbSShawn McCarney 
2333adc4f0dbSShawn McCarney /**
2334adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2335adc4f0dbSShawn McCarney  *
2336adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2337adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2338adc4f0dbSShawn McCarney  * array.
2339adc4f0dbSShawn McCarney  *
2340adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2341adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2342adc4f0dbSShawn McCarney  * object.
2343adc4f0dbSShawn McCarney  *
2344adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2345adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2346adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2347adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2348adc4f0dbSShawn McCarney  */
234923a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2350adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2351adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2352adc4f0dbSShawn McCarney {
2353adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2354adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2355adc4f0dbSShawn McCarney     {
2356adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2357adc4f0dbSShawn McCarney         {
2358adc4f0dbSShawn McCarney             return powerSupply;
2359adc4f0dbSShawn McCarney         }
2360adc4f0dbSShawn McCarney     }
2361adc4f0dbSShawn McCarney 
2362adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2363adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2364adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2365adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2366adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2367adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2368adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2369adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2370adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2371adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2372adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2373d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2374adc4f0dbSShawn McCarney 
237542cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
237642cbe538SGunnar Mills     {
237742cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
237842cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
237942cbe538SGunnar Mills     }
238042cbe538SGunnar Mills 
238142cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2382adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2383adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2384adc4f0dbSShawn McCarney 
2385adc4f0dbSShawn McCarney     return powerSupply;
23868fb49dd6SShawn McCarney }
23878fb49dd6SShawn McCarney 
23888fb49dd6SShawn McCarney /**
2389de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2390de629b6eSShawn McCarney  *
2391de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2392de629b6eSShawn McCarney  *
2393de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2394de629b6eSShawn McCarney  * information has been obtained.
2395de629b6eSShawn McCarney  *
2396adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2397de629b6eSShawn McCarney  *
2398de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2399de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2400de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2401de629b6eSShawn McCarney  *
2402de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2403de629b6eSShawn McCarney  *
2404de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2405de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2406de629b6eSShawn McCarney  *
2407adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2408adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2409adc4f0dbSShawn McCarney  *
2410de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2411adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2412de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2413de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2414de629b6eSShawn McCarney  * implements ObjectManager.
2415adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2416de629b6eSShawn McCarney  */
241723a21a1cSEd Tanous inline void getSensorData(
2418de629b6eSShawn McCarney     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
241949c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2420de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
24218fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2422adc4f0dbSShawn McCarney         objectMgrPaths,
2423adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2424de629b6eSShawn McCarney {
2425de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2426de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2427de629b6eSShawn McCarney     for (const std::string& connection : connections)
2428de629b6eSShawn McCarney     {
2429de629b6eSShawn McCarney         // Response handler to process managed objects
24308fb49dd6SShawn McCarney         auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames,
2431adc4f0dbSShawn McCarney                                     inventoryItems](
2432de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2433de629b6eSShawn McCarney                                        ManagedObjectsVectorType& resp) {
2434de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2435de629b6eSShawn McCarney             if (ec)
2436de629b6eSShawn McCarney             {
2437de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
2438de629b6eSShawn McCarney                 messages::internalError(SensorsAsyncResp->res);
2439de629b6eSShawn McCarney                 return;
2440de629b6eSShawn McCarney             }
2441de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2442de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2443de629b6eSShawn McCarney             {
2444de629b6eSShawn McCarney                 const std::string& objPath =
2445de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2446de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2447de629b6eSShawn McCarney                                  << objPath;
2448de629b6eSShawn McCarney 
2449de629b6eSShawn McCarney                 std::vector<std::string> split;
2450de629b6eSShawn McCarney                 // Reserve space for
2451de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2452de629b6eSShawn McCarney                 split.reserve(6);
2453de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2454de629b6eSShawn McCarney                 if (split.size() < 6)
2455de629b6eSShawn McCarney                 {
2456de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2457de629b6eSShawn McCarney                                      << objPath;
2458de629b6eSShawn McCarney                     continue;
2459de629b6eSShawn McCarney                 }
2460de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2461de629b6eSShawn McCarney                 // string at the beginning
2462de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2463de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2464de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2465de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
246649c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2467de629b6eSShawn McCarney                 {
2468de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2469de629b6eSShawn McCarney                     continue;
2470de629b6eSShawn McCarney                 }
2471de629b6eSShawn McCarney 
2472adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2473adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2474adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2475adc4f0dbSShawn McCarney 
247695a3ecadSAnthony Wilson                 const std::string& sensorSchema =
247795a3ecadSAnthony Wilson                     SensorsAsyncResp->chassisSubNode;
247895a3ecadSAnthony Wilson 
247995a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
248095a3ecadSAnthony Wilson 
2481a0ec28b6SAdrian Ambrożewicz                 if (sensorSchema == sensors::node::sensors)
248295a3ecadSAnthony Wilson                 {
248395a3ecadSAnthony Wilson                     SensorsAsyncResp->res.jsonValue["@odata.id"] =
248495a3ecadSAnthony Wilson                         "/redfish/v1/Chassis/" + SensorsAsyncResp->chassisId +
248595a3ecadSAnthony Wilson                         "/" + SensorsAsyncResp->chassisSubNode + "/" +
248695a3ecadSAnthony Wilson                         sensorName;
248795a3ecadSAnthony Wilson                     sensorJson = &(SensorsAsyncResp->res.jsonValue);
248895a3ecadSAnthony Wilson                 }
248995a3ecadSAnthony Wilson                 else
249095a3ecadSAnthony Wilson                 {
2491271584abSEd Tanous                     std::string fieldName;
2492de629b6eSShawn McCarney                     if (sensorType == "temperature")
2493de629b6eSShawn McCarney                     {
2494de629b6eSShawn McCarney                         fieldName = "Temperatures";
2495de629b6eSShawn McCarney                     }
2496de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2497de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2498de629b6eSShawn McCarney                     {
2499de629b6eSShawn McCarney                         fieldName = "Fans";
2500de629b6eSShawn McCarney                     }
2501de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2502de629b6eSShawn McCarney                     {
2503de629b6eSShawn McCarney                         fieldName = "Voltages";
2504de629b6eSShawn McCarney                     }
2505de629b6eSShawn McCarney                     else if (sensorType == "power")
2506de629b6eSShawn McCarney                     {
2507028f7ebcSEddie James                         if (!sensorName.compare("total_power"))
2508028f7ebcSEddie James                         {
2509028f7ebcSEddie James                             fieldName = "PowerControl";
2510028f7ebcSEddie James                         }
2511adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2512adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2513028f7ebcSEddie James                         {
2514de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2515de629b6eSShawn McCarney                         }
2516adc4f0dbSShawn McCarney                         else
2517adc4f0dbSShawn McCarney                         {
2518adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2519adc4f0dbSShawn McCarney                             continue;
2520adc4f0dbSShawn McCarney                         }
2521028f7ebcSEddie James                     }
2522de629b6eSShawn McCarney                     else
2523de629b6eSShawn McCarney                     {
2524de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2525de629b6eSShawn McCarney                                          << sensorType;
2526de629b6eSShawn McCarney                         continue;
2527de629b6eSShawn McCarney                     }
2528de629b6eSShawn McCarney 
2529de629b6eSShawn McCarney                     nlohmann::json& tempArray =
2530de629b6eSShawn McCarney                         SensorsAsyncResp->res.jsonValue[fieldName];
2531adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
253249c53ac9SJohnathan Mantey                     {
2533adc4f0dbSShawn McCarney                         if (tempArray.empty())
25347ab06f49SGunnar Mills                         {
253595a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
253695a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
253795a3ecadSAnthony Wilson                             // naming in power.hpp.
25387ab06f49SGunnar Mills                             tempArray.push_back(
2539adc4f0dbSShawn McCarney                                 {{"@odata.id",
2540adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
25417ab06f49SGunnar Mills                                       SensorsAsyncResp->chassisId + "/" +
2542adc4f0dbSShawn McCarney                                       SensorsAsyncResp->chassisSubNode + "#/" +
2543adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2544adc4f0dbSShawn McCarney                         }
2545adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2546adc4f0dbSShawn McCarney                     }
2547adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2548adc4f0dbSShawn McCarney                     {
2549adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2550adc4f0dbSShawn McCarney                         {
2551adc4f0dbSShawn McCarney                             sensorJson =
2552adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
2553adc4f0dbSShawn McCarney                                                  SensorsAsyncResp->chassisId));
2554adc4f0dbSShawn McCarney                         }
255549c53ac9SJohnathan Mantey                     }
255649c53ac9SJohnathan Mantey                     else
255749c53ac9SJohnathan Mantey                     {
2558de629b6eSShawn McCarney                         tempArray.push_back(
255995a3ecadSAnthony Wilson                             {{"@odata.id",
256095a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
256149c53ac9SJohnathan Mantey                                   SensorsAsyncResp->chassisId + "/" +
256295a3ecadSAnthony Wilson                                   SensorsAsyncResp->chassisSubNode + "#/" +
256395a3ecadSAnthony Wilson                                   fieldName + "/"}});
2564adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
256549c53ac9SJohnathan Mantey                     }
256695a3ecadSAnthony Wilson                 }
2567de629b6eSShawn McCarney 
2568adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2569adc4f0dbSShawn McCarney                 {
2570a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
2571a0ec28b6SAdrian Ambrożewicz                         sensorName, sensorType, SensorsAsyncResp,
2572a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2573adc4f0dbSShawn McCarney                 }
2574de629b6eSShawn McCarney             }
257549c53ac9SJohnathan Mantey             if (SensorsAsyncResp.use_count() == 1)
257649c53ac9SJohnathan Mantey             {
257749c53ac9SJohnathan Mantey                 sortJSONResponse(SensorsAsyncResp);
2578a0ec28b6SAdrian Ambrożewicz                 if (SensorsAsyncResp->chassisSubNode == sensors::node::thermal)
25798bd25ccdSJames Feist                 {
25808bd25ccdSJames Feist                     populateFanRedundancy(SensorsAsyncResp);
25818bd25ccdSJames Feist                 }
258249c53ac9SJohnathan Mantey             }
2583de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2584de629b6eSShawn McCarney         };
2585de629b6eSShawn McCarney 
2586de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2587de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
25888fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2589de629b6eSShawn McCarney         const std::string& objectMgrPath =
25908fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2591de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2592de629b6eSShawn McCarney                          << objectMgrPath;
2593de629b6eSShawn McCarney 
2594de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2595de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2596de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
259723a21a1cSEd Tanous     }
2598de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2599de629b6eSShawn McCarney }
2600de629b6eSShawn McCarney 
260123a21a1cSEd Tanous inline void processSensorList(
260295a3ecadSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
260395a3ecadSAnthony Wilson     std::shared_ptr<boost::container::flat_set<std::string>> sensorNames)
26041abe55efSEd Tanous {
260595a3ecadSAnthony Wilson     auto getConnectionCb =
260695a3ecadSAnthony Wilson         [SensorsAsyncResp, sensorNames](
260795a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
260855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2609de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
261049c53ac9SJohnathan Mantey                 [SensorsAsyncResp, sensorNames, connections](
261195a3ecadSAnthony Wilson                     std::shared_ptr<
261295a3ecadSAnthony Wilson                         boost::container::flat_map<std::string, std::string>>
26138fb49dd6SShawn McCarney                         objectMgrPaths) {
2614de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2615adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
2616adc4f0dbSShawn McCarney                         [SensorsAsyncResp, sensorNames, connections,
2617adc4f0dbSShawn McCarney                          objectMgrPaths](
2618adc4f0dbSShawn McCarney                             std::shared_ptr<std::vector<InventoryItem>>
2619adc4f0dbSShawn McCarney                                 inventoryItems) {
2620adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
262149c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
2622de629b6eSShawn McCarney                             getSensorData(SensorsAsyncResp, sensorNames,
2623adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2624adc4f0dbSShawn McCarney                                           inventoryItems);
2625adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2626adc4f0dbSShawn McCarney                         };
2627adc4f0dbSShawn McCarney 
2628adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
2629adc4f0dbSShawn McCarney                     getInventoryItems(SensorsAsyncResp, sensorNames,
2630adc4f0dbSShawn McCarney                                       objectMgrPaths,
2631adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2632adc4f0dbSShawn McCarney 
2633de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
263408777fb0SLewanczyk, Dawid                 };
2635de629b6eSShawn McCarney 
263649c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
263749c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
2638de629b6eSShawn McCarney             getObjectManagerPaths(SensorsAsyncResp,
2639de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
264055c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
264108777fb0SLewanczyk, Dawid         };
2642de629b6eSShawn McCarney 
2643de629b6eSShawn McCarney     // Get set of connections that provide sensor values
264495a3ecadSAnthony Wilson     getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb));
264595a3ecadSAnthony Wilson }
264695a3ecadSAnthony Wilson 
264795a3ecadSAnthony Wilson /**
264895a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
264995a3ecadSAnthony Wilson  *        chassis.
265095a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
265195a3ecadSAnthony Wilson  */
265223a21a1cSEd Tanous inline void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
265395a3ecadSAnthony Wilson {
265495a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
265595a3ecadSAnthony Wilson     auto getChassisCb =
265695a3ecadSAnthony Wilson         [SensorsAsyncResp](
265795a3ecadSAnthony Wilson             std::shared_ptr<boost::container::flat_set<std::string>>
265895a3ecadSAnthony Wilson                 sensorNames) {
265995a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
266095a3ecadSAnthony Wilson             processSensorList(SensorsAsyncResp, sensorNames);
266155c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
266208777fb0SLewanczyk, Dawid         };
26634f9a2130SJennifer Lee     SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
266408777fb0SLewanczyk, Dawid 
266526f03899SShawn McCarney     // Get set of sensors in chassis
2666588c3f0dSKowalski, Kamil     getChassis(SensorsAsyncResp, std::move(getChassisCb));
266755c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2668271584abSEd Tanous }
266908777fb0SLewanczyk, Dawid 
2670413961deSRichard Marian Thomaiyar /**
267149c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
267249c53ac9SJohnathan Mantey  * the chassis node
267349c53ac9SJohnathan Mantey  *
267449c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
267549c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
267649c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
267749c53ac9SJohnathan Mantey  *                         repeated calls to this function
267849c53ac9SJohnathan Mantey  */
267923a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
26800a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
268149c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
268249c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
268349c53ac9SJohnathan Mantey {
26840a86febdSRichard Marian Thomaiyar     for (std::string_view chassisSensor : sensorsList)
268549c53ac9SJohnathan Mantey     {
26860a86febdSRichard Marian Thomaiyar         std::size_t pos = chassisSensor.rfind("/");
26870a86febdSRichard Marian Thomaiyar         if (pos >= (chassisSensor.size() - 1))
268849c53ac9SJohnathan Mantey         {
268949c53ac9SJohnathan Mantey             continue;
269049c53ac9SJohnathan Mantey         }
26910a86febdSRichard Marian Thomaiyar         std::string_view thisSensorName = chassisSensor.substr(pos + 1);
269249c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
269349c53ac9SJohnathan Mantey         {
269449c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
269549c53ac9SJohnathan Mantey             return true;
269649c53ac9SJohnathan Mantey         }
269749c53ac9SJohnathan Mantey     }
269849c53ac9SJohnathan Mantey     return false;
269949c53ac9SJohnathan Mantey }
270049c53ac9SJohnathan Mantey 
270149c53ac9SJohnathan Mantey /**
2702413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2703413961deSRichard Marian Thomaiyar  *
2704413961deSRichard Marian Thomaiyar  * @param res   response object
27054bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2706413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2707413961deSRichard Marian Thomaiyar  */
270823a21a1cSEd Tanous inline void setSensorsOverride(
27094bb3dc34SCarol Wang     std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
27104bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2711397fd61fSjayaprakash Mutyala         allCollections)
2712413961deSRichard Marian Thomaiyar {
271370d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
27144bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2715413961deSRichard Marian Thomaiyar 
2716f65af9e8SRichard Marian Thomaiyar     const char* propertyValueName;
2717f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2718413961deSRichard Marian Thomaiyar     std::string memberId;
2719413961deSRichard Marian Thomaiyar     double value;
2720f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2721f65af9e8SRichard Marian Thomaiyar     {
2722f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2723f65af9e8SRichard Marian Thomaiyar         {
2724f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2725f65af9e8SRichard Marian Thomaiyar         }
2726f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2727f65af9e8SRichard Marian Thomaiyar         {
2728f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2729f65af9e8SRichard Marian Thomaiyar         }
2730f65af9e8SRichard Marian Thomaiyar         else
2731f65af9e8SRichard Marian Thomaiyar         {
2732f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2733f65af9e8SRichard Marian Thomaiyar         }
2734f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2735f65af9e8SRichard Marian Thomaiyar         {
27364bb3dc34SCarol Wang             if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId",
27374bb3dc34SCarol Wang                                      memberId, propertyValueName, value))
2738413961deSRichard Marian Thomaiyar             {
2739413961deSRichard Marian Thomaiyar                 return;
2740413961deSRichard Marian Thomaiyar             }
2741f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2742f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2743f65af9e8SRichard Marian Thomaiyar         }
2744f65af9e8SRichard Marian Thomaiyar     }
27454bb3dc34SCarol Wang 
274649c53ac9SJohnathan Mantey     auto getChassisSensorListCb = [sensorAsyncResp,
274749c53ac9SJohnathan Mantey                                    overrideMap](const std::shared_ptr<
274849c53ac9SJohnathan Mantey                                                 boost::container::flat_set<
274949c53ac9SJohnathan Mantey                                                     std::string>>
275049c53ac9SJohnathan Mantey                                                     sensorsList) {
275149c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
275249c53ac9SJohnathan Mantey         // chassis node
275349c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
275449c53ac9SJohnathan Mantey             sensorNames =
275549c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2756f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2757413961deSRichard Marian Thomaiyar         {
2758f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
275949c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
276049c53ac9SJohnathan Mantey                                                *sensorNames))
2761f65af9e8SRichard Marian Thomaiyar             {
2762f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
2763413961deSRichard Marian Thomaiyar                 messages::resourceNotFound(sensorAsyncResp->res,
2764f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2765413961deSRichard Marian Thomaiyar                 return;
2766413961deSRichard Marian Thomaiyar             }
2767f65af9e8SRichard Marian Thomaiyar         }
2768413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2769413961deSRichard Marian Thomaiyar         auto getObjectsWithConnectionCb =
2770f65af9e8SRichard Marian Thomaiyar             [sensorAsyncResp, overrideMap](
2771cb13a392SEd Tanous                 const boost::container::flat_set<std::string>& /*connections*/,
2772413961deSRichard Marian Thomaiyar                 const std::set<std::pair<std::string, std::string>>&
2773413961deSRichard Marian Thomaiyar                     objectsWithConnection) {
2774f65af9e8SRichard Marian Thomaiyar                 if (objectsWithConnection.size() != overrideMap.size())
2775413961deSRichard Marian Thomaiyar                 {
2776413961deSRichard Marian Thomaiyar                     BMCWEB_LOG_INFO
2777f65af9e8SRichard Marian Thomaiyar                         << "Unable to find all objects with proper connection "
2778f65af9e8SRichard Marian Thomaiyar                         << objectsWithConnection.size() << " requested "
2779f65af9e8SRichard Marian Thomaiyar                         << overrideMap.size() << "\n";
2780413961deSRichard Marian Thomaiyar                     messages::resourceNotFound(
2781413961deSRichard Marian Thomaiyar                         sensorAsyncResp->res,
2782a0ec28b6SAdrian Ambrożewicz                         sensorAsyncResp->chassisSubNode ==
2783a0ec28b6SAdrian Ambrożewicz                                 sensors::node::thermal
2784413961deSRichard Marian Thomaiyar                             ? "Temperatures"
2785413961deSRichard Marian Thomaiyar                             : "Voltages",
2786f65af9e8SRichard Marian Thomaiyar                         "Count");
2787f65af9e8SRichard Marian Thomaiyar                     return;
2788f65af9e8SRichard Marian Thomaiyar                 }
2789f65af9e8SRichard Marian Thomaiyar                 for (const auto& item : objectsWithConnection)
2790f65af9e8SRichard Marian Thomaiyar                 {
2791f65af9e8SRichard Marian Thomaiyar 
2792f65af9e8SRichard Marian Thomaiyar                     auto lastPos = item.first.rfind('/');
2793f65af9e8SRichard Marian Thomaiyar                     if (lastPos == std::string::npos)
2794f65af9e8SRichard Marian Thomaiyar                     {
2795f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2796f65af9e8SRichard Marian Thomaiyar                         return;
2797f65af9e8SRichard Marian Thomaiyar                     }
2798f65af9e8SRichard Marian Thomaiyar                     std::string sensorName = item.first.substr(lastPos + 1);
2799f65af9e8SRichard Marian Thomaiyar 
2800f65af9e8SRichard Marian Thomaiyar                     const auto& iterator = overrideMap.find(sensorName);
2801f65af9e8SRichard Marian Thomaiyar                     if (iterator == overrideMap.end())
2802f65af9e8SRichard Marian Thomaiyar                     {
2803f65af9e8SRichard Marian Thomaiyar                         BMCWEB_LOG_INFO << "Unable to find sensor object"
2804f65af9e8SRichard Marian Thomaiyar                                         << item.first << "\n";
2805f65af9e8SRichard Marian Thomaiyar                         messages::internalError(sensorAsyncResp->res);
2806413961deSRichard Marian Thomaiyar                         return;
2807413961deSRichard Marian Thomaiyar                     }
2808413961deSRichard Marian Thomaiyar                     crow::connections::systemBus->async_method_call(
2809f65af9e8SRichard Marian Thomaiyar                         [sensorAsyncResp](const boost::system::error_code ec) {
2810413961deSRichard Marian Thomaiyar                             if (ec)
2811413961deSRichard Marian Thomaiyar                             {
2812413961deSRichard Marian Thomaiyar                                 BMCWEB_LOG_DEBUG
2813f65af9e8SRichard Marian Thomaiyar                                     << "setOverrideValueStatus DBUS error: "
2814413961deSRichard Marian Thomaiyar                                     << ec;
2815413961deSRichard Marian Thomaiyar                                 messages::internalError(sensorAsyncResp->res);
2816413961deSRichard Marian Thomaiyar                                 return;
2817413961deSRichard Marian Thomaiyar                             }
2818413961deSRichard Marian Thomaiyar                         },
2819f65af9e8SRichard Marian Thomaiyar                         item.second, item.first,
2820413961deSRichard Marian Thomaiyar                         "org.freedesktop.DBus.Properties", "Set",
2821413961deSRichard Marian Thomaiyar                         "xyz.openbmc_project.Sensor.Value", "Value",
282219bd78d9SPatrick Williams                         std::variant<double>(iterator->second.first));
2823f65af9e8SRichard Marian Thomaiyar                 }
2824413961deSRichard Marian Thomaiyar             };
2825413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2826413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2827413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2828413961deSRichard Marian Thomaiyar     };
2829413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2830413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2831413961deSRichard Marian Thomaiyar }
2832413961deSRichard Marian Thomaiyar 
283323a21a1cSEd Tanous inline bool isOverridingAllowed(const std::string& manufacturingModeStatus)
283470d1d0aaSjayaprakash Mutyala {
283570d1d0aaSjayaprakash Mutyala     if (manufacturingModeStatus ==
283670d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.Control.Security.SpecialMode.Modes.Manufacturing")
283770d1d0aaSjayaprakash Mutyala     {
283870d1d0aaSjayaprakash Mutyala         return true;
283970d1d0aaSjayaprakash Mutyala     }
284070d1d0aaSjayaprakash Mutyala 
284170d1d0aaSjayaprakash Mutyala #ifdef BMCWEB_ENABLE_VALIDATION_UNSECURE_FEATURE
284270d1d0aaSjayaprakash Mutyala     if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
284370d1d0aaSjayaprakash Mutyala                                    "SpecialMode.Modes.ValidationUnsecure")
284470d1d0aaSjayaprakash Mutyala     {
284570d1d0aaSjayaprakash Mutyala         return true;
284670d1d0aaSjayaprakash Mutyala     }
284770d1d0aaSjayaprakash Mutyala 
284870d1d0aaSjayaprakash Mutyala #endif
284970d1d0aaSjayaprakash Mutyala 
285070d1d0aaSjayaprakash Mutyala     return false;
285170d1d0aaSjayaprakash Mutyala }
285270d1d0aaSjayaprakash Mutyala 
285370d1d0aaSjayaprakash Mutyala /**
285470d1d0aaSjayaprakash Mutyala  * @brief Entry point for Checking the manufacturing mode before doing sensor
285570d1d0aaSjayaprakash Mutyala  * override values of given sensor
285670d1d0aaSjayaprakash Mutyala  *
285770d1d0aaSjayaprakash Mutyala  * @param res   response object
285870d1d0aaSjayaprakash Mutyala  * @param allCollections   Collections extract from sensors' request patch info
285970d1d0aaSjayaprakash Mutyala  * @param chassisSubNode   Chassis Node for which the query has to happen
286070d1d0aaSjayaprakash Mutyala  */
286123a21a1cSEd Tanous inline void checkAndDoSensorsOverride(
286270d1d0aaSjayaprakash Mutyala     std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
2863397fd61fSjayaprakash Mutyala     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2864397fd61fSjayaprakash Mutyala         allCollections)
286570d1d0aaSjayaprakash Mutyala {
286670d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "checkAndDoSensorsOverride for subnode"
286770d1d0aaSjayaprakash Mutyala                     << sensorAsyncResp->chassisSubNode << "\n";
286870d1d0aaSjayaprakash Mutyala 
286970d1d0aaSjayaprakash Mutyala     const std::array<std::string, 1> interfaces = {
287070d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.Security.SpecialMode"};
287170d1d0aaSjayaprakash Mutyala 
287270d1d0aaSjayaprakash Mutyala     crow::connections::systemBus->async_method_call(
287323a21a1cSEd Tanous         [sensorAsyncResp, allCollections](const boost::system::error_code ec2,
287470d1d0aaSjayaprakash Mutyala                                           const GetSubTreeType& resp) mutable {
287523a21a1cSEd Tanous             if (ec2)
287670d1d0aaSjayaprakash Mutyala             {
287770d1d0aaSjayaprakash Mutyala                 BMCWEB_LOG_DEBUG
287870d1d0aaSjayaprakash Mutyala                     << "Error in querying GetSubTree with Object Mapper. "
287923a21a1cSEd Tanous                     << ec2;
288070d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
288170d1d0aaSjayaprakash Mutyala                 return;
288270d1d0aaSjayaprakash Mutyala             }
288391e130a3Sjayaprakash Mutyala #ifdef BMCWEB_INSECURE_UNRESTRICTED_SENSOR_OVERRIDE
288491e130a3Sjayaprakash Mutyala             // Proceed with sensor override
2885397fd61fSjayaprakash Mutyala             setSensorsOverride(sensorAsyncResp, allCollections);
288670d1d0aaSjayaprakash Mutyala             return;
288791e130a3Sjayaprakash Mutyala #endif
288870d1d0aaSjayaprakash Mutyala 
288970d1d0aaSjayaprakash Mutyala             if (resp.size() != 1)
289070d1d0aaSjayaprakash Mutyala             {
289191e130a3Sjayaprakash Mutyala                 BMCWEB_LOG_WARNING
289291e130a3Sjayaprakash Mutyala                     << "Overriding sensor value is not allowed - Internal "
289391e130a3Sjayaprakash Mutyala                        "error in querying SpecialMode property.";
289470d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
289570d1d0aaSjayaprakash Mutyala                 return;
289670d1d0aaSjayaprakash Mutyala             }
289770d1d0aaSjayaprakash Mutyala             const std::string& path = resp[0].first;
289870d1d0aaSjayaprakash Mutyala             const std::string& serviceName = resp[0].second.begin()->first;
289970d1d0aaSjayaprakash Mutyala 
290070d1d0aaSjayaprakash Mutyala             if (path.empty() || serviceName.empty())
290170d1d0aaSjayaprakash Mutyala             {
290270d1d0aaSjayaprakash Mutyala                 BMCWEB_LOG_DEBUG
290370d1d0aaSjayaprakash Mutyala                     << "Path or service name is returned as empty. ";
290470d1d0aaSjayaprakash Mutyala                 messages::internalError(sensorAsyncResp->res);
290570d1d0aaSjayaprakash Mutyala                 return;
290670d1d0aaSjayaprakash Mutyala             }
290770d1d0aaSjayaprakash Mutyala 
290870d1d0aaSjayaprakash Mutyala             // Sensor override is allowed only in manufacturing mode or
290970d1d0aaSjayaprakash Mutyala             // validation unsecure mode .
291070d1d0aaSjayaprakash Mutyala             crow::connections::systemBus->async_method_call(
2911397fd61fSjayaprakash Mutyala                 [sensorAsyncResp, allCollections,
291270d1d0aaSjayaprakash Mutyala                  path](const boost::system::error_code ec,
291370d1d0aaSjayaprakash Mutyala                        std::variant<std::string>& getManufactMode) mutable {
291470d1d0aaSjayaprakash Mutyala                     if (ec)
291570d1d0aaSjayaprakash Mutyala                     {
291670d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_DEBUG
291770d1d0aaSjayaprakash Mutyala                             << "Error in querying Special mode property " << ec;
291870d1d0aaSjayaprakash Mutyala                         messages::internalError(sensorAsyncResp->res);
291970d1d0aaSjayaprakash Mutyala                         return;
292070d1d0aaSjayaprakash Mutyala                     }
292170d1d0aaSjayaprakash Mutyala 
292270d1d0aaSjayaprakash Mutyala                     const std::string* manufacturingModeStatus =
292370d1d0aaSjayaprakash Mutyala                         std::get_if<std::string>(&getManufactMode);
292470d1d0aaSjayaprakash Mutyala 
292570d1d0aaSjayaprakash Mutyala                     if (nullptr == manufacturingModeStatus)
292670d1d0aaSjayaprakash Mutyala                     {
292770d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_DEBUG << "Sensor override mode is not "
292870d1d0aaSjayaprakash Mutyala                                             "Enabled. Returning ... ";
292970d1d0aaSjayaprakash Mutyala                         messages::internalError(sensorAsyncResp->res);
293070d1d0aaSjayaprakash Mutyala                         return;
293170d1d0aaSjayaprakash Mutyala                     }
293270d1d0aaSjayaprakash Mutyala 
293370d1d0aaSjayaprakash Mutyala                     if (isOverridingAllowed(*manufacturingModeStatus))
293470d1d0aaSjayaprakash Mutyala                     {
293570d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_INFO << "Manufacturing mode is Enabled. "
293670d1d0aaSjayaprakash Mutyala                                            "Proceeding further... ";
2937397fd61fSjayaprakash Mutyala                         setSensorsOverride(sensorAsyncResp, allCollections);
293870d1d0aaSjayaprakash Mutyala                     }
293970d1d0aaSjayaprakash Mutyala                     else
294070d1d0aaSjayaprakash Mutyala                     {
294170d1d0aaSjayaprakash Mutyala                         BMCWEB_LOG_WARNING
294270d1d0aaSjayaprakash Mutyala                             << "Manufacturing mode is not Enabled...can't "
294370d1d0aaSjayaprakash Mutyala                                "Override the sensor value. ";
294470d1d0aaSjayaprakash Mutyala 
294570d1d0aaSjayaprakash Mutyala                         messages::actionNotSupported(
294670d1d0aaSjayaprakash Mutyala                             sensorAsyncResp->res,
294770d1d0aaSjayaprakash Mutyala                             "Overriding of Sensor Value for non "
294870d1d0aaSjayaprakash Mutyala                             "manufacturing mode");
294970d1d0aaSjayaprakash Mutyala                         return;
295070d1d0aaSjayaprakash Mutyala                     }
295170d1d0aaSjayaprakash Mutyala                 },
295270d1d0aaSjayaprakash Mutyala                 serviceName, path, "org.freedesktop.DBus.Properties", "Get",
295370d1d0aaSjayaprakash Mutyala                 "xyz.openbmc_project.Security.SpecialMode", "SpecialMode");
295470d1d0aaSjayaprakash Mutyala         },
295570d1d0aaSjayaprakash Mutyala 
295670d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.ObjectMapper",
295770d1d0aaSjayaprakash Mutyala         "/xyz/openbmc_project/object_mapper",
295870d1d0aaSjayaprakash Mutyala         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 5, interfaces);
295970d1d0aaSjayaprakash Mutyala }
296070d1d0aaSjayaprakash Mutyala 
2961a0ec28b6SAdrian Ambrożewicz /**
2962a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2963a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2964a0ec28b6SAdrian Ambrożewicz  *
2965a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2966a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2967a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2968a0ec28b6SAdrian Ambrożewicz  *
2969a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2970a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2971a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2972a0ec28b6SAdrian Ambrożewicz  */
297323a21a1cSEd Tanous inline void retrieveUriToDbusMap(const std::string& chassis,
297423a21a1cSEd Tanous                                  const std::string& node,
2975a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2976a0ec28b6SAdrian Ambrożewicz {
2977a0ec28b6SAdrian Ambrożewicz     auto typesIt = sensors::dbus::types.find(node);
2978a0ec28b6SAdrian Ambrożewicz     if (typesIt == sensors::dbus::types.end())
2979a0ec28b6SAdrian Ambrożewicz     {
2980a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2981a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2982a0ec28b6SAdrian Ambrożewicz         return;
2983a0ec28b6SAdrian Ambrożewicz     }
2984a0ec28b6SAdrian Ambrożewicz 
2985a0ec28b6SAdrian Ambrożewicz     auto respBuffer = std::make_shared<crow::Response>();
2986a0ec28b6SAdrian Ambrożewicz     auto callback =
2987a0ec28b6SAdrian Ambrożewicz         [respBuffer, mapCompleteCb{std::move(mapComplete)}](
2988a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2989a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
2990a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2991a0ec28b6SAdrian Ambrożewicz 
2992a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2993a0ec28b6SAdrian Ambrożewicz         *respBuffer, chassis, typesIt->second, node, std::move(callback));
2994a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2995a0ec28b6SAdrian Ambrożewicz }
2996a0ec28b6SAdrian Ambrożewicz 
299795a3ecadSAnthony Wilson class SensorCollection : public Node
299895a3ecadSAnthony Wilson {
299995a3ecadSAnthony Wilson   public:
300052cc112dSEd Tanous     SensorCollection(App& app) :
3001f99c379dSGunnar Mills         Node(app, "/redfish/v1/Chassis/<str>/Sensors/", std::string())
300295a3ecadSAnthony Wilson     {
300395a3ecadSAnthony Wilson         entityPrivileges = {
300495a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
300595a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
300695a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
300795a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
300895a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
300995a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
301095a3ecadSAnthony Wilson     }
301195a3ecadSAnthony Wilson 
301295a3ecadSAnthony Wilson   private:
3013cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
301495a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
301595a3ecadSAnthony Wilson     {
301695a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
301795a3ecadSAnthony Wilson         if (params.size() != 1)
301895a3ecadSAnthony Wilson         {
301995a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1";
302095a3ecadSAnthony Wilson             messages::internalError(res);
302195a3ecadSAnthony Wilson             res.end();
302295a3ecadSAnthony Wilson             return;
302395a3ecadSAnthony Wilson         }
302495a3ecadSAnthony Wilson 
302595a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
302695a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
3027a0ec28b6SAdrian Ambrożewicz             std::make_shared<SensorsAsyncResp>(
3028a0ec28b6SAdrian Ambrożewicz                 res, chassisId, sensors::dbus::types.at(sensors::node::sensors),
3029a0ec28b6SAdrian Ambrożewicz                 sensors::node::sensors);
303095a3ecadSAnthony Wilson 
303195a3ecadSAnthony Wilson         auto getChassisCb =
303295a3ecadSAnthony Wilson             [asyncResp](std::shared_ptr<boost::container::flat_set<std::string>>
303395a3ecadSAnthony Wilson                             sensorNames) {
303495a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb enter";
303595a3ecadSAnthony Wilson 
303695a3ecadSAnthony Wilson                 nlohmann::json& entriesArray =
303795a3ecadSAnthony Wilson                     asyncResp->res.jsonValue["Members"];
303895a3ecadSAnthony Wilson                 for (auto& sensor : *sensorNames)
303995a3ecadSAnthony Wilson                 {
304095a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
304195a3ecadSAnthony Wilson 
304295a3ecadSAnthony Wilson                     std::size_t lastPos = sensor.rfind("/");
304395a3ecadSAnthony Wilson                     if (lastPos == std::string::npos ||
304495a3ecadSAnthony Wilson                         lastPos + 1 >= sensor.size())
304595a3ecadSAnthony Wilson                     {
304695a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
304795a3ecadSAnthony Wilson                         messages::internalError(asyncResp->res);
304895a3ecadSAnthony Wilson                         return;
304995a3ecadSAnthony Wilson                     }
305095a3ecadSAnthony Wilson                     std::string sensorName = sensor.substr(lastPos + 1);
305195a3ecadSAnthony Wilson                     entriesArray.push_back(
305295a3ecadSAnthony Wilson                         {{"@odata.id",
305395a3ecadSAnthony Wilson                           "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
305495a3ecadSAnthony Wilson                               asyncResp->chassisSubNode + "/" + sensorName}});
305595a3ecadSAnthony Wilson                 }
305695a3ecadSAnthony Wilson 
305795a3ecadSAnthony Wilson                 asyncResp->res.jsonValue["Members@odata.count"] =
305895a3ecadSAnthony Wilson                     entriesArray.size();
305995a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getChassisCb exit";
306095a3ecadSAnthony Wilson             };
306195a3ecadSAnthony Wilson 
306295a3ecadSAnthony Wilson         // Get set of sensors in chassis
306395a3ecadSAnthony Wilson         getChassis(asyncResp, std::move(getChassisCb));
306495a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
306595a3ecadSAnthony Wilson     }
306695a3ecadSAnthony Wilson };
306795a3ecadSAnthony Wilson 
306895a3ecadSAnthony Wilson class Sensor : public Node
306995a3ecadSAnthony Wilson {
307095a3ecadSAnthony Wilson   public:
307152cc112dSEd Tanous     Sensor(App& app) :
307295a3ecadSAnthony Wilson         Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(),
307395a3ecadSAnthony Wilson              std::string())
307495a3ecadSAnthony Wilson     {
307595a3ecadSAnthony Wilson         entityPrivileges = {
307695a3ecadSAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
307795a3ecadSAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
307895a3ecadSAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
307995a3ecadSAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
308095a3ecadSAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
308195a3ecadSAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
308295a3ecadSAnthony Wilson     }
308395a3ecadSAnthony Wilson 
308495a3ecadSAnthony Wilson   private:
3085cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
308695a3ecadSAnthony Wilson                const std::vector<std::string>& params) override
308795a3ecadSAnthony Wilson     {
308895a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "Sensor doGet enter";
308995a3ecadSAnthony Wilson         if (params.size() != 2)
309095a3ecadSAnthony Wilson         {
309195a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2";
309295a3ecadSAnthony Wilson             messages::internalError(res);
309395a3ecadSAnthony Wilson             res.end();
309495a3ecadSAnthony Wilson             return;
309595a3ecadSAnthony Wilson         }
309695a3ecadSAnthony Wilson         const std::string& chassisId = params[0];
309795a3ecadSAnthony Wilson         std::shared_ptr<SensorsAsyncResp> asyncResp =
3098a0ec28b6SAdrian Ambrożewicz             std::make_shared<SensorsAsyncResp>(res, chassisId,
3099a0ec28b6SAdrian Ambrożewicz                                                std::vector<const char*>(),
3100a0ec28b6SAdrian Ambrożewicz                                                sensors::node::sensors);
310195a3ecadSAnthony Wilson 
310295a3ecadSAnthony Wilson         const std::string& sensorName = params[1];
310395a3ecadSAnthony Wilson         const std::array<const char*, 1> interfaces = {
310495a3ecadSAnthony Wilson             "xyz.openbmc_project.Sensor.Value"};
310595a3ecadSAnthony Wilson 
310695a3ecadSAnthony Wilson         // Get a list of all of the sensors that implement Sensor.Value
310795a3ecadSAnthony Wilson         // and get the path and service name associated with the sensor
310895a3ecadSAnthony Wilson         crow::connections::systemBus->async_method_call(
310995a3ecadSAnthony Wilson             [asyncResp, sensorName](const boost::system::error_code ec,
311095a3ecadSAnthony Wilson                                     const GetSubTreeType& subtree) {
311195a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 enter";
311295a3ecadSAnthony Wilson                 if (ec)
311395a3ecadSAnthony Wilson                 {
311495a3ecadSAnthony Wilson                     messages::internalError(asyncResp->res);
311595a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
311695a3ecadSAnthony Wilson                                      << "Dbus error " << ec;
311795a3ecadSAnthony Wilson                     return;
311895a3ecadSAnthony Wilson                 }
311995a3ecadSAnthony Wilson 
312095a3ecadSAnthony Wilson                 GetSubTreeType::const_iterator it = std::find_if(
312195a3ecadSAnthony Wilson                     subtree.begin(), subtree.end(),
312295a3ecadSAnthony Wilson                     [sensorName](
312395a3ecadSAnthony Wilson                         const std::pair<
312495a3ecadSAnthony Wilson                             std::string,
312595a3ecadSAnthony Wilson                             std::vector<std::pair<std::string,
312695a3ecadSAnthony Wilson                                                   std::vector<std::string>>>>&
312795a3ecadSAnthony Wilson                             object) {
312895a3ecadSAnthony Wilson                         std::string_view sensor = object.first;
312995a3ecadSAnthony Wilson                         std::size_t lastPos = sensor.rfind("/");
313095a3ecadSAnthony Wilson                         if (lastPos == std::string::npos ||
313195a3ecadSAnthony Wilson                             lastPos + 1 >= sensor.size())
313295a3ecadSAnthony Wilson                         {
313395a3ecadSAnthony Wilson                             BMCWEB_LOG_ERROR << "Invalid sensor path: "
313495a3ecadSAnthony Wilson                                              << sensor;
313595a3ecadSAnthony Wilson                             return false;
313695a3ecadSAnthony Wilson                         }
313795a3ecadSAnthony Wilson                         std::string_view name = sensor.substr(lastPos + 1);
313895a3ecadSAnthony Wilson 
313995a3ecadSAnthony Wilson                         return name == sensorName;
314095a3ecadSAnthony Wilson                     });
314195a3ecadSAnthony Wilson 
314295a3ecadSAnthony Wilson                 if (it == subtree.end())
314395a3ecadSAnthony Wilson                 {
314495a3ecadSAnthony Wilson                     BMCWEB_LOG_ERROR << "Could not find path for sensor: "
314595a3ecadSAnthony Wilson                                      << sensorName;
314695a3ecadSAnthony Wilson                     messages::resourceNotFound(asyncResp->res, "Sensor",
314795a3ecadSAnthony Wilson                                                sensorName);
314895a3ecadSAnthony Wilson                     return;
314995a3ecadSAnthony Wilson                 }
315095a3ecadSAnthony Wilson                 std::string_view sensorPath = (*it).first;
315195a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
315295a3ecadSAnthony Wilson                                  << sensorName << "': " << sensorPath;
315395a3ecadSAnthony Wilson 
315495a3ecadSAnthony Wilson                 const std::shared_ptr<boost::container::flat_set<std::string>>
315595a3ecadSAnthony Wilson                     sensorList = std::make_shared<
315695a3ecadSAnthony Wilson                         boost::container::flat_set<std::string>>();
315795a3ecadSAnthony Wilson 
315895a3ecadSAnthony Wilson                 sensorList->emplace(sensorPath);
315995a3ecadSAnthony Wilson                 processSensorList(asyncResp, sensorList);
316095a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "respHandler1 exit";
316195a3ecadSAnthony Wilson             },
316295a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper",
316395a3ecadSAnthony Wilson             "/xyz/openbmc_project/object_mapper",
316495a3ecadSAnthony Wilson             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
316595a3ecadSAnthony Wilson             "/xyz/openbmc_project/sensors", 2, interfaces);
316695a3ecadSAnthony Wilson     }
316795a3ecadSAnthony Wilson };
316895a3ecadSAnthony Wilson 
316908777fb0SLewanczyk, Dawid } // namespace redfish
3170