blob: 5c86579c48ee255b756216181f9ff730758b4df6 [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 Tanous168e20c2021-12-13 14:39:53 -080024#include <dbus_utility.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070025#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070026#include <sdbusplus/asio/property.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053027#include <utils/json_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050028
29#include <cmath>
Ed Tanousb5a76932020-09-29 16:16:58 -070030#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080031#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010032
Ed Tanous1abe55e2018-09-05 08:30:59 -070033namespace redfish
34{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010035
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010036using GetSubTreeType = std::vector<
37 std::pair<std::string,
38 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
39
40using ManagedObjectsVectorType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 sdbusplus::message::object_path,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010042 boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -080043 std::string, boost::container::flat_map<
44 std::string, dbus::utility::DbusVariantType>>>>;
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";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700596 sdbusplus::asio::getProperty<std::vector<std::string>>(
597 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
598 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700599 [sensorsAsyncResp, callback{std::move(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700600 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700601 const std::vector<std::string>& nodeSensorList) {
Ed Tanous271584a2019-07-09 16:24:22 -0700602 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700603 {
Ed Tanous271584a2019-07-09 16:24:22 -0700604 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700605 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800606 messages::internalError(
607 sensorsAsyncResp->asyncResp->res);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700608 return;
609 }
610 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700611 const std::shared_ptr<boost::container::flat_set<std::string>>
612 culledSensorList = std::make_shared<
613 boost::container::flat_set<std::string>>();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700614 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700615 culledSensorList);
616 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700617 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100618 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100619
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700620 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700622 respHandler, "xyz.openbmc_project.ObjectMapper",
623 "/xyz/openbmc_project/object_mapper",
624 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700625 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700626 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100627}
628
629/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600630 * @brief Finds all DBus object paths that implement ObjectManager.
631 *
632 * Creates a mapping from the associated connection name to the object path.
633 *
634 * Finds the object paths asynchronously. Invokes callback when information has
635 * been obtained.
636 *
637 * The callback must have the following signature:
638 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500639 * callback(std::shared_ptr<boost::container::flat_map<std::string,
640 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600641 * @endcode
642 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700643 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600644 * @param callback Callback to invoke when object paths obtained.
645 */
646template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700647void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000648 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700649 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600650{
651 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
652 const std::array<std::string, 1> interfaces = {
653 "org.freedesktop.DBus.ObjectManager"};
654
655 // Response handler for GetSubTree DBus method
656 auto respHandler = [callback{std::move(callback)},
Ed Tanous81ce6092020-12-17 16:54:55 +0000657 sensorsAsyncResp](const boost::system::error_code ec,
Shawn McCarneyde629b62019-03-08 10:42:51 -0600658 const GetSubTreeType& subtree) {
659 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
660 if (ec)
661 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800662 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600663 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
664 << ec;
665 return;
666 }
667
668 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500669 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
670 objectMgrPaths = std::make_shared<
671 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600672 for (const std::pair<
673 std::string,
674 std::vector<std::pair<std::string, std::vector<std::string>>>>&
675 object : subtree)
676 {
677 // Loop over connections for current object path
678 const std::string& objectPath = object.first;
679 for (const std::pair<std::string, std::vector<std::string>>&
680 objData : object.second)
681 {
682 // Add mapping from connection to object path
683 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500684 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600685 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
686 << objectPath;
687 }
688 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500689 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600690 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
691 };
692
693 // Query mapper for all DBus object paths that implement ObjectManager
694 crow::connections::systemBus->async_method_call(
695 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
696 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700697 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600698 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
699}
700
701/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500702 * @brief Returns the Redfish State value for the specified inventory item.
703 * @param inventoryItem D-Bus inventory item associated with a sensor.
704 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700705 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000706inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500707{
708 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
709 {
710 return "Absent";
711 }
James Feist34dd1792019-05-17 14:10:54 -0700712
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500713 return "Enabled";
714}
715
716/**
717 * @brief Returns the Redfish Health value for the specified sensor.
718 * @param sensorJson Sensor JSON object.
719 * @param interfacesDict Map of all sensor interfaces.
720 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
721 * be nullptr if no associated inventory item was found.
722 * @return Health value for sensor.
723 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000724inline std::string getHealth(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500725 nlohmann::json& sensorJson,
James Feist34dd1792019-05-17 14:10:54 -0700726 const boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -0800727 std::string, boost::container::flat_map<
728 std::string, dbus::utility::DbusVariantType>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500729 interfacesDict,
730 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700731{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500732 // Get current health value (if any) in the sensor JSON object. Some JSON
733 // objects contain multiple sensors (such as PowerSupplies). We want to set
734 // the overall health to be the most severe of any of the sensors.
735 std::string currentHealth;
736 auto statusIt = sensorJson.find("Status");
737 if (statusIt != sensorJson.end())
738 {
739 auto healthIt = statusIt->find("Health");
740 if (healthIt != statusIt->end())
741 {
742 std::string* health = healthIt->get_ptr<std::string*>();
743 if (health != nullptr)
744 {
745 currentHealth = *health;
746 }
747 }
748 }
749
750 // If current health in JSON object is already Critical, return that. This
751 // should override the sensor health, which might be less severe.
752 if (currentHealth == "Critical")
753 {
754 return "Critical";
755 }
756
757 // Check if sensor has critical threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700758 auto criticalThresholdIt =
759 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
760 if (criticalThresholdIt != interfacesDict.end())
761 {
762 auto thresholdHighIt =
763 criticalThresholdIt->second.find("CriticalAlarmHigh");
764 auto thresholdLowIt =
765 criticalThresholdIt->second.find("CriticalAlarmLow");
766 if (thresholdHighIt != criticalThresholdIt->second.end())
767 {
768 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
769 if (asserted == nullptr)
770 {
771 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
772 }
773 else if (*asserted)
774 {
775 return "Critical";
776 }
777 }
778 if (thresholdLowIt != criticalThresholdIt->second.end())
779 {
780 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
781 if (asserted == nullptr)
782 {
783 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
784 }
785 else if (*asserted)
786 {
787 return "Critical";
788 }
789 }
790 }
791
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500792 // Check if associated inventory item is not functional
793 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
794 {
795 return "Critical";
796 }
797
798 // If current health in JSON object is already Warning, return that. This
799 // should override the sensor status, which might be less severe.
800 if (currentHealth == "Warning")
801 {
802 return "Warning";
803 }
804
805 // Check if sensor has warning threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700806 auto warningThresholdIt =
807 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
808 if (warningThresholdIt != interfacesDict.end())
809 {
810 auto thresholdHighIt =
811 warningThresholdIt->second.find("WarningAlarmHigh");
812 auto thresholdLowIt =
813 warningThresholdIt->second.find("WarningAlarmLow");
814 if (thresholdHighIt != warningThresholdIt->second.end())
815 {
816 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
817 if (asserted == nullptr)
818 {
819 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
820 }
821 else if (*asserted)
822 {
823 return "Warning";
824 }
825 }
826 if (thresholdLowIt != warningThresholdIt->second.end())
827 {
828 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
829 if (asserted == nullptr)
830 {
831 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
832 }
833 else if (*asserted)
834 {
835 return "Warning";
836 }
837 }
838 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500839
James Feist34dd1792019-05-17 14:10:54 -0700840 return "OK";
841}
842
Ed Tanous23a21a12020-07-25 04:45:05 +0000843inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500844 const InventoryItem* inventoryItem)
845{
846 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
847 {
848 switch (inventoryItem->ledState)
849 {
850 case LedState::OFF:
851 sensorJson["IndicatorLED"] = "Off";
852 break;
853 case LedState::ON:
854 sensorJson["IndicatorLED"] = "Lit";
855 break;
856 case LedState::BLINK:
857 sensorJson["IndicatorLED"] = "Blinking";
858 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000859 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500860 break;
861 }
862 }
863}
864
James Feist34dd1792019-05-17 14:10:54 -0700865/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100866 * @brief Builds a json sensor representation of a sensor.
867 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500868 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100869 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200870 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100871 * @param interfacesDict A dictionary of the interfaces and properties of said
872 * interfaces to be built from
873 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500874 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
875 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100876 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000877inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100878 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700879 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100880 const boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -0800881 std::string, boost::container::flat_map<
882 std::string, dbus::utility::DbusVariantType>>&
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100883 interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000884 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700885{
886 // We need a value interface before we can do anything with it
887 auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
888 if (valueIt == interfacesDict.end())
889 {
890 BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
891 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100892 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100893
Ed Tanous1abe55e2018-09-05 08:30:59 -0700894 // Assume values exist as is (10^0 == 1) if no scale exists
895 int64_t scaleMultiplier = 0;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100896
Ed Tanous1abe55e2018-09-05 08:30:59 -0700897 auto scaleIt = valueIt->second.find("Scale");
898 // If a scale exists, pull value as int64, and use the scaling.
899 if (scaleIt != valueIt->second.end())
900 {
Ed Tanousabf2add2019-01-22 16:40:12 -0800901 const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700902 if (int64Value != nullptr)
903 {
904 scaleMultiplier = *int64Value;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100905 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100906 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200908 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500909 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500910 // For sensors in SensorCollection we set Id instead of MemberId,
911 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000912 sensorJson["Id"] = sensorName;
913 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500914 }
915 else if (sensorType != "power")
916 {
917 // Set MemberId and Name for non-power sensors. For PowerSupplies and
918 // PowerControl, those properties have more general values because
919 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000920 sensorJson["MemberId"] = sensorName;
921 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500922 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700923
Ed Tanous81ce6092020-12-17 16:54:55 +0000924 sensorJson["Status"]["State"] = getState(inventoryItem);
925 sensorJson["Status"]["Health"] =
926 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700927
928 // Parameter to set to override the type we get from dbus, and force it to
929 // int, regardless of what is available. This is used for schemas like fan,
930 // that require integers, not floats.
931 bool forceToInt = false;
932
Anthony Wilson3929aca2019-07-19 15:42:33 -0500933 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200934 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500935 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000936 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000937
938 const std::string& readingType = sensors::toReadingType(sensorType);
939 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500940 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000941 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
942 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500943 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000944 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500945 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000946 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500947 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000948
949 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
950 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200951 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000952 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
953 << sensorType;
954 }
955 else
956 {
957 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200958 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500959 }
960 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500962 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000963 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700964 // TODO(ed) Documentation says that path should be type fan_tach,
965 // implementation seems to implement fan
966 }
967 else if (sensorType == "fan" || sensorType == "fan_tach")
968 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500969 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000970 sensorJson["ReadingUnits"] = "RPM";
971 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
972 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973 forceToInt = true;
974 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700975 else if (sensorType == "fan_pwm")
976 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500977 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000978 sensorJson["ReadingUnits"] = "Percent";
979 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
980 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700981 forceToInt = true;
982 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983 else if (sensorType == "voltage")
984 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500985 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000986 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700988 else if (sensorType == "power")
989 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700990 std::string sensorNameLower =
991 boost::algorithm::to_lower_copy(sensorName);
992
Eddie James028f7eb2019-05-17 21:24:36 +0000993 if (!sensorName.compare("total_power"))
994 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000995 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500996 // Put multiple "sensors" into a single PowerControl, so have
997 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000998 sensorJson["MemberId"] = "0";
999 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -05001000 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +00001001 }
1002 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001003 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001004 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001005 }
1006 else
1007 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001008 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001009 }
Ed Tanous2474adf2018-09-05 16:31:16 -07001010 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 else
1012 {
1013 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
1014 return;
1015 }
1016 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -05001017 std::vector<
1018 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
1019 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001020 properties.reserve(7);
1021
1022 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001023
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001024 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -05001025 {
1026 properties.emplace_back(
1027 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
1028 "/Thresholds/UpperCaution/Reading"_json_pointer);
1029 properties.emplace_back(
1030 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1031 "/Thresholds/LowerCaution/Reading"_json_pointer);
1032 properties.emplace_back(
1033 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1034 "/Thresholds/UpperCritical/Reading"_json_pointer);
1035 properties.emplace_back(
1036 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1037 "/Thresholds/LowerCritical/Reading"_json_pointer);
1038 }
1039 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001040 {
1041 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001042 "WarningHigh",
1043 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001044 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001045 "WarningLow",
1046 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001047 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001048 "CriticalHigh",
1049 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001050 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001051 "CriticalLow",
1052 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001053 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001054
Ed Tanous2474adf2018-09-05 16:31:16 -07001055 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1056
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001057 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001058 {
1059 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001060 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001061 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001062 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001063 }
1064 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065 {
1066 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001067 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001068 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001069 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001070 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001071 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001072 {
1073 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001074 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001075 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001076 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077 }
1078
Anthony Wilson3929aca2019-07-19 15:42:33 -05001079 for (const std::tuple<const char*, const char*,
1080 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001081 {
1082 auto interfaceProperties = interfacesDict.find(std::get<0>(p));
1083 if (interfaceProperties != interfacesDict.end())
1084 {
Ed Tanous271584a2019-07-09 16:24:22 -07001085 auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
1086 if (thisValueIt != interfaceProperties->second.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001087 {
Ed Tanous168e20c2021-12-13 14:39:53 -08001088 const dbus::utility::DbusVariantType& valueVariant =
1089 thisValueIt->second;
Anthony Wilson3929aca2019-07-19 15:42:33 -05001090
1091 // The property we want to set may be nested json, so use
1092 // a json_pointer for easy indexing into the json structure.
1093 const nlohmann::json::json_pointer& key = std::get<2>(p);
1094
Ed Tanous1abe55e2018-09-05 08:30:59 -07001095 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001096 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097
Ed Tanousabf2add2019-01-22 16:40:12 -08001098 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001099 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001100 double temp = 0.0;
1101 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001102 {
Ed Tanous271584a2019-07-09 16:24:22 -07001103 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001104 }
1105 else if (doubleValue != nullptr)
1106 {
1107 temp = *doubleValue;
1108 }
Eddie James028f7eb2019-05-17 21:24:36 +00001109 else if (uValue != nullptr)
1110 {
1111 temp = *uValue;
1112 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001113 else
1114 {
1115 BMCWEB_LOG_ERROR
1116 << "Got value interface that wasn't int or double";
1117 continue;
1118 }
1119 temp = temp * std::pow(10, scaleMultiplier);
1120 if (forceToInt)
1121 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001122 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001123 }
1124 else
1125 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001126 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001127 }
1128 }
1129 }
1130 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001131
Ed Tanous81ce6092020-12-17 16:54:55 +00001132 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001133 "/xyz/openbmc_project/sensors/" + sensorType +
1134 "/" + sensorName);
1135
Ed Tanous1abe55e2018-09-05 08:30:59 -07001136 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001137}
1138
Ed Tanousb5a76932020-09-29 16:16:58 -07001139inline void populateFanRedundancy(
1140 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001141{
1142 crow::connections::systemBus->async_method_call(
1143 [sensorsAsyncResp](const boost::system::error_code ec,
1144 const GetSubTreeType& resp) {
1145 if (ec)
1146 {
1147 return; // don't have to have this interface
1148 }
Ed Tanouse278c182019-03-13 16:23:37 -07001149 for (const std::pair<std::string,
1150 std::vector<std::pair<
1151 std::string, std::vector<std::string>>>>&
1152 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001153 {
Ed Tanouse278c182019-03-13 16:23:37 -07001154 const std::string& path = pathPair.first;
1155 const std::vector<
1156 std::pair<std::string, std::vector<std::string>>>& objDict =
1157 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001158 if (objDict.empty())
1159 {
1160 continue; // this should be impossible
1161 }
1162
1163 const std::string& owner = objDict.begin()->first;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001164 sdbusplus::asio::getProperty<std::vector<std::string>>(
1165 *crow::connections::systemBus,
1166 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1167 "xyz.openbmc_project.Association", "endpoints",
1168 [path, owner, sensorsAsyncResp](
1169 const boost::system::error_code e,
1170 const std::vector<std::string>& endpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001171 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001172 {
1173 return; // if they don't have an association we
1174 // can't tell what chassis is
1175 }
James Feist8bd25cc2019-03-15 15:14:00 -07001176 auto found = std::find_if(
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001177 endpoints.begin(), endpoints.end(),
James Feist8bd25cc2019-03-15 15:14:00 -07001178 [sensorsAsyncResp](const std::string& entry) {
1179 return entry.find(
1180 sensorsAsyncResp->chassisId) !=
1181 std::string::npos;
1182 });
1183
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001184 if (found == endpoints.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001185 {
1186 return;
1187 }
1188 crow::connections::systemBus->async_method_call(
1189 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001190 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001191 const boost::container::flat_map<
1192 std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -08001193 dbus::utility::DbusVariantType>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001194 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001195 {
1196 return; // don't have to have this
1197 // interface
1198 }
1199 auto findFailures = ret.find("AllowedFailures");
1200 auto findCollection = ret.find("Collection");
1201 auto findStatus = ret.find("Status");
1202
1203 if (findFailures == ret.end() ||
1204 findCollection == ret.end() ||
1205 findStatus == ret.end())
1206 {
1207 BMCWEB_LOG_ERROR
1208 << "Invalid redundancy interface";
1209 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001210 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001211 return;
1212 }
1213
1214 auto allowedFailures = std::get_if<uint8_t>(
1215 &(findFailures->second));
1216 auto collection =
1217 std::get_if<std::vector<std::string>>(
1218 &(findCollection->second));
1219 auto status = std::get_if<std::string>(
1220 &(findStatus->second));
1221
1222 if (allowedFailures == nullptr ||
1223 collection == nullptr || status == nullptr)
1224 {
1225
1226 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001227 << "Invalid redundancy interface types";
James Feist8bd25cc2019-03-15 15:14:00 -07001228 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001229 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001230 return;
1231 }
George Liu28aa8de2021-02-01 15:13:30 +08001232 sdbusplus::message::object_path objectPath(
1233 path);
1234 std::string name = objectPath.filename();
1235 if (name.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001236 {
1237 // this should be impossible
1238 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001239 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001240 return;
1241 }
James Feist8bd25cc2019-03-15 15:14:00 -07001242 std::replace(name.begin(), name.end(), '_',
1243 ' ');
1244
1245 std::string health;
1246
1247 if (boost::ends_with(*status, "Full"))
1248 {
1249 health = "OK";
1250 }
1251 else if (boost::ends_with(*status, "Degraded"))
1252 {
1253 health = "Warning";
1254 }
1255 else
1256 {
1257 health = "Critical";
1258 }
1259 std::vector<nlohmann::json> redfishCollection;
1260 const auto& fanRedfish =
zhanghch058d1b46d2021-04-01 11:18:24 +08001261 sensorsAsyncResp->asyncResp->res
1262 .jsonValue["Fans"];
James Feist8bd25cc2019-03-15 15:14:00 -07001263 for (const std::string& item : *collection)
1264 {
George Liu28aa8de2021-02-01 15:13:30 +08001265 sdbusplus::message::object_path path(item);
1266 std::string itemName = path.filename();
1267 if (itemName.empty())
1268 {
1269 continue;
1270 }
James Feist8bd25cc2019-03-15 15:14:00 -07001271 /*
1272 todo(ed): merge patch that fixes the names
1273 std::replace(itemName.begin(),
1274 itemName.end(), '_', ' ');*/
1275 auto schemaItem = std::find_if(
1276 fanRedfish.begin(), fanRedfish.end(),
1277 [itemName](const nlohmann::json& fan) {
1278 return fan["MemberId"] == itemName;
1279 });
1280 if (schemaItem != fanRedfish.end())
1281 {
1282 redfishCollection.push_back(
1283 {{"@odata.id",
1284 (*schemaItem)["@odata.id"]}});
1285 }
1286 else
1287 {
1288 BMCWEB_LOG_ERROR
1289 << "failed to find fan in schema";
1290 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001291 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001292 return;
1293 }
1294 }
1295
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001296 size_t minNumNeeded =
1297 collection->size() > 0
1298 ? collection->size() - *allowedFailures
1299 : 0;
Ed Tanous271584a2019-07-09 16:24:22 -07001300 nlohmann::json& jResp =
zhanghch058d1b46d2021-04-01 11:18:24 +08001301 sensorsAsyncResp->asyncResp->res
Ed Tanous271584a2019-07-09 16:24:22 -07001302 .jsonValue["Redundancy"];
1303 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001304 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301305 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001306 sensorsAsyncResp->chassisId + "/" +
1307 sensorsAsyncResp->chassisSubNode +
1308 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001309 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001310 {"@odata.type",
1311 "#Redundancy.v1_3_2.Redundancy"},
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001312 {"MinNumNeeded", minNumNeeded},
James Feist8bd25cc2019-03-15 15:14:00 -07001313 {"MemberId", name},
1314 {"Mode", "N+m"},
1315 {"Name", name},
1316 {"RedundancySet", redfishCollection},
1317 {"Status",
1318 {{"Health", health},
1319 {"State", "Enabled"}}}});
1320 },
1321 owner, path, "org.freedesktop.DBus.Properties",
1322 "GetAll",
1323 "xyz.openbmc_project.Control.FanRedundancy");
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001324 });
James Feist8bd25cc2019-03-15 15:14:00 -07001325 }
1326 },
1327 "xyz.openbmc_project.ObjectMapper",
1328 "/xyz/openbmc_project/object_mapper",
1329 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1330 "/xyz/openbmc_project/control", 2,
1331 std::array<const char*, 1>{
1332 "xyz.openbmc_project.Control.FanRedundancy"});
1333}
1334
Ed Tanousb5a76932020-09-29 16:16:58 -07001335inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001336 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001337{
zhanghch058d1b46d2021-04-01 11:18:24 +08001338 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001339 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001340 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001341 {
1342 sensorHeaders = {"Voltages", "PowerSupplies"};
1343 }
1344 for (const std::string& sensorGroup : sensorHeaders)
1345 {
1346 nlohmann::json::iterator entry = response.find(sensorGroup);
1347 if (entry != response.end())
1348 {
1349 std::sort(entry->begin(), entry->end(),
1350 [](nlohmann::json& c1, nlohmann::json& c2) {
1351 return c1["Name"] < c2["Name"];
1352 });
1353
1354 // add the index counts to the end of each entry
1355 size_t count = 0;
1356 for (nlohmann::json& sensorJson : *entry)
1357 {
1358 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1359 if (odata == sensorJson.end())
1360 {
1361 continue;
1362 }
1363 std::string* value = odata->get_ptr<std::string*>();
1364 if (value != nullptr)
1365 {
1366 *value += std::to_string(count);
1367 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001368 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001369 }
1370 }
1371 }
1372 }
1373}
1374
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001375/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001376 * @brief Finds the inventory item with the specified object path.
1377 * @param inventoryItems D-Bus inventory items associated with sensors.
1378 * @param invItemObjPath D-Bus object path of inventory item.
1379 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001380 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001381inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001382 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001383 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001384{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001385 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001386 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001387 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001388 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001389 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001390 }
1391 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001392 return nullptr;
1393}
1394
1395/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001396 * @brief Finds the inventory item associated with the specified sensor.
1397 * @param inventoryItems D-Bus inventory items associated with sensors.
1398 * @param sensorObjPath D-Bus object path of sensor.
1399 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001400 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001401inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001402 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001403 const std::string& sensorObjPath)
1404{
1405 for (InventoryItem& inventoryItem : *inventoryItems)
1406 {
1407 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1408 {
1409 return &inventoryItem;
1410 }
1411 }
1412 return nullptr;
1413}
1414
1415/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001416 * @brief Finds the inventory item associated with the specified led path.
1417 * @param inventoryItems D-Bus inventory items associated with sensors.
1418 * @param ledObjPath D-Bus object path of led.
1419 * @return Inventory item within vector, or nullptr if no match found.
1420 */
1421inline InventoryItem*
1422 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1423 const std::string& ledObjPath)
1424{
1425 for (InventoryItem& inventoryItem : inventoryItems)
1426 {
1427 if (inventoryItem.ledObjectPath == ledObjPath)
1428 {
1429 return &inventoryItem;
1430 }
1431 }
1432 return nullptr;
1433}
1434
1435/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001436 * @brief Adds inventory item and associated sensor to specified vector.
1437 *
1438 * Adds a new InventoryItem to the vector if necessary. Searches for an
1439 * existing InventoryItem with the specified object path. If not found, one is
1440 * added to the vector.
1441 *
1442 * Next, the specified sensor is added to the set of sensors associated with the
1443 * InventoryItem.
1444 *
1445 * @param inventoryItems D-Bus inventory items associated with sensors.
1446 * @param invItemObjPath D-Bus object path of inventory item.
1447 * @param sensorObjPath D-Bus object path of sensor
1448 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001449inline void addInventoryItem(
1450 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1451 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001452{
1453 // Look for inventory item in vector
1454 InventoryItem* inventoryItem =
1455 findInventoryItem(inventoryItems, invItemObjPath);
1456
1457 // If inventory item doesn't exist in vector, add it
1458 if (inventoryItem == nullptr)
1459 {
1460 inventoryItems->emplace_back(invItemObjPath);
1461 inventoryItem = &(inventoryItems->back());
1462 }
1463
1464 // Add sensor to set of sensors associated with inventory item
1465 inventoryItem->sensors.emplace(sensorObjPath);
1466}
1467
1468/**
1469 * @brief Stores D-Bus data in the specified inventory item.
1470 *
1471 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1472 * specified InventoryItem.
1473 *
1474 * This data is later used to provide sensor property values in the JSON
1475 * response.
1476 *
1477 * @param inventoryItem Inventory item where data will be stored.
1478 * @param interfacesDict Map containing D-Bus interfaces and their properties
1479 * for the specified inventory item.
1480 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001481inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001482 InventoryItem& inventoryItem,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483 const boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -08001484 std::string, boost::container::flat_map<
1485 std::string, dbus::utility::DbusVariantType>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001486 interfacesDict)
1487{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001488 // Get properties from Inventory.Item interface
1489 auto interfaceIt =
1490 interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1491 if (interfaceIt != interfacesDict.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001492 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001493 auto propertyIt = interfaceIt->second.find("Present");
1494 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001495 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 const bool* value = std::get_if<bool>(&propertyIt->second);
1497 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001499 inventoryItem.isPresent = *value;
1500 }
1501 }
1502 }
1503
1504 // Check if Inventory.Item.PowerSupply interface is present
1505 interfaceIt =
1506 interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1507 if (interfaceIt != interfacesDict.end())
1508 {
1509 inventoryItem.isPowerSupply = true;
1510 }
1511
1512 // Get properties from Inventory.Decorator.Asset interface
1513 interfaceIt =
1514 interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1515 if (interfaceIt != interfacesDict.end())
1516 {
1517 auto propertyIt = interfaceIt->second.find("Manufacturer");
1518 if (propertyIt != interfaceIt->second.end())
1519 {
1520 const std::string* value =
1521 std::get_if<std::string>(&propertyIt->second);
1522 if (value != nullptr)
1523 {
1524 inventoryItem.manufacturer = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001525 }
1526 }
1527
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001528 propertyIt = interfaceIt->second.find("Model");
1529 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001530 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001531 const std::string* value =
1532 std::get_if<std::string>(&propertyIt->second);
1533 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001535 inventoryItem.model = *value;
1536 }
1537 }
1538
1539 propertyIt = interfaceIt->second.find("PartNumber");
1540 if (propertyIt != interfaceIt->second.end())
1541 {
1542 const std::string* value =
1543 std::get_if<std::string>(&propertyIt->second);
1544 if (value != nullptr)
1545 {
1546 inventoryItem.partNumber = *value;
1547 }
1548 }
1549
1550 propertyIt = interfaceIt->second.find("SerialNumber");
1551 if (propertyIt != interfaceIt->second.end())
1552 {
1553 const std::string* value =
1554 std::get_if<std::string>(&propertyIt->second);
1555 if (value != nullptr)
1556 {
1557 inventoryItem.serialNumber = *value;
1558 }
1559 }
1560 }
1561
1562 // Get properties from State.Decorator.OperationalStatus interface
1563 interfaceIt = interfacesDict.find(
1564 "xyz.openbmc_project.State.Decorator.OperationalStatus");
1565 if (interfaceIt != interfacesDict.end())
1566 {
1567 auto propertyIt = interfaceIt->second.find("Functional");
1568 if (propertyIt != interfaceIt->second.end())
1569 {
1570 const bool* value = std::get_if<bool>(&propertyIt->second);
1571 if (value != nullptr)
1572 {
1573 inventoryItem.isFunctional = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001574 }
1575 }
1576 }
1577}
1578
1579/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001580 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001581 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001582 * Uses the specified connections (services) to obtain D-Bus data for inventory
1583 * items associated with sensors. Stores the resulting data in the
1584 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001585 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001586 * This data is later used to provide sensor property values in the JSON
1587 * response.
1588 *
1589 * Finds the inventory item data asynchronously. Invokes callback when data has
1590 * been obtained.
1591 *
1592 * The callback must have the following signature:
1593 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001594 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001595 * @endcode
1596 *
1597 * This function is called recursively, obtaining data asynchronously from one
1598 * connection in each call. This ensures the callback is not invoked until the
1599 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001600 *
1601 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001602 * @param inventoryItems D-Bus inventory items associated with sensors.
1603 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001604 * @param objectMgrPaths Mappings from connection name to DBus object path that
1605 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 * @param callback Callback to invoke when inventory data has been obtained.
1607 * @param invConnectionsIndex Current index in invConnections. Only specified
1608 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001609 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001610template <typename Callback>
1611static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001612 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001613 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001614 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1615 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001616 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001617 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001619 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001620
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001621 // If no more connections left, call callback
1622 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001623 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001624 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001625 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1626 return;
1627 }
1628
1629 // Get inventory item data from current connection
1630 auto it = invConnections->nth(invConnectionsIndex);
1631 if (it != invConnections->end())
1632 {
1633 const std::string& invConnection = *it;
1634
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001635 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001636 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1637 objectMgrPaths, callback{std::move(callback)},
1638 invConnectionsIndex](
1639 const boost::system::error_code ec,
1640 ManagedObjectsVectorType& resp) {
1641 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 if (ec)
1643 {
1644 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001645 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001646 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001647 return;
1648 }
1649
1650 // Loop through returned object paths
1651 for (const auto& objDictEntry : resp)
1652 {
1653 const std::string& objPath =
1654 static_cast<const std::string&>(objDictEntry.first);
1655
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001656 // If this object path is one of the specified inventory items
1657 InventoryItem* inventoryItem =
1658 findInventoryItem(inventoryItems, objPath);
1659 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001660 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001661 // Store inventory data in InventoryItem
1662 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001663 }
1664 }
1665
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001666 // Recurse to get inventory item data from next connection
1667 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1668 invConnections, objectMgrPaths,
1669 std::move(callback), invConnectionsIndex + 1);
1670
1671 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001672 };
1673
1674 // Find DBus object path that implements ObjectManager for the current
1675 // connection. If no mapping found, default to "/".
1676 auto iter = objectMgrPaths->find(invConnection);
1677 const std::string& objectMgrPath =
1678 (iter != objectMgrPaths->end()) ? iter->second : "/";
1679 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1680 << objectMgrPath;
1681
1682 // Get all object paths and their interfaces for current connection
1683 crow::connections::systemBus->async_method_call(
1684 std::move(respHandler), invConnection, objectMgrPath,
1685 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1686 }
1687
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001688 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001689}
1690
1691/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001692 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001693 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001694 * Gets the D-Bus connections (services) that provide data for the inventory
1695 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001696 *
1697 * Finds the connections asynchronously. Invokes callback when information has
1698 * been obtained.
1699 *
1700 * The callback must have the following signature:
1701 * @code
1702 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1703 * invConnections)
1704 * @endcode
1705 *
1706 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001707 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001708 * @param callback Callback to invoke when connections have been obtained.
1709 */
1710template <typename Callback>
1711static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001712 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1713 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001714 Callback&& callback)
1715{
1716 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1717
1718 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001719 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001720 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001721 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1722 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001723 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1724
1725 // Response handler for parsing output from GetSubTree
1726 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001727 inventoryItems](const boost::system::error_code ec,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001728 const GetSubTreeType& subtree) {
1729 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1730 if (ec)
1731 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001732 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001733 BMCWEB_LOG_ERROR
1734 << "getInventoryItemsConnections respHandler DBus error " << ec;
1735 return;
1736 }
1737
1738 // Make unique list of connections for desired inventory items
1739 std::shared_ptr<boost::container::flat_set<std::string>>
1740 invConnections =
1741 std::make_shared<boost::container::flat_set<std::string>>();
1742 invConnections->reserve(8);
1743
1744 // Loop through objects from GetSubTree
1745 for (const std::pair<
1746 std::string,
1747 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1748 object : subtree)
1749 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001750 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001751 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001752 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001753 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001754 // Store all connections to inventory item
1755 for (const std::pair<std::string, std::vector<std::string>>&
1756 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001757 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001758 const std::string& invConnection = objData.first;
1759 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001760 }
1761 }
1762 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001763
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001764 callback(invConnections);
1765 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1766 };
1767
1768 // Make call to ObjectMapper to find all inventory items
1769 crow::connections::systemBus->async_method_call(
1770 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1771 "/xyz/openbmc_project/object_mapper",
1772 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1773 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1774}
1775
1776/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001777 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001778 *
1779 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001780 * inventory items. Then finds the associations from those inventory items to
1781 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001782 *
1783 * Finds the inventory items asynchronously. Invokes callback when information
1784 * has been obtained.
1785 *
1786 * The callback must have the following signature:
1787 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001788 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001789 * @endcode
1790 *
1791 * @param sensorsAsyncResp Pointer to object holding response data.
1792 * @param sensorNames All sensors within the current chassis.
1793 * @param objectMgrPaths Mappings from connection name to DBus object path that
1794 * implements ObjectManager.
1795 * @param callback Callback to invoke when inventory items have been obtained.
1796 */
1797template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001798static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001799 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1800 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1801 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001802 objectMgrPaths,
1803 Callback&& callback)
1804{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001805 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001806
1807 // Response handler for GetManagedObjects
1808 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1809 sensorNames](const boost::system::error_code ec,
1810 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001811 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001812 if (ec)
1813 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001814 BMCWEB_LOG_ERROR
1815 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001816 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001817 return;
1818 }
1819
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001820 // Create vector to hold list of inventory items
1821 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1822 std::make_shared<std::vector<InventoryItem>>();
1823
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001824 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001825 std::string sensorAssocPath;
1826 sensorAssocPath.reserve(128); // avoid memory allocations
1827 for (const auto& objDictEntry : resp)
1828 {
1829 const std::string& objPath =
1830 static_cast<const std::string&>(objDictEntry.first);
1831 const boost::container::flat_map<
1832 std::string, boost::container::flat_map<
1833 std::string, dbus::utility::DbusVariantType>>&
1834 interfacesDict = objDictEntry.second;
1835
1836 // If path is inventory association for one of the specified sensors
1837 for (const std::string& sensorName : *sensorNames)
1838 {
1839 sensorAssocPath = sensorName;
1840 sensorAssocPath += "/inventory";
1841 if (objPath == sensorAssocPath)
1842 {
1843 // Get Association interface for object path
1844 auto assocIt =
1845 interfacesDict.find("xyz.openbmc_project.Association");
1846 if (assocIt != interfacesDict.end())
1847 {
1848 // Get inventory item from end point
1849 auto endpointsIt = assocIt->second.find("endpoints");
1850 if (endpointsIt != assocIt->second.end())
1851 {
1852 const std::vector<std::string>* endpoints =
1853 std::get_if<std::vector<std::string>>(
1854 &endpointsIt->second);
1855 if ((endpoints != nullptr) && !endpoints->empty())
1856 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001857 // Add inventory item to vector
1858 const std::string& invItemPath =
1859 endpoints->front();
1860 addInventoryItem(inventoryItems, invItemPath,
1861 sensorName);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001862 }
1863 }
1864 }
1865 break;
1866 }
1867 }
1868 }
1869
Anthony Wilsond5005492019-07-31 16:34:17 -05001870 // Now loop through the returned object paths again, this time to
1871 // find the leds associated with the inventory items we just found
1872 std::string inventoryAssocPath;
1873 inventoryAssocPath.reserve(128); // avoid memory allocations
1874 for (const auto& objDictEntry : resp)
1875 {
1876 const std::string& objPath =
1877 static_cast<const std::string&>(objDictEntry.first);
1878 const boost::container::flat_map<
1879 std::string, boost::container::flat_map<
1880 std::string, dbus::utility::DbusVariantType>>&
1881 interfacesDict = objDictEntry.second;
1882
1883 for (InventoryItem& inventoryItem : *inventoryItems)
1884 {
1885 inventoryAssocPath = inventoryItem.objectPath;
1886 inventoryAssocPath += "/leds";
1887 if (objPath == inventoryAssocPath)
1888 {
1889 // Get Association interface for object path
1890 auto assocIt =
1891 interfacesDict.find("xyz.openbmc_project.Association");
1892 if (assocIt != interfacesDict.end())
1893 {
1894 // Get inventory item from end point
1895 auto endpointsIt = assocIt->second.find("endpoints");
1896 if (endpointsIt != assocIt->second.end())
1897 {
1898 const std::vector<std::string>* endpoints =
1899 std::get_if<std::vector<std::string>>(
1900 &endpointsIt->second);
1901 if ((endpoints != nullptr) && !endpoints->empty())
1902 {
1903 // Store LED path in inventory item
1904 const std::string& ledPath = endpoints->front();
1905 inventoryItem.ledObjectPath = ledPath;
1906 }
1907 }
1908 }
1909 break;
1910 }
1911 }
1912 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001913 callback(inventoryItems);
1914 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001915 };
1916
1917 // Find DBus object path that implements ObjectManager for ObjectMapper
1918 std::string connection = "xyz.openbmc_project.ObjectMapper";
1919 auto iter = objectMgrPaths->find(connection);
1920 const std::string& objectMgrPath =
1921 (iter != objectMgrPaths->end()) ? iter->second : "/";
1922 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1923 << objectMgrPath;
1924
1925 // Call GetManagedObjects on the ObjectMapper to get all associations
1926 crow::connections::systemBus->async_method_call(
1927 std::move(respHandler), connection, objectMgrPath,
1928 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1929
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001930 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001931}
1932
1933/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001934 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1935 *
1936 * Uses the specified connections (services) to obtain D-Bus data for inventory
1937 * item leds associated with sensors. Stores the resulting data in the
1938 * inventoryItems vector.
1939 *
1940 * This data is later used to provide sensor property values in the JSON
1941 * response.
1942 *
1943 * Finds the inventory item led data asynchronously. Invokes callback when data
1944 * has been obtained.
1945 *
1946 * The callback must have the following signature:
1947 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001948 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001949 * @endcode
1950 *
1951 * This function is called recursively, obtaining data asynchronously from one
1952 * connection in each call. This ensures the callback is not invoked until the
1953 * last asynchronous function has completed.
1954 *
1955 * @param sensorsAsyncResp Pointer to object holding response data.
1956 * @param inventoryItems D-Bus inventory items associated with sensors.
1957 * @param ledConnections Connections that provide data for the inventory leds.
1958 * @param callback Callback to invoke when inventory data has been obtained.
1959 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1960 * in recursive calls to this function.
1961 */
1962template <typename Callback>
1963void getInventoryLedData(
1964 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1965 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1966 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1967 ledConnections,
1968 Callback&& callback, size_t ledConnectionsIndex = 0)
1969{
1970 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1971
1972 // If no more connections left, call callback
1973 if (ledConnectionsIndex >= ledConnections->size())
1974 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001975 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001976 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1977 return;
1978 }
1979
1980 // Get inventory item data from current connection
1981 auto it = ledConnections->nth(ledConnectionsIndex);
1982 if (it != ledConnections->end())
1983 {
1984 const std::string& ledPath = (*it).first;
1985 const std::string& ledConnection = (*it).second;
1986 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001987 auto respHandler =
1988 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1989 callback{std::move(callback)}, ledConnectionsIndex](
1990 const boost::system::error_code ec, const std::string& state) {
1991 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1992 if (ec)
1993 {
1994 BMCWEB_LOG_ERROR
1995 << "getInventoryLedData respHandler DBus error " << ec;
1996 messages::internalError(sensorsAsyncResp->asyncResp->res);
1997 return;
1998 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001999
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002000 BMCWEB_LOG_DEBUG << "Led state: " << state;
Ed Tanous168e20c2021-12-13 14:39:53 -08002001 // Find inventory item with this LED object path
2002 InventoryItem* inventoryItem =
2003 findInventoryItemForLed(*inventoryItems, ledPath);
2004 if (inventoryItem != nullptr)
Anthony Wilsond5005492019-07-31 16:34:17 -05002005 {
Ed Tanous168e20c2021-12-13 14:39:53 -08002006 // Store LED state in InventoryItem
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002007 if (boost::ends_with(state, "On"))
Anthony Wilsond5005492019-07-31 16:34:17 -05002008 {
Ed Tanous168e20c2021-12-13 14:39:53 -08002009 inventoryItem->ledState = LedState::ON;
2010 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002011 else if (boost::ends_with(state, "Blink"))
Ed Tanous168e20c2021-12-13 14:39:53 -08002012 {
2013 inventoryItem->ledState = LedState::BLINK;
2014 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002015 else if (boost::ends_with(state, "Off"))
Ed Tanous168e20c2021-12-13 14:39:53 -08002016 {
2017 inventoryItem->ledState = LedState::OFF;
2018 }
2019 else
2020 {
2021 inventoryItem->ledState = LedState::UNKNOWN;
Anthony Wilsond5005492019-07-31 16:34:17 -05002022 }
2023 }
Anthony Wilsond5005492019-07-31 16:34:17 -05002024
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002025 // Recurse to get LED data from next connection
2026 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2027 ledConnections, std::move(callback),
2028 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05002029
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002030 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2031 };
Anthony Wilsond5005492019-07-31 16:34:17 -05002032
2033 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002034 sdbusplus::asio::getProperty<std::string>(
2035 *crow::connections::systemBus, ledConnection, ledPath,
2036 "xyz.openbmc_project.Led.Physical", "State",
2037 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05002038 }
2039
2040 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2041}
2042
2043/**
2044 * @brief Gets LED data for LEDs associated with given inventory items.
2045 *
2046 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2047 * associated with the specified inventory items. Then gets the LED data from
2048 * each connection and stores it in the inventory item.
2049 *
2050 * This data is later used to provide sensor property values in the JSON
2051 * response.
2052 *
2053 * Finds the LED data asynchronously. Invokes callback when information has
2054 * been obtained.
2055 *
2056 * The callback must have the following signature:
2057 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002058 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002059 * @endcode
2060 *
2061 * @param sensorsAsyncResp Pointer to object holding response data.
2062 * @param inventoryItems D-Bus inventory items associated with sensors.
2063 * @param callback Callback to invoke when inventory items have been obtained.
2064 */
2065template <typename Callback>
2066void getInventoryLeds(
2067 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2068 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2069 Callback&& callback)
2070{
2071 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2072
2073 const std::string path = "/xyz/openbmc_project";
2074 const std::array<std::string, 1> interfaces = {
2075 "xyz.openbmc_project.Led.Physical"};
2076
2077 // Response handler for parsing output from GetSubTree
2078 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2079 inventoryItems](const boost::system::error_code ec,
2080 const GetSubTreeType& subtree) {
2081 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2082 if (ec)
2083 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002084 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002085 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2086 << ec;
2087 return;
2088 }
2089
2090 // Build map of LED object paths to connections
2091 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2092 ledConnections = std::make_shared<
2093 boost::container::flat_map<std::string, std::string>>();
2094
2095 // Loop through objects from GetSubTree
2096 for (const std::pair<
2097 std::string,
2098 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2099 object : subtree)
2100 {
2101 // Check if object path is LED for one of the specified inventory
2102 // items
2103 const std::string& ledPath = object.first;
2104 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2105 {
2106 // Add mapping from ledPath to connection
2107 const std::string& connection = object.second.begin()->first;
2108 (*ledConnections)[ledPath] = connection;
2109 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2110 << connection;
2111 }
2112 }
2113
2114 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2115 std::move(callback));
2116 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2117 };
2118 // Make call to ObjectMapper to find all inventory items
2119 crow::connections::systemBus->async_method_call(
2120 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2121 "/xyz/openbmc_project/object_mapper",
2122 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2123 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2124}
2125
2126/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002127 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2128 *
2129 * Uses the specified connections (services) (currently assumes just one) to
2130 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2131 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2132 *
2133 * This data is later used to provide sensor property values in the JSON
2134 * response.
2135 *
2136 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2137 * when data has been obtained.
2138 *
2139 * The callback must have the following signature:
2140 * @code
2141 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2142 * @endcode
2143 *
2144 * @param sensorsAsyncResp Pointer to object holding response data.
2145 * @param inventoryItems D-Bus inventory items associated with sensors.
2146 * @param psAttributesConnections Connections that provide data for the Power
2147 * Supply Attributes
2148 * @param callback Callback to invoke when data has been obtained.
2149 */
2150template <typename Callback>
2151void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002152 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002153 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2154 const boost::container::flat_map<std::string, std::string>&
2155 psAttributesConnections,
2156 Callback&& callback)
2157{
2158 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2159
2160 if (psAttributesConnections.empty())
2161 {
2162 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2163 callback(inventoryItems);
2164 return;
2165 }
2166
2167 // Assuming just one connection (service) for now
2168 auto it = psAttributesConnections.nth(0);
2169
2170 const std::string& psAttributesPath = (*it).first;
2171 const std::string& psAttributesConnection = (*it).second;
2172
2173 // Response handler for Get DeratingFactor property
2174 auto respHandler = [sensorsAsyncResp, inventoryItems,
2175 callback{std::move(callback)}](
2176 const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002177 const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002178 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2179 if (ec)
2180 {
2181 BMCWEB_LOG_ERROR
2182 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002183 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002184 return;
2185 }
2186
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002187 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2188 // Store value in Power Supply Inventory Items
2189 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002190 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002191 if (inventoryItem.isPowerSupply == true)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002192 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002193 inventoryItem.powerSupplyEfficiencyPercent =
2194 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002195 }
2196 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002197
2198 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2199 callback(inventoryItems);
2200 };
2201
2202 // Get the DeratingFactor property for the PowerSupplyAttributes
2203 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002204 sdbusplus::asio::getProperty<uint32_t>(
2205 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2206 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2207 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002208
2209 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2210}
2211
2212/**
2213 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2214 *
2215 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2216 * data. Then gets the Power Supply Attributes data from the connection
2217 * (currently just assumes 1 connection) and stores the data in the inventory
2218 * item.
2219 *
2220 * This data is later used to provide sensor property values in the JSON
2221 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2222 *
2223 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2224 * when information has been obtained.
2225 *
2226 * The callback must have the following signature:
2227 * @code
2228 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2229 * @endcode
2230 *
2231 * @param sensorsAsyncResp Pointer to object holding response data.
2232 * @param inventoryItems D-Bus inventory items associated with sensors.
2233 * @param callback Callback to invoke when data has been obtained.
2234 */
2235template <typename Callback>
2236void getPowerSupplyAttributes(
2237 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2238 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2239 Callback&& callback)
2240{
2241 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2242
2243 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002244 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002245 {
2246 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2247 callback(inventoryItems);
2248 return;
2249 }
2250
2251 const std::array<std::string, 1> interfaces = {
2252 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2253
2254 // Response handler for parsing output from GetSubTree
2255 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2256 inventoryItems](const boost::system::error_code ec,
2257 const GetSubTreeType& subtree) {
2258 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2259 if (ec)
2260 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002261 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002262 BMCWEB_LOG_ERROR
2263 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2264 return;
2265 }
2266 if (subtree.size() == 0)
2267 {
2268 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2269 callback(inventoryItems);
2270 return;
2271 }
2272
2273 // Currently we only support 1 power supply attribute, use this for
2274 // all the power supplies. Build map of object path to connection.
2275 // Assume just 1 connection and 1 path for now.
2276 boost::container::flat_map<std::string, std::string>
2277 psAttributesConnections;
2278
2279 if (subtree[0].first.empty() || subtree[0].second.empty())
2280 {
2281 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2282 callback(inventoryItems);
2283 return;
2284 }
2285
2286 const std::string& psAttributesPath = subtree[0].first;
2287 const std::string& connection = subtree[0].second.begin()->first;
2288
2289 if (connection.empty())
2290 {
2291 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2292 callback(inventoryItems);
2293 return;
2294 }
2295
2296 psAttributesConnections[psAttributesPath] = connection;
2297 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2298 << connection;
2299
2300 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2301 psAttributesConnections,
2302 std::move(callback));
2303 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2304 };
2305 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2306 crow::connections::systemBus->async_method_call(
2307 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2308 "/xyz/openbmc_project/object_mapper",
2309 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2310 "/xyz/openbmc_project", 0, interfaces);
2311 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2312}
2313
2314/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002315 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002316 *
2317 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002318 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002319 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002320 * This data is later used to provide sensor property values in the JSON
2321 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002322 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002323 * Finds the inventory items asynchronously. Invokes callback when the
2324 * inventory items have been obtained.
2325 *
2326 * The callback must have the following signature:
2327 * @code
2328 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2329 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002330 *
2331 * @param sensorsAsyncResp Pointer to object holding response data.
2332 * @param sensorNames All sensors within the current chassis.
2333 * @param objectMgrPaths Mappings from connection name to DBus object path that
2334 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002335 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002336 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002337template <typename Callback>
2338static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002339 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2340 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2341 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002342 objectMgrPaths,
2343 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002344{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002345 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2346 auto getInventoryItemAssociationsCb =
2347 [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2348 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2349 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002350 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002351 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2352 callback{std::move(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002353 std::shared_ptr<boost::container::flat_set<std::string>>
2354 invConnections) {
2355 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002356 auto getInventoryItemsDataCb =
2357 [sensorsAsyncResp, inventoryItems,
2358 callback{std::move(callback)}]() {
2359 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002360
2361 auto getInventoryLedsCb = [sensorsAsyncResp,
2362 inventoryItems,
2363 callback{std::move(
2364 callback)}]() {
2365 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2366 // Find Power Supply Attributes and get the data
2367 getPowerSupplyAttributes(sensorsAsyncResp,
2368 inventoryItems,
2369 std::move(callback));
2370 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2371 };
2372
Anthony Wilsond5005492019-07-31 16:34:17 -05002373 // Find led connections and get the data
2374 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002375 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002376 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2377 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002378
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002379 // Get inventory item data from connections
2380 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2381 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002382 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002383 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2384 };
2385
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002386 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002387 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002388 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002389 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002390 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002391 };
2392
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002393 // Get associations from sensors to inventory items
2394 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2395 std::move(getInventoryItemAssociationsCb));
2396 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2397}
2398
2399/**
2400 * @brief Returns JSON PowerSupply object for the specified inventory item.
2401 *
2402 * Searches for a JSON PowerSupply object that matches the specified inventory
2403 * item. If one is not found, a new PowerSupply object is added to the JSON
2404 * array.
2405 *
2406 * Multiple sensors are often associated with one power supply inventory item.
2407 * As a result, multiple sensor values are stored in one JSON PowerSupply
2408 * object.
2409 *
2410 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2411 * @param inventoryItem Inventory item for the power supply.
2412 * @param chassisId Chassis that contains the power supply.
2413 * @return JSON PowerSupply object for the specified inventory item.
2414 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002415inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002416 const InventoryItem& inventoryItem,
2417 const std::string& chassisId)
2418{
2419 // Check if matching PowerSupply object already exists in JSON array
2420 for (nlohmann::json& powerSupply : powerSupplyArray)
2421 {
2422 if (powerSupply["MemberId"] == inventoryItem.name)
2423 {
2424 return powerSupply;
2425 }
2426 }
2427
2428 // Add new PowerSupply object to JSON array
2429 powerSupplyArray.push_back({});
2430 nlohmann::json& powerSupply = powerSupplyArray.back();
2431 powerSupply["@odata.id"] =
2432 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2433 powerSupply["MemberId"] = inventoryItem.name;
2434 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2435 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2436 powerSupply["Model"] = inventoryItem.model;
2437 powerSupply["PartNumber"] = inventoryItem.partNumber;
2438 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002439 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002440
Gunnar Mills42cbe532019-08-15 15:26:54 -05002441 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2442 {
2443 powerSupply["EfficiencyPercent"] =
2444 inventoryItem.powerSupplyEfficiencyPercent;
2445 }
2446
2447 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002448 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2449 powerSupply["Status"]["Health"] = health;
2450
2451 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002452}
2453
2454/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002455 * @brief Gets the values of the specified sensors.
2456 *
2457 * Stores the results as JSON in the SensorsAsyncResp.
2458 *
2459 * Gets the sensor values asynchronously. Stores the results later when the
2460 * information has been obtained.
2461 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002462 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002463 *
2464 * To minimize the number of DBus calls, the DBus method
2465 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2466 * values of all sensors provided by a connection (service).
2467 *
2468 * The connections set contains all the connections that provide sensor values.
2469 *
2470 * The objectMgrPaths map contains mappings from a connection name to the
2471 * corresponding DBus object path that implements ObjectManager.
2472 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002473 * The InventoryItem vector contains D-Bus inventory items associated with the
2474 * sensors. Inventory item data is needed for some Redfish sensor properties.
2475 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002476 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002477 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002478 * @param connections Connections that provide sensor values.
2479 * @param objectMgrPaths Mappings from connection name to DBus object path that
2480 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002481 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002482 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002483inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002484 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002485 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002486 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002487 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002488 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002489 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002490{
2491 BMCWEB_LOG_DEBUG << "getSensorData enter";
2492 // Get managed objects from all services exposing sensors
2493 for (const std::string& connection : connections)
2494 {
2495 // Response handler to process managed objects
Ed Tanous81ce6092020-12-17 16:54:55 +00002496 auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002497 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002498 const boost::system::error_code ec,
2499 ManagedObjectsVectorType& resp) {
2500 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2501 if (ec)
2502 {
2503 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002504 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002505 return;
2506 }
2507 // Go through all objects and update response with sensor data
2508 for (const auto& objDictEntry : resp)
2509 {
2510 const std::string& objPath =
2511 static_cast<const std::string&>(objDictEntry.first);
2512 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2513 << objPath;
2514
Shawn McCarneyde629b62019-03-08 10:42:51 -06002515 std::vector<std::string> split;
2516 // Reserve space for
2517 // /xyz/openbmc_project/sensors/<name>/<subname>
2518 split.reserve(6);
2519 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2520 if (split.size() < 6)
2521 {
2522 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2523 << objPath;
2524 continue;
2525 }
2526 // These indexes aren't intuitive, as boost::split puts an empty
2527 // string at the beginning
2528 const std::string& sensorType = split[4];
2529 const std::string& sensorName = split[5];
2530 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2531 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002532 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002533 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002534 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002535 continue;
2536 }
2537
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002538 // Find inventory item (if any) associated with sensor
2539 InventoryItem* inventoryItem =
2540 findInventoryItemForSensor(inventoryItems, objPath);
2541
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002542 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002543 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002544
2545 nlohmann::json* sensorJson = nullptr;
2546
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002547 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002548 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002549 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002550 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2551 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002552 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002553 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002554 }
2555 else
2556 {
Ed Tanous271584a2019-07-09 16:24:22 -07002557 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002558 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002559 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002560 fieldName = "Temperatures";
2561 }
2562 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2563 sensorType == "fan_pwm")
2564 {
2565 fieldName = "Fans";
2566 }
2567 else if (sensorType == "voltage")
2568 {
2569 fieldName = "Voltages";
2570 }
2571 else if (sensorType == "power")
2572 {
2573 if (!sensorName.compare("total_power"))
2574 {
2575 fieldName = "PowerControl";
2576 }
2577 else if ((inventoryItem != nullptr) &&
2578 (inventoryItem->isPowerSupply))
2579 {
2580 fieldName = "PowerSupplies";
2581 }
2582 else
2583 {
2584 // Other power sensors are in SensorCollection
2585 continue;
2586 }
2587 }
2588 else
2589 {
2590 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2591 << sensorType;
2592 continue;
2593 }
2594
2595 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002596 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002597 if (fieldName == "PowerControl")
2598 {
2599 if (tempArray.empty())
2600 {
2601 // Put multiple "sensors" into a single
2602 // PowerControl. Follows MemberId naming and
2603 // naming in power.hpp.
2604 tempArray.push_back(
2605 {{"@odata.id",
2606 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002607 sensorsAsyncResp->chassisId + "/" +
2608 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002609 fieldName + "/0"}});
2610 }
2611 sensorJson = &(tempArray.back());
2612 }
2613 else if (fieldName == "PowerSupplies")
2614 {
2615 if (inventoryItem != nullptr)
2616 {
2617 sensorJson =
2618 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002619 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002620 }
2621 }
2622 else
2623 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002624 tempArray.push_back(
2625 {{"@odata.id",
2626 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002627 sensorsAsyncResp->chassisId + "/" +
2628 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002629 fieldName + "/"}});
2630 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002631 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002632 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002633
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002634 if (sensorJson != nullptr)
2635 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002636 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002637 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002638 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002639 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002640 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002641 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002642 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002643 sortJSONResponse(sensorsAsyncResp);
2644 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002645 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002646 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002647 }
James Feist8bd25cc2019-03-15 15:14:00 -07002648 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002649 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2650 };
2651
2652 // Find DBus object path that implements ObjectManager for the current
2653 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002654 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002655 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002656 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002657 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2658 << objectMgrPath;
2659
2660 crow::connections::systemBus->async_method_call(
2661 getManagedObjectsCb, connection, objectMgrPath,
2662 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002663 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002664 BMCWEB_LOG_DEBUG << "getSensorData exit";
2665}
2666
Ed Tanous23a21a12020-07-25 04:45:05 +00002667inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002668 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002669 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002670{
2671 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002672 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002673 const boost::container::flat_set<std::string>& connections) {
2674 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2675 auto getObjectManagerPathsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002676 [sensorsAsyncResp, sensorNames,
Ed Tanousb5a76932020-09-29 16:16:58 -07002677 connections](const std::shared_ptr<boost::container::flat_map<
2678 std::string, std::string>>& objectMgrPaths) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002679 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2680 auto getInventoryItemsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002681 [sensorsAsyncResp, sensorNames, connections,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002682 objectMgrPaths](
Ed Tanousf23b7292020-10-15 09:41:17 -07002683 const std::shared_ptr<std::vector<InventoryItem>>&
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002684 inventoryItems) {
2685 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2686 // Get sensor data and store results in JSON
Ed Tanous81ce6092020-12-17 16:54:55 +00002687 getSensorData(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002688 connections, objectMgrPaths,
Ed Tanousf23b7292020-10-15 09:41:17 -07002689 inventoryItems);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002690 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2691 };
2692
2693 // Get inventory items associated with sensors
Ed Tanous81ce6092020-12-17 16:54:55 +00002694 getInventoryItems(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002695 objectMgrPaths,
2696 std::move(getInventoryItemsCb));
2697
2698 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2699 };
2700
2701 // Get mapping from connection names to the DBus object
2702 // paths that implement the ObjectManager interface
Ed Tanous81ce6092020-12-17 16:54:55 +00002703 getObjectManagerPaths(sensorsAsyncResp,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002704 std::move(getObjectManagerPathsCb));
2705 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2706 };
2707
2708 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002709 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002710}
2711
Shawn McCarneyde629b62019-03-08 10:42:51 -06002712/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002713 * @brief Entry point for retrieving sensors data related to requested
2714 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002715 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002716 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002717inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002718 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002719{
2720 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002721 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002722 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002723 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002724 sensorNames) {
2725 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Ed Tanous81ce6092020-12-17 16:54:55 +00002726 processSensorList(sensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002727 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2728 };
zhanghch058d1b46d2021-04-01 11:18:24 +08002729 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2730 nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002731
Shawn McCarney26f03892019-05-03 13:20:24 -05002732 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002733 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002734 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002735}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002736
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302737/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002738 * @brief Find the requested sensorName in the list of all sensors supplied by
2739 * the chassis node
2740 *
2741 * @param sensorName The sensor name supplied in the PATCH request
2742 * @param sensorsList The list of sensors managed by the chassis node
2743 * @param sensorsModified The list of sensors that were found as a result of
2744 * repeated calls to this function
2745 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002746inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302747 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002748 boost::container::flat_set<std::string>& sensorsList,
2749 boost::container::flat_set<std::string>& sensorsModified)
2750{
George Liu28aa8de2021-02-01 15:13:30 +08002751 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002752 {
George Liu28aa8de2021-02-01 15:13:30 +08002753 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002754 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002755 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002756 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002757 continue;
2758 }
2759 if (thisSensorName == sensorName)
2760 {
2761 sensorsModified.emplace(chassisSensor);
2762 return true;
2763 }
2764 }
2765 return false;
2766}
2767
2768/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302769 * @brief Entry point for overriding sensor values of given sensor
2770 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002771 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002772 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002773 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302774 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002775inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002776 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002777 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002778 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302779{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002780 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002781 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302782
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302783 const char* propertyValueName;
2784 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302785 std::string memberId;
2786 double value;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302787 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302788 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302789 if (collectionItems.first == "Temperatures")
2790 {
2791 propertyValueName = "ReadingCelsius";
2792 }
2793 else if (collectionItems.first == "Fans")
2794 {
2795 propertyValueName = "Reading";
2796 }
2797 else
2798 {
2799 propertyValueName = "ReadingVolts";
2800 }
2801 for (auto& item : collectionItems.second)
2802 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002803 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2804 "MemberId", memberId, propertyValueName,
2805 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302806 {
2807 return;
2808 }
2809 overrideMap.emplace(memberId,
2810 std::make_pair(value, collectionItems.first));
2811 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302812 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002813
Ed Tanousb5a76932020-09-29 16:16:58 -07002814 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2815 const std::shared_ptr<
2816 boost::container::flat_set<
2817 std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002818 // Match sensor names in the PATCH request to those managed by the
2819 // chassis node
2820 const std::shared_ptr<boost::container::flat_set<std::string>>
2821 sensorNames =
2822 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302823 for (const auto& item : overrideMap)
2824 {
2825 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002826 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2827 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302828 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302829 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002830 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302831 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302832 return;
2833 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302834 }
2835 // Get the connection to which the memberId belongs
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002836 auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
2837 const boost::container::flat_set<
2838 std::string>& /*connections*/,
2839 const std::set<std::pair<
2840 std::string, std::string>>&
2841 objectsWithConnection) {
2842 if (objectsWithConnection.size() != overrideMap.size())
2843 {
2844 BMCWEB_LOG_INFO
2845 << "Unable to find all objects with proper connection "
2846 << objectsWithConnection.size() << " requested "
2847 << overrideMap.size() << "\n";
2848 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2849 sensorAsyncResp->chassisSubNode ==
2850 sensors::node::thermal
2851 ? "Temperatures"
2852 : "Voltages",
2853 "Count");
2854 return;
2855 }
2856 for (const auto& item : objectsWithConnection)
2857 {
2858 sdbusplus::message::object_path path(item.first);
2859 std::string sensorName = path.filename();
2860 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302861 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002862 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302863 return;
2864 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302865
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002866 const auto& iterator = overrideMap.find(sensorName);
2867 if (iterator == overrideMap.end())
2868 {
2869 BMCWEB_LOG_INFO << "Unable to find sensor object"
2870 << item.first << "\n";
2871 messages::internalError(sensorAsyncResp->asyncResp->res);
2872 return;
2873 }
2874 crow::connections::systemBus->async_method_call(
2875 [sensorAsyncResp](const boost::system::error_code ec) {
2876 if (ec)
2877 {
2878 if (ec.value() ==
2879 boost::system::errc::permission_denied)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302880 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002881 BMCWEB_LOG_WARNING
2882 << "Manufacturing mode is not Enabled...can't "
2883 "Override the sensor value. ";
2884
2885 messages::insufficientPrivilege(
zhanghch058d1b46d2021-04-01 11:18:24 +08002886 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302887 return;
2888 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002889 BMCWEB_LOG_DEBUG
2890 << "setOverrideValueStatus DBUS error: " << ec;
2891 messages::internalError(
2892 sensorAsyncResp->asyncResp->res);
2893 }
2894 },
2895 item.second, item.first, "org.freedesktop.DBus.Properties",
2896 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002897 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002898 }
2899 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302900 // Get object with connection for the given sensor name
2901 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2902 std::move(getObjectsWithConnectionCb));
2903 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302904 // get full sensor list for the given chassisId and cross verify the sensor.
2905 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2906}
2907
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002908/**
2909 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2910 * path of the sensor.
2911 *
2912 * Function builds valid Redfish response for sensor query of given chassis and
2913 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2914 * it to caller in a callback.
2915 *
2916 * @param chassis Chassis for which retrieval should be performed
2917 * @param node Node (group) of sensors. See sensors::node for supported values
2918 * @param mapComplete Callback to be called with retrieval result
2919 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002920inline void retrieveUriToDbusMap(const std::string& chassis,
2921 const std::string& node,
2922 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002923{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +00002924 auto pathIt = sensors::dbus::paths.find(node);
2925 if (pathIt == sensors::dbus::paths.end())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002926 {
2927 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2928 mapComplete(boost::beast::http::status::bad_request, {});
2929 return;
2930 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002931
Gunnar Mills9062d472021-11-16 11:37:47 -06002932 auto res = std::make_shared<crow::Response>();
2933 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(*res);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002934 auto callback =
Gunnar Mills9062d472021-11-16 11:37:47 -06002935 [res, asyncResp, mapCompleteCb{std::move(mapComplete)}](
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002936 const boost::beast::http::status status,
2937 const boost::container::flat_map<std::string, std::string>&
2938 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2939
2940 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002941 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002942 getChassisData(resp);
2943}
2944
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002945inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002946{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002947 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002948 .privileges(redfish::privileges::getSensorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002949 .methods(
2950 boost::beast::http::verb::get)([](const crow::Request&,
2951 const std::shared_ptr<
2952 bmcweb::AsyncResp>& aResp,
2953 const std::string& chassisId) {
2954 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002955
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002956 std::shared_ptr<SensorsAsyncResp> asyncResp =
2957 std::make_shared<SensorsAsyncResp>(
2958 aResp, chassisId,
2959 sensors::dbus::paths.at(sensors::node::sensors),
2960 sensors::node::sensors);
zhanghch058d1b46d2021-04-01 11:18:24 +08002961
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002962 auto getChassisCb =
2963 [asyncResp](
2964 const std::shared_ptr<
2965 boost::container::flat_set<std::string>>& sensorNames) {
2966 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002967
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002968 nlohmann::json& entriesArray =
2969 asyncResp->asyncResp->res.jsonValue["Members"];
2970 for (auto& sensor : *sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002971 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002972 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002973
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002974 sdbusplus::message::object_path path(sensor);
2975 std::string sensorName = path.filename();
2976 if (sensorName.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002977 {
2978 BMCWEB_LOG_ERROR << "Invalid sensor path: "
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002979 << sensor;
2980 messages::internalError(asyncResp->asyncResp->res);
2981 return;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002982 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002983 entriesArray.push_back(
2984 {{"@odata.id", "/redfish/v1/Chassis/" +
2985 asyncResp->chassisId + "/" +
2986 asyncResp->chassisSubNode + "/" +
2987 sensorName}});
2988 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002989
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002990 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2991 entriesArray.size();
2992 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2993 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002994
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002995 // Get set of sensors in chassis
2996 getChassis(asyncResp, std::move(getChassisCb));
2997 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
2998 });
2999}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003000
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003001inline void requestRoutesSensor(App& app)
3002{
3003 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003004 .privileges(redfish::privileges::getSensor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003005 .methods(
3006 boost::beast::http::verb::get)([](const crow::Request&,
3007 const std::shared_ptr<
3008 bmcweb::AsyncResp>& aResp,
3009 const std::string& chassisId,
3010 const std::string& sensorName) {
3011 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3012 std::shared_ptr<SensorsAsyncResp> asyncResp =
3013 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3014 std::vector<const char*>(),
3015 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003016
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003017 const std::array<const char*, 1> interfaces = {
3018 "xyz.openbmc_project.Sensor.Value"};
3019
3020 // Get a list of all of the sensors that implement Sensor.Value
3021 // and get the path and service name associated with the sensor
3022 crow::connections::systemBus->async_method_call(
3023 [asyncResp, sensorName](const boost::system::error_code ec,
3024 const GetSubTreeType& subtree) {
3025 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3026 if (ec)
3027 {
3028 messages::internalError(asyncResp->asyncResp->res);
3029 BMCWEB_LOG_ERROR
3030 << "Sensor getSensorPaths resp_handler: "
3031 << "Dbus error " << ec;
3032 return;
3033 }
3034
3035 GetSubTreeType::const_iterator it = std::find_if(
3036 subtree.begin(), subtree.end(),
3037 [sensorName](
3038 const std::pair<
3039 std::string,
3040 std::vector<std::pair<
3041 std::string, std::vector<std::string>>>>&
3042 object) {
3043 sdbusplus::message::object_path path(object.first);
3044 std::string name = path.filename();
3045 if (name.empty())
3046 {
3047 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3048 << object.first;
3049 return false;
3050 }
3051
3052 return name == sensorName;
3053 });
3054
3055 if (it == subtree.end())
3056 {
3057 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3058 << sensorName;
3059 messages::resourceNotFound(asyncResp->asyncResp->res,
3060 "Sensor", sensorName);
3061 return;
3062 }
3063 std::string_view sensorPath = (*it).first;
3064 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3065 << sensorName << "': " << sensorPath;
3066
3067 const std::shared_ptr<
3068 boost::container::flat_set<std::string>>
3069 sensorList = std::make_shared<
3070 boost::container::flat_set<std::string>>();
3071
3072 sensorList->emplace(sensorPath);
3073 processSensorList(asyncResp, sensorList);
3074 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3075 },
3076 "xyz.openbmc_project.ObjectMapper",
3077 "/xyz/openbmc_project/object_mapper",
3078 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3079 "/xyz/openbmc_project/sensors", 2, interfaces);
3080 });
3081}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003082
Ed Tanous1abe55e2018-09-05 08:30:59 -07003083} // namespace redfish