blob: 6257dce9fe1788b81b905cb1bb268325f283deb1 [file] [log] [blame]
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010019#include <boost/algorithm/string/predicate.hpp>
20#include <boost/algorithm/string/split.hpp>
21#include <boost/container/flat_map.hpp>
22#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070023#include <dbus_singleton.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053025#include <utils/json_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
27#include <cmath>
Ed Tanousb5a76932020-09-29 16:16:58 -070028#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080029#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031namespace redfish
32{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010033
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010034using GetSubTreeType = std::vector<
35 std::pair<std::string,
36 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
37
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -050038using SensorVariant =
39 std::variant<int64_t, double, uint32_t, bool, std::string>;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070040
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010041using ManagedObjectsVectorType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070042 sdbusplus::message::object_path,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010043 boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070044 std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010045
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020046namespace sensors
47{
48namespace node
49{
50static constexpr std::string_view power = "Power";
51static constexpr std::string_view sensors = "Sensors";
52static constexpr std::string_view thermal = "Thermal";
53} // namespace node
54
55namespace dbus
56{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000057
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020058static const boost::container::flat_map<std::string_view,
59 std::vector<const char*>>
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000060 paths = {{node::power,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020061 {"/xyz/openbmc_project/sensors/voltage",
62 "/xyz/openbmc_project/sensors/power"}},
63 {node::sensors,
64 {"/xyz/openbmc_project/sensors/power",
65 "/xyz/openbmc_project/sensors/current",
Basheer Ahmed Muddebihal70886902021-07-22 02:11:17 -070066 "/xyz/openbmc_project/sensors/airflow",
George Liue8204932021-02-01 14:42:49 +080067#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
68 "/xyz/openbmc_project/sensors/voltage",
69 "/xyz/openbmc_project/sensors/fan_tach",
70 "/xyz/openbmc_project/sensors/temperature",
71 "/xyz/openbmc_project/sensors/fan_pwm",
72 "/xyz/openbmc_project/sensors/altitude",
73 "/xyz/openbmc_project/sensors/energy",
74#endif
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020075 "/xyz/openbmc_project/sensors/utilization"}},
76 {node::thermal,
77 {"/xyz/openbmc_project/sensors/fan_tach",
78 "/xyz/openbmc_project/sensors/temperature",
79 "/xyz/openbmc_project/sensors/fan_pwm"}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000080} // namespace dbus
81
82inline const char* toReadingType(const std::string& sensorType)
83{
84 if (sensorType == "voltage")
85 {
86 return "Voltage";
87 }
88 if (sensorType == "power")
89 {
90 return "Power";
91 }
92 if (sensorType == "current")
93 {
94 return "Current";
95 }
96 if (sensorType == "fan_tach")
97 {
98 return "Rotational";
99 }
100 if (sensorType == "temperature")
101 {
102 return "Temperature";
103 }
104 if (sensorType == "fan_pwm" || sensorType == "utilization")
105 {
106 return "Percent";
107 }
108 if (sensorType == "altitude")
109 {
110 return "Altitude";
111 }
112 if (sensorType == "airflow")
113 {
114 return "AirFlow";
115 }
116 if (sensorType == "energy")
117 {
118 return "EnergyJoules";
119 }
120 return "";
121}
122
123inline const char* toReadingUnits(const std::string& sensorType)
124{
125 if (sensorType == "voltage")
126 {
127 return "V";
128 }
129 if (sensorType == "power")
130 {
131 return "W";
132 }
133 if (sensorType == "current")
134 {
135 return "A";
136 }
137 if (sensorType == "fan_tach")
138 {
139 return "RPM";
140 }
141 if (sensorType == "temperature")
142 {
143 return "Cel";
144 }
145 if (sensorType == "fan_pwm" || sensorType == "utilization")
146 {
147 return "%";
148 }
149 if (sensorType == "altitude")
150 {
151 return "m";
152 }
153 if (sensorType == "airflow")
154 {
155 return "cft_i/min";
156 }
157 if (sensorType == "energy")
158 {
159 return "J";
160 }
161 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200162}
163} // namespace sensors
164
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100165/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200166 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100167 * Gathers data needed for response processing after async calls are done
168 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169class SensorsAsyncResp
170{
171 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200172 using DataCompleteCb = std::function<void(
173 const boost::beast::http::status status,
174 const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
175
176 struct SensorData
177 {
178 const std::string name;
179 std::string uri;
180 const std::string valueKey;
181 const std::string dbusPath;
182 };
183
zhanghch058d1b46d2021-04-01 11:18:24 +0800184 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
185 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700186 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200187 const std::string_view& subNode) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800188 asyncResp(asyncResp),
Ed Tanous271584a2019-07-09 16:24:22 -0700189 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500190 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200191
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200192 // Store extra data about sensor mapping and return it in callback
zhanghch058d1b46d2021-04-01 11:18:24 +0800193 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700195 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200196 const std::string_view& subNode,
197 DataCompleteCb&& creationComplete) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800198 asyncResp(asyncResp),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200199 chassisId(chassisIdIn), types(typesIn),
200 chassisSubNode(subNode), metadata{std::vector<SensorData>()},
201 dataComplete{std::move(creationComplete)}
202 {}
203
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 ~SensorsAsyncResp()
205 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800206 if (asyncResp->res.result() ==
207 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 {
209 // Reset the json object to clear out any data that made it in
210 // before the error happened todo(ed) handle error condition with
211 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800212 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200214
215 if (dataComplete && metadata)
216 {
217 boost::container::flat_map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800218 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200219 {
220 for (auto& sensor : *metadata)
221 {
222 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
223 sensor.dbusPath));
224 }
225 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800226 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200227 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100229
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200230 void addMetadata(const nlohmann::json& sensorObject,
231 const std::string& valueKey, const std::string& dbusPath)
232 {
233 if (metadata)
234 {
235 metadata->emplace_back(SensorData{sensorObject["Name"],
236 sensorObject["@odata.id"],
237 valueKey, dbusPath});
238 }
239 }
240
241 void updateUri(const std::string& name, const std::string& uri)
242 {
243 if (metadata)
244 {
245 for (auto& sensor : *metadata)
246 {
247 if (sensor.name == name)
248 {
249 sensor.uri = uri;
250 }
251 }
252 }
253 }
254
zhanghch058d1b46d2021-04-01 11:18:24 +0800255 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200256 const std::string chassisId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700257 const std::vector<const char*> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200258 const std::string chassisSubNode;
259
260 private:
261 std::optional<std::vector<SensorData>> metadata;
262 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100263};
264
265/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500266 * Possible states for physical inventory leds
267 */
268enum class LedState
269{
270 OFF,
271 ON,
272 BLINK,
273 UNKNOWN
274};
275
276/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500277 * D-Bus inventory item associated with one or more sensors.
278 */
279class InventoryItem
280{
281 public:
282 InventoryItem(const std::string& objPath) :
283 objectPath(objPath), name(), isPresent(true), isFunctional(true),
Gunnar Mills42cbe532019-08-15 15:26:54 -0500284 isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
285 model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""),
Anthony Wilsond5005492019-07-31 16:34:17 -0500286 ledState(LedState::UNKNOWN)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500287 {
288 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800289 sdbusplus::message::object_path path(objectPath);
290 name = path.filename();
291 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500292 {
George Liu28aa8de2021-02-01 15:13:30 +0800293 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500294 }
295 }
296
297 std::string objectPath;
298 std::string name;
299 bool isPresent;
300 bool isFunctional;
301 bool isPowerSupply;
Gunnar Mills42cbe532019-08-15 15:26:54 -0500302 int powerSupplyEfficiencyPercent;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500303 std::string manufacturer;
304 std::string model;
305 std::string partNumber;
306 std::string serialNumber;
307 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500308 std::string ledObjectPath;
309 LedState ledState;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500310};
311
312/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530313 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200314 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100315 * @param sensorNames Sensors retrieved from chassis
316 * @param callback Callback for processing gathered connections
317 */
318template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530319void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000320 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700321 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530322 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700323{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530324 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700325 const std::string path = "/xyz/openbmc_project/sensors";
326 const std::array<std::string, 1> interfaces = {
327 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100328
Ed Tanous1abe55e2018-09-05 08:30:59 -0700329 // Response handler for parsing objects subtree
Ed Tanous81ce6092020-12-17 16:54:55 +0000330 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700331 sensorNames](const boost::system::error_code ec,
332 const GetSubTreeType& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530333 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700334 if (ec)
335 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800336 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530337 BMCWEB_LOG_ERROR
338 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700339 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100340 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100341
Ed Tanous1abe55e2018-09-05 08:30:59 -0700342 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
343
344 // Make unique list of connections only for requested sensor types and
345 // found in the chassis
346 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530347 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700348 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
349 // producers
350 connections.reserve(8);
351
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700352 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
353 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700354 {
355 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
356 }
357
358 for (const std::pair<
359 std::string,
360 std::vector<std::pair<std::string, std::vector<std::string>>>>&
361 object : subtree)
362 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700363 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700364 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700365 for (const std::pair<std::string, std::vector<std::string>>&
366 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700367 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700368 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
369 connections.insert(objData.first);
370 objectsWithConnection.insert(
371 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700372 }
373 }
374 }
375 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530376 callback(std::move(connections), std::move(objectsWithConnection));
377 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700378 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700379 // Make call to ObjectMapper to find all sensors objects
380 crow::connections::systemBus->async_method_call(
381 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
382 "/xyz/openbmc_project/object_mapper",
383 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530384 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
385}
386
387/**
388 * @brief Create connections necessary for sensors
389 * @param SensorsAsyncResp Pointer to object holding response data
390 * @param sensorNames Sensors retrieved from chassis
391 * @param callback Callback for processing gathered connections
392 */
393template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700394void getConnections(
Ed Tanous81ce6092020-12-17 16:54:55 +0000395 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700396 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
397 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530398{
399 auto objectsWithConnectionCb =
400 [callback](const boost::container::flat_set<std::string>& connections,
401 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700402 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000403 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530404 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100405}
406
407/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700408 * @brief Shrinks the list of sensors for processing
409 * @param SensorsAysncResp The class holding the Redfish response
410 * @param allSensors A list of all the sensors associated to the
411 * chassis element (i.e. baseboard, front panel, etc...)
412 * @param activeSensors A list that is a reduction of the incoming
413 * allSensors list. Eliminate Thermal sensors when a Power request is
414 * made, and eliminate Power sensors when a Thermal request is made.
415 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000416inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000417 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700418 const std::vector<std::string>* allSensors,
Ed Tanousb5a76932020-09-29 16:16:58 -0700419 const std::shared_ptr<boost::container::flat_set<std::string>>&
420 activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700421{
Ed Tanous81ce6092020-12-17 16:54:55 +0000422 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700423 {
424 return;
425 }
426 if ((allSensors == nullptr) || (activeSensors == nullptr))
427 {
428 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800429 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000430 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200431 ? "Temperatures"
432 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700433
434 return;
435 }
436 if (allSensors->empty())
437 {
438 // Nothing to do, the activeSensors object is also empty
439 return;
440 }
441
Ed Tanous81ce6092020-12-17 16:54:55 +0000442 for (const char* type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700443 {
444 for (const std::string& sensor : *allSensors)
445 {
446 if (boost::starts_with(sensor, type))
447 {
448 activeSensors->emplace(sensor);
449 }
450 }
451 }
452}
453
454/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800455 * @brief Retrieves valid chassis path
456 * @param asyncResp Pointer to object holding response data
457 * @param callback Callback for next step to get valid chassis path
458 */
459template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700460void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800461 Callback&& callback)
462{
463 BMCWEB_LOG_DEBUG << "checkChassisId enter";
464 const std::array<const char*, 2> interfaces = {
465 "xyz.openbmc_project.Inventory.Item.Board",
466 "xyz.openbmc_project.Inventory.Item.Chassis"};
467
468 auto respHandler =
469 [callback{std::move(callback)},
470 asyncResp](const boost::system::error_code ec,
471 const std::vector<std::string>& chassisPaths) mutable {
472 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
473 if (ec)
474 {
475 BMCWEB_LOG_ERROR
476 << "getValidChassisPath respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800477 messages::internalError(asyncResp->asyncResp->res);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800478 return;
479 }
480
481 std::optional<std::string> chassisPath;
482 std::string chassisName;
483 for (const std::string& chassis : chassisPaths)
484 {
George Liu28aa8de2021-02-01 15:13:30 +0800485 sdbusplus::message::object_path path(chassis);
486 chassisName = path.filename();
487 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800488 {
489 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
490 continue;
491 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800492 if (chassisName == asyncResp->chassisId)
493 {
494 chassisPath = chassis;
495 break;
496 }
497 }
498 callback(chassisPath);
499 };
500
501 // Get the Chassis Collection
502 crow::connections::systemBus->async_method_call(
503 respHandler, "xyz.openbmc_project.ObjectMapper",
504 "/xyz/openbmc_project/object_mapper",
505 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
506 "/xyz/openbmc_project/inventory", 0, interfaces);
507 BMCWEB_LOG_DEBUG << "checkChassisId exit";
508}
509
510/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100511 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200512 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100513 * @param callback Callback for next step in gathered sensor processing
514 */
515template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700516void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700517 Callback&& callback)
518{
519 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500520 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700521 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500522 "xyz.openbmc_project.Inventory.Item.Chassis"};
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700523 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
524 const boost::system::error_code ec,
525 const std::vector<std::string>& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
527 if (ec)
528 {
529 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800530 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700531 return;
532 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100533
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700534 const std::string* chassisPath = nullptr;
535 std::string chassisName;
536 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700537 {
George Liu28aa8de2021-02-01 15:13:30 +0800538 sdbusplus::message::object_path path(chassis);
539 chassisName = path.filename();
540 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700542 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543 continue;
544 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700545 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700547 chassisPath = &chassis;
548 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700549 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700550 }
551 if (chassisPath == nullptr)
552 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800553 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
554 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700555 return;
556 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700557
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700558 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200559 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700560 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800561 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700562 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700563 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200564 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700565 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800566 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700567 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800568 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
569 nlohmann::json::array();
570 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800571 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700572 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200573 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500574 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800575 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500576 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500578 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800579 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500580 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800581 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
582 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500583 }
584
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200585 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500586 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800587 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500588 }
589
zhanghch058d1b46d2021-04-01 11:18:24 +0800590 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700591 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
592 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800593 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500594 // Get the list of all sensors for this Chassis element
595 std::string sensorPath = *chassisPath + "/all_sensors";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700596 crow::connections::systemBus->async_method_call(
597 [sensorsAsyncResp, callback{std::move(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700598 const boost::system::error_code& e,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700599 const std::variant<std::vector<std::string>>&
600 variantEndpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -0700601 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700602 {
Ed Tanous271584a2019-07-09 16:24:22 -0700603 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700604 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800605 messages::internalError(
606 sensorsAsyncResp->asyncResp->res);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700607 return;
608 }
609 }
610 const std::vector<std::string>* nodeSensorList =
611 std::get_if<std::vector<std::string>>(&(variantEndpoints));
612 if (nodeSensorList == nullptr)
613 {
614 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800615 sensorsAsyncResp->asyncResp->res,
616 sensorsAsyncResp->chassisSubNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200617 sensorsAsyncResp->chassisSubNode ==
618 sensors::node::thermal
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700619 ? "Temperatures"
Patrick Williams738c1e62021-02-22 17:14:25 -0600620 : sensorsAsyncResp->chassisSubNode ==
621 sensors::node::power
622 ? "Voltages"
623 : "Sensors");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700624 return;
625 }
626 const std::shared_ptr<boost::container::flat_set<std::string>>
627 culledSensorList = std::make_shared<
628 boost::container::flat_set<std::string>>();
629 reduceSensorList(sensorsAsyncResp, nodeSensorList,
630 culledSensorList);
631 callback(culledSensorList);
632 },
633 "xyz.openbmc_project.ObjectMapper", sensorPath,
634 "org.freedesktop.DBus.Properties", "Get",
635 "xyz.openbmc_project.Association", "endpoints");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100636 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100637
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700638 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700639 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700640 respHandler, "xyz.openbmc_project.ObjectMapper",
641 "/xyz/openbmc_project/object_mapper",
642 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700643 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100645}
646
647/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600648 * @brief Finds all DBus object paths that implement ObjectManager.
649 *
650 * Creates a mapping from the associated connection name to the object path.
651 *
652 * Finds the object paths asynchronously. Invokes callback when information has
653 * been obtained.
654 *
655 * The callback must have the following signature:
656 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500657 * callback(std::shared_ptr<boost::container::flat_map<std::string,
658 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600659 * @endcode
660 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700661 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600662 * @param callback Callback to invoke when object paths obtained.
663 */
664template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700665void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000666 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700667 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600668{
669 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
670 const std::array<std::string, 1> interfaces = {
671 "org.freedesktop.DBus.ObjectManager"};
672
673 // Response handler for GetSubTree DBus method
674 auto respHandler = [callback{std::move(callback)},
Ed Tanous81ce6092020-12-17 16:54:55 +0000675 sensorsAsyncResp](const boost::system::error_code ec,
Shawn McCarneyde629b62019-03-08 10:42:51 -0600676 const GetSubTreeType& subtree) {
677 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
678 if (ec)
679 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800680 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600681 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
682 << ec;
683 return;
684 }
685
686 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500687 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
688 objectMgrPaths = std::make_shared<
689 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600690 for (const std::pair<
691 std::string,
692 std::vector<std::pair<std::string, std::vector<std::string>>>>&
693 object : subtree)
694 {
695 // Loop over connections for current object path
696 const std::string& objectPath = object.first;
697 for (const std::pair<std::string, std::vector<std::string>>&
698 objData : object.second)
699 {
700 // Add mapping from connection to object path
701 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500702 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600703 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
704 << objectPath;
705 }
706 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500707 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600708 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
709 };
710
711 // Query mapper for all DBus object paths that implement ObjectManager
712 crow::connections::systemBus->async_method_call(
713 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
714 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700715 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600716 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
717}
718
719/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500720 * @brief Returns the Redfish State value for the specified inventory item.
721 * @param inventoryItem D-Bus inventory item associated with a sensor.
722 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700723 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000724inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500725{
726 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
727 {
728 return "Absent";
729 }
James Feist34dd1792019-05-17 14:10:54 -0700730
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500731 return "Enabled";
732}
733
734/**
735 * @brief Returns the Redfish Health value for the specified sensor.
736 * @param sensorJson Sensor JSON object.
737 * @param interfacesDict Map of all sensor interfaces.
738 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
739 * be nullptr if no associated inventory item was found.
740 * @return Health value for sensor.
741 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000742inline std::string getHealth(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500743 nlohmann::json& sensorJson,
James Feist34dd1792019-05-17 14:10:54 -0700744 const boost::container::flat_map<
745 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500746 interfacesDict,
747 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700748{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500749 // Get current health value (if any) in the sensor JSON object. Some JSON
750 // objects contain multiple sensors (such as PowerSupplies). We want to set
751 // the overall health to be the most severe of any of the sensors.
752 std::string currentHealth;
753 auto statusIt = sensorJson.find("Status");
754 if (statusIt != sensorJson.end())
755 {
756 auto healthIt = statusIt->find("Health");
757 if (healthIt != statusIt->end())
758 {
759 std::string* health = healthIt->get_ptr<std::string*>();
760 if (health != nullptr)
761 {
762 currentHealth = *health;
763 }
764 }
765 }
766
767 // If current health in JSON object is already Critical, return that. This
768 // should override the sensor health, which might be less severe.
769 if (currentHealth == "Critical")
770 {
771 return "Critical";
772 }
773
774 // Check if sensor has critical threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700775 auto criticalThresholdIt =
776 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
777 if (criticalThresholdIt != interfacesDict.end())
778 {
779 auto thresholdHighIt =
780 criticalThresholdIt->second.find("CriticalAlarmHigh");
781 auto thresholdLowIt =
782 criticalThresholdIt->second.find("CriticalAlarmLow");
783 if (thresholdHighIt != criticalThresholdIt->second.end())
784 {
785 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
786 if (asserted == nullptr)
787 {
788 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
789 }
790 else if (*asserted)
791 {
792 return "Critical";
793 }
794 }
795 if (thresholdLowIt != criticalThresholdIt->second.end())
796 {
797 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
798 if (asserted == nullptr)
799 {
800 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
801 }
802 else if (*asserted)
803 {
804 return "Critical";
805 }
806 }
807 }
808
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500809 // Check if associated inventory item is not functional
810 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
811 {
812 return "Critical";
813 }
814
815 // If current health in JSON object is already Warning, return that. This
816 // should override the sensor status, which might be less severe.
817 if (currentHealth == "Warning")
818 {
819 return "Warning";
820 }
821
822 // Check if sensor has warning threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700823 auto warningThresholdIt =
824 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
825 if (warningThresholdIt != interfacesDict.end())
826 {
827 auto thresholdHighIt =
828 warningThresholdIt->second.find("WarningAlarmHigh");
829 auto thresholdLowIt =
830 warningThresholdIt->second.find("WarningAlarmLow");
831 if (thresholdHighIt != warningThresholdIt->second.end())
832 {
833 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
834 if (asserted == nullptr)
835 {
836 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
837 }
838 else if (*asserted)
839 {
840 return "Warning";
841 }
842 }
843 if (thresholdLowIt != warningThresholdIt->second.end())
844 {
845 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
846 if (asserted == nullptr)
847 {
848 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
849 }
850 else if (*asserted)
851 {
852 return "Warning";
853 }
854 }
855 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500856
James Feist34dd1792019-05-17 14:10:54 -0700857 return "OK";
858}
859
Ed Tanous23a21a12020-07-25 04:45:05 +0000860inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500861 const InventoryItem* inventoryItem)
862{
863 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
864 {
865 switch (inventoryItem->ledState)
866 {
867 case LedState::OFF:
868 sensorJson["IndicatorLED"] = "Off";
869 break;
870 case LedState::ON:
871 sensorJson["IndicatorLED"] = "Lit";
872 break;
873 case LedState::BLINK:
874 sensorJson["IndicatorLED"] = "Blinking";
875 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000876 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500877 break;
878 }
879 }
880}
881
James Feist34dd1792019-05-17 14:10:54 -0700882/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100883 * @brief Builds a json sensor representation of a sensor.
884 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500885 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100886 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200887 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100888 * @param interfacesDict A dictionary of the interfaces and properties of said
889 * interfaces to be built from
890 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500891 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
892 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100893 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000894inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100895 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700896 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100897 const boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700898 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100899 interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000900 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901{
902 // We need a value interface before we can do anything with it
903 auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
904 if (valueIt == interfacesDict.end())
905 {
906 BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
907 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100908 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100909
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 // Assume values exist as is (10^0 == 1) if no scale exists
911 int64_t scaleMultiplier = 0;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100912
Ed Tanous1abe55e2018-09-05 08:30:59 -0700913 auto scaleIt = valueIt->second.find("Scale");
914 // If a scale exists, pull value as int64, and use the scaling.
915 if (scaleIt != valueIt->second.end())
916 {
Ed Tanousabf2add2019-01-22 16:40:12 -0800917 const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 if (int64Value != nullptr)
919 {
920 scaleMultiplier = *int64Value;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100921 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100922 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200924 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500925 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500926 // For sensors in SensorCollection we set Id instead of MemberId,
927 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000928 sensorJson["Id"] = sensorName;
929 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500930 }
931 else if (sensorType != "power")
932 {
933 // Set MemberId and Name for non-power sensors. For PowerSupplies and
934 // PowerControl, those properties have more general values because
935 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000936 sensorJson["MemberId"] = sensorName;
937 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500938 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700939
Ed Tanous81ce6092020-12-17 16:54:55 +0000940 sensorJson["Status"]["State"] = getState(inventoryItem);
941 sensorJson["Status"]["Health"] =
942 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943
944 // Parameter to set to override the type we get from dbus, and force it to
945 // int, regardless of what is available. This is used for schemas like fan,
946 // that require integers, not floats.
947 bool forceToInt = false;
948
Anthony Wilson3929aca2019-07-19 15:42:33 -0500949 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200950 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500951 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000952 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000953
954 const std::string& readingType = sensors::toReadingType(sensorType);
955 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500956 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000957 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
958 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500959 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000960 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500961 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000962 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500963 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000964
965 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
966 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200967 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000968 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
969 << sensorType;
970 }
971 else
972 {
973 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200974 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500975 }
976 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700977 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500978 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000979 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 // TODO(ed) Documentation says that path should be type fan_tach,
981 // implementation seems to implement fan
982 }
983 else if (sensorType == "fan" || sensorType == "fan_tach")
984 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500985 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000986 sensorJson["ReadingUnits"] = "RPM";
987 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
988 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 forceToInt = true;
990 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700991 else if (sensorType == "fan_pwm")
992 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500993 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000994 sensorJson["ReadingUnits"] = "Percent";
995 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
996 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700997 forceToInt = true;
998 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700999 else if (sensorType == "voltage")
1000 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001001 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +00001002 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001003 }
Ed Tanous2474adf2018-09-05 16:31:16 -07001004 else if (sensorType == "power")
1005 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001006 std::string sensorNameLower =
1007 boost::algorithm::to_lower_copy(sensorName);
1008
Eddie James028f7eb2019-05-17 21:24:36 +00001009 if (!sensorName.compare("total_power"))
1010 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001011 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -05001012 // Put multiple "sensors" into a single PowerControl, so have
1013 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +00001014 sensorJson["MemberId"] = "0";
1015 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -05001016 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +00001017 }
1018 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001019 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001020 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001021 }
1022 else
1023 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001024 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001025 }
Ed Tanous2474adf2018-09-05 16:31:16 -07001026 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001027 else
1028 {
1029 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
1030 return;
1031 }
1032 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -05001033 std::vector<
1034 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
1035 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001036 properties.reserve(7);
1037
1038 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001039
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001040 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -05001041 {
1042 properties.emplace_back(
1043 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
1044 "/Thresholds/UpperCaution/Reading"_json_pointer);
1045 properties.emplace_back(
1046 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1047 "/Thresholds/LowerCaution/Reading"_json_pointer);
1048 properties.emplace_back(
1049 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1050 "/Thresholds/UpperCritical/Reading"_json_pointer);
1051 properties.emplace_back(
1052 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1053 "/Thresholds/LowerCritical/Reading"_json_pointer);
1054 }
1055 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001056 {
1057 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001058 "WarningHigh",
1059 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001060 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001061 "WarningLow",
1062 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001063 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001064 "CriticalHigh",
1065 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001066 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001067 "CriticalLow",
1068 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001069 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001070
Ed Tanous2474adf2018-09-05 16:31:16 -07001071 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1072
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001073 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001074 {
1075 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001076 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001077 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001078 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001079 }
1080 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001081 {
1082 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001083 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001084 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001085 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001086 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001087 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001088 {
1089 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001090 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001091 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001092 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001093 }
1094
Anthony Wilson3929aca2019-07-19 15:42:33 -05001095 for (const std::tuple<const char*, const char*,
1096 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097 {
1098 auto interfaceProperties = interfacesDict.find(std::get<0>(p));
1099 if (interfaceProperties != interfacesDict.end())
1100 {
Ed Tanous271584a2019-07-09 16:24:22 -07001101 auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
1102 if (thisValueIt != interfaceProperties->second.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001103 {
Ed Tanous271584a2019-07-09 16:24:22 -07001104 const SensorVariant& valueVariant = thisValueIt->second;
Anthony Wilson3929aca2019-07-19 15:42:33 -05001105
1106 // The property we want to set may be nested json, so use
1107 // a json_pointer for easy indexing into the json structure.
1108 const nlohmann::json::json_pointer& key = std::get<2>(p);
1109
Ed Tanous1abe55e2018-09-05 08:30:59 -07001110 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001111 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001112
Ed Tanousabf2add2019-01-22 16:40:12 -08001113 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001114 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001115 double temp = 0.0;
1116 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001117 {
Ed Tanous271584a2019-07-09 16:24:22 -07001118 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001119 }
1120 else if (doubleValue != nullptr)
1121 {
1122 temp = *doubleValue;
1123 }
Eddie James028f7eb2019-05-17 21:24:36 +00001124 else if (uValue != nullptr)
1125 {
1126 temp = *uValue;
1127 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001128 else
1129 {
1130 BMCWEB_LOG_ERROR
1131 << "Got value interface that wasn't int or double";
1132 continue;
1133 }
1134 temp = temp * std::pow(10, scaleMultiplier);
1135 if (forceToInt)
1136 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001137 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001138 }
1139 else
1140 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001141 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001142 }
1143 }
1144 }
1145 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001146
Ed Tanous81ce6092020-12-17 16:54:55 +00001147 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001148 "/xyz/openbmc_project/sensors/" + sensorType +
1149 "/" + sensorName);
1150
Ed Tanous1abe55e2018-09-05 08:30:59 -07001151 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001152}
1153
Ed Tanousb5a76932020-09-29 16:16:58 -07001154inline void populateFanRedundancy(
1155 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001156{
1157 crow::connections::systemBus->async_method_call(
1158 [sensorsAsyncResp](const boost::system::error_code ec,
1159 const GetSubTreeType& resp) {
1160 if (ec)
1161 {
1162 return; // don't have to have this interface
1163 }
Ed Tanouse278c182019-03-13 16:23:37 -07001164 for (const std::pair<std::string,
1165 std::vector<std::pair<
1166 std::string, std::vector<std::string>>>>&
1167 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001168 {
Ed Tanouse278c182019-03-13 16:23:37 -07001169 const std::string& path = pathPair.first;
1170 const std::vector<
1171 std::pair<std::string, std::vector<std::string>>>& objDict =
1172 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001173 if (objDict.empty())
1174 {
1175 continue; // this should be impossible
1176 }
1177
1178 const std::string& owner = objDict.begin()->first;
1179 crow::connections::systemBus->async_method_call(
1180 [path, owner,
Ed Tanous271584a2019-07-09 16:24:22 -07001181 sensorsAsyncResp](const boost::system::error_code e,
James Feist8bd25cc2019-03-15 15:14:00 -07001182 std::variant<std::vector<std::string>>
1183 variantEndpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001184 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001185 {
1186 return; // if they don't have an association we
1187 // can't tell what chassis is
1188 }
1189 // verify part of the right chassis
1190 auto endpoints = std::get_if<std::vector<std::string>>(
1191 &variantEndpoints);
1192
1193 if (endpoints == nullptr)
1194 {
1195 BMCWEB_LOG_ERROR << "Invalid association interface";
zhanghch058d1b46d2021-04-01 11:18:24 +08001196 messages::internalError(
1197 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001198 return;
1199 }
1200
1201 auto found = std::find_if(
1202 endpoints->begin(), endpoints->end(),
1203 [sensorsAsyncResp](const std::string& entry) {
1204 return entry.find(
1205 sensorsAsyncResp->chassisId) !=
1206 std::string::npos;
1207 });
1208
1209 if (found == endpoints->end())
1210 {
1211 return;
1212 }
1213 crow::connections::systemBus->async_method_call(
1214 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001215 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001216 const boost::container::flat_map<
1217 std::string,
1218 std::variant<uint8_t,
1219 std::vector<std::string>,
1220 std::string>>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001221 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001222 {
1223 return; // don't have to have this
1224 // interface
1225 }
1226 auto findFailures = ret.find("AllowedFailures");
1227 auto findCollection = ret.find("Collection");
1228 auto findStatus = ret.find("Status");
1229
1230 if (findFailures == ret.end() ||
1231 findCollection == ret.end() ||
1232 findStatus == ret.end())
1233 {
1234 BMCWEB_LOG_ERROR
1235 << "Invalid redundancy interface";
1236 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001237 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001238 return;
1239 }
1240
1241 auto allowedFailures = std::get_if<uint8_t>(
1242 &(findFailures->second));
1243 auto collection =
1244 std::get_if<std::vector<std::string>>(
1245 &(findCollection->second));
1246 auto status = std::get_if<std::string>(
1247 &(findStatus->second));
1248
1249 if (allowedFailures == nullptr ||
1250 collection == nullptr || status == nullptr)
1251 {
1252
1253 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001254 << "Invalid redundancy interface types";
James Feist8bd25cc2019-03-15 15:14:00 -07001255 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001256 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001257 return;
1258 }
George Liu28aa8de2021-02-01 15:13:30 +08001259 sdbusplus::message::object_path objectPath(
1260 path);
1261 std::string name = objectPath.filename();
1262 if (name.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001263 {
1264 // this should be impossible
1265 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001266 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001267 return;
1268 }
James Feist8bd25cc2019-03-15 15:14:00 -07001269 std::replace(name.begin(), name.end(), '_',
1270 ' ');
1271
1272 std::string health;
1273
1274 if (boost::ends_with(*status, "Full"))
1275 {
1276 health = "OK";
1277 }
1278 else if (boost::ends_with(*status, "Degraded"))
1279 {
1280 health = "Warning";
1281 }
1282 else
1283 {
1284 health = "Critical";
1285 }
1286 std::vector<nlohmann::json> redfishCollection;
1287 const auto& fanRedfish =
zhanghch058d1b46d2021-04-01 11:18:24 +08001288 sensorsAsyncResp->asyncResp->res
1289 .jsonValue["Fans"];
James Feist8bd25cc2019-03-15 15:14:00 -07001290 for (const std::string& item : *collection)
1291 {
George Liu28aa8de2021-02-01 15:13:30 +08001292 sdbusplus::message::object_path path(item);
1293 std::string itemName = path.filename();
1294 if (itemName.empty())
1295 {
1296 continue;
1297 }
James Feist8bd25cc2019-03-15 15:14:00 -07001298 /*
1299 todo(ed): merge patch that fixes the names
1300 std::replace(itemName.begin(),
1301 itemName.end(), '_', ' ');*/
1302 auto schemaItem = std::find_if(
1303 fanRedfish.begin(), fanRedfish.end(),
1304 [itemName](const nlohmann::json& fan) {
1305 return fan["MemberId"] == itemName;
1306 });
1307 if (schemaItem != fanRedfish.end())
1308 {
1309 redfishCollection.push_back(
1310 {{"@odata.id",
1311 (*schemaItem)["@odata.id"]}});
1312 }
1313 else
1314 {
1315 BMCWEB_LOG_ERROR
1316 << "failed to find fan in schema";
1317 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001318 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001319 return;
1320 }
1321 }
1322
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001323 size_t minNumNeeded =
1324 collection->size() > 0
1325 ? collection->size() - *allowedFailures
1326 : 0;
Ed Tanous271584a2019-07-09 16:24:22 -07001327 nlohmann::json& jResp =
zhanghch058d1b46d2021-04-01 11:18:24 +08001328 sensorsAsyncResp->asyncResp->res
Ed Tanous271584a2019-07-09 16:24:22 -07001329 .jsonValue["Redundancy"];
1330 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001331 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301332 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001333 sensorsAsyncResp->chassisId + "/" +
1334 sensorsAsyncResp->chassisSubNode +
1335 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001336 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001337 {"@odata.type",
1338 "#Redundancy.v1_3_2.Redundancy"},
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001339 {"MinNumNeeded", minNumNeeded},
James Feist8bd25cc2019-03-15 15:14:00 -07001340 {"MemberId", name},
1341 {"Mode", "N+m"},
1342 {"Name", name},
1343 {"RedundancySet", redfishCollection},
1344 {"Status",
1345 {{"Health", health},
1346 {"State", "Enabled"}}}});
1347 },
1348 owner, path, "org.freedesktop.DBus.Properties",
1349 "GetAll",
1350 "xyz.openbmc_project.Control.FanRedundancy");
1351 },
James Feist02e92e32019-06-26 12:07:05 -07001352 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
James Feist8bd25cc2019-03-15 15:14:00 -07001353 "org.freedesktop.DBus.Properties", "Get",
1354 "xyz.openbmc_project.Association", "endpoints");
1355 }
1356 },
1357 "xyz.openbmc_project.ObjectMapper",
1358 "/xyz/openbmc_project/object_mapper",
1359 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1360 "/xyz/openbmc_project/control", 2,
1361 std::array<const char*, 1>{
1362 "xyz.openbmc_project.Control.FanRedundancy"});
1363}
1364
Ed Tanousb5a76932020-09-29 16:16:58 -07001365inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001366 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001367{
zhanghch058d1b46d2021-04-01 11:18:24 +08001368 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001369 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001370 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001371 {
1372 sensorHeaders = {"Voltages", "PowerSupplies"};
1373 }
1374 for (const std::string& sensorGroup : sensorHeaders)
1375 {
1376 nlohmann::json::iterator entry = response.find(sensorGroup);
1377 if (entry != response.end())
1378 {
1379 std::sort(entry->begin(), entry->end(),
1380 [](nlohmann::json& c1, nlohmann::json& c2) {
1381 return c1["Name"] < c2["Name"];
1382 });
1383
1384 // add the index counts to the end of each entry
1385 size_t count = 0;
1386 for (nlohmann::json& sensorJson : *entry)
1387 {
1388 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1389 if (odata == sensorJson.end())
1390 {
1391 continue;
1392 }
1393 std::string* value = odata->get_ptr<std::string*>();
1394 if (value != nullptr)
1395 {
1396 *value += std::to_string(count);
1397 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001398 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001399 }
1400 }
1401 }
1402 }
1403}
1404
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001405/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001406 * @brief Finds the inventory item with the specified object path.
1407 * @param inventoryItems D-Bus inventory items associated with sensors.
1408 * @param invItemObjPath D-Bus object path of inventory item.
1409 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001410 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001411inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001412 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001413 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001414{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001415 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001416 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001417 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001418 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001419 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001420 }
1421 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001422 return nullptr;
1423}
1424
1425/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426 * @brief Finds the inventory item associated with the specified sensor.
1427 * @param inventoryItems D-Bus inventory items associated with sensors.
1428 * @param sensorObjPath D-Bus object path of sensor.
1429 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001430 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001431inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001432 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001433 const std::string& sensorObjPath)
1434{
1435 for (InventoryItem& inventoryItem : *inventoryItems)
1436 {
1437 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1438 {
1439 return &inventoryItem;
1440 }
1441 }
1442 return nullptr;
1443}
1444
1445/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001446 * @brief Finds the inventory item associated with the specified led path.
1447 * @param inventoryItems D-Bus inventory items associated with sensors.
1448 * @param ledObjPath D-Bus object path of led.
1449 * @return Inventory item within vector, or nullptr if no match found.
1450 */
1451inline InventoryItem*
1452 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1453 const std::string& ledObjPath)
1454{
1455 for (InventoryItem& inventoryItem : inventoryItems)
1456 {
1457 if (inventoryItem.ledObjectPath == ledObjPath)
1458 {
1459 return &inventoryItem;
1460 }
1461 }
1462 return nullptr;
1463}
1464
1465/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001466 * @brief Adds inventory item and associated sensor to specified vector.
1467 *
1468 * Adds a new InventoryItem to the vector if necessary. Searches for an
1469 * existing InventoryItem with the specified object path. If not found, one is
1470 * added to the vector.
1471 *
1472 * Next, the specified sensor is added to the set of sensors associated with the
1473 * InventoryItem.
1474 *
1475 * @param inventoryItems D-Bus inventory items associated with sensors.
1476 * @param invItemObjPath D-Bus object path of inventory item.
1477 * @param sensorObjPath D-Bus object path of sensor
1478 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001479inline void addInventoryItem(
1480 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1481 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001482{
1483 // Look for inventory item in vector
1484 InventoryItem* inventoryItem =
1485 findInventoryItem(inventoryItems, invItemObjPath);
1486
1487 // If inventory item doesn't exist in vector, add it
1488 if (inventoryItem == nullptr)
1489 {
1490 inventoryItems->emplace_back(invItemObjPath);
1491 inventoryItem = &(inventoryItems->back());
1492 }
1493
1494 // Add sensor to set of sensors associated with inventory item
1495 inventoryItem->sensors.emplace(sensorObjPath);
1496}
1497
1498/**
1499 * @brief Stores D-Bus data in the specified inventory item.
1500 *
1501 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1502 * specified InventoryItem.
1503 *
1504 * This data is later used to provide sensor property values in the JSON
1505 * response.
1506 *
1507 * @param inventoryItem Inventory item where data will be stored.
1508 * @param interfacesDict Map containing D-Bus interfaces and their properties
1509 * for the specified inventory item.
1510 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001511inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001512 InventoryItem& inventoryItem,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001513 const boost::container::flat_map<
1514 std::string, boost::container::flat_map<std::string, SensorVariant>>&
1515 interfacesDict)
1516{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001517 // Get properties from Inventory.Item interface
1518 auto interfaceIt =
1519 interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1520 if (interfaceIt != interfacesDict.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001521 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001522 auto propertyIt = interfaceIt->second.find("Present");
1523 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001524 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001525 const bool* value = std::get_if<bool>(&propertyIt->second);
1526 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001527 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001528 inventoryItem.isPresent = *value;
1529 }
1530 }
1531 }
1532
1533 // Check if Inventory.Item.PowerSupply interface is present
1534 interfaceIt =
1535 interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1536 if (interfaceIt != interfacesDict.end())
1537 {
1538 inventoryItem.isPowerSupply = true;
1539 }
1540
1541 // Get properties from Inventory.Decorator.Asset interface
1542 interfaceIt =
1543 interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1544 if (interfaceIt != interfacesDict.end())
1545 {
1546 auto propertyIt = interfaceIt->second.find("Manufacturer");
1547 if (propertyIt != interfaceIt->second.end())
1548 {
1549 const std::string* value =
1550 std::get_if<std::string>(&propertyIt->second);
1551 if (value != nullptr)
1552 {
1553 inventoryItem.manufacturer = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554 }
1555 }
1556
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001557 propertyIt = interfaceIt->second.find("Model");
1558 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001560 const std::string* value =
1561 std::get_if<std::string>(&propertyIt->second);
1562 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001563 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001564 inventoryItem.model = *value;
1565 }
1566 }
1567
1568 propertyIt = interfaceIt->second.find("PartNumber");
1569 if (propertyIt != interfaceIt->second.end())
1570 {
1571 const std::string* value =
1572 std::get_if<std::string>(&propertyIt->second);
1573 if (value != nullptr)
1574 {
1575 inventoryItem.partNumber = *value;
1576 }
1577 }
1578
1579 propertyIt = interfaceIt->second.find("SerialNumber");
1580 if (propertyIt != interfaceIt->second.end())
1581 {
1582 const std::string* value =
1583 std::get_if<std::string>(&propertyIt->second);
1584 if (value != nullptr)
1585 {
1586 inventoryItem.serialNumber = *value;
1587 }
1588 }
1589 }
1590
1591 // Get properties from State.Decorator.OperationalStatus interface
1592 interfaceIt = interfacesDict.find(
1593 "xyz.openbmc_project.State.Decorator.OperationalStatus");
1594 if (interfaceIt != interfacesDict.end())
1595 {
1596 auto propertyIt = interfaceIt->second.find("Functional");
1597 if (propertyIt != interfaceIt->second.end())
1598 {
1599 const bool* value = std::get_if<bool>(&propertyIt->second);
1600 if (value != nullptr)
1601 {
1602 inventoryItem.isFunctional = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001603 }
1604 }
1605 }
1606}
1607
1608/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001609 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001610 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001611 * Uses the specified connections (services) to obtain D-Bus data for inventory
1612 * items associated with sensors. Stores the resulting data in the
1613 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001614 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001615 * This data is later used to provide sensor property values in the JSON
1616 * response.
1617 *
1618 * Finds the inventory item data asynchronously. Invokes callback when data has
1619 * been obtained.
1620 *
1621 * The callback must have the following signature:
1622 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001623 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001624 * @endcode
1625 *
1626 * This function is called recursively, obtaining data asynchronously from one
1627 * connection in each call. This ensures the callback is not invoked until the
1628 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001629 *
1630 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001631 * @param inventoryItems D-Bus inventory items associated with sensors.
1632 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633 * @param objectMgrPaths Mappings from connection name to DBus object path that
1634 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001635 * @param callback Callback to invoke when inventory data has been obtained.
1636 * @param invConnectionsIndex Current index in invConnections. Only specified
1637 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001638 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001639template <typename Callback>
1640static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001641 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001642 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001643 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1644 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001645 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001646 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001647{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001648 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001649
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001650 // If no more connections left, call callback
1651 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001652 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001653 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001654 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1655 return;
1656 }
1657
1658 // Get inventory item data from current connection
1659 auto it = invConnections->nth(invConnectionsIndex);
1660 if (it != invConnections->end())
1661 {
1662 const std::string& invConnection = *it;
1663
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001664 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001665 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1666 objectMgrPaths, callback{std::move(callback)},
1667 invConnectionsIndex](
1668 const boost::system::error_code ec,
1669 ManagedObjectsVectorType& resp) {
1670 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001671 if (ec)
1672 {
1673 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001674 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001675 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001676 return;
1677 }
1678
1679 // Loop through returned object paths
1680 for (const auto& objDictEntry : resp)
1681 {
1682 const std::string& objPath =
1683 static_cast<const std::string&>(objDictEntry.first);
1684
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001685 // If this object path is one of the specified inventory items
1686 InventoryItem* inventoryItem =
1687 findInventoryItem(inventoryItems, objPath);
1688 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001689 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001690 // Store inventory data in InventoryItem
1691 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001692 }
1693 }
1694
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001695 // Recurse to get inventory item data from next connection
1696 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1697 invConnections, objectMgrPaths,
1698 std::move(callback), invConnectionsIndex + 1);
1699
1700 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001701 };
1702
1703 // Find DBus object path that implements ObjectManager for the current
1704 // connection. If no mapping found, default to "/".
1705 auto iter = objectMgrPaths->find(invConnection);
1706 const std::string& objectMgrPath =
1707 (iter != objectMgrPaths->end()) ? iter->second : "/";
1708 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1709 << objectMgrPath;
1710
1711 // Get all object paths and their interfaces for current connection
1712 crow::connections::systemBus->async_method_call(
1713 std::move(respHandler), invConnection, objectMgrPath,
1714 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1715 }
1716
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001717 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001718}
1719
1720/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001721 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001722 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001723 * Gets the D-Bus connections (services) that provide data for the inventory
1724 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725 *
1726 * Finds the connections asynchronously. Invokes callback when information has
1727 * been obtained.
1728 *
1729 * The callback must have the following signature:
1730 * @code
1731 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1732 * invConnections)
1733 * @endcode
1734 *
1735 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001736 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001737 * @param callback Callback to invoke when connections have been obtained.
1738 */
1739template <typename Callback>
1740static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001741 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1742 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001743 Callback&& callback)
1744{
1745 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1746
1747 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001748 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001749 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001750 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1751 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001752 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1753
1754 // Response handler for parsing output from GetSubTree
1755 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001756 inventoryItems](const boost::system::error_code ec,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001757 const GetSubTreeType& subtree) {
1758 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1759 if (ec)
1760 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001761 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001762 BMCWEB_LOG_ERROR
1763 << "getInventoryItemsConnections respHandler DBus error " << ec;
1764 return;
1765 }
1766
1767 // Make unique list of connections for desired inventory items
1768 std::shared_ptr<boost::container::flat_set<std::string>>
1769 invConnections =
1770 std::make_shared<boost::container::flat_set<std::string>>();
1771 invConnections->reserve(8);
1772
1773 // Loop through objects from GetSubTree
1774 for (const std::pair<
1775 std::string,
1776 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1777 object : subtree)
1778 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001779 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001780 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001781 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001782 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001783 // Store all connections to inventory item
1784 for (const std::pair<std::string, std::vector<std::string>>&
1785 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001786 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001787 const std::string& invConnection = objData.first;
1788 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001789 }
1790 }
1791 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001792
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001793 callback(invConnections);
1794 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1795 };
1796
1797 // Make call to ObjectMapper to find all inventory items
1798 crow::connections::systemBus->async_method_call(
1799 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1800 "/xyz/openbmc_project/object_mapper",
1801 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1802 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1803}
1804
1805/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001806 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001807 *
1808 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001809 * inventory items. Then finds the associations from those inventory items to
1810 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001811 *
1812 * Finds the inventory items asynchronously. Invokes callback when information
1813 * has been obtained.
1814 *
1815 * The callback must have the following signature:
1816 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001817 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001818 * @endcode
1819 *
1820 * @param sensorsAsyncResp Pointer to object holding response data.
1821 * @param sensorNames All sensors within the current chassis.
1822 * @param objectMgrPaths Mappings from connection name to DBus object path that
1823 * implements ObjectManager.
1824 * @param callback Callback to invoke when inventory items have been obtained.
1825 */
1826template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001827static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001828 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1829 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1830 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001831 objectMgrPaths,
1832 Callback&& callback)
1833{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001834 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001835
1836 // Response handler for GetManagedObjects
1837 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1838 sensorNames](const boost::system::error_code ec,
1839 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001840 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001841 if (ec)
1842 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001843 BMCWEB_LOG_ERROR
1844 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001845 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001846 return;
1847 }
1848
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001849 // Create vector to hold list of inventory items
1850 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1851 std::make_shared<std::vector<InventoryItem>>();
1852
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001853 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001854 std::string sensorAssocPath;
1855 sensorAssocPath.reserve(128); // avoid memory allocations
1856 for (const auto& objDictEntry : resp)
1857 {
1858 const std::string& objPath =
1859 static_cast<const std::string&>(objDictEntry.first);
1860 const boost::container::flat_map<
1861 std::string, boost::container::flat_map<
1862 std::string, dbus::utility::DbusVariantType>>&
1863 interfacesDict = objDictEntry.second;
1864
1865 // If path is inventory association for one of the specified sensors
1866 for (const std::string& sensorName : *sensorNames)
1867 {
1868 sensorAssocPath = sensorName;
1869 sensorAssocPath += "/inventory";
1870 if (objPath == sensorAssocPath)
1871 {
1872 // Get Association interface for object path
1873 auto assocIt =
1874 interfacesDict.find("xyz.openbmc_project.Association");
1875 if (assocIt != interfacesDict.end())
1876 {
1877 // Get inventory item from end point
1878 auto endpointsIt = assocIt->second.find("endpoints");
1879 if (endpointsIt != assocIt->second.end())
1880 {
1881 const std::vector<std::string>* endpoints =
1882 std::get_if<std::vector<std::string>>(
1883 &endpointsIt->second);
1884 if ((endpoints != nullptr) && !endpoints->empty())
1885 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001886 // Add inventory item to vector
1887 const std::string& invItemPath =
1888 endpoints->front();
1889 addInventoryItem(inventoryItems, invItemPath,
1890 sensorName);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001891 }
1892 }
1893 }
1894 break;
1895 }
1896 }
1897 }
1898
Anthony Wilsond5005492019-07-31 16:34:17 -05001899 // Now loop through the returned object paths again, this time to
1900 // find the leds associated with the inventory items we just found
1901 std::string inventoryAssocPath;
1902 inventoryAssocPath.reserve(128); // avoid memory allocations
1903 for (const auto& objDictEntry : resp)
1904 {
1905 const std::string& objPath =
1906 static_cast<const std::string&>(objDictEntry.first);
1907 const boost::container::flat_map<
1908 std::string, boost::container::flat_map<
1909 std::string, dbus::utility::DbusVariantType>>&
1910 interfacesDict = objDictEntry.second;
1911
1912 for (InventoryItem& inventoryItem : *inventoryItems)
1913 {
1914 inventoryAssocPath = inventoryItem.objectPath;
1915 inventoryAssocPath += "/leds";
1916 if (objPath == inventoryAssocPath)
1917 {
1918 // Get Association interface for object path
1919 auto assocIt =
1920 interfacesDict.find("xyz.openbmc_project.Association");
1921 if (assocIt != interfacesDict.end())
1922 {
1923 // Get inventory item from end point
1924 auto endpointsIt = assocIt->second.find("endpoints");
1925 if (endpointsIt != assocIt->second.end())
1926 {
1927 const std::vector<std::string>* endpoints =
1928 std::get_if<std::vector<std::string>>(
1929 &endpointsIt->second);
1930 if ((endpoints != nullptr) && !endpoints->empty())
1931 {
1932 // Store LED path in inventory item
1933 const std::string& ledPath = endpoints->front();
1934 inventoryItem.ledObjectPath = ledPath;
1935 }
1936 }
1937 }
1938 break;
1939 }
1940 }
1941 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001942 callback(inventoryItems);
1943 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001944 };
1945
1946 // Find DBus object path that implements ObjectManager for ObjectMapper
1947 std::string connection = "xyz.openbmc_project.ObjectMapper";
1948 auto iter = objectMgrPaths->find(connection);
1949 const std::string& objectMgrPath =
1950 (iter != objectMgrPaths->end()) ? iter->second : "/";
1951 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1952 << objectMgrPath;
1953
1954 // Call GetManagedObjects on the ObjectMapper to get all associations
1955 crow::connections::systemBus->async_method_call(
1956 std::move(respHandler), connection, objectMgrPath,
1957 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1958
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001959 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001960}
1961
1962/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001963 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1964 *
1965 * Uses the specified connections (services) to obtain D-Bus data for inventory
1966 * item leds associated with sensors. Stores the resulting data in the
1967 * inventoryItems vector.
1968 *
1969 * This data is later used to provide sensor property values in the JSON
1970 * response.
1971 *
1972 * Finds the inventory item led data asynchronously. Invokes callback when data
1973 * has been obtained.
1974 *
1975 * The callback must have the following signature:
1976 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001977 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001978 * @endcode
1979 *
1980 * This function is called recursively, obtaining data asynchronously from one
1981 * connection in each call. This ensures the callback is not invoked until the
1982 * last asynchronous function has completed.
1983 *
1984 * @param sensorsAsyncResp Pointer to object holding response data.
1985 * @param inventoryItems D-Bus inventory items associated with sensors.
1986 * @param ledConnections Connections that provide data for the inventory leds.
1987 * @param callback Callback to invoke when inventory data has been obtained.
1988 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1989 * in recursive calls to this function.
1990 */
1991template <typename Callback>
1992void getInventoryLedData(
1993 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1994 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1995 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1996 ledConnections,
1997 Callback&& callback, size_t ledConnectionsIndex = 0)
1998{
1999 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
2000
2001 // If no more connections left, call callback
2002 if (ledConnectionsIndex >= ledConnections->size())
2003 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002004 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05002005 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2006 return;
2007 }
2008
2009 // Get inventory item data from current connection
2010 auto it = ledConnections->nth(ledConnectionsIndex);
2011 if (it != ledConnections->end())
2012 {
2013 const std::string& ledPath = (*it).first;
2014 const std::string& ledConnection = (*it).second;
2015 // Response handler for Get State property
2016 auto respHandler =
2017 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
2018 callback{std::move(callback)},
2019 ledConnectionsIndex](const boost::system::error_code ec,
2020 const std::variant<std::string>& ledState) {
2021 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
2022 if (ec)
2023 {
2024 BMCWEB_LOG_ERROR
2025 << "getInventoryLedData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002026 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002027 return;
2028 }
2029
2030 const std::string* state = std::get_if<std::string>(&ledState);
2031 if (state != nullptr)
2032 {
2033 BMCWEB_LOG_DEBUG << "Led state: " << *state;
2034 // Find inventory item with this LED object path
2035 InventoryItem* inventoryItem =
2036 findInventoryItemForLed(*inventoryItems, ledPath);
2037 if (inventoryItem != nullptr)
2038 {
2039 // Store LED state in InventoryItem
2040 if (boost::ends_with(*state, "On"))
2041 {
2042 inventoryItem->ledState = LedState::ON;
2043 }
2044 else if (boost::ends_with(*state, "Blink"))
2045 {
2046 inventoryItem->ledState = LedState::BLINK;
2047 }
2048 else if (boost::ends_with(*state, "Off"))
2049 {
2050 inventoryItem->ledState = LedState::OFF;
2051 }
2052 else
2053 {
2054 inventoryItem->ledState = LedState::UNKNOWN;
2055 }
2056 }
2057 }
2058 else
2059 {
2060 BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
2061 << ledPath;
2062 }
2063
2064 // Recurse to get LED data from next connection
2065 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2066 ledConnections, std::move(callback),
2067 ledConnectionsIndex + 1);
2068
2069 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2070 };
2071
2072 // Get the State property for the current LED
2073 crow::connections::systemBus->async_method_call(
2074 std::move(respHandler), ledConnection, ledPath,
2075 "org.freedesktop.DBus.Properties", "Get",
2076 "xyz.openbmc_project.Led.Physical", "State");
2077 }
2078
2079 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2080}
2081
2082/**
2083 * @brief Gets LED data for LEDs associated with given inventory items.
2084 *
2085 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2086 * associated with the specified inventory items. Then gets the LED data from
2087 * each connection and stores it in the inventory item.
2088 *
2089 * This data is later used to provide sensor property values in the JSON
2090 * response.
2091 *
2092 * Finds the LED data asynchronously. Invokes callback when information has
2093 * been obtained.
2094 *
2095 * The callback must have the following signature:
2096 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002097 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002098 * @endcode
2099 *
2100 * @param sensorsAsyncResp Pointer to object holding response data.
2101 * @param inventoryItems D-Bus inventory items associated with sensors.
2102 * @param callback Callback to invoke when inventory items have been obtained.
2103 */
2104template <typename Callback>
2105void getInventoryLeds(
2106 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2107 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2108 Callback&& callback)
2109{
2110 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2111
2112 const std::string path = "/xyz/openbmc_project";
2113 const std::array<std::string, 1> interfaces = {
2114 "xyz.openbmc_project.Led.Physical"};
2115
2116 // Response handler for parsing output from GetSubTree
2117 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2118 inventoryItems](const boost::system::error_code ec,
2119 const GetSubTreeType& subtree) {
2120 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2121 if (ec)
2122 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002123 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002124 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2125 << ec;
2126 return;
2127 }
2128
2129 // Build map of LED object paths to connections
2130 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2131 ledConnections = std::make_shared<
2132 boost::container::flat_map<std::string, std::string>>();
2133
2134 // Loop through objects from GetSubTree
2135 for (const std::pair<
2136 std::string,
2137 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2138 object : subtree)
2139 {
2140 // Check if object path is LED for one of the specified inventory
2141 // items
2142 const std::string& ledPath = object.first;
2143 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2144 {
2145 // Add mapping from ledPath to connection
2146 const std::string& connection = object.second.begin()->first;
2147 (*ledConnections)[ledPath] = connection;
2148 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2149 << connection;
2150 }
2151 }
2152
2153 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2154 std::move(callback));
2155 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2156 };
2157 // Make call to ObjectMapper to find all inventory items
2158 crow::connections::systemBus->async_method_call(
2159 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2160 "/xyz/openbmc_project/object_mapper",
2161 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2162 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2163}
2164
2165/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002166 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2167 *
2168 * Uses the specified connections (services) (currently assumes just one) to
2169 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2170 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2171 *
2172 * This data is later used to provide sensor property values in the JSON
2173 * response.
2174 *
2175 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2176 * when data has been obtained.
2177 *
2178 * The callback must have the following signature:
2179 * @code
2180 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2181 * @endcode
2182 *
2183 * @param sensorsAsyncResp Pointer to object holding response data.
2184 * @param inventoryItems D-Bus inventory items associated with sensors.
2185 * @param psAttributesConnections Connections that provide data for the Power
2186 * Supply Attributes
2187 * @param callback Callback to invoke when data has been obtained.
2188 */
2189template <typename Callback>
2190void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002191 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002192 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2193 const boost::container::flat_map<std::string, std::string>&
2194 psAttributesConnections,
2195 Callback&& callback)
2196{
2197 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2198
2199 if (psAttributesConnections.empty())
2200 {
2201 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2202 callback(inventoryItems);
2203 return;
2204 }
2205
2206 // Assuming just one connection (service) for now
2207 auto it = psAttributesConnections.nth(0);
2208
2209 const std::string& psAttributesPath = (*it).first;
2210 const std::string& psAttributesConnection = (*it).second;
2211
2212 // Response handler for Get DeratingFactor property
2213 auto respHandler = [sensorsAsyncResp, inventoryItems,
2214 callback{std::move(callback)}](
2215 const boost::system::error_code ec,
2216 const std::variant<uint32_t>& deratingFactor) {
2217 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2218 if (ec)
2219 {
2220 BMCWEB_LOG_ERROR
2221 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002222 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002223 return;
2224 }
2225
2226 const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
2227 if (value != nullptr)
2228 {
2229 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
2230 // Store value in Power Supply Inventory Items
2231 for (InventoryItem& inventoryItem : *inventoryItems)
2232 {
2233 if (inventoryItem.isPowerSupply == true)
2234 {
2235 inventoryItem.powerSupplyEfficiencyPercent =
2236 static_cast<int>(*value);
2237 }
2238 }
2239 }
2240 else
2241 {
2242 BMCWEB_LOG_DEBUG
2243 << "Failed to find EfficiencyPercent value for PowerSupplies";
2244 }
2245
2246 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2247 callback(inventoryItems);
2248 };
2249
2250 // Get the DeratingFactor property for the PowerSupplyAttributes
2251 // Currently only property on the interface/only one we care about
2252 crow::connections::systemBus->async_method_call(
2253 std::move(respHandler), psAttributesConnection, psAttributesPath,
2254 "org.freedesktop.DBus.Properties", "Get",
2255 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
2256
2257 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2258}
2259
2260/**
2261 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2262 *
2263 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2264 * data. Then gets the Power Supply Attributes data from the connection
2265 * (currently just assumes 1 connection) and stores the data in the inventory
2266 * item.
2267 *
2268 * This data is later used to provide sensor property values in the JSON
2269 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2270 *
2271 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2272 * when information has been obtained.
2273 *
2274 * The callback must have the following signature:
2275 * @code
2276 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2277 * @endcode
2278 *
2279 * @param sensorsAsyncResp Pointer to object holding response data.
2280 * @param inventoryItems D-Bus inventory items associated with sensors.
2281 * @param callback Callback to invoke when data has been obtained.
2282 */
2283template <typename Callback>
2284void getPowerSupplyAttributes(
2285 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2286 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2287 Callback&& callback)
2288{
2289 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2290
2291 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002292 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002293 {
2294 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2295 callback(inventoryItems);
2296 return;
2297 }
2298
2299 const std::array<std::string, 1> interfaces = {
2300 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2301
2302 // Response handler for parsing output from GetSubTree
2303 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2304 inventoryItems](const boost::system::error_code ec,
2305 const GetSubTreeType& subtree) {
2306 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2307 if (ec)
2308 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002309 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002310 BMCWEB_LOG_ERROR
2311 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2312 return;
2313 }
2314 if (subtree.size() == 0)
2315 {
2316 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2317 callback(inventoryItems);
2318 return;
2319 }
2320
2321 // Currently we only support 1 power supply attribute, use this for
2322 // all the power supplies. Build map of object path to connection.
2323 // Assume just 1 connection and 1 path for now.
2324 boost::container::flat_map<std::string, std::string>
2325 psAttributesConnections;
2326
2327 if (subtree[0].first.empty() || subtree[0].second.empty())
2328 {
2329 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2330 callback(inventoryItems);
2331 return;
2332 }
2333
2334 const std::string& psAttributesPath = subtree[0].first;
2335 const std::string& connection = subtree[0].second.begin()->first;
2336
2337 if (connection.empty())
2338 {
2339 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2340 callback(inventoryItems);
2341 return;
2342 }
2343
2344 psAttributesConnections[psAttributesPath] = connection;
2345 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2346 << connection;
2347
2348 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2349 psAttributesConnections,
2350 std::move(callback));
2351 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2352 };
2353 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2354 crow::connections::systemBus->async_method_call(
2355 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2356 "/xyz/openbmc_project/object_mapper",
2357 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2358 "/xyz/openbmc_project", 0, interfaces);
2359 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2360}
2361
2362/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002363 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002364 *
2365 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002366 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002367 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002368 * This data is later used to provide sensor property values in the JSON
2369 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002370 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002371 * Finds the inventory items asynchronously. Invokes callback when the
2372 * inventory items have been obtained.
2373 *
2374 * The callback must have the following signature:
2375 * @code
2376 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2377 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002378 *
2379 * @param sensorsAsyncResp Pointer to object holding response data.
2380 * @param sensorNames All sensors within the current chassis.
2381 * @param objectMgrPaths Mappings from connection name to DBus object path that
2382 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002383 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002384 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002385template <typename Callback>
2386static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002387 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2388 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2389 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002390 objectMgrPaths,
2391 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002392{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002393 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2394 auto getInventoryItemAssociationsCb =
2395 [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2396 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2397 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002398 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002399 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2400 callback{std::move(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002401 std::shared_ptr<boost::container::flat_set<std::string>>
2402 invConnections) {
2403 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002404 auto getInventoryItemsDataCb =
2405 [sensorsAsyncResp, inventoryItems,
2406 callback{std::move(callback)}]() {
2407 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002408
2409 auto getInventoryLedsCb = [sensorsAsyncResp,
2410 inventoryItems,
2411 callback{std::move(
2412 callback)}]() {
2413 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2414 // Find Power Supply Attributes and get the data
2415 getPowerSupplyAttributes(sensorsAsyncResp,
2416 inventoryItems,
2417 std::move(callback));
2418 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2419 };
2420
Anthony Wilsond5005492019-07-31 16:34:17 -05002421 // Find led connections and get the data
2422 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002423 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002424 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2425 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002426
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002427 // Get inventory item data from connections
2428 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2429 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002430 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002431 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2432 };
2433
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002434 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002435 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002436 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002437 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002438 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002439 };
2440
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002441 // Get associations from sensors to inventory items
2442 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2443 std::move(getInventoryItemAssociationsCb));
2444 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2445}
2446
2447/**
2448 * @brief Returns JSON PowerSupply object for the specified inventory item.
2449 *
2450 * Searches for a JSON PowerSupply object that matches the specified inventory
2451 * item. If one is not found, a new PowerSupply object is added to the JSON
2452 * array.
2453 *
2454 * Multiple sensors are often associated with one power supply inventory item.
2455 * As a result, multiple sensor values are stored in one JSON PowerSupply
2456 * object.
2457 *
2458 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2459 * @param inventoryItem Inventory item for the power supply.
2460 * @param chassisId Chassis that contains the power supply.
2461 * @return JSON PowerSupply object for the specified inventory item.
2462 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002463inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002464 const InventoryItem& inventoryItem,
2465 const std::string& chassisId)
2466{
2467 // Check if matching PowerSupply object already exists in JSON array
2468 for (nlohmann::json& powerSupply : powerSupplyArray)
2469 {
2470 if (powerSupply["MemberId"] == inventoryItem.name)
2471 {
2472 return powerSupply;
2473 }
2474 }
2475
2476 // Add new PowerSupply object to JSON array
2477 powerSupplyArray.push_back({});
2478 nlohmann::json& powerSupply = powerSupplyArray.back();
2479 powerSupply["@odata.id"] =
2480 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2481 powerSupply["MemberId"] = inventoryItem.name;
2482 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2483 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2484 powerSupply["Model"] = inventoryItem.model;
2485 powerSupply["PartNumber"] = inventoryItem.partNumber;
2486 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002487 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002488
Gunnar Mills42cbe532019-08-15 15:26:54 -05002489 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2490 {
2491 powerSupply["EfficiencyPercent"] =
2492 inventoryItem.powerSupplyEfficiencyPercent;
2493 }
2494
2495 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002496 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2497 powerSupply["Status"]["Health"] = health;
2498
2499 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002500}
2501
2502/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002503 * @brief Gets the values of the specified sensors.
2504 *
2505 * Stores the results as JSON in the SensorsAsyncResp.
2506 *
2507 * Gets the sensor values asynchronously. Stores the results later when the
2508 * information has been obtained.
2509 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002510 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002511 *
2512 * To minimize the number of DBus calls, the DBus method
2513 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2514 * values of all sensors provided by a connection (service).
2515 *
2516 * The connections set contains all the connections that provide sensor values.
2517 *
2518 * The objectMgrPaths map contains mappings from a connection name to the
2519 * corresponding DBus object path that implements ObjectManager.
2520 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002521 * The InventoryItem vector contains D-Bus inventory items associated with the
2522 * sensors. Inventory item data is needed for some Redfish sensor properties.
2523 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002524 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002525 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002526 * @param connections Connections that provide sensor values.
2527 * @param objectMgrPaths Mappings from connection name to DBus object path that
2528 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002529 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002530 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002531inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002532 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002533 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002534 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002535 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002536 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002537 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002538{
2539 BMCWEB_LOG_DEBUG << "getSensorData enter";
2540 // Get managed objects from all services exposing sensors
2541 for (const std::string& connection : connections)
2542 {
2543 // Response handler to process managed objects
Ed Tanous81ce6092020-12-17 16:54:55 +00002544 auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002545 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002546 const boost::system::error_code ec,
2547 ManagedObjectsVectorType& resp) {
2548 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2549 if (ec)
2550 {
2551 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002552 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002553 return;
2554 }
2555 // Go through all objects and update response with sensor data
2556 for (const auto& objDictEntry : resp)
2557 {
2558 const std::string& objPath =
2559 static_cast<const std::string&>(objDictEntry.first);
2560 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2561 << objPath;
2562
Shawn McCarneyde629b62019-03-08 10:42:51 -06002563 std::vector<std::string> split;
2564 // Reserve space for
2565 // /xyz/openbmc_project/sensors/<name>/<subname>
2566 split.reserve(6);
2567 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2568 if (split.size() < 6)
2569 {
2570 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2571 << objPath;
2572 continue;
2573 }
2574 // These indexes aren't intuitive, as boost::split puts an empty
2575 // string at the beginning
2576 const std::string& sensorType = split[4];
2577 const std::string& sensorName = split[5];
2578 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2579 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002580 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002581 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002582 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002583 continue;
2584 }
2585
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002586 // Find inventory item (if any) associated with sensor
2587 InventoryItem* inventoryItem =
2588 findInventoryItemForSensor(inventoryItems, objPath);
2589
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002590 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002591 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002592
2593 nlohmann::json* sensorJson = nullptr;
2594
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002595 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002596 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002597 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002598 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2599 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002600 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002601 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002602 }
2603 else
2604 {
Ed Tanous271584a2019-07-09 16:24:22 -07002605 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002606 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002607 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002608 fieldName = "Temperatures";
2609 }
2610 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2611 sensorType == "fan_pwm")
2612 {
2613 fieldName = "Fans";
2614 }
2615 else if (sensorType == "voltage")
2616 {
2617 fieldName = "Voltages";
2618 }
2619 else if (sensorType == "power")
2620 {
2621 if (!sensorName.compare("total_power"))
2622 {
2623 fieldName = "PowerControl";
2624 }
2625 else if ((inventoryItem != nullptr) &&
2626 (inventoryItem->isPowerSupply))
2627 {
2628 fieldName = "PowerSupplies";
2629 }
2630 else
2631 {
2632 // Other power sensors are in SensorCollection
2633 continue;
2634 }
2635 }
2636 else
2637 {
2638 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2639 << sensorType;
2640 continue;
2641 }
2642
2643 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002644 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002645 if (fieldName == "PowerControl")
2646 {
2647 if (tempArray.empty())
2648 {
2649 // Put multiple "sensors" into a single
2650 // PowerControl. Follows MemberId naming and
2651 // naming in power.hpp.
2652 tempArray.push_back(
2653 {{"@odata.id",
2654 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002655 sensorsAsyncResp->chassisId + "/" +
2656 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002657 fieldName + "/0"}});
2658 }
2659 sensorJson = &(tempArray.back());
2660 }
2661 else if (fieldName == "PowerSupplies")
2662 {
2663 if (inventoryItem != nullptr)
2664 {
2665 sensorJson =
2666 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002667 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002668 }
2669 }
2670 else
2671 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002672 tempArray.push_back(
2673 {{"@odata.id",
2674 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002675 sensorsAsyncResp->chassisId + "/" +
2676 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002677 fieldName + "/"}});
2678 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002679 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002680 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002681
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002682 if (sensorJson != nullptr)
2683 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002684 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002685 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002686 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002687 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002688 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002689 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002690 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002691 sortJSONResponse(sensorsAsyncResp);
2692 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002693 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002694 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002695 }
James Feist8bd25cc2019-03-15 15:14:00 -07002696 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002697 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2698 };
2699
2700 // Find DBus object path that implements ObjectManager for the current
2701 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002702 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002703 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002704 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002705 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2706 << objectMgrPath;
2707
2708 crow::connections::systemBus->async_method_call(
2709 getManagedObjectsCb, connection, objectMgrPath,
2710 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002711 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002712 BMCWEB_LOG_DEBUG << "getSensorData exit";
2713}
2714
Ed Tanous23a21a12020-07-25 04:45:05 +00002715inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002716 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002717 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002718{
2719 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002720 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002721 const boost::container::flat_set<std::string>& connections) {
2722 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2723 auto getObjectManagerPathsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002724 [sensorsAsyncResp, sensorNames,
Ed Tanousb5a76932020-09-29 16:16:58 -07002725 connections](const std::shared_ptr<boost::container::flat_map<
2726 std::string, std::string>>& objectMgrPaths) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002727 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2728 auto getInventoryItemsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002729 [sensorsAsyncResp, sensorNames, connections,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002730 objectMgrPaths](
Ed Tanousf23b7292020-10-15 09:41:17 -07002731 const std::shared_ptr<std::vector<InventoryItem>>&
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002732 inventoryItems) {
2733 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2734 // Get sensor data and store results in JSON
Ed Tanous81ce6092020-12-17 16:54:55 +00002735 getSensorData(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002736 connections, objectMgrPaths,
Ed Tanousf23b7292020-10-15 09:41:17 -07002737 inventoryItems);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002738 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2739 };
2740
2741 // Get inventory items associated with sensors
Ed Tanous81ce6092020-12-17 16:54:55 +00002742 getInventoryItems(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002743 objectMgrPaths,
2744 std::move(getInventoryItemsCb));
2745
2746 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2747 };
2748
2749 // Get mapping from connection names to the DBus object
2750 // paths that implement the ObjectManager interface
Ed Tanous81ce6092020-12-17 16:54:55 +00002751 getObjectManagerPaths(sensorsAsyncResp,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002752 std::move(getObjectManagerPathsCb));
2753 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2754 };
2755
2756 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002757 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002758}
2759
Shawn McCarneyde629b62019-03-08 10:42:51 -06002760/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002761 * @brief Entry point for retrieving sensors data related to requested
2762 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002763 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002764 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002765inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002766 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002767{
2768 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002769 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002770 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002771 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002772 sensorNames) {
2773 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Ed Tanous81ce6092020-12-17 16:54:55 +00002774 processSensorList(sensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002775 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2776 };
zhanghch058d1b46d2021-04-01 11:18:24 +08002777 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2778 nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002779
Shawn McCarney26f03892019-05-03 13:20:24 -05002780 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002781 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002782 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002783}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002784
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302785/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002786 * @brief Find the requested sensorName in the list of all sensors supplied by
2787 * the chassis node
2788 *
2789 * @param sensorName The sensor name supplied in the PATCH request
2790 * @param sensorsList The list of sensors managed by the chassis node
2791 * @param sensorsModified The list of sensors that were found as a result of
2792 * repeated calls to this function
2793 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002794inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302795 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002796 boost::container::flat_set<std::string>& sensorsList,
2797 boost::container::flat_set<std::string>& sensorsModified)
2798{
George Liu28aa8de2021-02-01 15:13:30 +08002799 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002800 {
George Liu28aa8de2021-02-01 15:13:30 +08002801 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002802 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002803 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002804 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002805 continue;
2806 }
2807 if (thisSensorName == sensorName)
2808 {
2809 sensorsModified.emplace(chassisSensor);
2810 return true;
2811 }
2812 }
2813 return false;
2814}
2815
2816/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302817 * @brief Entry point for overriding sensor values of given sensor
2818 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002819 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002820 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002821 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302822 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002823inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002824 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002825 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002826 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302827{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002828 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002829 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302830
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302831 const char* propertyValueName;
2832 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302833 std::string memberId;
2834 double value;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302835 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302836 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302837 if (collectionItems.first == "Temperatures")
2838 {
2839 propertyValueName = "ReadingCelsius";
2840 }
2841 else if (collectionItems.first == "Fans")
2842 {
2843 propertyValueName = "Reading";
2844 }
2845 else
2846 {
2847 propertyValueName = "ReadingVolts";
2848 }
2849 for (auto& item : collectionItems.second)
2850 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002851 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2852 "MemberId", memberId, propertyValueName,
2853 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302854 {
2855 return;
2856 }
2857 overrideMap.emplace(memberId,
2858 std::make_pair(value, collectionItems.first));
2859 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302860 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002861
Ed Tanousb5a76932020-09-29 16:16:58 -07002862 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2863 const std::shared_ptr<
2864 boost::container::flat_set<
2865 std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002866 // Match sensor names in the PATCH request to those managed by the
2867 // chassis node
2868 const std::shared_ptr<boost::container::flat_set<std::string>>
2869 sensorNames =
2870 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302871 for (const auto& item : overrideMap)
2872 {
2873 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002874 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2875 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302876 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302877 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002878 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302879 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302880 return;
2881 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302882 }
2883 // Get the connection to which the memberId belongs
2884 auto getObjectsWithConnectionCb =
2885 [sensorAsyncResp, overrideMap](
Ed Tanouscb13a392020-07-25 19:02:03 +00002886 const boost::container::flat_set<std::string>& /*connections*/,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302887 const std::set<std::pair<std::string, std::string>>&
2888 objectsWithConnection) {
2889 if (objectsWithConnection.size() != overrideMap.size())
2890 {
2891 BMCWEB_LOG_INFO
2892 << "Unable to find all objects with proper connection "
2893 << objectsWithConnection.size() << " requested "
2894 << overrideMap.size() << "\n";
2895 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +08002896 sensorAsyncResp->asyncResp->res,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002897 sensorAsyncResp->chassisSubNode ==
2898 sensors::node::thermal
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302899 ? "Temperatures"
2900 : "Voltages",
2901 "Count");
2902 return;
2903 }
2904 for (const auto& item : objectsWithConnection)
2905 {
George Liu28aa8de2021-02-01 15:13:30 +08002906 sdbusplus::message::object_path path(item.first);
2907 std::string sensorName = path.filename();
2908 if (sensorName.empty())
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302909 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002910 messages::internalError(
2911 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302912 return;
2913 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302914
2915 const auto& iterator = overrideMap.find(sensorName);
2916 if (iterator == overrideMap.end())
2917 {
2918 BMCWEB_LOG_INFO << "Unable to find sensor object"
2919 << item.first << "\n";
zhanghch058d1b46d2021-04-01 11:18:24 +08002920 messages::internalError(
2921 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302922 return;
2923 }
2924 crow::connections::systemBus->async_method_call(
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302925 [sensorAsyncResp](const boost::system::error_code ec) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302926 if (ec)
2927 {
2928 BMCWEB_LOG_DEBUG
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302929 << "setOverrideValueStatus DBUS error: "
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302930 << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002931 messages::internalError(
2932 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302933 return;
2934 }
2935 },
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302936 item.second, item.first,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302937 "org.freedesktop.DBus.Properties", "Set",
2938 "xyz.openbmc_project.Sensor.Value", "Value",
Patrick Williams19bd78d2020-05-13 17:38:24 -05002939 std::variant<double>(iterator->second.first));
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302940 }
2941 };
2942 // Get object with connection for the given sensor name
2943 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2944 std::move(getObjectsWithConnectionCb));
2945 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302946 // get full sensor list for the given chassisId and cross verify the sensor.
2947 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2948}
2949
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002950/**
2951 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2952 * path of the sensor.
2953 *
2954 * Function builds valid Redfish response for sensor query of given chassis and
2955 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2956 * it to caller in a callback.
2957 *
2958 * @param chassis Chassis for which retrieval should be performed
2959 * @param node Node (group) of sensors. See sensors::node for supported values
2960 * @param mapComplete Callback to be called with retrieval result
2961 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002962inline void retrieveUriToDbusMap(const std::string& chassis,
2963 const std::string& node,
2964 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002965{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +00002966 auto pathIt = sensors::dbus::paths.find(node);
2967 if (pathIt == sensors::dbus::paths.end())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002968 {
2969 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2970 mapComplete(boost::beast::http::status::bad_request, {});
2971 return;
2972 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002973
Gunnar Mills9062d472021-11-16 11:37:47 -06002974 auto res = std::make_shared<crow::Response>();
2975 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(*res);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002976 auto callback =
Gunnar Mills9062d472021-11-16 11:37:47 -06002977 [res, asyncResp, mapCompleteCb{std::move(mapComplete)}](
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002978 const boost::beast::http::status status,
2979 const boost::container::flat_map<std::string, std::string>&
2980 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2981
2982 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002983 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002984 getChassisData(resp);
2985}
2986
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002987inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002988{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002989 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002990 .privileges(redfish::privileges::getSensorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002991 .methods(
2992 boost::beast::http::verb::get)([](const crow::Request&,
2993 const std::shared_ptr<
2994 bmcweb::AsyncResp>& aResp,
2995 const std::string& chassisId) {
2996 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002997
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002998 std::shared_ptr<SensorsAsyncResp> asyncResp =
2999 std::make_shared<SensorsAsyncResp>(
3000 aResp, chassisId,
3001 sensors::dbus::paths.at(sensors::node::sensors),
3002 sensors::node::sensors);
zhanghch058d1b46d2021-04-01 11:18:24 +08003003
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003004 auto getChassisCb =
3005 [asyncResp](
3006 const std::shared_ptr<
3007 boost::container::flat_set<std::string>>& sensorNames) {
3008 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003009
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003010 nlohmann::json& entriesArray =
3011 asyncResp->asyncResp->res.jsonValue["Members"];
3012 for (auto& sensor : *sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003013 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003014 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003015
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003016 sdbusplus::message::object_path path(sensor);
3017 std::string sensorName = path.filename();
3018 if (sensorName.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003019 {
3020 BMCWEB_LOG_ERROR << "Invalid sensor path: "
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003021 << sensor;
3022 messages::internalError(asyncResp->asyncResp->res);
3023 return;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003024 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003025 entriesArray.push_back(
3026 {{"@odata.id", "/redfish/v1/Chassis/" +
3027 asyncResp->chassisId + "/" +
3028 asyncResp->chassisSubNode + "/" +
3029 sensorName}});
3030 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003031
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003032 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
3033 entriesArray.size();
3034 BMCWEB_LOG_DEBUG << "getChassisCb exit";
3035 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003036
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003037 // Get set of sensors in chassis
3038 getChassis(asyncResp, std::move(getChassisCb));
3039 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
3040 });
3041}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003042
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003043inline void requestRoutesSensor(App& app)
3044{
3045 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003046 .privileges(redfish::privileges::getSensor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003047 .methods(
3048 boost::beast::http::verb::get)([](const crow::Request&,
3049 const std::shared_ptr<
3050 bmcweb::AsyncResp>& aResp,
3051 const std::string& chassisId,
3052 const std::string& sensorName) {
3053 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3054 std::shared_ptr<SensorsAsyncResp> asyncResp =
3055 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3056 std::vector<const char*>(),
3057 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003058
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003059 const std::array<const char*, 1> interfaces = {
3060 "xyz.openbmc_project.Sensor.Value"};
3061
3062 // Get a list of all of the sensors that implement Sensor.Value
3063 // and get the path and service name associated with the sensor
3064 crow::connections::systemBus->async_method_call(
3065 [asyncResp, sensorName](const boost::system::error_code ec,
3066 const GetSubTreeType& subtree) {
3067 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3068 if (ec)
3069 {
3070 messages::internalError(asyncResp->asyncResp->res);
3071 BMCWEB_LOG_ERROR
3072 << "Sensor getSensorPaths resp_handler: "
3073 << "Dbus error " << ec;
3074 return;
3075 }
3076
3077 GetSubTreeType::const_iterator it = std::find_if(
3078 subtree.begin(), subtree.end(),
3079 [sensorName](
3080 const std::pair<
3081 std::string,
3082 std::vector<std::pair<
3083 std::string, std::vector<std::string>>>>&
3084 object) {
3085 sdbusplus::message::object_path path(object.first);
3086 std::string name = path.filename();
3087 if (name.empty())
3088 {
3089 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3090 << object.first;
3091 return false;
3092 }
3093
3094 return name == sensorName;
3095 });
3096
3097 if (it == subtree.end())
3098 {
3099 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3100 << sensorName;
3101 messages::resourceNotFound(asyncResp->asyncResp->res,
3102 "Sensor", sensorName);
3103 return;
3104 }
3105 std::string_view sensorPath = (*it).first;
3106 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3107 << sensorName << "': " << sensorPath;
3108
3109 const std::shared_ptr<
3110 boost::container::flat_set<std::string>>
3111 sensorList = std::make_shared<
3112 boost::container::flat_set<std::string>>();
3113
3114 sensorList->emplace(sensorPath);
3115 processSensorList(asyncResp, sensorList);
3116 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3117 },
3118 "xyz.openbmc_project.ObjectMapper",
3119 "/xyz/openbmc_project/object_mapper",
3120 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3121 "/xyz/openbmc_project/sensors", 2, interfaces);
3122 });
3123}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003124
Ed Tanous1abe55e2018-09-05 08:30:59 -07003125} // namespace redfish