blob: e1e25d576e3983f5256f91c671c22ee74acd3637 [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 Tanous45ca1b82022-03-25 13:07:27 -070025#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070026#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070027#include <sdbusplus/asio/property.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053028#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070029#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030
31#include <cmath>
Ed Tanousb5a76932020-09-29 16:16:58 -070032#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080033#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010034
Ed Tanous1abe55e2018-09-05 08:30:59 -070035namespace redfish
36{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010037
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020038namespace sensors
39{
40namespace node
41{
42static constexpr std::string_view power = "Power";
43static constexpr std::string_view sensors = "Sensors";
44static constexpr std::string_view thermal = "Thermal";
45} // namespace node
46
Ed Tanous02da7c52022-02-27 00:09:02 -080047// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020048namespace dbus
49{
Ed Tanous02da7c52022-02-27 00:09:02 -080050auto powerPaths = std::to_array<std::string_view>({
51 "/xyz/openbmc_project/sensors/voltage",
52 "/xyz/openbmc_project/sensors/power"
53});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000054
Ed Tanous02da7c52022-02-27 00:09:02 -080055auto sensorPaths = std::to_array<std::string_view>({
56 "/xyz/openbmc_project/sensors/power",
57 "/xyz/openbmc_project/sensors/current",
58 "/xyz/openbmc_project/sensors/airflow",
Gunnar Mills5deabed2022-04-20 13:43:45 -060059 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080060#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080061 "/xyz/openbmc_project/sensors/voltage",
62 "/xyz/openbmc_project/sensors/fan_tach",
63 "/xyz/openbmc_project/sensors/temperature",
64 "/xyz/openbmc_project/sensors/fan_pwm",
65 "/xyz/openbmc_project/sensors/altitude",
66 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080067#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080068 "/xyz/openbmc_project/sensors/utilization"
69});
70
71auto thermalPaths = std::to_array<std::string_view>({
72 "/xyz/openbmc_project/sensors/fan_tach",
73 "/xyz/openbmc_project/sensors/temperature",
74 "/xyz/openbmc_project/sensors/fan_pwm"
75});
76
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000077} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080078// clang-format on
79
80using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
81static constexpr std::array<sensorPair, 3> paths = {
82 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
83 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
84 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000085
86inline const char* toReadingType(const std::string& sensorType)
87{
88 if (sensorType == "voltage")
89 {
90 return "Voltage";
91 }
92 if (sensorType == "power")
93 {
94 return "Power";
95 }
96 if (sensorType == "current")
97 {
98 return "Current";
99 }
100 if (sensorType == "fan_tach")
101 {
102 return "Rotational";
103 }
104 if (sensorType == "temperature")
105 {
106 return "Temperature";
107 }
108 if (sensorType == "fan_pwm" || sensorType == "utilization")
109 {
110 return "Percent";
111 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600112 if (sensorType == "humidity")
113 {
114 return "Humidity";
115 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000116 if (sensorType == "altitude")
117 {
118 return "Altitude";
119 }
120 if (sensorType == "airflow")
121 {
122 return "AirFlow";
123 }
124 if (sensorType == "energy")
125 {
126 return "EnergyJoules";
127 }
128 return "";
129}
130
131inline const char* toReadingUnits(const std::string& sensorType)
132{
133 if (sensorType == "voltage")
134 {
135 return "V";
136 }
137 if (sensorType == "power")
138 {
139 return "W";
140 }
141 if (sensorType == "current")
142 {
143 return "A";
144 }
145 if (sensorType == "fan_tach")
146 {
147 return "RPM";
148 }
149 if (sensorType == "temperature")
150 {
151 return "Cel";
152 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600153 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
154 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000155 {
156 return "%";
157 }
158 if (sensorType == "altitude")
159 {
160 return "m";
161 }
162 if (sensorType == "airflow")
163 {
164 return "cft_i/min";
165 }
166 if (sensorType == "energy")
167 {
168 return "J";
169 }
170 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200171}
172} // namespace sensors
173
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100174/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200175 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100176 * Gathers data needed for response processing after async calls are done
177 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178class SensorsAsyncResp
179{
180 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200181 using DataCompleteCb = std::function<void(
182 const boost::beast::http::status status,
183 const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
184
185 struct SensorData
186 {
187 const std::string name;
188 std::string uri;
189 const std::string valueKey;
190 const std::string dbusPath;
191 };
192
zhanghch058d1b46d2021-04-01 11:18:24 +0800193 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800195 std::span<std::string_view> typesIn,
196 std::string_view subNode) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800197 asyncResp(asyncResp),
Nan Zhou928fefb2022-03-28 08:45:00 -0700198 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
199 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500200 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200201
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200202 // Store extra data about sensor mapping and return it in callback
zhanghch058d1b46d2021-04-01 11:18:24 +0800203 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
204 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800205 std::span<std::string_view> typesIn,
206 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200207 DataCompleteCb&& creationComplete) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800208 asyncResp(asyncResp),
Nan Zhou928fefb2022-03-28 08:45:00 -0700209 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
210 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200211 dataComplete{std::move(creationComplete)}
212 {}
213
Nan Zhou928fefb2022-03-28 08:45:00 -0700214 // sensor collections expand
215 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
216 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800217 const std::span<std::string_view> typesIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700218 const std::string_view& subNode, bool efficientExpand) :
219 asyncResp(asyncResp),
220 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
221 efficientExpand(efficientExpand)
222 {}
223
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 ~SensorsAsyncResp()
225 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800226 if (asyncResp->res.result() ==
227 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 {
229 // Reset the json object to clear out any data that made it in
230 // before the error happened todo(ed) handle error condition with
231 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800232 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700233 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200234
235 if (dataComplete && metadata)
236 {
237 boost::container::flat_map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800238 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200239 {
240 for (auto& sensor : *metadata)
241 {
242 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
243 sensor.dbusPath));
244 }
245 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800246 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200247 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100249
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800250 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
251 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
252 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
253 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
254
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200255 void addMetadata(const nlohmann::json& sensorObject,
256 const std::string& valueKey, const std::string& dbusPath)
257 {
258 if (metadata)
259 {
260 metadata->emplace_back(SensorData{sensorObject["Name"],
261 sensorObject["@odata.id"],
262 valueKey, dbusPath});
263 }
264 }
265
266 void updateUri(const std::string& name, const std::string& uri)
267 {
268 if (metadata)
269 {
270 for (auto& sensor : *metadata)
271 {
272 if (sensor.name == name)
273 {
274 sensor.uri = uri;
275 }
276 }
277 }
278 }
279
zhanghch058d1b46d2021-04-01 11:18:24 +0800280 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200281 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800282 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200283 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700284 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200285
286 private:
287 std::optional<std::vector<SensorData>> metadata;
288 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100289};
290
291/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500292 * Possible states for physical inventory leds
293 */
294enum class LedState
295{
296 OFF,
297 ON,
298 BLINK,
299 UNKNOWN
300};
301
302/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500303 * D-Bus inventory item associated with one or more sensors.
304 */
305class InventoryItem
306{
307 public:
Ed Tanouse05aec52022-01-25 10:28:56 -0800308 InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500309 {
310 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800311 sdbusplus::message::object_path path(objectPath);
312 name = path.filename();
313 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500314 {
George Liu28aa8de2021-02-01 15:13:30 +0800315 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500316 }
317 }
318
319 std::string objectPath;
320 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800321 bool isPresent = true;
322 bool isFunctional = true;
323 bool isPowerSupply = false;
324 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500325 std::string manufacturer;
326 std::string model;
327 std::string partNumber;
328 std::string serialNumber;
329 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500330 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800331 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500332};
333
334/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530335 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200336 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100337 * @param sensorNames Sensors retrieved from chassis
338 * @param callback Callback for processing gathered connections
339 */
340template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530341void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000342 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700343 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530344 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700345{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530346 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700347 const std::string path = "/xyz/openbmc_project/sensors";
348 const std::array<std::string, 1> interfaces = {
349 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100350
Ed Tanous1abe55e2018-09-05 08:30:59 -0700351 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700352 auto respHandler =
353 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
354 sensorNames](const boost::system::error_code ec,
355 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530356 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700357 if (ec)
358 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800359 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530360 BMCWEB_LOG_ERROR
361 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700362 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100363 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100364
Ed Tanous1abe55e2018-09-05 08:30:59 -0700365 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
366
367 // Make unique list of connections only for requested sensor types and
368 // found in the chassis
369 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530370 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
372 // producers
373 connections.reserve(8);
374
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700375 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
376 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377 {
378 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
379 }
380
381 for (const std::pair<
382 std::string,
383 std::vector<std::pair<std::string, std::vector<std::string>>>>&
384 object : subtree)
385 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700386 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700387 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700388 for (const std::pair<std::string, std::vector<std::string>>&
389 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700391 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
392 connections.insert(objData.first);
393 objectsWithConnection.insert(
394 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700395 }
396 }
397 }
398 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530399 callback(std::move(connections), std::move(objectsWithConnection));
400 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 // Make call to ObjectMapper to find all sensors objects
403 crow::connections::systemBus->async_method_call(
404 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
405 "/xyz/openbmc_project/object_mapper",
406 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530407 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
408}
409
410/**
411 * @brief Create connections necessary for sensors
412 * @param SensorsAsyncResp Pointer to object holding response data
413 * @param sensorNames Sensors retrieved from chassis
414 * @param callback Callback for processing gathered connections
415 */
416template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700417void getConnections(
Ed Tanous81ce6092020-12-17 16:54:55 +0000418 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700419 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
420 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530421{
422 auto objectsWithConnectionCb =
423 [callback](const boost::container::flat_set<std::string>& connections,
424 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700425 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000426 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530427 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100428}
429
430/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700431 * @brief Shrinks the list of sensors for processing
432 * @param SensorsAysncResp The class holding the Redfish response
433 * @param allSensors A list of all the sensors associated to the
434 * chassis element (i.e. baseboard, front panel, etc...)
435 * @param activeSensors A list that is a reduction of the incoming
436 * allSensors list. Eliminate Thermal sensors when a Power request is
437 * made, and eliminate Power sensors when a Thermal request is made.
438 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000439inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000440 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700441 const std::vector<std::string>* allSensors,
Ed Tanousb5a76932020-09-29 16:16:58 -0700442 const std::shared_ptr<boost::container::flat_set<std::string>>&
443 activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700444{
Ed Tanous81ce6092020-12-17 16:54:55 +0000445 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700446 {
447 return;
448 }
449 if ((allSensors == nullptr) || (activeSensors == nullptr))
450 {
451 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800452 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000453 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200454 ? "Temperatures"
455 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700456
457 return;
458 }
459 if (allSensors->empty())
460 {
461 // Nothing to do, the activeSensors object is also empty
462 return;
463 }
464
Ed Tanous02da7c52022-02-27 00:09:02 -0800465 for (std::string_view type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700466 {
467 for (const std::string& sensor : *allSensors)
468 {
469 if (boost::starts_with(sensor, type))
470 {
471 activeSensors->emplace(sensor);
472 }
473 }
474 }
475}
476
477/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800478 * @brief Retrieves valid chassis path
479 * @param asyncResp Pointer to object holding response data
480 * @param callback Callback for next step to get valid chassis path
481 */
482template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700483void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800484 Callback&& callback)
485{
486 BMCWEB_LOG_DEBUG << "checkChassisId enter";
487 const std::array<const char*, 2> interfaces = {
488 "xyz.openbmc_project.Inventory.Item.Board",
489 "xyz.openbmc_project.Inventory.Item.Chassis"};
490
Ed Tanousb9d36b42022-02-26 21:42:46 -0800491 auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
492 const boost::system::error_code ec,
493 const dbus::utility::MapperGetSubTreePathsResponse&
494 chassisPaths) mutable {
495 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
496 if (ec)
497 {
498 BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
499 << ec;
500 messages::internalError(asyncResp->asyncResp->res);
501 return;
502 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800503
Ed Tanousb9d36b42022-02-26 21:42:46 -0800504 std::optional<std::string> chassisPath;
505 std::string chassisName;
506 for (const std::string& chassis : chassisPaths)
507 {
508 sdbusplus::message::object_path path(chassis);
509 chassisName = path.filename();
510 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800511 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800512 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
513 continue;
Carol Wang4bb3dc32019-10-17 18:15:02 +0800514 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800515 if (chassisName == asyncResp->chassisId)
516 {
517 chassisPath = chassis;
518 break;
519 }
520 }
521 callback(chassisPath);
522 };
Carol Wang4bb3dc32019-10-17 18:15:02 +0800523
524 // Get the Chassis Collection
525 crow::connections::systemBus->async_method_call(
526 respHandler, "xyz.openbmc_project.ObjectMapper",
527 "/xyz/openbmc_project/object_mapper",
528 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
529 "/xyz/openbmc_project/inventory", 0, interfaces);
530 BMCWEB_LOG_DEBUG << "checkChassisId exit";
531}
532
533/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100534 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200535 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100536 * @param callback Callback for next step in gathered sensor processing
537 */
538template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700539void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 Callback&& callback)
541{
542 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500543 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700544 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500545 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700546 auto respHandler =
547 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
548 const boost::system::error_code ec,
549 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
551 if (ec)
552 {
553 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800554 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700555 return;
556 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100557
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700558 const std::string* chassisPath = nullptr;
559 std::string chassisName;
560 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561 {
George Liu28aa8de2021-02-01 15:13:30 +0800562 sdbusplus::message::object_path path(chassis);
563 chassisName = path.filename();
564 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700565 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700566 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700567 continue;
568 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700569 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700570 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700571 chassisPath = &chassis;
572 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700573 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700574 }
575 if (chassisPath == nullptr)
576 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
578 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700579 return;
580 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700581
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700582 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200583 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700584 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800585 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700586 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700587 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200588 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800590 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700591 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800592 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
593 nlohmann::json::array();
594 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800595 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700596 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200597 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500598 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800599 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500600 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800601 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500602 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800603 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500604 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800605 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
606 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500607 }
608
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200609 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500610 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800611 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500612 }
613
zhanghch058d1b46d2021-04-01 11:18:24 +0800614 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700615 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
616 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800617 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500618 // Get the list of all sensors for this Chassis element
619 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700620 sdbusplus::asio::getProperty<std::vector<std::string>>(
621 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
622 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800623 [sensorsAsyncResp,
624 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700625 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700626 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700627 if (e)
628 {
629 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700630 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700631 messages::internalError(sensorsAsyncResp->asyncResp->res);
632 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700633 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 }
635 const std::shared_ptr<boost::container::flat_set<std::string>>
636 culledSensorList =
637 std::make_shared<boost::container::flat_set<std::string>>();
638 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
639 culledSensorList);
640 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700641 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100642 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100643
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700644 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700645 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700646 respHandler, "xyz.openbmc_project.ObjectMapper",
647 "/xyz/openbmc_project/object_mapper",
648 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700649 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100651}
652
653/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600654 * @brief Finds all DBus object paths that implement ObjectManager.
655 *
656 * Creates a mapping from the associated connection name to the object path.
657 *
658 * Finds the object paths asynchronously. Invokes callback when information has
659 * been obtained.
660 *
661 * The callback must have the following signature:
662 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500663 * callback(std::shared_ptr<boost::container::flat_map<std::string,
664 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600665 * @endcode
666 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700667 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600668 * @param callback Callback to invoke when object paths obtained.
669 */
670template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700671void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000672 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700673 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600674{
675 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
676 const std::array<std::string, 1> interfaces = {
677 "org.freedesktop.DBus.ObjectManager"};
678
679 // Response handler for GetSubTree DBus method
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 auto respHandler =
681 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
682 const boost::system::error_code ec,
683 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600684 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
685 if (ec)
686 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800687 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600688 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
689 << ec;
690 return;
691 }
692
693 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500694 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
695 objectMgrPaths = std::make_shared<
696 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600697 for (const std::pair<
698 std::string,
699 std::vector<std::pair<std::string, std::vector<std::string>>>>&
700 object : subtree)
701 {
702 // Loop over connections for current object path
703 const std::string& objectPath = object.first;
704 for (const std::pair<std::string, std::vector<std::string>>&
705 objData : object.second)
706 {
707 // Add mapping from connection to object path
708 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500709 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600710 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
711 << objectPath;
712 }
713 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500714 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600715 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
716 };
717
718 // Query mapper for all DBus object paths that implement ObjectManager
719 crow::connections::systemBus->async_method_call(
720 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
721 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700722 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600723 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
724}
725
726/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500727 * @brief Returns the Redfish State value for the specified inventory item.
728 * @param inventoryItem D-Bus inventory item associated with a sensor.
729 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700730 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000731inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500732{
733 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
734 {
735 return "Absent";
736 }
James Feist34dd1792019-05-17 14:10:54 -0700737
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500738 return "Enabled";
739}
740
741/**
742 * @brief Returns the Redfish Health value for the specified sensor.
743 * @param sensorJson Sensor JSON object.
744 * @param interfacesDict Map of all sensor interfaces.
745 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
746 * be nullptr if no associated inventory item was found.
747 * @return Health value for sensor.
748 */
Ed Tanous711ac7a2021-12-20 09:34:41 -0800749inline std::string
750 getHealth(nlohmann::json& sensorJson,
751 const dbus::utility::DBusInteracesMap& interfacesDict,
752 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700753{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500754 // Get current health value (if any) in the sensor JSON object. Some JSON
755 // objects contain multiple sensors (such as PowerSupplies). We want to set
756 // the overall health to be the most severe of any of the sensors.
757 std::string currentHealth;
758 auto statusIt = sensorJson.find("Status");
759 if (statusIt != sensorJson.end())
760 {
761 auto healthIt = statusIt->find("Health");
762 if (healthIt != statusIt->end())
763 {
764 std::string* health = healthIt->get_ptr<std::string*>();
765 if (health != nullptr)
766 {
767 currentHealth = *health;
768 }
769 }
770 }
771
772 // If current health in JSON object is already Critical, return that. This
773 // should override the sensor health, which might be less severe.
774 if (currentHealth == "Critical")
775 {
776 return "Critical";
777 }
778
779 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800780
Ed Tanous9eb808c2022-01-25 10:19:23 -0800781 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700782 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800783 if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
James Feist34dd1792019-05-17 14:10:54 -0700784 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800785 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700786 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800787 if (valueName == "CriticalAlarmHigh" ||
788 valueName == "CriticalAlarmLow")
789 {
790 const bool* asserted = std::get_if<bool>(&value);
791 if (asserted == nullptr)
792 {
793 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
794 }
795 else if (*asserted)
796 {
797 return "Critical";
798 }
799 }
James Feist34dd1792019-05-17 14:10:54 -0700800 }
801 }
802 }
803
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500804 // Check if associated inventory item is not functional
805 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
806 {
807 return "Critical";
808 }
809
810 // If current health in JSON object is already Warning, return that. This
811 // should override the sensor status, which might be less severe.
812 if (currentHealth == "Warning")
813 {
814 return "Warning";
815 }
816
817 // Check if sensor has warning threshold alarm
Ed Tanous9eb808c2022-01-25 10:19:23 -0800818 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700819 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800820 if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
James Feist34dd1792019-05-17 14:10:54 -0700821 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800822 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700823 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800824 if (valueName == "WarningAlarmHigh" ||
825 valueName == "WarningAlarmLow")
826 {
827 const bool* asserted = std::get_if<bool>(&value);
828 if (asserted == nullptr)
829 {
830 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
831 }
832 else if (*asserted)
833 {
Ed Tanousebe4d912022-01-12 12:38:17 -0800834 return "Warning";
Ed Tanous711ac7a2021-12-20 09:34:41 -0800835 }
836 }
James Feist34dd1792019-05-17 14:10:54 -0700837 }
838 }
839 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500840
James Feist34dd1792019-05-17 14:10:54 -0700841 return "OK";
842}
843
Ed Tanous23a21a12020-07-25 04:45:05 +0000844inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500845 const InventoryItem* inventoryItem)
846{
847 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
848 {
849 switch (inventoryItem->ledState)
850 {
851 case LedState::OFF:
852 sensorJson["IndicatorLED"] = "Off";
853 break;
854 case LedState::ON:
855 sensorJson["IndicatorLED"] = "Lit";
856 break;
857 case LedState::BLINK:
858 sensorJson["IndicatorLED"] = "Blinking";
859 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000860 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500861 break;
862 }
863 }
864}
865
James Feist34dd1792019-05-17 14:10:54 -0700866/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100867 * @brief Builds a json sensor representation of a sensor.
868 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500869 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100870 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200871 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100872 * @param interfacesDict A dictionary of the interfaces and properties of said
873 * interfaces to be built from
874 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500875 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
876 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100877 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000878inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100879 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700880 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800881 const dbus::utility::DBusInteracesMap& interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000882 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700883{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700884 // Assume values exist as is (10^0 == 1) if no scale exists
885 int64_t scaleMultiplier = 0;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800886 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800888 if (interface == "xyz.openbmc_project.Sensor.Value")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700889 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800890 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800891 {
892 if (valueName == "Scale")
893 {
894 const int64_t* int64Value = std::get_if<int64_t>(&value);
895 if (int64Value != nullptr)
896 {
897 scaleMultiplier = *int64Value;
898 }
899 }
900 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100901 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100902 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200904 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500905 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500906 // For sensors in SensorCollection we set Id instead of MemberId,
907 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000908 sensorJson["Id"] = sensorName;
909 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500910 }
911 else if (sensorType != "power")
912 {
913 // Set MemberId and Name for non-power sensors. For PowerSupplies and
914 // PowerControl, those properties have more general values because
915 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000916 sensorJson["MemberId"] = sensorName;
917 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500918 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700919
Ed Tanous81ce6092020-12-17 16:54:55 +0000920 sensorJson["Status"]["State"] = getState(inventoryItem);
921 sensorJson["Status"]["Health"] =
922 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923
924 // Parameter to set to override the type we get from dbus, and force it to
925 // int, regardless of what is available. This is used for schemas like fan,
926 // that require integers, not floats.
927 bool forceToInt = false;
928
Anthony Wilson3929aca2019-07-19 15:42:33 -0500929 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200930 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500931 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000932 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000933
934 const std::string& readingType = sensors::toReadingType(sensorType);
935 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500936 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000937 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
938 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500939 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000940 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500941 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000942 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500943 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000944
945 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
946 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200947 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000948 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
949 << sensorType;
950 }
951 else
952 {
953 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200954 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500955 }
956 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500958 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000959 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700960 // TODO(ed) Documentation says that path should be type fan_tach,
961 // implementation seems to implement fan
962 }
963 else if (sensorType == "fan" || sensorType == "fan_tach")
964 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500965 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000966 sensorJson["ReadingUnits"] = "RPM";
967 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
968 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700969 forceToInt = true;
970 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700971 else if (sensorType == "fan_pwm")
972 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500973 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000974 sensorJson["ReadingUnits"] = "Percent";
975 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
976 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700977 forceToInt = true;
978 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700979 else if (sensorType == "voltage")
980 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500981 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000982 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700984 else if (sensorType == "power")
985 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700986 std::string sensorNameLower =
987 boost::algorithm::to_lower_copy(sensorName);
988
Ed Tanous55f79e62022-01-25 11:26:16 -0800989 if (sensorName == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000990 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000991 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500992 // Put multiple "sensors" into a single PowerControl, so have
993 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000994 sensorJson["MemberId"] = "0";
995 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500996 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000997 }
998 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700999 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001000 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001001 }
1002 else
1003 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001004 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001005 }
Ed Tanous2474adf2018-09-05 16:31:16 -07001006 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001007 else
1008 {
1009 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
1010 return;
1011 }
1012 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -05001013 std::vector<
1014 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
1015 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001016 properties.reserve(7);
1017
1018 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001019
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001020 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -05001021 {
1022 properties.emplace_back(
1023 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
1024 "/Thresholds/UpperCaution/Reading"_json_pointer);
1025 properties.emplace_back(
1026 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1027 "/Thresholds/LowerCaution/Reading"_json_pointer);
1028 properties.emplace_back(
1029 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1030 "/Thresholds/UpperCritical/Reading"_json_pointer);
1031 properties.emplace_back(
1032 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1033 "/Thresholds/LowerCritical/Reading"_json_pointer);
1034 }
1035 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001036 {
1037 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001038 "WarningHigh",
1039 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001040 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001041 "WarningLow",
1042 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001043 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001044 "CriticalHigh",
1045 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001046 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001047 "CriticalLow",
1048 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001049 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001050
Ed Tanous2474adf2018-09-05 16:31:16 -07001051 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1052
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001053 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001054 {
1055 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001056 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001057 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001058 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001059 }
1060 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001061 {
1062 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001063 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001064 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001065 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001066 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001067 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001068 {
1069 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001070 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001071 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001072 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001073 }
1074
Anthony Wilson3929aca2019-07-19 15:42:33 -05001075 for (const std::tuple<const char*, const char*,
1076 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001078 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001079 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001080 if (interface != std::get<0>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001081 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001082 continue;
1083 }
Ed Tanous55f79e62022-01-25 11:26:16 -08001084 for (const auto& [valueName, valueVariant] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001085 {
1086 if (valueName != std::get<1>(p))
1087 {
1088 continue;
1089 }
Anthony Wilson3929aca2019-07-19 15:42:33 -05001090
1091 // The property we want to set may be nested json, so use
1092 // a json_pointer for easy indexing into the json structure.
1093 const nlohmann::json::json_pointer& key = std::get<2>(p);
1094
Ed Tanous1abe55e2018-09-05 08:30:59 -07001095 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001096 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097
Ed Tanousabf2add2019-01-22 16:40:12 -08001098 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001099 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001100 double temp = 0.0;
1101 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001102 {
Ed Tanous271584a2019-07-09 16:24:22 -07001103 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001104 }
1105 else if (doubleValue != nullptr)
1106 {
1107 temp = *doubleValue;
1108 }
Eddie James028f7eb2019-05-17 21:24:36 +00001109 else if (uValue != nullptr)
1110 {
1111 temp = *uValue;
1112 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001113 else
1114 {
1115 BMCWEB_LOG_ERROR
1116 << "Got value interface that wasn't int or double";
1117 continue;
1118 }
1119 temp = temp * std::pow(10, scaleMultiplier);
1120 if (forceToInt)
1121 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001122 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001123 }
1124 else
1125 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001126 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001127 }
1128 }
1129 }
1130 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001131
Ed Tanous81ce6092020-12-17 16:54:55 +00001132 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001133 "/xyz/openbmc_project/sensors/" + sensorType +
1134 "/" + sensorName);
1135
Ed Tanous1abe55e2018-09-05 08:30:59 -07001136 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001137}
1138
Ed Tanousb5a76932020-09-29 16:16:58 -07001139inline void populateFanRedundancy(
1140 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001141{
1142 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001143 [sensorsAsyncResp](
1144 const boost::system::error_code ec,
1145 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001146 if (ec)
1147 {
1148 return; // don't have to have this interface
1149 }
1150 for (const std::pair<
1151 std::string,
1152 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1153 pathPair : resp)
1154 {
1155 const std::string& path = pathPair.first;
1156 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1157 objDict = pathPair.second;
1158 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001159 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001160 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001161 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001162
1163 const std::string& owner = objDict.begin()->first;
1164 sdbusplus::asio::getProperty<std::vector<std::string>>(
1165 *crow::connections::systemBus,
1166 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1167 "xyz.openbmc_project.Association", "endpoints",
1168 [path, owner,
1169 sensorsAsyncResp](const boost::system::error_code e,
1170 const std::vector<std::string>& endpoints) {
1171 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001172 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001173 return; // if they don't have an association we
1174 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001175 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001176 auto found =
1177 std::find_if(endpoints.begin(), endpoints.end(),
1178 [sensorsAsyncResp](const std::string& entry) {
1179 return entry.find(sensorsAsyncResp->chassisId) !=
1180 std::string::npos;
1181 });
James Feist8bd25cc2019-03-15 15:14:00 -07001182
Ed Tanous002d39b2022-05-31 08:59:27 -07001183 if (found == endpoints.end())
1184 {
1185 return;
1186 }
1187 crow::connections::systemBus->async_method_call(
1188 [path, sensorsAsyncResp](
1189 const boost::system::error_code& err,
1190 const boost::container::flat_map<
1191 std::string, dbus::utility::DbusVariantType>& ret) {
1192 if (err)
1193 {
1194 return; // don't have to have this
1195 // interface
1196 }
1197 auto findFailures = ret.find("AllowedFailures");
1198 auto findCollection = ret.find("Collection");
1199 auto findStatus = ret.find("Status");
1200
1201 if (findFailures == ret.end() ||
1202 findCollection == ret.end() || findStatus == ret.end())
1203 {
1204 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1205 messages::internalError(
1206 sensorsAsyncResp->asyncResp->res);
1207 return;
1208 }
1209
1210 const uint8_t* allowedFailures =
1211 std::get_if<uint8_t>(&(findFailures->second));
1212 const std::vector<std::string>* collection =
1213 std::get_if<std::vector<std::string>>(
1214 &(findCollection->second));
1215 const std::string* status =
1216 std::get_if<std::string>(&(findStatus->second));
1217
1218 if (allowedFailures == nullptr || collection == nullptr ||
1219 status == nullptr)
1220 {
1221
1222 BMCWEB_LOG_ERROR
1223 << "Invalid redundancy interface types";
1224 messages::internalError(
1225 sensorsAsyncResp->asyncResp->res);
1226 return;
1227 }
1228 sdbusplus::message::object_path objectPath(path);
1229 std::string name = objectPath.filename();
1230 if (name.empty())
1231 {
1232 // this should be impossible
1233 messages::internalError(
1234 sensorsAsyncResp->asyncResp->res);
1235 return;
1236 }
1237 std::replace(name.begin(), name.end(), '_', ' ');
1238
1239 std::string health;
1240
1241 if (boost::ends_with(*status, "Full"))
1242 {
1243 health = "OK";
1244 }
1245 else if (boost::ends_with(*status, "Degraded"))
1246 {
1247 health = "Warning";
1248 }
1249 else
1250 {
1251 health = "Critical";
1252 }
1253 nlohmann::json::array_t redfishCollection;
1254 const auto& fanRedfish =
1255 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1256 for (const std::string& item : *collection)
1257 {
1258 sdbusplus::message::object_path path(item);
1259 std::string itemName = path.filename();
1260 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001261 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001262 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001263 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001264 /*
1265 todo(ed): merge patch that fixes the names
1266 std::replace(itemName.begin(),
1267 itemName.end(), '_', ' ');*/
1268 auto schemaItem =
1269 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1270 [itemName](const nlohmann::json& fan) {
1271 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001272 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001273 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001274 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001275 nlohmann::json::object_t collection;
1276 collection["@odata.id"] =
1277 (*schemaItem)["@odata.id"];
1278 redfishCollection.emplace_back(
1279 std::move(collection));
1280 }
1281 else
1282 {
1283 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1284 messages::internalError(
1285 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001286 return;
1287 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001288 }
James Feist8bd25cc2019-03-15 15:14:00 -07001289
Ed Tanous002d39b2022-05-31 08:59:27 -07001290 size_t minNumNeeded =
1291 collection->empty()
1292 ? 0
1293 : collection->size() - *allowedFailures;
1294 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1295 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001296
Ed Tanous002d39b2022-05-31 08:59:27 -07001297 nlohmann::json::object_t redundancy;
1298 redundancy["@odata.id"] =
1299 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1300 "/" + sensorsAsyncResp->chassisSubNode +
1301 "#/Redundancy/" + std::to_string(jResp.size());
1302 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1303 redundancy["MinNumNeeded"] = minNumNeeded;
1304 redundancy["MemberId"] = name;
1305 redundancy["Mode"] = "N+m";
1306 redundancy["Name"] = name;
1307 redundancy["RedundancySet"] = redfishCollection;
1308 redundancy["Status"]["Health"] = health;
1309 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001310
Ed Tanous002d39b2022-05-31 08:59:27 -07001311 jResp.push_back(std::move(redundancy));
1312 },
1313 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1314 "xyz.openbmc_project.Control.FanRedundancy");
1315 });
1316 }
James Feist8bd25cc2019-03-15 15:14:00 -07001317 },
1318 "xyz.openbmc_project.ObjectMapper",
1319 "/xyz/openbmc_project/object_mapper",
1320 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1321 "/xyz/openbmc_project/control", 2,
1322 std::array<const char*, 1>{
1323 "xyz.openbmc_project.Control.FanRedundancy"});
1324}
1325
Ed Tanousb5a76932020-09-29 16:16:58 -07001326inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001327 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001328{
zhanghch058d1b46d2021-04-01 11:18:24 +08001329 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001330 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001331 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001332 {
1333 sensorHeaders = {"Voltages", "PowerSupplies"};
1334 }
1335 for (const std::string& sensorGroup : sensorHeaders)
1336 {
1337 nlohmann::json::iterator entry = response.find(sensorGroup);
1338 if (entry != response.end())
1339 {
1340 std::sort(entry->begin(), entry->end(),
1341 [](nlohmann::json& c1, nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001342 return c1["Name"] < c2["Name"];
1343 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001344
1345 // add the index counts to the end of each entry
1346 size_t count = 0;
1347 for (nlohmann::json& sensorJson : *entry)
1348 {
1349 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1350 if (odata == sensorJson.end())
1351 {
1352 continue;
1353 }
1354 std::string* value = odata->get_ptr<std::string*>();
1355 if (value != nullptr)
1356 {
1357 *value += std::to_string(count);
1358 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001359 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001360 }
1361 }
1362 }
1363 }
1364}
1365
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001366/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001367 * @brief Finds the inventory item with the specified object path.
1368 * @param inventoryItems D-Bus inventory items associated with sensors.
1369 * @param invItemObjPath D-Bus object path of inventory item.
1370 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001371 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001372inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001373 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001374 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001375{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001376 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001377 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001378 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001379 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001380 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001381 }
1382 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001383 return nullptr;
1384}
1385
1386/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001387 * @brief Finds the inventory item associated with the specified sensor.
1388 * @param inventoryItems D-Bus inventory items associated with sensors.
1389 * @param sensorObjPath D-Bus object path of sensor.
1390 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001391 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001392inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001393 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001394 const std::string& sensorObjPath)
1395{
1396 for (InventoryItem& inventoryItem : *inventoryItems)
1397 {
1398 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1399 {
1400 return &inventoryItem;
1401 }
1402 }
1403 return nullptr;
1404}
1405
1406/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001407 * @brief Finds the inventory item associated with the specified led path.
1408 * @param inventoryItems D-Bus inventory items associated with sensors.
1409 * @param ledObjPath D-Bus object path of led.
1410 * @return Inventory item within vector, or nullptr if no match found.
1411 */
1412inline InventoryItem*
1413 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1414 const std::string& ledObjPath)
1415{
1416 for (InventoryItem& inventoryItem : inventoryItems)
1417 {
1418 if (inventoryItem.ledObjectPath == ledObjPath)
1419 {
1420 return &inventoryItem;
1421 }
1422 }
1423 return nullptr;
1424}
1425
1426/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001427 * @brief Adds inventory item and associated sensor to specified vector.
1428 *
1429 * Adds a new InventoryItem to the vector if necessary. Searches for an
1430 * existing InventoryItem with the specified object path. If not found, one is
1431 * added to the vector.
1432 *
1433 * Next, the specified sensor is added to the set of sensors associated with the
1434 * InventoryItem.
1435 *
1436 * @param inventoryItems D-Bus inventory items associated with sensors.
1437 * @param invItemObjPath D-Bus object path of inventory item.
1438 * @param sensorObjPath D-Bus object path of sensor
1439 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001440inline void addInventoryItem(
1441 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1442 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001443{
1444 // Look for inventory item in vector
1445 InventoryItem* inventoryItem =
1446 findInventoryItem(inventoryItems, invItemObjPath);
1447
1448 // If inventory item doesn't exist in vector, add it
1449 if (inventoryItem == nullptr)
1450 {
1451 inventoryItems->emplace_back(invItemObjPath);
1452 inventoryItem = &(inventoryItems->back());
1453 }
1454
1455 // Add sensor to set of sensors associated with inventory item
1456 inventoryItem->sensors.emplace(sensorObjPath);
1457}
1458
1459/**
1460 * @brief Stores D-Bus data in the specified inventory item.
1461 *
1462 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1463 * specified InventoryItem.
1464 *
1465 * This data is later used to provide sensor property values in the JSON
1466 * response.
1467 *
1468 * @param inventoryItem Inventory item where data will be stored.
1469 * @param interfacesDict Map containing D-Bus interfaces and their properties
1470 * for the specified inventory item.
1471 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001472inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001473 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001474 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001475{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001476 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001477
Ed Tanous9eb808c2022-01-25 10:19:23 -08001478 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001479 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001480 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001481 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001482 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001484 if (name == "Present")
1485 {
1486 const bool* value = std::get_if<bool>(&dbusValue);
1487 if (value != nullptr)
1488 {
1489 inventoryItem.isPresent = *value;
1490 }
1491 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001492 }
1493 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001494 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001495
Ed Tanous711ac7a2021-12-20 09:34:41 -08001496 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001497 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001498 inventoryItem.isPowerSupply = true;
1499 }
1500
1501 // Get properties from Inventory.Decorator.Asset interface
1502 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1503 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001504 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001505 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001506 if (name == "Manufacturer")
1507 {
1508 const std::string* value =
1509 std::get_if<std::string>(&dbusValue);
1510 if (value != nullptr)
1511 {
1512 inventoryItem.manufacturer = *value;
1513 }
1514 }
1515 if (name == "Model")
1516 {
1517 const std::string* value =
1518 std::get_if<std::string>(&dbusValue);
1519 if (value != nullptr)
1520 {
1521 inventoryItem.model = *value;
1522 }
1523 }
1524 if (name == "SerialNumber")
1525 {
1526 const std::string* value =
1527 std::get_if<std::string>(&dbusValue);
1528 if (value != nullptr)
1529 {
1530 inventoryItem.serialNumber = *value;
1531 }
1532 }
1533 if (name == "PartNumber")
1534 {
1535 const std::string* value =
1536 std::get_if<std::string>(&dbusValue);
1537 if (value != nullptr)
1538 {
1539 inventoryItem.partNumber = *value;
1540 }
1541 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001542 }
1543 }
1544
Ed Tanous711ac7a2021-12-20 09:34:41 -08001545 if (interface ==
1546 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001547 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001548 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001549 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001550 if (name == "Functional")
1551 {
1552 const bool* value = std::get_if<bool>(&dbusValue);
1553 if (value != nullptr)
1554 {
1555 inventoryItem.isFunctional = *value;
1556 }
1557 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001558 }
1559 }
1560 }
1561}
1562
1563/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001564 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001565 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001566 * Uses the specified connections (services) to obtain D-Bus data for inventory
1567 * items associated with sensors. Stores the resulting data in the
1568 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001569 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001570 * This data is later used to provide sensor property values in the JSON
1571 * response.
1572 *
1573 * Finds the inventory item data asynchronously. Invokes callback when data has
1574 * been obtained.
1575 *
1576 * The callback must have the following signature:
1577 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001578 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579 * @endcode
1580 *
1581 * This function is called recursively, obtaining data asynchronously from one
1582 * connection in each call. This ensures the callback is not invoked until the
1583 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001584 *
1585 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001586 * @param inventoryItems D-Bus inventory items associated with sensors.
1587 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001588 * @param objectMgrPaths Mappings from connection name to DBus object path that
1589 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001590 * @param callback Callback to invoke when inventory data has been obtained.
1591 * @param invConnectionsIndex Current index in invConnections. Only specified
1592 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001593 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001594template <typename Callback>
1595static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001596 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001597 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001598 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1599 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001600 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001601 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001602{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001603 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001604
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001605 // If no more connections left, call callback
1606 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001607 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001608 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001609 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1610 return;
1611 }
1612
1613 // Get inventory item data from current connection
1614 auto it = invConnections->nth(invConnectionsIndex);
1615 if (it != invConnections->end())
1616 {
1617 const std::string& invConnection = *it;
1618
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001619 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001620 auto respHandler =
1621 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
1622 callback{std::forward<Callback>(callback)},
1623 invConnectionsIndex](const boost::system::error_code ec,
1624 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001625 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626 if (ec)
1627 {
1628 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001629 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001630 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631 return;
1632 }
1633
1634 // Loop through returned object paths
1635 for (const auto& objDictEntry : resp)
1636 {
1637 const std::string& objPath =
1638 static_cast<const std::string&>(objDictEntry.first);
1639
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001640 // If this object path is one of the specified inventory items
1641 InventoryItem* inventoryItem =
1642 findInventoryItem(inventoryItems, objPath);
1643 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001644 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001645 // Store inventory data in InventoryItem
1646 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001647 }
1648 }
1649
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001650 // Recurse to get inventory item data from next connection
1651 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1652 invConnections, objectMgrPaths,
1653 std::move(callback), invConnectionsIndex + 1);
1654
1655 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001656 };
1657
1658 // Find DBus object path that implements ObjectManager for the current
1659 // connection. If no mapping found, default to "/".
1660 auto iter = objectMgrPaths->find(invConnection);
1661 const std::string& objectMgrPath =
1662 (iter != objectMgrPaths->end()) ? iter->second : "/";
1663 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1664 << objectMgrPath;
1665
1666 // Get all object paths and their interfaces for current connection
1667 crow::connections::systemBus->async_method_call(
1668 std::move(respHandler), invConnection, objectMgrPath,
1669 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1670 }
1671
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001672 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001673}
1674
1675/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001676 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001677 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001678 * Gets the D-Bus connections (services) that provide data for the inventory
1679 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001680 *
1681 * Finds the connections asynchronously. Invokes callback when information has
1682 * been obtained.
1683 *
1684 * The callback must have the following signature:
1685 * @code
1686 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1687 * invConnections)
1688 * @endcode
1689 *
1690 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001691 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001692 * @param callback Callback to invoke when connections have been obtained.
1693 */
1694template <typename Callback>
1695static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001696 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1697 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001698 Callback&& callback)
1699{
1700 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1701
1702 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001703 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001704 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001705 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1706 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001707 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1708
1709 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001710 auto respHandler =
1711 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1712 inventoryItems](
1713 const boost::system::error_code ec,
1714 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001715 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1716 if (ec)
1717 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001718 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001719 BMCWEB_LOG_ERROR
1720 << "getInventoryItemsConnections respHandler DBus error " << ec;
1721 return;
1722 }
1723
1724 // Make unique list of connections for desired inventory items
1725 std::shared_ptr<boost::container::flat_set<std::string>>
1726 invConnections =
1727 std::make_shared<boost::container::flat_set<std::string>>();
1728 invConnections->reserve(8);
1729
1730 // Loop through objects from GetSubTree
1731 for (const std::pair<
1732 std::string,
1733 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1734 object : subtree)
1735 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001736 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001737 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001738 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001739 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001740 // Store all connections to inventory item
1741 for (const std::pair<std::string, std::vector<std::string>>&
1742 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001743 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001744 const std::string& invConnection = objData.first;
1745 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001746 }
1747 }
1748 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001749
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001750 callback(invConnections);
1751 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1752 };
1753
1754 // Make call to ObjectMapper to find all inventory items
1755 crow::connections::systemBus->async_method_call(
1756 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1757 "/xyz/openbmc_project/object_mapper",
1758 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1759 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1760}
1761
1762/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001763 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001764 *
1765 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001766 * inventory items. Then finds the associations from those inventory items to
1767 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001768 *
1769 * Finds the inventory items asynchronously. Invokes callback when information
1770 * has been obtained.
1771 *
1772 * The callback must have the following signature:
1773 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001774 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001775 * @endcode
1776 *
1777 * @param sensorsAsyncResp Pointer to object holding response data.
1778 * @param sensorNames All sensors within the current chassis.
1779 * @param objectMgrPaths Mappings from connection name to DBus object path that
1780 * implements ObjectManager.
1781 * @param callback Callback to invoke when inventory items have been obtained.
1782 */
1783template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001784static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001785 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1786 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1787 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001788 objectMgrPaths,
1789 Callback&& callback)
1790{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001791 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001792
1793 // Response handler for GetManagedObjects
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001794 auto respHandler = [callback{std::forward<Callback>(callback)},
1795 sensorsAsyncResp,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001796 sensorNames](const boost::system::error_code ec,
1797 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001798 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001799 if (ec)
1800 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001801 BMCWEB_LOG_ERROR
1802 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001803 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001804 return;
1805 }
1806
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001807 // Create vector to hold list of inventory items
1808 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1809 std::make_shared<std::vector<InventoryItem>>();
1810
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001811 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001812 std::string sensorAssocPath;
1813 sensorAssocPath.reserve(128); // avoid memory allocations
1814 for (const auto& objDictEntry : resp)
1815 {
1816 const std::string& objPath =
1817 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001818
1819 // If path is inventory association for one of the specified sensors
1820 for (const std::string& sensorName : *sensorNames)
1821 {
1822 sensorAssocPath = sensorName;
1823 sensorAssocPath += "/inventory";
1824 if (objPath == sensorAssocPath)
1825 {
1826 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001827 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001828 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001829 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001830 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001831 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001832 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001833 if (valueName == "endpoints")
1834 {
1835 const std::vector<std::string>* endpoints =
1836 std::get_if<std::vector<std::string>>(
1837 &value);
1838 if ((endpoints != nullptr) &&
1839 !endpoints->empty())
1840 {
1841 // Add inventory item to vector
1842 const std::string& invItemPath =
1843 endpoints->front();
1844 addInventoryItem(inventoryItems,
1845 invItemPath,
1846 sensorName);
1847 }
1848 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001849 }
1850 }
1851 }
1852 break;
1853 }
1854 }
1855 }
1856
Anthony Wilsond5005492019-07-31 16:34:17 -05001857 // Now loop through the returned object paths again, this time to
1858 // find the leds associated with the inventory items we just found
1859 std::string inventoryAssocPath;
1860 inventoryAssocPath.reserve(128); // avoid memory allocations
1861 for (const auto& objDictEntry : resp)
1862 {
1863 const std::string& objPath =
1864 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001865
1866 for (InventoryItem& inventoryItem : *inventoryItems)
1867 {
1868 inventoryAssocPath = inventoryItem.objectPath;
1869 inventoryAssocPath += "/leds";
1870 if (objPath == inventoryAssocPath)
1871 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001872 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001873 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001874 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001875 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001876 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001877 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001878 if (valueName == "endpoints")
1879 {
1880 const std::vector<std::string>* endpoints =
1881 std::get_if<std::vector<std::string>>(
1882 &value);
1883 if ((endpoints != nullptr) &&
1884 !endpoints->empty())
1885 {
1886 // Add inventory item to vector
1887 // Store LED path in inventory item
1888 const std::string& ledPath =
1889 endpoints->front();
1890 inventoryItem.ledObjectPath = ledPath;
1891 }
1892 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001893 }
1894 }
1895 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001896
Anthony Wilsond5005492019-07-31 16:34:17 -05001897 break;
1898 }
1899 }
1900 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001901 callback(inventoryItems);
1902 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001903 };
1904
1905 // Find DBus object path that implements ObjectManager for ObjectMapper
1906 std::string connection = "xyz.openbmc_project.ObjectMapper";
1907 auto iter = objectMgrPaths->find(connection);
1908 const std::string& objectMgrPath =
1909 (iter != objectMgrPaths->end()) ? iter->second : "/";
1910 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1911 << objectMgrPath;
1912
1913 // Call GetManagedObjects on the ObjectMapper to get all associations
1914 crow::connections::systemBus->async_method_call(
1915 std::move(respHandler), connection, objectMgrPath,
1916 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1917
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001918 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001919}
1920
1921/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001922 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1923 *
1924 * Uses the specified connections (services) to obtain D-Bus data for inventory
1925 * item leds associated with sensors. Stores the resulting data in the
1926 * inventoryItems vector.
1927 *
1928 * This data is later used to provide sensor property values in the JSON
1929 * response.
1930 *
1931 * Finds the inventory item led data asynchronously. Invokes callback when data
1932 * has been obtained.
1933 *
1934 * The callback must have the following signature:
1935 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001936 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001937 * @endcode
1938 *
1939 * This function is called recursively, obtaining data asynchronously from one
1940 * connection in each call. This ensures the callback is not invoked until the
1941 * last asynchronous function has completed.
1942 *
1943 * @param sensorsAsyncResp Pointer to object holding response data.
1944 * @param inventoryItems D-Bus inventory items associated with sensors.
1945 * @param ledConnections Connections that provide data for the inventory leds.
1946 * @param callback Callback to invoke when inventory data has been obtained.
1947 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1948 * in recursive calls to this function.
1949 */
1950template <typename Callback>
1951void getInventoryLedData(
1952 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1953 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1954 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1955 ledConnections,
1956 Callback&& callback, size_t ledConnectionsIndex = 0)
1957{
1958 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1959
1960 // If no more connections left, call callback
1961 if (ledConnectionsIndex >= ledConnections->size())
1962 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001963 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001964 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1965 return;
1966 }
1967
1968 // Get inventory item data from current connection
1969 auto it = ledConnections->nth(ledConnectionsIndex);
1970 if (it != ledConnections->end())
1971 {
1972 const std::string& ledPath = (*it).first;
1973 const std::string& ledConnection = (*it).second;
1974 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001975 auto respHandler =
1976 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001977 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001978 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001979 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1980 if (ec)
1981 {
1982 BMCWEB_LOG_ERROR
1983 << "getInventoryLedData respHandler DBus error " << ec;
1984 messages::internalError(sensorsAsyncResp->asyncResp->res);
1985 return;
1986 }
1987
1988 BMCWEB_LOG_DEBUG << "Led state: " << state;
1989 // Find inventory item with this LED object path
1990 InventoryItem* inventoryItem =
1991 findInventoryItemForLed(*inventoryItems, ledPath);
1992 if (inventoryItem != nullptr)
1993 {
1994 // Store LED state in InventoryItem
1995 if (boost::ends_with(state, "On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001996 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001997 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001998 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001999 else if (boost::ends_with(state, "Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05002000 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002001 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05002002 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002003 else if (boost::ends_with(state, "Off"))
2004 {
2005 inventoryItem->ledState = LedState::OFF;
2006 }
2007 else
2008 {
2009 inventoryItem->ledState = LedState::UNKNOWN;
2010 }
2011 }
Anthony Wilsond5005492019-07-31 16:34:17 -05002012
Ed Tanous002d39b2022-05-31 08:59:27 -07002013 // Recurse to get LED data from next connection
2014 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2015 ledConnections, std::move(callback),
2016 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05002017
Ed Tanous002d39b2022-05-31 08:59:27 -07002018 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2019 };
Anthony Wilsond5005492019-07-31 16:34:17 -05002020
2021 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002022 sdbusplus::asio::getProperty<std::string>(
2023 *crow::connections::systemBus, ledConnection, ledPath,
2024 "xyz.openbmc_project.Led.Physical", "State",
2025 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05002026 }
2027
2028 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2029}
2030
2031/**
2032 * @brief Gets LED data for LEDs associated with given inventory items.
2033 *
2034 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2035 * associated with the specified inventory items. Then gets the LED data from
2036 * each connection and stores it in the inventory item.
2037 *
2038 * This data is later used to provide sensor property values in the JSON
2039 * response.
2040 *
2041 * Finds the LED data asynchronously. Invokes callback when information has
2042 * been obtained.
2043 *
2044 * The callback must have the following signature:
2045 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002046 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002047 * @endcode
2048 *
2049 * @param sensorsAsyncResp Pointer to object holding response data.
2050 * @param inventoryItems D-Bus inventory items associated with sensors.
2051 * @param callback Callback to invoke when inventory items have been obtained.
2052 */
2053template <typename Callback>
2054void getInventoryLeds(
2055 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2056 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2057 Callback&& callback)
2058{
2059 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2060
2061 const std::string path = "/xyz/openbmc_project";
2062 const std::array<std::string, 1> interfaces = {
2063 "xyz.openbmc_project.Led.Physical"};
2064
2065 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07002066 auto respHandler =
2067 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2068 inventoryItems](
2069 const boost::system::error_code ec,
2070 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05002071 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2072 if (ec)
2073 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002074 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002075 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2076 << ec;
2077 return;
2078 }
2079
2080 // Build map of LED object paths to connections
2081 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2082 ledConnections = std::make_shared<
2083 boost::container::flat_map<std::string, std::string>>();
2084
2085 // Loop through objects from GetSubTree
2086 for (const std::pair<
2087 std::string,
2088 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2089 object : subtree)
2090 {
2091 // Check if object path is LED for one of the specified inventory
2092 // items
2093 const std::string& ledPath = object.first;
2094 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2095 {
2096 // Add mapping from ledPath to connection
2097 const std::string& connection = object.second.begin()->first;
2098 (*ledConnections)[ledPath] = connection;
2099 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2100 << connection;
2101 }
2102 }
2103
2104 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2105 std::move(callback));
2106 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2107 };
2108 // Make call to ObjectMapper to find all inventory items
2109 crow::connections::systemBus->async_method_call(
2110 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2111 "/xyz/openbmc_project/object_mapper",
2112 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2113 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2114}
2115
2116/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002117 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2118 *
2119 * Uses the specified connections (services) (currently assumes just one) to
2120 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2121 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2122 *
2123 * This data is later used to provide sensor property values in the JSON
2124 * response.
2125 *
2126 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2127 * when data has been obtained.
2128 *
2129 * The callback must have the following signature:
2130 * @code
2131 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2132 * @endcode
2133 *
2134 * @param sensorsAsyncResp Pointer to object holding response data.
2135 * @param inventoryItems D-Bus inventory items associated with sensors.
2136 * @param psAttributesConnections Connections that provide data for the Power
2137 * Supply Attributes
2138 * @param callback Callback to invoke when data has been obtained.
2139 */
2140template <typename Callback>
2141void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002142 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002143 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2144 const boost::container::flat_map<std::string, std::string>&
2145 psAttributesConnections,
2146 Callback&& callback)
2147{
2148 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2149
2150 if (psAttributesConnections.empty())
2151 {
2152 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2153 callback(inventoryItems);
2154 return;
2155 }
2156
2157 // Assuming just one connection (service) for now
2158 auto it = psAttributesConnections.nth(0);
2159
2160 const std::string& psAttributesPath = (*it).first;
2161 const std::string& psAttributesConnection = (*it).second;
2162
2163 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002164 auto respHandler =
2165 [sensorsAsyncResp, inventoryItems,
2166 callback{std::forward<Callback>(callback)}](
2167 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002168 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2169 if (ec)
2170 {
2171 BMCWEB_LOG_ERROR
2172 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002173 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002174 return;
2175 }
2176
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002177 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2178 // Store value in Power Supply Inventory Items
2179 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002180 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002181 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002182 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002183 inventoryItem.powerSupplyEfficiencyPercent =
2184 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002185 }
2186 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002187
2188 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2189 callback(inventoryItems);
2190 };
2191
2192 // Get the DeratingFactor property for the PowerSupplyAttributes
2193 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002194 sdbusplus::asio::getProperty<uint32_t>(
2195 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2196 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2197 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002198
2199 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2200}
2201
2202/**
2203 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2204 *
2205 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2206 * data. Then gets the Power Supply Attributes data from the connection
2207 * (currently just assumes 1 connection) and stores the data in the inventory
2208 * item.
2209 *
2210 * This data is later used to provide sensor property values in the JSON
2211 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2212 *
2213 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2214 * when information has been obtained.
2215 *
2216 * The callback must have the following signature:
2217 * @code
2218 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2219 * @endcode
2220 *
2221 * @param sensorsAsyncResp Pointer to object holding response data.
2222 * @param inventoryItems D-Bus inventory items associated with sensors.
2223 * @param callback Callback to invoke when data has been obtained.
2224 */
2225template <typename Callback>
2226void getPowerSupplyAttributes(
2227 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2228 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2229 Callback&& callback)
2230{
2231 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2232
2233 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002234 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002235 {
2236 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2237 callback(inventoryItems);
2238 return;
2239 }
2240
2241 const std::array<std::string, 1> interfaces = {
2242 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2243
2244 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002245 auto respHandler =
2246 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2247 inventoryItems](
2248 const boost::system::error_code ec,
2249 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002250 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2251 if (ec)
2252 {
2253 messages::internalError(sensorsAsyncResp->asyncResp->res);
2254 BMCWEB_LOG_ERROR
2255 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2256 return;
2257 }
2258 if (subtree.empty())
2259 {
2260 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2261 callback(inventoryItems);
2262 return;
2263 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002264
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 // Currently we only support 1 power supply attribute, use this for
2266 // all the power supplies. Build map of object path to connection.
2267 // Assume just 1 connection and 1 path for now.
2268 boost::container::flat_map<std::string, std::string>
2269 psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002270
Ed Tanous002d39b2022-05-31 08:59:27 -07002271 if (subtree[0].first.empty() || subtree[0].second.empty())
2272 {
2273 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2274 callback(inventoryItems);
2275 return;
2276 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002277
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 const std::string& psAttributesPath = subtree[0].first;
2279 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002280
Ed Tanous002d39b2022-05-31 08:59:27 -07002281 if (connection.empty())
2282 {
2283 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2284 callback(inventoryItems);
2285 return;
2286 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002287
Ed Tanous002d39b2022-05-31 08:59:27 -07002288 psAttributesConnections[psAttributesPath] = connection;
2289 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2290 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002291
Ed Tanous002d39b2022-05-31 08:59:27 -07002292 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2293 psAttributesConnections,
2294 std::move(callback));
2295 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2296 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002297 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2298 crow::connections::systemBus->async_method_call(
2299 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2300 "/xyz/openbmc_project/object_mapper",
2301 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2302 "/xyz/openbmc_project", 0, interfaces);
2303 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2304}
2305
2306/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002307 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002308 *
2309 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002310 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002311 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002312 * This data is later used to provide sensor property values in the JSON
2313 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002314 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002315 * Finds the inventory items asynchronously. Invokes callback when the
2316 * inventory items have been obtained.
2317 *
2318 * The callback must have the following signature:
2319 * @code
2320 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2321 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002322 *
2323 * @param sensorsAsyncResp Pointer to object holding response data.
2324 * @param sensorNames All sensors within the current chassis.
2325 * @param objectMgrPaths Mappings from connection name to DBus object path that
2326 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002327 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002328 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002329template <typename Callback>
2330static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002331 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2332 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2333 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002334 objectMgrPaths,
2335 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002336{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002337 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2338 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002339 [sensorsAsyncResp, objectMgrPaths,
2340 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002341 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002342 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2343 auto getInventoryItemsConnectionsCb =
2344 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2345 callback{std::forward<const Callback>(callback)}](
2346 std::shared_ptr<boost::container::flat_set<std::string>>
2347 invConnections) {
2348 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2349 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2350 callback{std::move(callback)}]() {
2351 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002352
Ed Tanous002d39b2022-05-31 08:59:27 -07002353 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2354 callback{std::move(callback)}]() {
2355 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2356 // Find Power Supply Attributes and get the data
2357 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2358 std::move(callback));
2359 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002360 };
2361
Ed Tanous002d39b2022-05-31 08:59:27 -07002362 // Find led connections and get the data
2363 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2364 std::move(getInventoryLedsCb));
2365 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2366 };
2367
2368 // Get inventory item data from connections
2369 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2370 invConnections, objectMgrPaths,
2371 std::move(getInventoryItemsDataCb));
2372 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002373 };
2374
Ed Tanous002d39b2022-05-31 08:59:27 -07002375 // Get connections that provide inventory item data
2376 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2377 std::move(getInventoryItemsConnectionsCb));
2378 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2379 };
2380
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002381 // Get associations from sensors to inventory items
2382 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2383 std::move(getInventoryItemAssociationsCb));
2384 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2385}
2386
2387/**
2388 * @brief Returns JSON PowerSupply object for the specified inventory item.
2389 *
2390 * Searches for a JSON PowerSupply object that matches the specified inventory
2391 * item. If one is not found, a new PowerSupply object is added to the JSON
2392 * array.
2393 *
2394 * Multiple sensors are often associated with one power supply inventory item.
2395 * As a result, multiple sensor values are stored in one JSON PowerSupply
2396 * object.
2397 *
2398 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2399 * @param inventoryItem Inventory item for the power supply.
2400 * @param chassisId Chassis that contains the power supply.
2401 * @return JSON PowerSupply object for the specified inventory item.
2402 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002403inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002404 const InventoryItem& inventoryItem,
2405 const std::string& chassisId)
2406{
2407 // Check if matching PowerSupply object already exists in JSON array
2408 for (nlohmann::json& powerSupply : powerSupplyArray)
2409 {
2410 if (powerSupply["MemberId"] == inventoryItem.name)
2411 {
2412 return powerSupply;
2413 }
2414 }
2415
2416 // Add new PowerSupply object to JSON array
2417 powerSupplyArray.push_back({});
2418 nlohmann::json& powerSupply = powerSupplyArray.back();
2419 powerSupply["@odata.id"] =
2420 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2421 powerSupply["MemberId"] = inventoryItem.name;
2422 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2423 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2424 powerSupply["Model"] = inventoryItem.model;
2425 powerSupply["PartNumber"] = inventoryItem.partNumber;
2426 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002427 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002428
Gunnar Mills42cbe532019-08-15 15:26:54 -05002429 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2430 {
2431 powerSupply["EfficiencyPercent"] =
2432 inventoryItem.powerSupplyEfficiencyPercent;
2433 }
2434
2435 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002436 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2437 powerSupply["Status"]["Health"] = health;
2438
2439 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002440}
2441
2442/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002443 * @brief Gets the values of the specified sensors.
2444 *
2445 * Stores the results as JSON in the SensorsAsyncResp.
2446 *
2447 * Gets the sensor values asynchronously. Stores the results later when the
2448 * information has been obtained.
2449 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002450 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002451 *
2452 * To minimize the number of DBus calls, the DBus method
2453 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2454 * values of all sensors provided by a connection (service).
2455 *
2456 * The connections set contains all the connections that provide sensor values.
2457 *
2458 * The objectMgrPaths map contains mappings from a connection name to the
2459 * corresponding DBus object path that implements ObjectManager.
2460 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002461 * The InventoryItem vector contains D-Bus inventory items associated with the
2462 * sensors. Inventory item data is needed for some Redfish sensor properties.
2463 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002464 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002465 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002466 * @param connections Connections that provide sensor values.
2467 * @param objectMgrPaths Mappings from connection name to DBus object path that
2468 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002469 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002470 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002471inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002472 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002473 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002474 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002475 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002476 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002477 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002478{
2479 BMCWEB_LOG_DEBUG << "getSensorData enter";
2480 // Get managed objects from all services exposing sensors
2481 for (const std::string& connection : connections)
2482 {
2483 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002484 auto getManagedObjectsCb =
2485 [sensorsAsyncResp, sensorNames,
2486 inventoryItems](const boost::system::error_code ec,
2487 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002488 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2489 if (ec)
2490 {
2491 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002492 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002493 return;
2494 }
2495 // Go through all objects and update response with sensor data
2496 for (const auto& objDictEntry : resp)
2497 {
2498 const std::string& objPath =
2499 static_cast<const std::string&>(objDictEntry.first);
2500 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2501 << objPath;
2502
Shawn McCarneyde629b62019-03-08 10:42:51 -06002503 std::vector<std::string> split;
2504 // Reserve space for
2505 // /xyz/openbmc_project/sensors/<name>/<subname>
2506 split.reserve(6);
2507 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2508 if (split.size() < 6)
2509 {
2510 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2511 << objPath;
2512 continue;
2513 }
2514 // These indexes aren't intuitive, as boost::split puts an empty
2515 // string at the beginning
2516 const std::string& sensorType = split[4];
2517 const std::string& sensorName = split[5];
2518 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2519 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002520 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002521 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002522 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002523 continue;
2524 }
2525
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002526 // Find inventory item (if any) associated with sensor
2527 InventoryItem* inventoryItem =
2528 findInventoryItemForSensor(inventoryItems, objPath);
2529
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002530 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002531 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002532
2533 nlohmann::json* sensorJson = nullptr;
2534
Nan Zhou928fefb2022-03-28 08:45:00 -07002535 if (sensorSchema == sensors::node::sensors &&
2536 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002537 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002538 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002539 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2540 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002541 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002542 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002543 }
2544 else
2545 {
Ed Tanous271584a2019-07-09 16:24:22 -07002546 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002547 if (sensorsAsyncResp->efficientExpand)
2548 {
2549 fieldName = "Members";
2550 }
2551 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002552 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002553 fieldName = "Temperatures";
2554 }
2555 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2556 sensorType == "fan_pwm")
2557 {
2558 fieldName = "Fans";
2559 }
2560 else if (sensorType == "voltage")
2561 {
2562 fieldName = "Voltages";
2563 }
2564 else if (sensorType == "power")
2565 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002566 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002567 {
2568 fieldName = "PowerControl";
2569 }
2570 else if ((inventoryItem != nullptr) &&
2571 (inventoryItem->isPowerSupply))
2572 {
2573 fieldName = "PowerSupplies";
2574 }
2575 else
2576 {
2577 // Other power sensors are in SensorCollection
2578 continue;
2579 }
2580 }
2581 else
2582 {
2583 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2584 << sensorType;
2585 continue;
2586 }
2587
2588 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002589 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002590 if (fieldName == "PowerControl")
2591 {
2592 if (tempArray.empty())
2593 {
2594 // Put multiple "sensors" into a single
2595 // PowerControl. Follows MemberId naming and
2596 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002597 nlohmann::json::object_t power;
2598 power["@odata.id"] =
2599 "/redfish/v1/Chassis/" +
2600 sensorsAsyncResp->chassisId + "/" +
2601 sensorsAsyncResp->chassisSubNode + "#/" +
2602 fieldName + "/0";
2603 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002604 }
2605 sensorJson = &(tempArray.back());
2606 }
2607 else if (fieldName == "PowerSupplies")
2608 {
2609 if (inventoryItem != nullptr)
2610 {
2611 sensorJson =
2612 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002613 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002614 }
2615 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002616 else if (fieldName == "Members")
2617 {
Ed Tanous14766872022-03-15 10:44:42 -07002618 nlohmann::json::object_t member;
2619 member["@odata.id"] =
2620 "/redfish/v1/Chassis/" +
2621 sensorsAsyncResp->chassisId + "/" +
2622 sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2623 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002624 sensorJson = &(tempArray.back());
2625 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002626 else
2627 {
Ed Tanous14766872022-03-15 10:44:42 -07002628 nlohmann::json::object_t member;
2629 member["@odata.id"] = "/redfish/v1/Chassis/" +
2630 sensorsAsyncResp->chassisId +
2631 "/" +
2632 sensorsAsyncResp->chassisSubNode +
2633 "#/" + fieldName + "/";
2634 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002635 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002636 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002637 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002638
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002639 if (sensorJson != nullptr)
2640 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002641 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002642 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002643 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002644 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002645 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002646 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002647 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002648 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002649 if (sensorsAsyncResp->chassisSubNode ==
2650 sensors::node::sensors &&
2651 sensorsAsyncResp->efficientExpand)
2652 {
2653 sensorsAsyncResp->asyncResp->res
2654 .jsonValue["Members@odata.count"] =
2655 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2656 .size();
2657 }
2658 else if (sensorsAsyncResp->chassisSubNode ==
2659 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002660 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002661 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002662 }
James Feist8bd25cc2019-03-15 15:14:00 -07002663 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002664 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2665 };
2666
2667 // Find DBus object path that implements ObjectManager for the current
2668 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002669 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002670 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002671 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002672 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2673 << objectMgrPath;
2674
2675 crow::connections::systemBus->async_method_call(
2676 getManagedObjectsCb, connection, objectMgrPath,
2677 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002678 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002679 BMCWEB_LOG_DEBUG << "getSensorData exit";
2680}
2681
Ed Tanous23a21a12020-07-25 04:45:05 +00002682inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002683 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002684 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002685{
2686 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002687 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002688 const boost::container::flat_set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002689 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2690 auto getObjectManagerPathsCb =
2691 [sensorsAsyncResp, sensorNames,
2692 connections](const std::shared_ptr<boost::container::flat_map<
2693 std::string, std::string>>& objectMgrPaths) {
2694 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2695 auto getInventoryItemsCb =
2696 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2697 const std::shared_ptr<std::vector<InventoryItem>>&
2698 inventoryItems) {
2699 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2700 // Get sensor data and store results in JSON
2701 getSensorData(sensorsAsyncResp, sensorNames, connections,
2702 objectMgrPaths, inventoryItems);
2703 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2704 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002705
Ed Tanous002d39b2022-05-31 08:59:27 -07002706 // Get inventory items associated with sensors
2707 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2708 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002709
Ed Tanous002d39b2022-05-31 08:59:27 -07002710 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002711 };
2712
Ed Tanous002d39b2022-05-31 08:59:27 -07002713 // Get mapping from connection names to the DBus object
2714 // paths that implement the ObjectManager interface
2715 getObjectManagerPaths(sensorsAsyncResp,
2716 std::move(getObjectManagerPathsCb));
2717 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2718 };
2719
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002720 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002721 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002722}
2723
Shawn McCarneyde629b62019-03-08 10:42:51 -06002724/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002725 * @brief Entry point for retrieving sensors data related to requested
2726 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002727 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002728 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002729inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002730 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002731{
2732 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002733 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002734 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002735 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002736 sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002737 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2738 processSensorList(sensorsAsyncResp, sensorNames);
2739 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2740 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002741 // SensorCollection doesn't contain the Redundancy property
2742 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2743 {
2744 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2745 nlohmann::json::array();
2746 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002747 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002748 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002749 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002750}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002751
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302752/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002753 * @brief Find the requested sensorName in the list of all sensors supplied by
2754 * the chassis node
2755 *
2756 * @param sensorName The sensor name supplied in the PATCH request
2757 * @param sensorsList The list of sensors managed by the chassis node
2758 * @param sensorsModified The list of sensors that were found as a result of
2759 * repeated calls to this function
2760 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002761inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302762 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002763 boost::container::flat_set<std::string>& sensorsList,
2764 boost::container::flat_set<std::string>& sensorsModified)
2765{
George Liu28aa8de2021-02-01 15:13:30 +08002766 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002767 {
George Liu28aa8de2021-02-01 15:13:30 +08002768 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002769 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002770 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002771 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002772 continue;
2773 }
2774 if (thisSensorName == sensorName)
2775 {
2776 sensorsModified.emplace(chassisSensor);
2777 return true;
2778 }
2779 }
2780 return false;
2781}
2782
2783/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302784 * @brief Entry point for overriding sensor values of given sensor
2785 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002786 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002787 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002788 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302789 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002790inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002791 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002792 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002793 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302794{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002795 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002796 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302797
Ed Tanous543f4402022-01-06 13:12:53 -08002798 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302799 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302800 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002801 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302802 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302803 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302804 if (collectionItems.first == "Temperatures")
2805 {
2806 propertyValueName = "ReadingCelsius";
2807 }
2808 else if (collectionItems.first == "Fans")
2809 {
2810 propertyValueName = "Reading";
2811 }
2812 else
2813 {
2814 propertyValueName = "ReadingVolts";
2815 }
2816 for (auto& item : collectionItems.second)
2817 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002818 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2819 "MemberId", memberId, propertyValueName,
2820 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302821 {
2822 return;
2823 }
2824 overrideMap.emplace(memberId,
2825 std::make_pair(value, collectionItems.first));
2826 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302827 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002828
Ed Tanous002d39b2022-05-31 08:59:27 -07002829 auto getChassisSensorListCb =
2830 [sensorAsyncResp, overrideMap](
2831 const std::shared_ptr<boost::container::flat_set<std::string>>&
2832 sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002833 // Match sensor names in the PATCH request to those managed by the
2834 // chassis node
2835 const std::shared_ptr<boost::container::flat_set<std::string>>
2836 sensorNames =
2837 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302838 for (const auto& item : overrideMap)
2839 {
2840 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002841 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2842 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302843 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302844 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002845 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302846 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302847 return;
2848 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302849 }
2850 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002851 auto getObjectsWithConnectionCb =
2852 [sensorAsyncResp, overrideMap](
2853 const boost::container::flat_set<std::string>& /*connections*/,
2854 const std::set<std::pair<std::string, std::string>>&
2855 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002856 if (objectsWithConnection.size() != overrideMap.size())
2857 {
2858 BMCWEB_LOG_INFO
2859 << "Unable to find all objects with proper connection "
2860 << objectsWithConnection.size() << " requested "
2861 << overrideMap.size() << "\n";
2862 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2863 sensorAsyncResp->chassisSubNode ==
2864 sensors::node::thermal
2865 ? "Temperatures"
2866 : "Voltages",
2867 "Count");
2868 return;
2869 }
2870 for (const auto& item : objectsWithConnection)
2871 {
2872 sdbusplus::message::object_path path(item.first);
2873 std::string sensorName = path.filename();
2874 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302875 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002876 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302877 return;
2878 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302879
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002880 const auto& iterator = overrideMap.find(sensorName);
2881 if (iterator == overrideMap.end())
2882 {
2883 BMCWEB_LOG_INFO << "Unable to find sensor object"
2884 << item.first << "\n";
2885 messages::internalError(sensorAsyncResp->asyncResp->res);
2886 return;
2887 }
2888 crow::connections::systemBus->async_method_call(
2889 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002890 if (ec)
2891 {
2892 if (ec.value() ==
2893 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002894 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002895 BMCWEB_LOG_WARNING
2896 << "Manufacturing mode is not Enabled...can't "
2897 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002898
Ed Tanous002d39b2022-05-31 08:59:27 -07002899 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002900 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002901 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002902 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002903 BMCWEB_LOG_DEBUG
2904 << "setOverrideValueStatus DBUS error: " << ec;
2905 messages::internalError(
2906 sensorAsyncResp->asyncResp->res);
2907 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002908 },
2909 item.second, item.first, "org.freedesktop.DBus.Properties",
2910 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002911 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002912 }
2913 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302914 // Get object with connection for the given sensor name
2915 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2916 std::move(getObjectsWithConnectionCb));
2917 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302918 // get full sensor list for the given chassisId and cross verify the sensor.
2919 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2920}
2921
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002922/**
2923 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2924 * path of the sensor.
2925 *
2926 * Function builds valid Redfish response for sensor query of given chassis and
2927 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2928 * it to caller in a callback.
2929 *
2930 * @param chassis Chassis for which retrieval should be performed
2931 * @param node Node (group) of sensors. See sensors::node for supported values
2932 * @param mapComplete Callback to be called with retrieval result
2933 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002934inline void retrieveUriToDbusMap(const std::string& chassis,
2935 const std::string& node,
2936 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002937{
Ed Tanous02da7c52022-02-27 00:09:02 -08002938 decltype(sensors::paths)::const_iterator pathIt =
2939 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2940 [&node](auto&& val) { return val.first == node; });
2941 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002942 {
2943 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2944 mapComplete(boost::beast::http::status::bad_request, {});
2945 return;
2946 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002947
Nan Zhou72374eb2022-01-27 17:06:51 -08002948 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002949 auto callback =
Nan Zhou72374eb2022-01-27 17:06:51 -08002950 [asyncResp, mapCompleteCb{std::move(mapComplete)}](
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002951 const boost::beast::http::status status,
2952 const boost::container::flat_map<std::string, std::string>&
2953 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2954
2955 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002956 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002957 getChassisData(resp);
2958}
2959
Nan Zhoubacb2162022-04-06 11:28:32 -07002960namespace sensors
2961{
Nan Zhou928fefb2022-03-28 08:45:00 -07002962
Nan Zhoubacb2162022-04-06 11:28:32 -07002963inline void getChassisCallback(
2964 const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2965 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
2966{
2967 BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2968
2969 nlohmann::json& entriesArray =
2970 asyncResp->asyncResp->res.jsonValue["Members"];
2971 for (auto& sensor : *sensorNames)
2972 {
2973 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2974
2975 sdbusplus::message::object_path path(sensor);
2976 std::string sensorName = path.filename();
2977 if (sensorName.empty())
2978 {
2979 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2980 messages::internalError(asyncResp->asyncResp->res);
2981 return;
2982 }
Ed Tanous14766872022-03-15 10:44:42 -07002983 nlohmann::json::object_t member;
2984 member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
2985 "/" + asyncResp->chassisSubNode + "/" +
2986 sensorName;
2987 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002988 }
2989
2990 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2991 entriesArray.size();
2992 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2993}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002994
2995inline void handleSensorGet(App& app, const crow::Request& req,
2996 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2997 const std::string& chassisId,
2998 const std::string& sensorName)
2999{
3000 if (!redfish::setUpRedfishRoute(app, req, aResp->res))
3001 {
3002 return;
3003 }
3004 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3005 std::shared_ptr<SensorsAsyncResp> asyncResp =
3006 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3007 std::span<std::string_view>(),
3008 sensors::node::sensors);
3009
3010 const std::array<const char*, 1> interfaces = {
3011 "xyz.openbmc_project.Sensor.Value"};
3012
3013 // Get a list of all of the sensors that implement Sensor.Value
3014 // and get the path and service name associated with the sensor
3015 crow::connections::systemBus->async_method_call(
3016 [asyncResp,
3017 sensorName](const boost::system::error_code ec,
3018 const ::dbus::utility::MapperGetSubTreeResponse& subtree) {
3019 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3020 if (ec)
3021 {
3022 messages::internalError(asyncResp->asyncResp->res);
3023 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3024 << "Dbus error " << ec;
3025 return;
3026 }
3027
3028 ::dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3029 std::find_if(
3030 subtree.begin(), subtree.end(),
3031 [sensorName](
3032 const std::pair<
3033 std::string,
3034 std::vector<std::pair<
3035 std::string, std::vector<std::string>>>>& object) {
3036 sdbusplus::message::object_path path(object.first);
3037 std::string name = path.filename();
3038 if (name.empty())
3039 {
3040 BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first;
3041 return false;
3042 }
3043
3044 return name == sensorName;
3045 });
3046
3047 if (it == subtree.end())
3048 {
3049 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3050 << sensorName;
3051 messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor",
3052 sensorName);
3053 return;
3054 }
3055 std::string_view sensorPath = (*it).first;
3056 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
3057 << "': " << sensorPath;
3058
3059 const std::shared_ptr<boost::container::flat_set<std::string>>
3060 sensorList =
3061 std::make_shared<boost::container::flat_set<std::string>>();
3062
3063 sensorList->emplace(sensorPath);
3064 processSensorList(asyncResp, sensorList);
3065 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3066 },
3067 "xyz.openbmc_project.ObjectMapper",
3068 "/xyz/openbmc_project/object_mapper",
3069 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3070 "/xyz/openbmc_project/sensors", 2, interfaces);
3071}
3072
Nan Zhoubacb2162022-04-06 11:28:32 -07003073} // namespace sensors
3074
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003075inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003076{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003077 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003078 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003079 .methods(boost::beast::http::verb::get)(
3080 [&app](const crow::Request& req,
3081 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3082 const std::string& chassisId) {
3083 query_param::QueryCapabilities capabilities = {
3084 .canDelegateExpandLevel = 1,
3085 };
3086 query_param::Query delegatedQuery;
3087 if (!redfish::setUpRedfishRouteWithDelegation(
3088 app, req, aResp->res, delegatedQuery, capabilities))
3089 {
3090 return;
3091 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003092
Ed Tanous002d39b2022-05-31 08:59:27 -07003093 if (delegatedQuery.expandType != query_param::ExpandType::None)
3094 {
3095 // we perform efficient expand.
Nan Zhou928fefb2022-03-28 08:45:00 -07003096 auto asyncResp = std::make_shared<SensorsAsyncResp>(
Ed Tanous02da7c52022-02-27 00:09:02 -08003097 aResp, chassisId, sensors::dbus::sensorPaths,
Ed Tanous002d39b2022-05-31 08:59:27 -07003098 sensors::node::sensors,
3099 /*efficientExpand=*/true);
3100 getChassisData(asyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07003101
Ed Tanous002d39b2022-05-31 08:59:27 -07003102 BMCWEB_LOG_DEBUG
3103 << "SensorCollection doGet exit via efficient expand handler";
3104 return;
3105 };
3106
3107 // if there's no efficient expand available, we use the default
3108 // Query Parameters route
3109 auto asyncResp = std::make_shared<SensorsAsyncResp>(
3110 aResp, chassisId, sensors::dbus::sensorPaths,
3111 sensors::node::sensors);
3112
3113 // We get all sensors as hyperlinkes in the chassis (this
3114 // implies we reply on the default query parameters handler)
3115 getChassis(asyncResp,
3116 std::bind_front(sensors::getChassisCallback, asyncResp));
3117 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
Nan Zhou928fefb2022-03-28 08:45:00 -07003118 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003119}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003120
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003121inline void requestRoutesSensor(App& app)
3122{
3123 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003124 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003125 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003126 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003127}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003128
Ed Tanous1abe55e2018-09-05 08:30:59 -07003129} // namespace redfish