blob: d501b7002239c24805c72c9fd0648cd97cf0dc01 [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
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020036namespace sensors
37{
38namespace node
39{
40static constexpr std::string_view power = "Power";
41static constexpr std::string_view sensors = "Sensors";
42static constexpr std::string_view thermal = "Thermal";
43} // namespace node
44
45namespace dbus
46{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000047
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020048static const boost::container::flat_map<std::string_view,
49 std::vector<const char*>>
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000050 paths = {{node::power,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020051 {"/xyz/openbmc_project/sensors/voltage",
52 "/xyz/openbmc_project/sensors/power"}},
53 {node::sensors,
54 {"/xyz/openbmc_project/sensors/power",
55 "/xyz/openbmc_project/sensors/current",
Basheer Ahmed Muddebihal70886902021-07-22 02:11:17 -070056 "/xyz/openbmc_project/sensors/airflow",
George Liue8204932021-02-01 14:42:49 +080057#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
58 "/xyz/openbmc_project/sensors/voltage",
59 "/xyz/openbmc_project/sensors/fan_tach",
60 "/xyz/openbmc_project/sensors/temperature",
61 "/xyz/openbmc_project/sensors/fan_pwm",
62 "/xyz/openbmc_project/sensors/altitude",
63 "/xyz/openbmc_project/sensors/energy",
64#endif
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020065 "/xyz/openbmc_project/sensors/utilization"}},
66 {node::thermal,
67 {"/xyz/openbmc_project/sensors/fan_tach",
68 "/xyz/openbmc_project/sensors/temperature",
69 "/xyz/openbmc_project/sensors/fan_pwm"}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000070} // namespace dbus
71
72inline const char* toReadingType(const std::string& sensorType)
73{
74 if (sensorType == "voltage")
75 {
76 return "Voltage";
77 }
78 if (sensorType == "power")
79 {
80 return "Power";
81 }
82 if (sensorType == "current")
83 {
84 return "Current";
85 }
86 if (sensorType == "fan_tach")
87 {
88 return "Rotational";
89 }
90 if (sensorType == "temperature")
91 {
92 return "Temperature";
93 }
94 if (sensorType == "fan_pwm" || sensorType == "utilization")
95 {
96 return "Percent";
97 }
98 if (sensorType == "altitude")
99 {
100 return "Altitude";
101 }
102 if (sensorType == "airflow")
103 {
104 return "AirFlow";
105 }
106 if (sensorType == "energy")
107 {
108 return "EnergyJoules";
109 }
110 return "";
111}
112
113inline const char* toReadingUnits(const std::string& sensorType)
114{
115 if (sensorType == "voltage")
116 {
117 return "V";
118 }
119 if (sensorType == "power")
120 {
121 return "W";
122 }
123 if (sensorType == "current")
124 {
125 return "A";
126 }
127 if (sensorType == "fan_tach")
128 {
129 return "RPM";
130 }
131 if (sensorType == "temperature")
132 {
133 return "Cel";
134 }
135 if (sensorType == "fan_pwm" || sensorType == "utilization")
136 {
137 return "%";
138 }
139 if (sensorType == "altitude")
140 {
141 return "m";
142 }
143 if (sensorType == "airflow")
144 {
145 return "cft_i/min";
146 }
147 if (sensorType == "energy")
148 {
149 return "J";
150 }
151 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200152}
153} // namespace sensors
154
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100155/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200156 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100157 * Gathers data needed for response processing after async calls are done
158 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159class SensorsAsyncResp
160{
161 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200162 using DataCompleteCb = std::function<void(
163 const boost::beast::http::status status,
164 const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
165
166 struct SensorData
167 {
168 const std::string name;
169 std::string uri;
170 const std::string valueKey;
171 const std::string dbusPath;
172 };
173
zhanghch058d1b46d2021-04-01 11:18:24 +0800174 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
175 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700176 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200177 const std::string_view& subNode) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800178 asyncResp(asyncResp),
Ed Tanous271584a2019-07-09 16:24:22 -0700179 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500180 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200181
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200182 // Store extra data about sensor mapping and return it in callback
zhanghch058d1b46d2021-04-01 11:18:24 +0800183 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
184 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700185 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200186 const std::string_view& subNode,
187 DataCompleteCb&& creationComplete) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800188 asyncResp(asyncResp),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200189 chassisId(chassisIdIn), types(typesIn),
190 chassisSubNode(subNode), metadata{std::vector<SensorData>()},
191 dataComplete{std::move(creationComplete)}
192 {}
193
Ed Tanous1abe55e2018-09-05 08:30:59 -0700194 ~SensorsAsyncResp()
195 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800196 if (asyncResp->res.result() ==
197 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700198 {
199 // Reset the json object to clear out any data that made it in
200 // before the error happened todo(ed) handle error condition with
201 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800202 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700203 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200204
205 if (dataComplete && metadata)
206 {
207 boost::container::flat_map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800208 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200209 {
210 for (auto& sensor : *metadata)
211 {
212 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
213 sensor.dbusPath));
214 }
215 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800216 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200217 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100219
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800220 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
221 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
222 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
223 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
224
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200225 void addMetadata(const nlohmann::json& sensorObject,
226 const std::string& valueKey, const std::string& dbusPath)
227 {
228 if (metadata)
229 {
230 metadata->emplace_back(SensorData{sensorObject["Name"],
231 sensorObject["@odata.id"],
232 valueKey, dbusPath});
233 }
234 }
235
236 void updateUri(const std::string& name, const std::string& uri)
237 {
238 if (metadata)
239 {
240 for (auto& sensor : *metadata)
241 {
242 if (sensor.name == name)
243 {
244 sensor.uri = uri;
245 }
246 }
247 }
248 }
249
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200251 const std::string chassisId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252 const std::vector<const char*> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200253 const std::string chassisSubNode;
254
255 private:
256 std::optional<std::vector<SensorData>> metadata;
257 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100258};
259
260/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500261 * Possible states for physical inventory leds
262 */
263enum class LedState
264{
265 OFF,
266 ON,
267 BLINK,
268 UNKNOWN
269};
270
271/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500272 * D-Bus inventory item associated with one or more sensors.
273 */
274class InventoryItem
275{
276 public:
Ed Tanouse05aec52022-01-25 10:28:56 -0800277 InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500278 {
279 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800280 sdbusplus::message::object_path path(objectPath);
281 name = path.filename();
282 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500283 {
George Liu28aa8de2021-02-01 15:13:30 +0800284 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500285 }
286 }
287
288 std::string objectPath;
289 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800290 bool isPresent = true;
291 bool isFunctional = true;
292 bool isPowerSupply = false;
293 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500294 std::string manufacturer;
295 std::string model;
296 std::string partNumber;
297 std::string serialNumber;
298 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500299 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800300 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500301};
302
303/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530304 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200305 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100306 * @param sensorNames Sensors retrieved from chassis
307 * @param callback Callback for processing gathered connections
308 */
309template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530310void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000311 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700312 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530313 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700314{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530315 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700316 const std::string path = "/xyz/openbmc_project/sensors";
317 const std::array<std::string, 1> interfaces = {
318 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100319
Ed Tanous1abe55e2018-09-05 08:30:59 -0700320 // Response handler for parsing objects subtree
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800321 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -0800322 sensorsAsyncResp, sensorNames](
323 const boost::system::error_code ec,
324 const dbus::utility::MapperGetSubTreeResponse&
325 subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530326 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700327 if (ec)
328 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800329 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530330 BMCWEB_LOG_ERROR
331 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700332 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100333 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100334
Ed Tanous1abe55e2018-09-05 08:30:59 -0700335 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
336
337 // Make unique list of connections only for requested sensor types and
338 // found in the chassis
339 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530340 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700341 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
342 // producers
343 connections.reserve(8);
344
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700345 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
346 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700347 {
348 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
349 }
350
351 for (const std::pair<
352 std::string,
353 std::vector<std::pair<std::string, std::vector<std::string>>>>&
354 object : subtree)
355 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700356 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700357 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700358 for (const std::pair<std::string, std::vector<std::string>>&
359 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700360 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700361 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
362 connections.insert(objData.first);
363 objectsWithConnection.insert(
364 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700365 }
366 }
367 }
368 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530369 callback(std::move(connections), std::move(objectsWithConnection));
370 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700372 // Make call to ObjectMapper to find all sensors objects
373 crow::connections::systemBus->async_method_call(
374 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
375 "/xyz/openbmc_project/object_mapper",
376 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530377 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
378}
379
380/**
381 * @brief Create connections necessary for sensors
382 * @param SensorsAsyncResp Pointer to object holding response data
383 * @param sensorNames Sensors retrieved from chassis
384 * @param callback Callback for processing gathered connections
385 */
386template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700387void getConnections(
Ed Tanous81ce6092020-12-17 16:54:55 +0000388 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700389 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
390 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530391{
392 auto objectsWithConnectionCb =
393 [callback](const boost::container::flat_set<std::string>& connections,
394 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700395 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000396 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530397 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100398}
399
400/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700401 * @brief Shrinks the list of sensors for processing
402 * @param SensorsAysncResp The class holding the Redfish response
403 * @param allSensors A list of all the sensors associated to the
404 * chassis element (i.e. baseboard, front panel, etc...)
405 * @param activeSensors A list that is a reduction of the incoming
406 * allSensors list. Eliminate Thermal sensors when a Power request is
407 * made, and eliminate Power sensors when a Thermal request is made.
408 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000409inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000410 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700411 const std::vector<std::string>* allSensors,
Ed Tanousb5a76932020-09-29 16:16:58 -0700412 const std::shared_ptr<boost::container::flat_set<std::string>>&
413 activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700414{
Ed Tanous81ce6092020-12-17 16:54:55 +0000415 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700416 {
417 return;
418 }
419 if ((allSensors == nullptr) || (activeSensors == nullptr))
420 {
421 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800422 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000423 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200424 ? "Temperatures"
425 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700426
427 return;
428 }
429 if (allSensors->empty())
430 {
431 // Nothing to do, the activeSensors object is also empty
432 return;
433 }
434
Ed Tanous81ce6092020-12-17 16:54:55 +0000435 for (const char* type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700436 {
437 for (const std::string& sensor : *allSensors)
438 {
439 if (boost::starts_with(sensor, type))
440 {
441 activeSensors->emplace(sensor);
442 }
443 }
444 }
445}
446
447/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800448 * @brief Retrieves valid chassis path
449 * @param asyncResp Pointer to object holding response data
450 * @param callback Callback for next step to get valid chassis path
451 */
452template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700453void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800454 Callback&& callback)
455{
456 BMCWEB_LOG_DEBUG << "checkChassisId enter";
457 const std::array<const char*, 2> interfaces = {
458 "xyz.openbmc_project.Inventory.Item.Board",
459 "xyz.openbmc_project.Inventory.Item.Chassis"};
460
Ed Tanousb9d36b42022-02-26 21:42:46 -0800461 auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
462 const boost::system::error_code ec,
463 const dbus::utility::MapperGetSubTreePathsResponse&
464 chassisPaths) mutable {
465 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
466 if (ec)
467 {
468 BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
469 << ec;
470 messages::internalError(asyncResp->asyncResp->res);
471 return;
472 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800473
Ed Tanousb9d36b42022-02-26 21:42:46 -0800474 std::optional<std::string> chassisPath;
475 std::string chassisName;
476 for (const std::string& chassis : chassisPaths)
477 {
478 sdbusplus::message::object_path path(chassis);
479 chassisName = path.filename();
480 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800481 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800482 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
483 continue;
Carol Wang4bb3dc32019-10-17 18:15:02 +0800484 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800485 if (chassisName == asyncResp->chassisId)
486 {
487 chassisPath = chassis;
488 break;
489 }
490 }
491 callback(chassisPath);
492 };
Carol Wang4bb3dc32019-10-17 18:15:02 +0800493
494 // Get the Chassis Collection
495 crow::connections::systemBus->async_method_call(
496 respHandler, "xyz.openbmc_project.ObjectMapper",
497 "/xyz/openbmc_project/object_mapper",
498 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
499 "/xyz/openbmc_project/inventory", 0, interfaces);
500 BMCWEB_LOG_DEBUG << "checkChassisId exit";
501}
502
503/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100504 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200505 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100506 * @param callback Callback for next step in gathered sensor processing
507 */
508template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700509void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700510 Callback&& callback)
511{
512 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500513 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700514 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500515 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800516 auto respHandler = [callback{std::forward<Callback>(callback)},
517 sensorsAsyncResp](
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700518 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800519 const dbus::utility::MapperGetSubTreePathsResponse&
520 chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700521 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
522 if (ec)
523 {
524 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800525 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 return;
527 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100528
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700529 const std::string* chassisPath = nullptr;
530 std::string chassisName;
531 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 {
George Liu28aa8de2021-02-01 15:13:30 +0800533 sdbusplus::message::object_path path(chassis);
534 chassisName = path.filename();
535 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700536 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700537 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700538 continue;
539 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700540 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700542 chassisPath = &chassis;
543 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700544 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700545 }
546 if (chassisPath == nullptr)
547 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800548 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
549 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700550 return;
551 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700553 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200554 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700555 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800556 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700557 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700558 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200559 else if (chassisSubNode == sensors::node::thermal)
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 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800563 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
564 nlohmann::json::array();
565 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800566 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700567 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200568 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500569 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800570 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500571 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800572 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500573 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800574 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500575 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800576 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
577 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500578 }
579
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200580 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500581 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800582 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500583 }
584
zhanghch058d1b46d2021-04-01 11:18:24 +0800585 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700586 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
587 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800588 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500589 // Get the list of all sensors for this Chassis element
590 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700591 sdbusplus::asio::getProperty<std::vector<std::string>>(
592 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
593 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800594 [sensorsAsyncResp,
595 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700596 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700597 const std::vector<std::string>& nodeSensorList) {
Ed Tanous271584a2019-07-09 16:24:22 -0700598 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700599 {
Ed Tanous271584a2019-07-09 16:24:22 -0700600 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700601 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800602 messages::internalError(
603 sensorsAsyncResp->asyncResp->res);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700604 return;
605 }
606 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700607 const std::shared_ptr<boost::container::flat_set<std::string>>
608 culledSensorList = std::make_shared<
609 boost::container::flat_set<std::string>>();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700610 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700611 culledSensorList);
612 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700613 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100614 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100615
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700616 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700617 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700618 respHandler, "xyz.openbmc_project.ObjectMapper",
619 "/xyz/openbmc_project/object_mapper",
620 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700621 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700622 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100623}
624
625/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600626 * @brief Finds all DBus object paths that implement ObjectManager.
627 *
628 * Creates a mapping from the associated connection name to the object path.
629 *
630 * Finds the object paths asynchronously. Invokes callback when information has
631 * been obtained.
632 *
633 * The callback must have the following signature:
634 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500635 * callback(std::shared_ptr<boost::container::flat_map<std::string,
636 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600637 * @endcode
638 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700639 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600640 * @param callback Callback to invoke when object paths obtained.
641 */
642template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700643void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000644 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700645 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600646{
647 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
648 const std::array<std::string, 1> interfaces = {
649 "org.freedesktop.DBus.ObjectManager"};
650
651 // Response handler for GetSubTree DBus method
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800652 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -0800653 sensorsAsyncResp](
654 const boost::system::error_code ec,
655 const dbus::utility::MapperGetSubTreeResponse&
656 subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600657 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
658 if (ec)
659 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800660 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600661 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
662 << ec;
663 return;
664 }
665
666 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500667 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
668 objectMgrPaths = std::make_shared<
669 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600670 for (const std::pair<
671 std::string,
672 std::vector<std::pair<std::string, std::vector<std::string>>>>&
673 object : subtree)
674 {
675 // Loop over connections for current object path
676 const std::string& objectPath = object.first;
677 for (const std::pair<std::string, std::vector<std::string>>&
678 objData : object.second)
679 {
680 // Add mapping from connection to object path
681 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500682 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600683 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
684 << objectPath;
685 }
686 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500687 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600688 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
689 };
690
691 // Query mapper for all DBus object paths that implement ObjectManager
692 crow::connections::systemBus->async_method_call(
693 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
694 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700695 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600696 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
697}
698
699/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500700 * @brief Returns the Redfish State value for the specified inventory item.
701 * @param inventoryItem D-Bus inventory item associated with a sensor.
702 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700703 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000704inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500705{
706 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
707 {
708 return "Absent";
709 }
James Feist34dd1792019-05-17 14:10:54 -0700710
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500711 return "Enabled";
712}
713
714/**
715 * @brief Returns the Redfish Health value for the specified sensor.
716 * @param sensorJson Sensor JSON object.
717 * @param interfacesDict Map of all sensor interfaces.
718 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
719 * be nullptr if no associated inventory item was found.
720 * @return Health value for sensor.
721 */
Ed Tanous711ac7a2021-12-20 09:34:41 -0800722inline std::string
723 getHealth(nlohmann::json& sensorJson,
724 const dbus::utility::DBusInteracesMap& interfacesDict,
725 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700726{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500727 // Get current health value (if any) in the sensor JSON object. Some JSON
728 // objects contain multiple sensors (such as PowerSupplies). We want to set
729 // the overall health to be the most severe of any of the sensors.
730 std::string currentHealth;
731 auto statusIt = sensorJson.find("Status");
732 if (statusIt != sensorJson.end())
733 {
734 auto healthIt = statusIt->find("Health");
735 if (healthIt != statusIt->end())
736 {
737 std::string* health = healthIt->get_ptr<std::string*>();
738 if (health != nullptr)
739 {
740 currentHealth = *health;
741 }
742 }
743 }
744
745 // If current health in JSON object is already Critical, return that. This
746 // should override the sensor health, which might be less severe.
747 if (currentHealth == "Critical")
748 {
749 return "Critical";
750 }
751
752 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800753
Ed Tanous9eb808c2022-01-25 10:19:23 -0800754 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700755 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800756 if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
James Feist34dd1792019-05-17 14:10:54 -0700757 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800758 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700759 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800760 if (valueName == "CriticalAlarmHigh" ||
761 valueName == "CriticalAlarmLow")
762 {
763 const bool* asserted = std::get_if<bool>(&value);
764 if (asserted == nullptr)
765 {
766 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
767 }
768 else if (*asserted)
769 {
770 return "Critical";
771 }
772 }
James Feist34dd1792019-05-17 14:10:54 -0700773 }
774 }
775 }
776
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500777 // Check if associated inventory item is not functional
778 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
779 {
780 return "Critical";
781 }
782
783 // If current health in JSON object is already Warning, return that. This
784 // should override the sensor status, which might be less severe.
785 if (currentHealth == "Warning")
786 {
787 return "Warning";
788 }
789
790 // Check if sensor has warning threshold alarm
Ed Tanous9eb808c2022-01-25 10:19:23 -0800791 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700792 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800793 if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
James Feist34dd1792019-05-17 14:10:54 -0700794 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800795 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700796 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800797 if (valueName == "WarningAlarmHigh" ||
798 valueName == "WarningAlarmLow")
799 {
800 const bool* asserted = std::get_if<bool>(&value);
801 if (asserted == nullptr)
802 {
803 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
804 }
805 else if (*asserted)
806 {
Ed Tanousebe4d912022-01-12 12:38:17 -0800807 return "Warning";
Ed Tanous711ac7a2021-12-20 09:34:41 -0800808 }
809 }
James Feist34dd1792019-05-17 14:10:54 -0700810 }
811 }
812 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500813
James Feist34dd1792019-05-17 14:10:54 -0700814 return "OK";
815}
816
Ed Tanous23a21a12020-07-25 04:45:05 +0000817inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500818 const InventoryItem* inventoryItem)
819{
820 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
821 {
822 switch (inventoryItem->ledState)
823 {
824 case LedState::OFF:
825 sensorJson["IndicatorLED"] = "Off";
826 break;
827 case LedState::ON:
828 sensorJson["IndicatorLED"] = "Lit";
829 break;
830 case LedState::BLINK:
831 sensorJson["IndicatorLED"] = "Blinking";
832 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000833 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500834 break;
835 }
836 }
837}
838
James Feist34dd1792019-05-17 14:10:54 -0700839/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100840 * @brief Builds a json sensor representation of a sensor.
841 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500842 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100843 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200844 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100845 * @param interfacesDict A dictionary of the interfaces and properties of said
846 * interfaces to be built from
847 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500848 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
849 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100850 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000851inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100852 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700853 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800854 const dbus::utility::DBusInteracesMap& interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000855 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700856{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 // Assume values exist as is (10^0 == 1) if no scale exists
858 int64_t scaleMultiplier = 0;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800859 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700860 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800861 if (interface == "xyz.openbmc_project.Sensor.Value")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700862 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800863 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800864 {
865 if (valueName == "Scale")
866 {
867 const int64_t* int64Value = std::get_if<int64_t>(&value);
868 if (int64Value != nullptr)
869 {
870 scaleMultiplier = *int64Value;
871 }
872 }
873 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100874 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100875 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700876
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200877 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500878 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500879 // For sensors in SensorCollection we set Id instead of MemberId,
880 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000881 sensorJson["Id"] = sensorName;
882 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500883 }
884 else if (sensorType != "power")
885 {
886 // Set MemberId and Name for non-power sensors. For PowerSupplies and
887 // PowerControl, those properties have more general values because
888 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000889 sensorJson["MemberId"] = sensorName;
890 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500891 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700892
Ed Tanous81ce6092020-12-17 16:54:55 +0000893 sensorJson["Status"]["State"] = getState(inventoryItem);
894 sensorJson["Status"]["Health"] =
895 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700896
897 // Parameter to set to override the type we get from dbus, and force it to
898 // int, regardless of what is available. This is used for schemas like fan,
899 // that require integers, not floats.
900 bool forceToInt = false;
901
Anthony Wilson3929aca2019-07-19 15:42:33 -0500902 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200903 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500904 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000905 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000906
907 const std::string& readingType = sensors::toReadingType(sensorType);
908 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500909 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000910 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
911 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500912 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000913 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500914 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000915 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500916 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000917
918 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
919 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200920 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000921 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
922 << sensorType;
923 }
924 else
925 {
926 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200927 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500928 }
929 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700930 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500931 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000932 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700933 // TODO(ed) Documentation says that path should be type fan_tach,
934 // implementation seems to implement fan
935 }
936 else if (sensorType == "fan" || sensorType == "fan_tach")
937 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500938 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000939 sensorJson["ReadingUnits"] = "RPM";
940 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
941 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 forceToInt = true;
943 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700944 else if (sensorType == "fan_pwm")
945 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500946 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000947 sensorJson["ReadingUnits"] = "Percent";
948 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
949 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700950 forceToInt = true;
951 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700952 else if (sensorType == "voltage")
953 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500954 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000955 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700956 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700957 else if (sensorType == "power")
958 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700959 std::string sensorNameLower =
960 boost::algorithm::to_lower_copy(sensorName);
961
Ed Tanous55f79e62022-01-25 11:26:16 -0800962 if (sensorName == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000963 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000964 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500965 // Put multiple "sensors" into a single PowerControl, so have
966 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000967 sensorJson["MemberId"] = "0";
968 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500969 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000970 }
971 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700972 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500973 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700974 }
975 else
976 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500977 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700978 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700979 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 else
981 {
982 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
983 return;
984 }
985 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500986 std::vector<
987 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
988 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 properties.reserve(7);
990
991 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600992
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200993 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500994 {
995 properties.emplace_back(
996 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
997 "/Thresholds/UpperCaution/Reading"_json_pointer);
998 properties.emplace_back(
999 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1000 "/Thresholds/LowerCaution/Reading"_json_pointer);
1001 properties.emplace_back(
1002 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1003 "/Thresholds/UpperCritical/Reading"_json_pointer);
1004 properties.emplace_back(
1005 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1006 "/Thresholds/LowerCritical/Reading"_json_pointer);
1007 }
1008 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001009 {
1010 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001011 "WarningHigh",
1012 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001013 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001014 "WarningLow",
1015 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001016 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001017 "CriticalHigh",
1018 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001019 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001020 "CriticalLow",
1021 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001022 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001023
Ed Tanous2474adf2018-09-05 16:31:16 -07001024 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1025
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001026 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001027 {
1028 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001029 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001030 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001031 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001032 }
1033 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034 {
1035 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001036 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001037 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001038 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001039 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001040 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001041 {
1042 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001043 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001044 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001045 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001046 }
1047
Anthony Wilson3929aca2019-07-19 15:42:33 -05001048 for (const std::tuple<const char*, const char*,
1049 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001050 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001051 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001052 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001053 if (interface != std::get<0>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001054 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001055 continue;
1056 }
Ed Tanous55f79e62022-01-25 11:26:16 -08001057 for (const auto& [valueName, valueVariant] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001058 {
1059 if (valueName != std::get<1>(p))
1060 {
1061 continue;
1062 }
Anthony Wilson3929aca2019-07-19 15:42:33 -05001063
1064 // The property we want to set may be nested json, so use
1065 // a json_pointer for easy indexing into the json structure.
1066 const nlohmann::json::json_pointer& key = std::get<2>(p);
1067
Ed Tanous1abe55e2018-09-05 08:30:59 -07001068 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001069 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001070
Ed Tanousabf2add2019-01-22 16:40:12 -08001071 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001072 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001073 double temp = 0.0;
1074 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001075 {
Ed Tanous271584a2019-07-09 16:24:22 -07001076 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001077 }
1078 else if (doubleValue != nullptr)
1079 {
1080 temp = *doubleValue;
1081 }
Eddie James028f7eb2019-05-17 21:24:36 +00001082 else if (uValue != nullptr)
1083 {
1084 temp = *uValue;
1085 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001086 else
1087 {
1088 BMCWEB_LOG_ERROR
1089 << "Got value interface that wasn't int or double";
1090 continue;
1091 }
1092 temp = temp * std::pow(10, scaleMultiplier);
1093 if (forceToInt)
1094 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001095 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001096 }
1097 else
1098 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001099 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001100 }
1101 }
1102 }
1103 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001104
Ed Tanous81ce6092020-12-17 16:54:55 +00001105 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001106 "/xyz/openbmc_project/sensors/" + sensorType +
1107 "/" + sensorName);
1108
Ed Tanous1abe55e2018-09-05 08:30:59 -07001109 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001110}
1111
Ed Tanousb5a76932020-09-29 16:16:58 -07001112inline void populateFanRedundancy(
1113 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001114{
1115 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001116 [sensorsAsyncResp](
1117 const boost::system::error_code ec,
1118 const dbus::utility::MapperGetSubTreeResponse& resp) {
James Feist8bd25cc2019-03-15 15:14:00 -07001119 if (ec)
1120 {
1121 return; // don't have to have this interface
1122 }
Ed Tanouse278c182019-03-13 16:23:37 -07001123 for (const std::pair<std::string,
1124 std::vector<std::pair<
1125 std::string, std::vector<std::string>>>>&
1126 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001127 {
Ed Tanouse278c182019-03-13 16:23:37 -07001128 const std::string& path = pathPair.first;
1129 const std::vector<
1130 std::pair<std::string, std::vector<std::string>>>& objDict =
1131 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001132 if (objDict.empty())
1133 {
1134 continue; // this should be impossible
1135 }
1136
1137 const std::string& owner = objDict.begin()->first;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001138 sdbusplus::asio::getProperty<std::vector<std::string>>(
1139 *crow::connections::systemBus,
1140 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1141 "xyz.openbmc_project.Association", "endpoints",
1142 [path, owner, sensorsAsyncResp](
1143 const boost::system::error_code e,
1144 const std::vector<std::string>& endpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001145 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001146 {
1147 return; // if they don't have an association we
1148 // can't tell what chassis is
1149 }
James Feist8bd25cc2019-03-15 15:14:00 -07001150 auto found = std::find_if(
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001151 endpoints.begin(), endpoints.end(),
James Feist8bd25cc2019-03-15 15:14:00 -07001152 [sensorsAsyncResp](const std::string& entry) {
1153 return entry.find(
1154 sensorsAsyncResp->chassisId) !=
1155 std::string::npos;
1156 });
1157
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001158 if (found == endpoints.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001159 {
1160 return;
1161 }
1162 crow::connections::systemBus->async_method_call(
1163 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001164 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001165 const boost::container::flat_map<
1166 std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -08001167 dbus::utility::DbusVariantType>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001168 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001169 {
1170 return; // don't have to have this
1171 // interface
1172 }
1173 auto findFailures = ret.find("AllowedFailures");
1174 auto findCollection = ret.find("Collection");
1175 auto findStatus = ret.find("Status");
1176
1177 if (findFailures == ret.end() ||
1178 findCollection == ret.end() ||
1179 findStatus == ret.end())
1180 {
1181 BMCWEB_LOG_ERROR
1182 << "Invalid redundancy interface";
1183 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001184 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001185 return;
1186 }
1187
Ed Tanous9eb808c2022-01-25 10:19:23 -08001188 const uint8_t* allowedFailures =
1189 std::get_if<uint8_t>(
1190 &(findFailures->second));
1191 const std::vector<std::string>* collection =
James Feist8bd25cc2019-03-15 15:14:00 -07001192 std::get_if<std::vector<std::string>>(
1193 &(findCollection->second));
Ed Tanous9eb808c2022-01-25 10:19:23 -08001194 const std::string* status =
1195 std::get_if<std::string>(
1196 &(findStatus->second));
James Feist8bd25cc2019-03-15 15:14:00 -07001197
1198 if (allowedFailures == nullptr ||
1199 collection == nullptr || status == nullptr)
1200 {
1201
1202 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001203 << "Invalid redundancy interface types";
James Feist8bd25cc2019-03-15 15:14:00 -07001204 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001205 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001206 return;
1207 }
George Liu28aa8de2021-02-01 15:13:30 +08001208 sdbusplus::message::object_path objectPath(
1209 path);
1210 std::string name = objectPath.filename();
1211 if (name.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001212 {
1213 // this should be impossible
1214 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001215 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001216 return;
1217 }
James Feist8bd25cc2019-03-15 15:14:00 -07001218 std::replace(name.begin(), name.end(), '_',
1219 ' ');
1220
1221 std::string health;
1222
1223 if (boost::ends_with(*status, "Full"))
1224 {
1225 health = "OK";
1226 }
1227 else if (boost::ends_with(*status, "Degraded"))
1228 {
1229 health = "Warning";
1230 }
1231 else
1232 {
1233 health = "Critical";
1234 }
1235 std::vector<nlohmann::json> redfishCollection;
1236 const auto& fanRedfish =
zhanghch058d1b46d2021-04-01 11:18:24 +08001237 sensorsAsyncResp->asyncResp->res
1238 .jsonValue["Fans"];
James Feist8bd25cc2019-03-15 15:14:00 -07001239 for (const std::string& item : *collection)
1240 {
George Liu28aa8de2021-02-01 15:13:30 +08001241 sdbusplus::message::object_path path(item);
1242 std::string itemName = path.filename();
1243 if (itemName.empty())
1244 {
1245 continue;
1246 }
James Feist8bd25cc2019-03-15 15:14:00 -07001247 /*
1248 todo(ed): merge patch that fixes the names
1249 std::replace(itemName.begin(),
1250 itemName.end(), '_', ' ');*/
1251 auto schemaItem = std::find_if(
1252 fanRedfish.begin(), fanRedfish.end(),
1253 [itemName](const nlohmann::json& fan) {
1254 return fan["MemberId"] == itemName;
1255 });
1256 if (schemaItem != fanRedfish.end())
1257 {
1258 redfishCollection.push_back(
1259 {{"@odata.id",
1260 (*schemaItem)["@odata.id"]}});
1261 }
1262 else
1263 {
1264 BMCWEB_LOG_ERROR
1265 << "failed to find fan in schema";
1266 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001267 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001268 return;
1269 }
1270 }
1271
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001272 size_t minNumNeeded =
Ed Tanous26f69762022-01-25 09:49:11 -08001273 collection->empty()
1274 ? 0
1275 : collection->size() - *allowedFailures;
Ed Tanous271584a2019-07-09 16:24:22 -07001276 nlohmann::json& jResp =
zhanghch058d1b46d2021-04-01 11:18:24 +08001277 sensorsAsyncResp->asyncResp->res
Ed Tanous271584a2019-07-09 16:24:22 -07001278 .jsonValue["Redundancy"];
1279 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001280 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301281 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001282 sensorsAsyncResp->chassisId + "/" +
1283 sensorsAsyncResp->chassisSubNode +
1284 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001285 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001286 {"@odata.type",
1287 "#Redundancy.v1_3_2.Redundancy"},
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001288 {"MinNumNeeded", minNumNeeded},
James Feist8bd25cc2019-03-15 15:14:00 -07001289 {"MemberId", name},
1290 {"Mode", "N+m"},
1291 {"Name", name},
1292 {"RedundancySet", redfishCollection},
1293 {"Status",
1294 {{"Health", health},
1295 {"State", "Enabled"}}}});
1296 },
1297 owner, path, "org.freedesktop.DBus.Properties",
1298 "GetAll",
1299 "xyz.openbmc_project.Control.FanRedundancy");
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001300 });
James Feist8bd25cc2019-03-15 15:14:00 -07001301 }
1302 },
1303 "xyz.openbmc_project.ObjectMapper",
1304 "/xyz/openbmc_project/object_mapper",
1305 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1306 "/xyz/openbmc_project/control", 2,
1307 std::array<const char*, 1>{
1308 "xyz.openbmc_project.Control.FanRedundancy"});
1309}
1310
Ed Tanousb5a76932020-09-29 16:16:58 -07001311inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001312 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001313{
zhanghch058d1b46d2021-04-01 11:18:24 +08001314 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001315 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001316 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001317 {
1318 sensorHeaders = {"Voltages", "PowerSupplies"};
1319 }
1320 for (const std::string& sensorGroup : sensorHeaders)
1321 {
1322 nlohmann::json::iterator entry = response.find(sensorGroup);
1323 if (entry != response.end())
1324 {
1325 std::sort(entry->begin(), entry->end(),
1326 [](nlohmann::json& c1, nlohmann::json& c2) {
1327 return c1["Name"] < c2["Name"];
1328 });
1329
1330 // add the index counts to the end of each entry
1331 size_t count = 0;
1332 for (nlohmann::json& sensorJson : *entry)
1333 {
1334 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1335 if (odata == sensorJson.end())
1336 {
1337 continue;
1338 }
1339 std::string* value = odata->get_ptr<std::string*>();
1340 if (value != nullptr)
1341 {
1342 *value += std::to_string(count);
1343 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001344 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001345 }
1346 }
1347 }
1348 }
1349}
1350
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001351/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001352 * @brief Finds the inventory item with the specified object path.
1353 * @param inventoryItems D-Bus inventory items associated with sensors.
1354 * @param invItemObjPath D-Bus object path of inventory item.
1355 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001356 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001357inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001358 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001359 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001360{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001361 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001362 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001363 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001364 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001365 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001366 }
1367 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001368 return nullptr;
1369}
1370
1371/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001372 * @brief Finds the inventory item associated with the specified sensor.
1373 * @param inventoryItems D-Bus inventory items associated with sensors.
1374 * @param sensorObjPath D-Bus object path of sensor.
1375 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001376 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001377inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001378 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001379 const std::string& sensorObjPath)
1380{
1381 for (InventoryItem& inventoryItem : *inventoryItems)
1382 {
1383 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1384 {
1385 return &inventoryItem;
1386 }
1387 }
1388 return nullptr;
1389}
1390
1391/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001392 * @brief Finds the inventory item associated with the specified led path.
1393 * @param inventoryItems D-Bus inventory items associated with sensors.
1394 * @param ledObjPath D-Bus object path of led.
1395 * @return Inventory item within vector, or nullptr if no match found.
1396 */
1397inline InventoryItem*
1398 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1399 const std::string& ledObjPath)
1400{
1401 for (InventoryItem& inventoryItem : inventoryItems)
1402 {
1403 if (inventoryItem.ledObjectPath == ledObjPath)
1404 {
1405 return &inventoryItem;
1406 }
1407 }
1408 return nullptr;
1409}
1410
1411/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001412 * @brief Adds inventory item and associated sensor to specified vector.
1413 *
1414 * Adds a new InventoryItem to the vector if necessary. Searches for an
1415 * existing InventoryItem with the specified object path. If not found, one is
1416 * added to the vector.
1417 *
1418 * Next, the specified sensor is added to the set of sensors associated with the
1419 * InventoryItem.
1420 *
1421 * @param inventoryItems D-Bus inventory items associated with sensors.
1422 * @param invItemObjPath D-Bus object path of inventory item.
1423 * @param sensorObjPath D-Bus object path of sensor
1424 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001425inline void addInventoryItem(
1426 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1427 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001428{
1429 // Look for inventory item in vector
1430 InventoryItem* inventoryItem =
1431 findInventoryItem(inventoryItems, invItemObjPath);
1432
1433 // If inventory item doesn't exist in vector, add it
1434 if (inventoryItem == nullptr)
1435 {
1436 inventoryItems->emplace_back(invItemObjPath);
1437 inventoryItem = &(inventoryItems->back());
1438 }
1439
1440 // Add sensor to set of sensors associated with inventory item
1441 inventoryItem->sensors.emplace(sensorObjPath);
1442}
1443
1444/**
1445 * @brief Stores D-Bus data in the specified inventory item.
1446 *
1447 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1448 * specified InventoryItem.
1449 *
1450 * This data is later used to provide sensor property values in the JSON
1451 * response.
1452 *
1453 * @param inventoryItem Inventory item where data will be stored.
1454 * @param interfacesDict Map containing D-Bus interfaces and their properties
1455 * for the specified inventory item.
1456 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001457inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001458 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001459 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001460{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001461 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001462
Ed Tanous9eb808c2022-01-25 10:19:23 -08001463 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001464 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001465 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001466 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001467 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001468 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001469 if (name == "Present")
1470 {
1471 const bool* value = std::get_if<bool>(&dbusValue);
1472 if (value != nullptr)
1473 {
1474 inventoryItem.isPresent = *value;
1475 }
1476 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001477 }
1478 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001479 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001480
Ed Tanous711ac7a2021-12-20 09:34:41 -08001481 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001482 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001483 inventoryItem.isPowerSupply = true;
1484 }
1485
1486 // Get properties from Inventory.Decorator.Asset interface
1487 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1488 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001489 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001490 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001491 if (name == "Manufacturer")
1492 {
1493 const std::string* value =
1494 std::get_if<std::string>(&dbusValue);
1495 if (value != nullptr)
1496 {
1497 inventoryItem.manufacturer = *value;
1498 }
1499 }
1500 if (name == "Model")
1501 {
1502 const std::string* value =
1503 std::get_if<std::string>(&dbusValue);
1504 if (value != nullptr)
1505 {
1506 inventoryItem.model = *value;
1507 }
1508 }
1509 if (name == "SerialNumber")
1510 {
1511 const std::string* value =
1512 std::get_if<std::string>(&dbusValue);
1513 if (value != nullptr)
1514 {
1515 inventoryItem.serialNumber = *value;
1516 }
1517 }
1518 if (name == "PartNumber")
1519 {
1520 const std::string* value =
1521 std::get_if<std::string>(&dbusValue);
1522 if (value != nullptr)
1523 {
1524 inventoryItem.partNumber = *value;
1525 }
1526 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001527 }
1528 }
1529
Ed Tanous711ac7a2021-12-20 09:34:41 -08001530 if (interface ==
1531 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001532 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001533 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001535 if (name == "Functional")
1536 {
1537 const bool* value = std::get_if<bool>(&dbusValue);
1538 if (value != nullptr)
1539 {
1540 inventoryItem.isFunctional = *value;
1541 }
1542 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001543 }
1544 }
1545 }
1546}
1547
1548/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001549 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001550 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001551 * Uses the specified connections (services) to obtain D-Bus data for inventory
1552 * items associated with sensors. Stores the resulting data in the
1553 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001555 * This data is later used to provide sensor property values in the JSON
1556 * response.
1557 *
1558 * Finds the inventory item data asynchronously. Invokes callback when data has
1559 * been obtained.
1560 *
1561 * The callback must have the following signature:
1562 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001563 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001564 * @endcode
1565 *
1566 * This function is called recursively, obtaining data asynchronously from one
1567 * connection in each call. This ensures the callback is not invoked until the
1568 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001569 *
1570 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001571 * @param inventoryItems D-Bus inventory items associated with sensors.
1572 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001573 * @param objectMgrPaths Mappings from connection name to DBus object path that
1574 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001575 * @param callback Callback to invoke when inventory data has been obtained.
1576 * @param invConnectionsIndex Current index in invConnections. Only specified
1577 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579template <typename Callback>
1580static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001581 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001582 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001583 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1584 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001585 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001586 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001587{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001588 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001589
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001590 // If no more connections left, call callback
1591 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001592 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001593 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001594 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1595 return;
1596 }
1597
1598 // Get inventory item data from current connection
1599 auto it = invConnections->nth(invConnectionsIndex);
1600 if (it != invConnections->end())
1601 {
1602 const std::string& invConnection = *it;
1603
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001604 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001605 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001606 objectMgrPaths,
1607 callback{std::forward<Callback>(callback)},
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001608 invConnectionsIndex](
1609 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001610 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001611 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001612 if (ec)
1613 {
1614 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001615 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001616 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001617 return;
1618 }
1619
1620 // Loop through returned object paths
1621 for (const auto& objDictEntry : resp)
1622 {
1623 const std::string& objPath =
1624 static_cast<const std::string&>(objDictEntry.first);
1625
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001626 // If this object path is one of the specified inventory items
1627 InventoryItem* inventoryItem =
1628 findInventoryItem(inventoryItems, objPath);
1629 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001630 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001631 // Store inventory data in InventoryItem
1632 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633 }
1634 }
1635
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001636 // Recurse to get inventory item data from next connection
1637 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1638 invConnections, objectMgrPaths,
1639 std::move(callback), invConnectionsIndex + 1);
1640
1641 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 };
1643
1644 // Find DBus object path that implements ObjectManager for the current
1645 // connection. If no mapping found, default to "/".
1646 auto iter = objectMgrPaths->find(invConnection);
1647 const std::string& objectMgrPath =
1648 (iter != objectMgrPaths->end()) ? iter->second : "/";
1649 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1650 << objectMgrPath;
1651
1652 // Get all object paths and their interfaces for current connection
1653 crow::connections::systemBus->async_method_call(
1654 std::move(respHandler), invConnection, objectMgrPath,
1655 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1656 }
1657
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001658 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001659}
1660
1661/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001662 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001663 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001664 * Gets the D-Bus connections (services) that provide data for the inventory
1665 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001666 *
1667 * Finds the connections asynchronously. Invokes callback when information has
1668 * been obtained.
1669 *
1670 * The callback must have the following signature:
1671 * @code
1672 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1673 * invConnections)
1674 * @endcode
1675 *
1676 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001677 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001678 * @param callback Callback to invoke when connections have been obtained.
1679 */
1680template <typename Callback>
1681static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001682 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1683 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001684 Callback&& callback)
1685{
1686 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1687
1688 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001689 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001690 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001691 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1692 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001693 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1694
1695 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001696 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -08001697 sensorsAsyncResp, inventoryItems](
1698 const boost::system::error_code ec,
1699 const dbus::utility::MapperGetSubTreeResponse&
1700 subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001701 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1702 if (ec)
1703 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001704 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001705 BMCWEB_LOG_ERROR
1706 << "getInventoryItemsConnections respHandler DBus error " << ec;
1707 return;
1708 }
1709
1710 // Make unique list of connections for desired inventory items
1711 std::shared_ptr<boost::container::flat_set<std::string>>
1712 invConnections =
1713 std::make_shared<boost::container::flat_set<std::string>>();
1714 invConnections->reserve(8);
1715
1716 // Loop through objects from GetSubTree
1717 for (const std::pair<
1718 std::string,
1719 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1720 object : subtree)
1721 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001722 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001723 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001724 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001726 // Store all connections to inventory item
1727 for (const std::pair<std::string, std::vector<std::string>>&
1728 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001729 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001730 const std::string& invConnection = objData.first;
1731 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001732 }
1733 }
1734 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001735
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001736 callback(invConnections);
1737 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1738 };
1739
1740 // Make call to ObjectMapper to find all inventory items
1741 crow::connections::systemBus->async_method_call(
1742 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1743 "/xyz/openbmc_project/object_mapper",
1744 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1745 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1746}
1747
1748/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001749 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001750 *
1751 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001752 * inventory items. Then finds the associations from those inventory items to
1753 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001754 *
1755 * Finds the inventory items asynchronously. Invokes callback when information
1756 * has been obtained.
1757 *
1758 * The callback must have the following signature:
1759 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001760 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001761 * @endcode
1762 *
1763 * @param sensorsAsyncResp Pointer to object holding response data.
1764 * @param sensorNames All sensors within the current chassis.
1765 * @param objectMgrPaths Mappings from connection name to DBus object path that
1766 * implements ObjectManager.
1767 * @param callback Callback to invoke when inventory items have been obtained.
1768 */
1769template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001770static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001771 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1772 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1773 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001774 objectMgrPaths,
1775 Callback&& callback)
1776{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001777 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001778
1779 // Response handler for GetManagedObjects
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001780 auto respHandler = [callback{std::forward<Callback>(callback)},
1781 sensorsAsyncResp,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001782 sensorNames](const boost::system::error_code ec,
1783 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001784 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001785 if (ec)
1786 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001787 BMCWEB_LOG_ERROR
1788 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001789 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001790 return;
1791 }
1792
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001793 // Create vector to hold list of inventory items
1794 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1795 std::make_shared<std::vector<InventoryItem>>();
1796
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001797 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001798 std::string sensorAssocPath;
1799 sensorAssocPath.reserve(128); // avoid memory allocations
1800 for (const auto& objDictEntry : resp)
1801 {
1802 const std::string& objPath =
1803 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001804
1805 // If path is inventory association for one of the specified sensors
1806 for (const std::string& sensorName : *sensorNames)
1807 {
1808 sensorAssocPath = sensorName;
1809 sensorAssocPath += "/inventory";
1810 if (objPath == sensorAssocPath)
1811 {
1812 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001813 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001814 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001815 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001816 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001817 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001818 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001819 if (valueName == "endpoints")
1820 {
1821 const std::vector<std::string>* endpoints =
1822 std::get_if<std::vector<std::string>>(
1823 &value);
1824 if ((endpoints != nullptr) &&
1825 !endpoints->empty())
1826 {
1827 // Add inventory item to vector
1828 const std::string& invItemPath =
1829 endpoints->front();
1830 addInventoryItem(inventoryItems,
1831 invItemPath,
1832 sensorName);
1833 }
1834 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001835 }
1836 }
1837 }
1838 break;
1839 }
1840 }
1841 }
1842
Anthony Wilsond5005492019-07-31 16:34:17 -05001843 // Now loop through the returned object paths again, this time to
1844 // find the leds associated with the inventory items we just found
1845 std::string inventoryAssocPath;
1846 inventoryAssocPath.reserve(128); // avoid memory allocations
1847 for (const auto& objDictEntry : resp)
1848 {
1849 const std::string& objPath =
1850 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001851
1852 for (InventoryItem& inventoryItem : *inventoryItems)
1853 {
1854 inventoryAssocPath = inventoryItem.objectPath;
1855 inventoryAssocPath += "/leds";
1856 if (objPath == inventoryAssocPath)
1857 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001858 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001859 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001860 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001861 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001862 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001863 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001864 if (valueName == "endpoints")
1865 {
1866 const std::vector<std::string>* endpoints =
1867 std::get_if<std::vector<std::string>>(
1868 &value);
1869 if ((endpoints != nullptr) &&
1870 !endpoints->empty())
1871 {
1872 // Add inventory item to vector
1873 // Store LED path in inventory item
1874 const std::string& ledPath =
1875 endpoints->front();
1876 inventoryItem.ledObjectPath = ledPath;
1877 }
1878 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001879 }
1880 }
1881 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001882
Anthony Wilsond5005492019-07-31 16:34:17 -05001883 break;
1884 }
1885 }
1886 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001887 callback(inventoryItems);
1888 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001889 };
1890
1891 // Find DBus object path that implements ObjectManager for ObjectMapper
1892 std::string connection = "xyz.openbmc_project.ObjectMapper";
1893 auto iter = objectMgrPaths->find(connection);
1894 const std::string& objectMgrPath =
1895 (iter != objectMgrPaths->end()) ? iter->second : "/";
1896 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1897 << objectMgrPath;
1898
1899 // Call GetManagedObjects on the ObjectMapper to get all associations
1900 crow::connections::systemBus->async_method_call(
1901 std::move(respHandler), connection, objectMgrPath,
1902 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1903
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001904 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001905}
1906
1907/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001908 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1909 *
1910 * Uses the specified connections (services) to obtain D-Bus data for inventory
1911 * item leds associated with sensors. Stores the resulting data in the
1912 * inventoryItems vector.
1913 *
1914 * This data is later used to provide sensor property values in the JSON
1915 * response.
1916 *
1917 * Finds the inventory item led data asynchronously. Invokes callback when data
1918 * has been obtained.
1919 *
1920 * The callback must have the following signature:
1921 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001922 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001923 * @endcode
1924 *
1925 * This function is called recursively, obtaining data asynchronously from one
1926 * connection in each call. This ensures the callback is not invoked until the
1927 * last asynchronous function has completed.
1928 *
1929 * @param sensorsAsyncResp Pointer to object holding response data.
1930 * @param inventoryItems D-Bus inventory items associated with sensors.
1931 * @param ledConnections Connections that provide data for the inventory leds.
1932 * @param callback Callback to invoke when inventory data has been obtained.
1933 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1934 * in recursive calls to this function.
1935 */
1936template <typename Callback>
1937void getInventoryLedData(
1938 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1939 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1940 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1941 ledConnections,
1942 Callback&& callback, size_t ledConnectionsIndex = 0)
1943{
1944 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1945
1946 // If no more connections left, call callback
1947 if (ledConnectionsIndex >= ledConnections->size())
1948 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001949 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001950 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1951 return;
1952 }
1953
1954 // Get inventory item data from current connection
1955 auto it = ledConnections->nth(ledConnectionsIndex);
1956 if (it != ledConnections->end())
1957 {
1958 const std::string& ledPath = (*it).first;
1959 const std::string& ledConnection = (*it).second;
1960 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001961 auto respHandler =
1962 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001963 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001964 const boost::system::error_code ec, const std::string& state) {
1965 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1966 if (ec)
1967 {
1968 BMCWEB_LOG_ERROR
1969 << "getInventoryLedData respHandler DBus error " << ec;
1970 messages::internalError(sensorsAsyncResp->asyncResp->res);
1971 return;
1972 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001973
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001974 BMCWEB_LOG_DEBUG << "Led state: " << state;
Ed Tanous168e20c2021-12-13 14:39:53 -08001975 // Find inventory item with this LED object path
1976 InventoryItem* inventoryItem =
1977 findInventoryItemForLed(*inventoryItems, ledPath);
1978 if (inventoryItem != nullptr)
Anthony Wilsond5005492019-07-31 16:34:17 -05001979 {
Ed Tanous168e20c2021-12-13 14:39:53 -08001980 // Store LED state in InventoryItem
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001981 if (boost::ends_with(state, "On"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001982 {
Ed Tanous168e20c2021-12-13 14:39:53 -08001983 inventoryItem->ledState = LedState::ON;
1984 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001985 else if (boost::ends_with(state, "Blink"))
Ed Tanous168e20c2021-12-13 14:39:53 -08001986 {
1987 inventoryItem->ledState = LedState::BLINK;
1988 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001989 else if (boost::ends_with(state, "Off"))
Ed Tanous168e20c2021-12-13 14:39:53 -08001990 {
1991 inventoryItem->ledState = LedState::OFF;
1992 }
1993 else
1994 {
1995 inventoryItem->ledState = LedState::UNKNOWN;
Anthony Wilsond5005492019-07-31 16:34:17 -05001996 }
1997 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001998
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001999 // Recurse to get LED data from next connection
2000 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2001 ledConnections, std::move(callback),
2002 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05002003
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002004 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2005 };
Anthony Wilsond5005492019-07-31 16:34:17 -05002006
2007 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002008 sdbusplus::asio::getProperty<std::string>(
2009 *crow::connections::systemBus, ledConnection, ledPath,
2010 "xyz.openbmc_project.Led.Physical", "State",
2011 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05002012 }
2013
2014 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2015}
2016
2017/**
2018 * @brief Gets LED data for LEDs associated with given inventory items.
2019 *
2020 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2021 * associated with the specified inventory items. Then gets the LED data from
2022 * each connection and stores it in the inventory item.
2023 *
2024 * This data is later used to provide sensor property values in the JSON
2025 * response.
2026 *
2027 * Finds the LED data asynchronously. Invokes callback when information has
2028 * been obtained.
2029 *
2030 * The callback must have the following signature:
2031 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002032 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002033 * @endcode
2034 *
2035 * @param sensorsAsyncResp Pointer to object holding response data.
2036 * @param inventoryItems D-Bus inventory items associated with sensors.
2037 * @param callback Callback to invoke when inventory items have been obtained.
2038 */
2039template <typename Callback>
2040void getInventoryLeds(
2041 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2042 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2043 Callback&& callback)
2044{
2045 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2046
2047 const std::string path = "/xyz/openbmc_project";
2048 const std::array<std::string, 1> interfaces = {
2049 "xyz.openbmc_project.Led.Physical"};
2050
2051 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002052 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -08002053 sensorsAsyncResp, inventoryItems](
2054 const boost::system::error_code ec,
2055 const dbus::utility::MapperGetSubTreeResponse&
2056 subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05002057 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2058 if (ec)
2059 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002060 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002061 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2062 << ec;
2063 return;
2064 }
2065
2066 // Build map of LED object paths to connections
2067 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2068 ledConnections = std::make_shared<
2069 boost::container::flat_map<std::string, std::string>>();
2070
2071 // Loop through objects from GetSubTree
2072 for (const std::pair<
2073 std::string,
2074 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2075 object : subtree)
2076 {
2077 // Check if object path is LED for one of the specified inventory
2078 // items
2079 const std::string& ledPath = object.first;
2080 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2081 {
2082 // Add mapping from ledPath to connection
2083 const std::string& connection = object.second.begin()->first;
2084 (*ledConnections)[ledPath] = connection;
2085 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2086 << connection;
2087 }
2088 }
2089
2090 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2091 std::move(callback));
2092 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2093 };
2094 // Make call to ObjectMapper to find all inventory items
2095 crow::connections::systemBus->async_method_call(
2096 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2097 "/xyz/openbmc_project/object_mapper",
2098 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2099 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2100}
2101
2102/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002103 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2104 *
2105 * Uses the specified connections (services) (currently assumes just one) to
2106 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2107 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2108 *
2109 * This data is later used to provide sensor property values in the JSON
2110 * response.
2111 *
2112 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2113 * when data has been obtained.
2114 *
2115 * The callback must have the following signature:
2116 * @code
2117 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2118 * @endcode
2119 *
2120 * @param sensorsAsyncResp Pointer to object holding response data.
2121 * @param inventoryItems D-Bus inventory items associated with sensors.
2122 * @param psAttributesConnections Connections that provide data for the Power
2123 * Supply Attributes
2124 * @param callback Callback to invoke when data has been obtained.
2125 */
2126template <typename Callback>
2127void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002128 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002129 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2130 const boost::container::flat_map<std::string, std::string>&
2131 psAttributesConnections,
2132 Callback&& callback)
2133{
2134 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2135
2136 if (psAttributesConnections.empty())
2137 {
2138 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2139 callback(inventoryItems);
2140 return;
2141 }
2142
2143 // Assuming just one connection (service) for now
2144 auto it = psAttributesConnections.nth(0);
2145
2146 const std::string& psAttributesPath = (*it).first;
2147 const std::string& psAttributesConnection = (*it).second;
2148
2149 // Response handler for Get DeratingFactor property
2150 auto respHandler = [sensorsAsyncResp, inventoryItems,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002151 callback{std::forward<Callback>(callback)}](
Gunnar Mills42cbe532019-08-15 15:26:54 -05002152 const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002153 const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002154 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2155 if (ec)
2156 {
2157 BMCWEB_LOG_ERROR
2158 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002159 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002160 return;
2161 }
2162
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002163 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2164 // Store value in Power Supply Inventory Items
2165 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002166 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002167 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002168 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002169 inventoryItem.powerSupplyEfficiencyPercent =
2170 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002171 }
2172 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002173
2174 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2175 callback(inventoryItems);
2176 };
2177
2178 // Get the DeratingFactor property for the PowerSupplyAttributes
2179 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002180 sdbusplus::asio::getProperty<uint32_t>(
2181 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2182 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2183 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002184
2185 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2186}
2187
2188/**
2189 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2190 *
2191 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2192 * data. Then gets the Power Supply Attributes data from the connection
2193 * (currently just assumes 1 connection) and stores the data in the inventory
2194 * item.
2195 *
2196 * This data is later used to provide sensor property values in the JSON
2197 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2198 *
2199 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2200 * when information has been obtained.
2201 *
2202 * The callback must have the following signature:
2203 * @code
2204 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2205 * @endcode
2206 *
2207 * @param sensorsAsyncResp Pointer to object holding response data.
2208 * @param inventoryItems D-Bus inventory items associated with sensors.
2209 * @param callback Callback to invoke when data has been obtained.
2210 */
2211template <typename Callback>
2212void getPowerSupplyAttributes(
2213 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2214 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2215 Callback&& callback)
2216{
2217 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2218
2219 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002220 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002221 {
2222 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2223 callback(inventoryItems);
2224 return;
2225 }
2226
2227 const std::array<std::string, 1> interfaces = {
2228 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2229
2230 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002231 auto respHandler =
2232 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2233 inventoryItems](
2234 const boost::system::error_code ec,
2235 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2236 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2237 if (ec)
2238 {
2239 messages::internalError(sensorsAsyncResp->asyncResp->res);
2240 BMCWEB_LOG_ERROR
2241 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2242 return;
2243 }
2244 if (subtree.empty())
2245 {
2246 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2247 callback(inventoryItems);
2248 return;
2249 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002250
Ed Tanousb9d36b42022-02-26 21:42:46 -08002251 // Currently we only support 1 power supply attribute, use this for
2252 // all the power supplies. Build map of object path to connection.
2253 // Assume just 1 connection and 1 path for now.
2254 boost::container::flat_map<std::string, std::string>
2255 psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002256
Ed Tanousb9d36b42022-02-26 21:42:46 -08002257 if (subtree[0].first.empty() || subtree[0].second.empty())
2258 {
2259 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2260 callback(inventoryItems);
2261 return;
2262 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002263
Ed Tanousb9d36b42022-02-26 21:42:46 -08002264 const std::string& psAttributesPath = subtree[0].first;
2265 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002266
Ed Tanousb9d36b42022-02-26 21:42:46 -08002267 if (connection.empty())
2268 {
2269 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2270 callback(inventoryItems);
2271 return;
2272 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002273
Ed Tanousb9d36b42022-02-26 21:42:46 -08002274 psAttributesConnections[psAttributesPath] = connection;
2275 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2276 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002277
Ed Tanousb9d36b42022-02-26 21:42:46 -08002278 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2279 psAttributesConnections,
2280 std::move(callback));
2281 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2282 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002283 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2284 crow::connections::systemBus->async_method_call(
2285 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2286 "/xyz/openbmc_project/object_mapper",
2287 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2288 "/xyz/openbmc_project", 0, interfaces);
2289 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2290}
2291
2292/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002293 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002294 *
2295 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002296 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002297 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002298 * This data is later used to provide sensor property values in the JSON
2299 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002300 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002301 * Finds the inventory items asynchronously. Invokes callback when the
2302 * inventory items have been obtained.
2303 *
2304 * The callback must have the following signature:
2305 * @code
2306 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2307 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002308 *
2309 * @param sensorsAsyncResp Pointer to object holding response data.
2310 * @param sensorNames All sensors within the current chassis.
2311 * @param objectMgrPaths Mappings from connection name to DBus object path that
2312 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002313 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002314 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002315template <typename Callback>
2316static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002317 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2318 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2319 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002320 objectMgrPaths,
2321 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002322{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002323 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2324 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002325 [sensorsAsyncResp, objectMgrPaths,
2326 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002327 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2328 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002329 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002330 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002331 callback{std::forward<const Callback>(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002332 std::shared_ptr<boost::container::flat_set<std::string>>
2333 invConnections) {
2334 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002335 auto getInventoryItemsDataCb =
2336 [sensorsAsyncResp, inventoryItems,
2337 callback{std::move(callback)}]() {
2338 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002339
2340 auto getInventoryLedsCb = [sensorsAsyncResp,
2341 inventoryItems,
2342 callback{std::move(
2343 callback)}]() {
2344 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2345 // Find Power Supply Attributes and get the data
2346 getPowerSupplyAttributes(sensorsAsyncResp,
2347 inventoryItems,
2348 std::move(callback));
2349 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2350 };
2351
Anthony Wilsond5005492019-07-31 16:34:17 -05002352 // Find led connections and get the data
2353 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002354 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002355 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2356 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002357
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002358 // Get inventory item data from connections
2359 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2360 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002361 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002362 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2363 };
2364
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002365 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002366 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002367 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002368 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002369 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002370 };
2371
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002372 // Get associations from sensors to inventory items
2373 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2374 std::move(getInventoryItemAssociationsCb));
2375 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2376}
2377
2378/**
2379 * @brief Returns JSON PowerSupply object for the specified inventory item.
2380 *
2381 * Searches for a JSON PowerSupply object that matches the specified inventory
2382 * item. If one is not found, a new PowerSupply object is added to the JSON
2383 * array.
2384 *
2385 * Multiple sensors are often associated with one power supply inventory item.
2386 * As a result, multiple sensor values are stored in one JSON PowerSupply
2387 * object.
2388 *
2389 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2390 * @param inventoryItem Inventory item for the power supply.
2391 * @param chassisId Chassis that contains the power supply.
2392 * @return JSON PowerSupply object for the specified inventory item.
2393 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002394inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002395 const InventoryItem& inventoryItem,
2396 const std::string& chassisId)
2397{
2398 // Check if matching PowerSupply object already exists in JSON array
2399 for (nlohmann::json& powerSupply : powerSupplyArray)
2400 {
2401 if (powerSupply["MemberId"] == inventoryItem.name)
2402 {
2403 return powerSupply;
2404 }
2405 }
2406
2407 // Add new PowerSupply object to JSON array
2408 powerSupplyArray.push_back({});
2409 nlohmann::json& powerSupply = powerSupplyArray.back();
2410 powerSupply["@odata.id"] =
2411 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2412 powerSupply["MemberId"] = inventoryItem.name;
2413 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2414 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2415 powerSupply["Model"] = inventoryItem.model;
2416 powerSupply["PartNumber"] = inventoryItem.partNumber;
2417 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002418 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002419
Gunnar Mills42cbe532019-08-15 15:26:54 -05002420 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2421 {
2422 powerSupply["EfficiencyPercent"] =
2423 inventoryItem.powerSupplyEfficiencyPercent;
2424 }
2425
2426 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002427 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2428 powerSupply["Status"]["Health"] = health;
2429
2430 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002431}
2432
2433/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002434 * @brief Gets the values of the specified sensors.
2435 *
2436 * Stores the results as JSON in the SensorsAsyncResp.
2437 *
2438 * Gets the sensor values asynchronously. Stores the results later when the
2439 * information has been obtained.
2440 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002441 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002442 *
2443 * To minimize the number of DBus calls, the DBus method
2444 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2445 * values of all sensors provided by a connection (service).
2446 *
2447 * The connections set contains all the connections that provide sensor values.
2448 *
2449 * The objectMgrPaths map contains mappings from a connection name to the
2450 * corresponding DBus object path that implements ObjectManager.
2451 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002452 * The InventoryItem vector contains D-Bus inventory items associated with the
2453 * sensors. Inventory item data is needed for some Redfish sensor properties.
2454 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002455 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002456 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002457 * @param connections Connections that provide sensor values.
2458 * @param objectMgrPaths Mappings from connection name to DBus object path that
2459 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002460 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002461 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002462inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002463 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002464 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002465 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002466 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002467 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002468 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002469{
2470 BMCWEB_LOG_DEBUG << "getSensorData enter";
2471 // Get managed objects from all services exposing sensors
2472 for (const std::string& connection : connections)
2473 {
2474 // Response handler to process managed objects
Ed Tanous81ce6092020-12-17 16:54:55 +00002475 auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002476 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002477 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08002478 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002479 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2480 if (ec)
2481 {
2482 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002483 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002484 return;
2485 }
2486 // Go through all objects and update response with sensor data
2487 for (const auto& objDictEntry : resp)
2488 {
2489 const std::string& objPath =
2490 static_cast<const std::string&>(objDictEntry.first);
2491 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2492 << objPath;
2493
Shawn McCarneyde629b62019-03-08 10:42:51 -06002494 std::vector<std::string> split;
2495 // Reserve space for
2496 // /xyz/openbmc_project/sensors/<name>/<subname>
2497 split.reserve(6);
2498 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2499 if (split.size() < 6)
2500 {
2501 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2502 << objPath;
2503 continue;
2504 }
2505 // These indexes aren't intuitive, as boost::split puts an empty
2506 // string at the beginning
2507 const std::string& sensorType = split[4];
2508 const std::string& sensorName = split[5];
2509 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2510 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002511 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002512 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002513 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002514 continue;
2515 }
2516
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002517 // Find inventory item (if any) associated with sensor
2518 InventoryItem* inventoryItem =
2519 findInventoryItemForSensor(inventoryItems, objPath);
2520
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002521 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002522 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002523
2524 nlohmann::json* sensorJson = nullptr;
2525
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002526 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002527 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002528 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002529 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2530 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002531 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002532 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002533 }
2534 else
2535 {
Ed Tanous271584a2019-07-09 16:24:22 -07002536 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002537 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002538 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002539 fieldName = "Temperatures";
2540 }
2541 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2542 sensorType == "fan_pwm")
2543 {
2544 fieldName = "Fans";
2545 }
2546 else if (sensorType == "voltage")
2547 {
2548 fieldName = "Voltages";
2549 }
2550 else if (sensorType == "power")
2551 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002552 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002553 {
2554 fieldName = "PowerControl";
2555 }
2556 else if ((inventoryItem != nullptr) &&
2557 (inventoryItem->isPowerSupply))
2558 {
2559 fieldName = "PowerSupplies";
2560 }
2561 else
2562 {
2563 // Other power sensors are in SensorCollection
2564 continue;
2565 }
2566 }
2567 else
2568 {
2569 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2570 << sensorType;
2571 continue;
2572 }
2573
2574 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002575 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002576 if (fieldName == "PowerControl")
2577 {
2578 if (tempArray.empty())
2579 {
2580 // Put multiple "sensors" into a single
2581 // PowerControl. Follows MemberId naming and
2582 // naming in power.hpp.
2583 tempArray.push_back(
2584 {{"@odata.id",
2585 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002586 sensorsAsyncResp->chassisId + "/" +
2587 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002588 fieldName + "/0"}});
2589 }
2590 sensorJson = &(tempArray.back());
2591 }
2592 else if (fieldName == "PowerSupplies")
2593 {
2594 if (inventoryItem != nullptr)
2595 {
2596 sensorJson =
2597 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002598 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002599 }
2600 }
2601 else
2602 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002603 tempArray.push_back(
2604 {{"@odata.id",
2605 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002606 sensorsAsyncResp->chassisId + "/" +
2607 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002608 fieldName + "/"}});
2609 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002610 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002611 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002612
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002613 if (sensorJson != nullptr)
2614 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002615 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002616 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002617 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002618 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002619 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002620 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002621 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002622 sortJSONResponse(sensorsAsyncResp);
2623 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002624 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002625 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002626 }
James Feist8bd25cc2019-03-15 15:14:00 -07002627 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002628 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2629 };
2630
2631 // Find DBus object path that implements ObjectManager for the current
2632 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002633 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002634 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002635 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002636 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2637 << objectMgrPath;
2638
2639 crow::connections::systemBus->async_method_call(
2640 getManagedObjectsCb, connection, objectMgrPath,
2641 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002642 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002643 BMCWEB_LOG_DEBUG << "getSensorData exit";
2644}
2645
Ed Tanous23a21a12020-07-25 04:45:05 +00002646inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002647 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002648 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002649{
2650 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002651 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002652 const boost::container::flat_set<std::string>& connections) {
2653 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2654 auto getObjectManagerPathsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002655 [sensorsAsyncResp, sensorNames,
Ed Tanousb5a76932020-09-29 16:16:58 -07002656 connections](const std::shared_ptr<boost::container::flat_map<
2657 std::string, std::string>>& objectMgrPaths) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002658 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2659 auto getInventoryItemsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002660 [sensorsAsyncResp, sensorNames, connections,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002661 objectMgrPaths](
Ed Tanousf23b7292020-10-15 09:41:17 -07002662 const std::shared_ptr<std::vector<InventoryItem>>&
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002663 inventoryItems) {
2664 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2665 // Get sensor data and store results in JSON
Ed Tanous81ce6092020-12-17 16:54:55 +00002666 getSensorData(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002667 connections, objectMgrPaths,
Ed Tanousf23b7292020-10-15 09:41:17 -07002668 inventoryItems);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002669 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2670 };
2671
2672 // Get inventory items associated with sensors
Ed Tanous81ce6092020-12-17 16:54:55 +00002673 getInventoryItems(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002674 objectMgrPaths,
2675 std::move(getInventoryItemsCb));
2676
2677 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2678 };
2679
2680 // Get mapping from connection names to the DBus object
2681 // paths that implement the ObjectManager interface
Ed Tanous81ce6092020-12-17 16:54:55 +00002682 getObjectManagerPaths(sensorsAsyncResp,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002683 std::move(getObjectManagerPathsCb));
2684 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2685 };
2686
2687 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002688 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002689}
2690
Shawn McCarneyde629b62019-03-08 10:42:51 -06002691/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002692 * @brief Entry point for retrieving sensors data related to requested
2693 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002694 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002695 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002696inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002697 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002698{
2699 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002700 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002701 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002702 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002703 sensorNames) {
2704 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Ed Tanous81ce6092020-12-17 16:54:55 +00002705 processSensorList(sensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002706 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2707 };
zhanghch058d1b46d2021-04-01 11:18:24 +08002708 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2709 nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002710
Shawn McCarney26f03892019-05-03 13:20:24 -05002711 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002712 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002713 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002714}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002715
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302716/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002717 * @brief Find the requested sensorName in the list of all sensors supplied by
2718 * the chassis node
2719 *
2720 * @param sensorName The sensor name supplied in the PATCH request
2721 * @param sensorsList The list of sensors managed by the chassis node
2722 * @param sensorsModified The list of sensors that were found as a result of
2723 * repeated calls to this function
2724 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002725inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302726 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002727 boost::container::flat_set<std::string>& sensorsList,
2728 boost::container::flat_set<std::string>& sensorsModified)
2729{
George Liu28aa8de2021-02-01 15:13:30 +08002730 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002731 {
George Liu28aa8de2021-02-01 15:13:30 +08002732 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002733 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002734 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002735 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002736 continue;
2737 }
2738 if (thisSensorName == sensorName)
2739 {
2740 sensorsModified.emplace(chassisSensor);
2741 return true;
2742 }
2743 }
2744 return false;
2745}
2746
2747/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302748 * @brief Entry point for overriding sensor values of given sensor
2749 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002750 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002751 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002752 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302753 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002754inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002755 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002756 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002757 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302758{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002759 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002760 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302761
Ed Tanous543f4402022-01-06 13:12:53 -08002762 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302763 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302764 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002765 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302766 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302767 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302768 if (collectionItems.first == "Temperatures")
2769 {
2770 propertyValueName = "ReadingCelsius";
2771 }
2772 else if (collectionItems.first == "Fans")
2773 {
2774 propertyValueName = "Reading";
2775 }
2776 else
2777 {
2778 propertyValueName = "ReadingVolts";
2779 }
2780 for (auto& item : collectionItems.second)
2781 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002782 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2783 "MemberId", memberId, propertyValueName,
2784 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302785 {
2786 return;
2787 }
2788 overrideMap.emplace(memberId,
2789 std::make_pair(value, collectionItems.first));
2790 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302791 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002792
Ed Tanousb5a76932020-09-29 16:16:58 -07002793 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2794 const std::shared_ptr<
2795 boost::container::flat_set<
2796 std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002797 // Match sensor names in the PATCH request to those managed by the
2798 // chassis node
2799 const std::shared_ptr<boost::container::flat_set<std::string>>
2800 sensorNames =
2801 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302802 for (const auto& item : overrideMap)
2803 {
2804 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002805 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2806 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302807 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302808 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002809 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302810 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302811 return;
2812 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302813 }
2814 // Get the connection to which the memberId belongs
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002815 auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
2816 const boost::container::flat_set<
2817 std::string>& /*connections*/,
2818 const std::set<std::pair<
2819 std::string, std::string>>&
2820 objectsWithConnection) {
2821 if (objectsWithConnection.size() != overrideMap.size())
2822 {
2823 BMCWEB_LOG_INFO
2824 << "Unable to find all objects with proper connection "
2825 << objectsWithConnection.size() << " requested "
2826 << overrideMap.size() << "\n";
2827 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2828 sensorAsyncResp->chassisSubNode ==
2829 sensors::node::thermal
2830 ? "Temperatures"
2831 : "Voltages",
2832 "Count");
2833 return;
2834 }
2835 for (const auto& item : objectsWithConnection)
2836 {
2837 sdbusplus::message::object_path path(item.first);
2838 std::string sensorName = path.filename();
2839 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302840 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002841 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302842 return;
2843 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302844
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002845 const auto& iterator = overrideMap.find(sensorName);
2846 if (iterator == overrideMap.end())
2847 {
2848 BMCWEB_LOG_INFO << "Unable to find sensor object"
2849 << item.first << "\n";
2850 messages::internalError(sensorAsyncResp->asyncResp->res);
2851 return;
2852 }
2853 crow::connections::systemBus->async_method_call(
2854 [sensorAsyncResp](const boost::system::error_code ec) {
2855 if (ec)
2856 {
2857 if (ec.value() ==
2858 boost::system::errc::permission_denied)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302859 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002860 BMCWEB_LOG_WARNING
2861 << "Manufacturing mode is not Enabled...can't "
2862 "Override the sensor value. ";
2863
2864 messages::insufficientPrivilege(
zhanghch058d1b46d2021-04-01 11:18:24 +08002865 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302866 return;
2867 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002868 BMCWEB_LOG_DEBUG
2869 << "setOverrideValueStatus DBUS error: " << ec;
2870 messages::internalError(
2871 sensorAsyncResp->asyncResp->res);
2872 }
2873 },
2874 item.second, item.first, "org.freedesktop.DBus.Properties",
2875 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002876 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002877 }
2878 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302879 // Get object with connection for the given sensor name
2880 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2881 std::move(getObjectsWithConnectionCb));
2882 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302883 // get full sensor list for the given chassisId and cross verify the sensor.
2884 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2885}
2886
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002887/**
2888 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2889 * path of the sensor.
2890 *
2891 * Function builds valid Redfish response for sensor query of given chassis and
2892 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2893 * it to caller in a callback.
2894 *
2895 * @param chassis Chassis for which retrieval should be performed
2896 * @param node Node (group) of sensors. See sensors::node for supported values
2897 * @param mapComplete Callback to be called with retrieval result
2898 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002899inline void retrieveUriToDbusMap(const std::string& chassis,
2900 const std::string& node,
2901 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002902{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +00002903 auto pathIt = sensors::dbus::paths.find(node);
2904 if (pathIt == sensors::dbus::paths.end())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002905 {
2906 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2907 mapComplete(boost::beast::http::status::bad_request, {});
2908 return;
2909 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002910
Nan Zhou72374eb2022-01-27 17:06:51 -08002911 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002912 auto callback =
Nan Zhou72374eb2022-01-27 17:06:51 -08002913 [asyncResp, mapCompleteCb{std::move(mapComplete)}](
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002914 const boost::beast::http::status status,
2915 const boost::container::flat_map<std::string, std::string>&
2916 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2917
2918 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002919 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002920 getChassisData(resp);
2921}
2922
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002923inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002924{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002925 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002926 .privileges(redfish::privileges::getSensorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002927 .methods(
2928 boost::beast::http::verb::get)([](const crow::Request&,
2929 const std::shared_ptr<
2930 bmcweb::AsyncResp>& aResp,
2931 const std::string& chassisId) {
2932 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002933
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002934 std::shared_ptr<SensorsAsyncResp> asyncResp =
2935 std::make_shared<SensorsAsyncResp>(
2936 aResp, chassisId,
2937 sensors::dbus::paths.at(sensors::node::sensors),
2938 sensors::node::sensors);
zhanghch058d1b46d2021-04-01 11:18:24 +08002939
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002940 auto getChassisCb =
2941 [asyncResp](
2942 const std::shared_ptr<
2943 boost::container::flat_set<std::string>>& sensorNames) {
2944 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002945
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002946 nlohmann::json& entriesArray =
2947 asyncResp->asyncResp->res.jsonValue["Members"];
2948 for (auto& sensor : *sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002949 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002950 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002951
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002952 sdbusplus::message::object_path path(sensor);
2953 std::string sensorName = path.filename();
2954 if (sensorName.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002955 {
2956 BMCWEB_LOG_ERROR << "Invalid sensor path: "
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002957 << sensor;
2958 messages::internalError(asyncResp->asyncResp->res);
2959 return;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002960 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002961 entriesArray.push_back(
2962 {{"@odata.id", "/redfish/v1/Chassis/" +
2963 asyncResp->chassisId + "/" +
2964 asyncResp->chassisSubNode + "/" +
2965 sensorName}});
2966 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002967
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002968 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2969 entriesArray.size();
2970 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2971 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002972
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002973 // Get set of sensors in chassis
2974 getChassis(asyncResp, std::move(getChassisCb));
2975 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
2976 });
2977}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002978
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002979inline void requestRoutesSensor(App& app)
2980{
2981 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002982 .privileges(redfish::privileges::getSensor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002983 .methods(
2984 boost::beast::http::verb::get)([](const crow::Request&,
2985 const std::shared_ptr<
2986 bmcweb::AsyncResp>& aResp,
2987 const std::string& chassisId,
2988 const std::string& sensorName) {
2989 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2990 std::shared_ptr<SensorsAsyncResp> asyncResp =
2991 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
2992 std::vector<const char*>(),
2993 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002994
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002995 const std::array<const char*, 1> interfaces = {
2996 "xyz.openbmc_project.Sensor.Value"};
2997
2998 // Get a list of all of the sensors that implement Sensor.Value
2999 // and get the path and service name associated with the sensor
3000 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08003001 [asyncResp, sensorName](
3002 const boost::system::error_code ec,
3003 const dbus::utility::MapperGetSubTreeResponse& subtree) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003004 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3005 if (ec)
3006 {
3007 messages::internalError(asyncResp->asyncResp->res);
3008 BMCWEB_LOG_ERROR
3009 << "Sensor getSensorPaths resp_handler: "
3010 << "Dbus error " << ec;
3011 return;
3012 }
3013
Ed Tanousb9d36b42022-02-26 21:42:46 -08003014 dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3015 std::find_if(
3016 subtree.begin(), subtree.end(),
3017 [sensorName](
3018 const std::pair<std::string,
3019 std::vector<std::pair<
3020 std::string,
3021 std::vector<std::string>>>>&
3022 object) {
3023 sdbusplus::message::object_path path(
3024 object.first);
3025 std::string name = path.filename();
3026 if (name.empty())
3027 {
3028 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3029 << object.first;
3030 return false;
3031 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003032
Ed Tanousb9d36b42022-02-26 21:42:46 -08003033 return name == sensorName;
3034 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003035
3036 if (it == subtree.end())
3037 {
3038 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3039 << sensorName;
3040 messages::resourceNotFound(asyncResp->asyncResp->res,
3041 "Sensor", sensorName);
3042 return;
3043 }
3044 std::string_view sensorPath = (*it).first;
3045 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3046 << sensorName << "': " << sensorPath;
3047
3048 const std::shared_ptr<
3049 boost::container::flat_set<std::string>>
3050 sensorList = std::make_shared<
3051 boost::container::flat_set<std::string>>();
3052
3053 sensorList->emplace(sensorPath);
3054 processSensorList(asyncResp, sensorList);
3055 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3056 },
3057 "xyz.openbmc_project.ObjectMapper",
3058 "/xyz/openbmc_project/object_mapper",
3059 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3060 "/xyz/openbmc_project/sensors", 2, interfaces);
3061 });
3062}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003063
Ed Tanous1abe55e2018-09-05 08:30:59 -07003064} // namespace redfish