blob: c7676db0a711b2d1feb000dbc1003338ae9fa930 [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 Tanousf94c4ec2022-01-06 12:44:41 -0800352 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -0800353 sensorsAsyncResp, sensorNames](
354 const boost::system::error_code ec,
355 const dbus::utility::MapperGetSubTreeResponse&
356 subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530357 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700358 if (ec)
359 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800360 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530361 BMCWEB_LOG_ERROR
362 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700363 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100364 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100365
Ed Tanous1abe55e2018-09-05 08:30:59 -0700366 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
367
368 // Make unique list of connections only for requested sensor types and
369 // found in the chassis
370 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530371 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700372 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
373 // producers
374 connections.reserve(8);
375
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700376 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
377 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700378 {
379 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
380 }
381
382 for (const std::pair<
383 std::string,
384 std::vector<std::pair<std::string, std::vector<std::string>>>>&
385 object : subtree)
386 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700387 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700388 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700389 for (const std::pair<std::string, std::vector<std::string>>&
390 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700391 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700392 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
393 connections.insert(objData.first);
394 objectsWithConnection.insert(
395 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700396 }
397 }
398 }
399 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530400 callback(std::move(connections), std::move(objectsWithConnection));
401 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700403 // Make call to ObjectMapper to find all sensors objects
404 crow::connections::systemBus->async_method_call(
405 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
406 "/xyz/openbmc_project/object_mapper",
407 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530408 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
409}
410
411/**
412 * @brief Create connections necessary for sensors
413 * @param SensorsAsyncResp Pointer to object holding response data
414 * @param sensorNames Sensors retrieved from chassis
415 * @param callback Callback for processing gathered connections
416 */
417template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700418void getConnections(
Ed Tanous81ce6092020-12-17 16:54:55 +0000419 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700420 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
421 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530422{
423 auto objectsWithConnectionCb =
424 [callback](const boost::container::flat_set<std::string>& connections,
425 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700426 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000427 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530428 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100429}
430
431/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700432 * @brief Shrinks the list of sensors for processing
433 * @param SensorsAysncResp The class holding the Redfish response
434 * @param allSensors A list of all the sensors associated to the
435 * chassis element (i.e. baseboard, front panel, etc...)
436 * @param activeSensors A list that is a reduction of the incoming
437 * allSensors list. Eliminate Thermal sensors when a Power request is
438 * made, and eliminate Power sensors when a Thermal request is made.
439 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000440inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000441 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700442 const std::vector<std::string>* allSensors,
Ed Tanousb5a76932020-09-29 16:16:58 -0700443 const std::shared_ptr<boost::container::flat_set<std::string>>&
444 activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445{
Ed Tanous81ce6092020-12-17 16:54:55 +0000446 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447 {
448 return;
449 }
450 if ((allSensors == nullptr) || (activeSensors == nullptr))
451 {
452 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800453 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000454 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200455 ? "Temperatures"
456 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700457
458 return;
459 }
460 if (allSensors->empty())
461 {
462 // Nothing to do, the activeSensors object is also empty
463 return;
464 }
465
Ed Tanous02da7c52022-02-27 00:09:02 -0800466 for (std::string_view type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700467 {
468 for (const std::string& sensor : *allSensors)
469 {
470 if (boost::starts_with(sensor, type))
471 {
472 activeSensors->emplace(sensor);
473 }
474 }
475 }
476}
477
478/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800479 * @brief Retrieves valid chassis path
480 * @param asyncResp Pointer to object holding response data
481 * @param callback Callback for next step to get valid chassis path
482 */
483template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700484void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800485 Callback&& callback)
486{
487 BMCWEB_LOG_DEBUG << "checkChassisId enter";
488 const std::array<const char*, 2> interfaces = {
489 "xyz.openbmc_project.Inventory.Item.Board",
490 "xyz.openbmc_project.Inventory.Item.Chassis"};
491
Ed Tanousb9d36b42022-02-26 21:42:46 -0800492 auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
493 const boost::system::error_code ec,
494 const dbus::utility::MapperGetSubTreePathsResponse&
495 chassisPaths) mutable {
496 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
497 if (ec)
498 {
499 BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
500 << ec;
501 messages::internalError(asyncResp->asyncResp->res);
502 return;
503 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800504
Ed Tanousb9d36b42022-02-26 21:42:46 -0800505 std::optional<std::string> chassisPath;
506 std::string chassisName;
507 for (const std::string& chassis : chassisPaths)
508 {
509 sdbusplus::message::object_path path(chassis);
510 chassisName = path.filename();
511 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800512 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800513 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
514 continue;
Carol Wang4bb3dc32019-10-17 18:15:02 +0800515 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800516 if (chassisName == asyncResp->chassisId)
517 {
518 chassisPath = chassis;
519 break;
520 }
521 }
522 callback(chassisPath);
523 };
Carol Wang4bb3dc32019-10-17 18:15:02 +0800524
525 // Get the Chassis Collection
526 crow::connections::systemBus->async_method_call(
527 respHandler, "xyz.openbmc_project.ObjectMapper",
528 "/xyz/openbmc_project/object_mapper",
529 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
530 "/xyz/openbmc_project/inventory", 0, interfaces);
531 BMCWEB_LOG_DEBUG << "checkChassisId exit";
532}
533
534/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100535 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200536 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100537 * @param callback Callback for next step in gathered sensor processing
538 */
539template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700540void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 Callback&& callback)
542{
543 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500544 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700545 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500546 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800547 auto respHandler = [callback{std::forward<Callback>(callback)},
548 sensorsAsyncResp](
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700549 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800550 const dbus::utility::MapperGetSubTreePathsResponse&
551 chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
553 if (ec)
554 {
555 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800556 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700557 return;
558 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100559
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700560 const std::string* chassisPath = nullptr;
561 std::string chassisName;
562 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700563 {
George Liu28aa8de2021-02-01 15:13:30 +0800564 sdbusplus::message::object_path path(chassis);
565 chassisName = path.filename();
566 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700567 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700568 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700569 continue;
570 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700571 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700572 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700573 chassisPath = &chassis;
574 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700575 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700576 }
577 if (chassisPath == nullptr)
578 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800579 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
580 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700581 return;
582 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700583
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700584 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200585 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700586 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800587 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700588 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200590 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700591 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800592 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700593 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800594 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
595 nlohmann::json::array();
596 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800597 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700598 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200599 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500600 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800601 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500602 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800603 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500604 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800605 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500606 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800607 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
608 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500609 }
610
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200611 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500612 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800613 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500614 }
615
zhanghch058d1b46d2021-04-01 11:18:24 +0800616 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700617 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
618 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800619 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500620 // Get the list of all sensors for this Chassis element
621 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700622 sdbusplus::asio::getProperty<std::vector<std::string>>(
623 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
624 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800625 [sensorsAsyncResp,
626 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700627 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700628 const std::vector<std::string>& nodeSensorList) {
Ed Tanous271584a2019-07-09 16:24:22 -0700629 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700630 {
Ed Tanous271584a2019-07-09 16:24:22 -0700631 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700632 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800633 messages::internalError(
634 sensorsAsyncResp->asyncResp->res);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700635 return;
636 }
637 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700638 const std::shared_ptr<boost::container::flat_set<std::string>>
639 culledSensorList = std::make_shared<
640 boost::container::flat_set<std::string>>();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700641 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700642 culledSensorList);
643 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700644 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100645 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100646
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700647 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700648 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700649 respHandler, "xyz.openbmc_project.ObjectMapper",
650 "/xyz/openbmc_project/object_mapper",
651 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700652 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700653 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100654}
655
656/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600657 * @brief Finds all DBus object paths that implement ObjectManager.
658 *
659 * Creates a mapping from the associated connection name to the object path.
660 *
661 * Finds the object paths asynchronously. Invokes callback when information has
662 * been obtained.
663 *
664 * The callback must have the following signature:
665 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500666 * callback(std::shared_ptr<boost::container::flat_map<std::string,
667 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600668 * @endcode
669 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700670 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600671 * @param callback Callback to invoke when object paths obtained.
672 */
673template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700674void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000675 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700676 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600677{
678 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
679 const std::array<std::string, 1> interfaces = {
680 "org.freedesktop.DBus.ObjectManager"};
681
682 // Response handler for GetSubTree DBus method
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800683 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -0800684 sensorsAsyncResp](
685 const boost::system::error_code ec,
686 const dbus::utility::MapperGetSubTreeResponse&
687 subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600688 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
689 if (ec)
690 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800691 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600692 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
693 << ec;
694 return;
695 }
696
697 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500698 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
699 objectMgrPaths = std::make_shared<
700 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600701 for (const std::pair<
702 std::string,
703 std::vector<std::pair<std::string, std::vector<std::string>>>>&
704 object : subtree)
705 {
706 // Loop over connections for current object path
707 const std::string& objectPath = object.first;
708 for (const std::pair<std::string, std::vector<std::string>>&
709 objData : object.second)
710 {
711 // Add mapping from connection to object path
712 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500713 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600714 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
715 << objectPath;
716 }
717 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500718 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600719 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
720 };
721
722 // Query mapper for all DBus object paths that implement ObjectManager
723 crow::connections::systemBus->async_method_call(
724 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
725 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700726 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600727 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
728}
729
730/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500731 * @brief Returns the Redfish State value for the specified inventory item.
732 * @param inventoryItem D-Bus inventory item associated with a sensor.
733 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700734 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000735inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500736{
737 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
738 {
739 return "Absent";
740 }
James Feist34dd1792019-05-17 14:10:54 -0700741
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500742 return "Enabled";
743}
744
745/**
746 * @brief Returns the Redfish Health value for the specified sensor.
747 * @param sensorJson Sensor JSON object.
748 * @param interfacesDict Map of all sensor interfaces.
749 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
750 * be nullptr if no associated inventory item was found.
751 * @return Health value for sensor.
752 */
Ed Tanous711ac7a2021-12-20 09:34:41 -0800753inline std::string
754 getHealth(nlohmann::json& sensorJson,
755 const dbus::utility::DBusInteracesMap& interfacesDict,
756 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700757{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500758 // Get current health value (if any) in the sensor JSON object. Some JSON
759 // objects contain multiple sensors (such as PowerSupplies). We want to set
760 // the overall health to be the most severe of any of the sensors.
761 std::string currentHealth;
762 auto statusIt = sensorJson.find("Status");
763 if (statusIt != sensorJson.end())
764 {
765 auto healthIt = statusIt->find("Health");
766 if (healthIt != statusIt->end())
767 {
768 std::string* health = healthIt->get_ptr<std::string*>();
769 if (health != nullptr)
770 {
771 currentHealth = *health;
772 }
773 }
774 }
775
776 // If current health in JSON object is already Critical, return that. This
777 // should override the sensor health, which might be less severe.
778 if (currentHealth == "Critical")
779 {
780 return "Critical";
781 }
782
783 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800784
Ed Tanous9eb808c2022-01-25 10:19:23 -0800785 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700786 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800787 if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
James Feist34dd1792019-05-17 14:10:54 -0700788 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800789 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700790 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800791 if (valueName == "CriticalAlarmHigh" ||
792 valueName == "CriticalAlarmLow")
793 {
794 const bool* asserted = std::get_if<bool>(&value);
795 if (asserted == nullptr)
796 {
797 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
798 }
799 else if (*asserted)
800 {
801 return "Critical";
802 }
803 }
James Feist34dd1792019-05-17 14:10:54 -0700804 }
805 }
806 }
807
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500808 // Check if associated inventory item is not functional
809 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
810 {
811 return "Critical";
812 }
813
814 // If current health in JSON object is already Warning, return that. This
815 // should override the sensor status, which might be less severe.
816 if (currentHealth == "Warning")
817 {
818 return "Warning";
819 }
820
821 // Check if sensor has warning threshold alarm
Ed Tanous9eb808c2022-01-25 10:19:23 -0800822 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700823 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800824 if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
James Feist34dd1792019-05-17 14:10:54 -0700825 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800826 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700827 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800828 if (valueName == "WarningAlarmHigh" ||
829 valueName == "WarningAlarmLow")
830 {
831 const bool* asserted = std::get_if<bool>(&value);
832 if (asserted == nullptr)
833 {
834 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
835 }
836 else if (*asserted)
837 {
Ed Tanousebe4d912022-01-12 12:38:17 -0800838 return "Warning";
Ed Tanous711ac7a2021-12-20 09:34:41 -0800839 }
840 }
James Feist34dd1792019-05-17 14:10:54 -0700841 }
842 }
843 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500844
James Feist34dd1792019-05-17 14:10:54 -0700845 return "OK";
846}
847
Ed Tanous23a21a12020-07-25 04:45:05 +0000848inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500849 const InventoryItem* inventoryItem)
850{
851 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
852 {
853 switch (inventoryItem->ledState)
854 {
855 case LedState::OFF:
856 sensorJson["IndicatorLED"] = "Off";
857 break;
858 case LedState::ON:
859 sensorJson["IndicatorLED"] = "Lit";
860 break;
861 case LedState::BLINK:
862 sensorJson["IndicatorLED"] = "Blinking";
863 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000864 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500865 break;
866 }
867 }
868}
869
James Feist34dd1792019-05-17 14:10:54 -0700870/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100871 * @brief Builds a json sensor representation of a sensor.
872 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500873 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100874 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200875 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100876 * @param interfacesDict A dictionary of the interfaces and properties of said
877 * interfaces to be built from
878 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500879 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
880 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100881 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000882inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100883 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700884 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800885 const dbus::utility::DBusInteracesMap& interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000886 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700888 // Assume values exist as is (10^0 == 1) if no scale exists
889 int64_t scaleMultiplier = 0;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800890 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700891 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800892 if (interface == "xyz.openbmc_project.Sensor.Value")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700893 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800894 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800895 {
896 if (valueName == "Scale")
897 {
898 const int64_t* int64Value = std::get_if<int64_t>(&value);
899 if (int64Value != nullptr)
900 {
901 scaleMultiplier = *int64Value;
902 }
903 }
904 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100905 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100906 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200908 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500909 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500910 // For sensors in SensorCollection we set Id instead of MemberId,
911 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000912 sensorJson["Id"] = sensorName;
913 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500914 }
915 else if (sensorType != "power")
916 {
917 // Set MemberId and Name for non-power sensors. For PowerSupplies and
918 // PowerControl, those properties have more general values because
919 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000920 sensorJson["MemberId"] = sensorName;
921 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500922 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700923
Ed Tanous81ce6092020-12-17 16:54:55 +0000924 sensorJson["Status"]["State"] = getState(inventoryItem);
925 sensorJson["Status"]["Health"] =
926 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700927
928 // Parameter to set to override the type we get from dbus, and force it to
929 // int, regardless of what is available. This is used for schemas like fan,
930 // that require integers, not floats.
931 bool forceToInt = false;
932
Anthony Wilson3929aca2019-07-19 15:42:33 -0500933 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200934 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500935 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000936 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000937
938 const std::string& readingType = sensors::toReadingType(sensorType);
939 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500940 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000941 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
942 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500943 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000944 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500945 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000946 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500947 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000948
949 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
950 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200951 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000952 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
953 << sensorType;
954 }
955 else
956 {
957 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200958 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500959 }
960 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500962 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000963 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700964 // TODO(ed) Documentation says that path should be type fan_tach,
965 // implementation seems to implement fan
966 }
967 else if (sensorType == "fan" || sensorType == "fan_tach")
968 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500969 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000970 sensorJson["ReadingUnits"] = "RPM";
971 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
972 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973 forceToInt = true;
974 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700975 else if (sensorType == "fan_pwm")
976 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500977 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000978 sensorJson["ReadingUnits"] = "Percent";
979 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
980 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700981 forceToInt = true;
982 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983 else if (sensorType == "voltage")
984 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500985 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000986 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700988 else if (sensorType == "power")
989 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700990 std::string sensorNameLower =
991 boost::algorithm::to_lower_copy(sensorName);
992
Ed Tanous55f79e62022-01-25 11:26:16 -0800993 if (sensorName == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000994 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000995 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500996 // Put multiple "sensors" into a single PowerControl, so have
997 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000998 sensorJson["MemberId"] = "0";
999 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -05001000 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +00001001 }
1002 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001003 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001004 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001005 }
1006 else
1007 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001008 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001009 }
Ed Tanous2474adf2018-09-05 16:31:16 -07001010 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 else
1012 {
1013 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
1014 return;
1015 }
1016 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -05001017 std::vector<
1018 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
1019 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001020 properties.reserve(7);
1021
1022 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001023
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001024 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -05001025 {
1026 properties.emplace_back(
1027 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
1028 "/Thresholds/UpperCaution/Reading"_json_pointer);
1029 properties.emplace_back(
1030 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1031 "/Thresholds/LowerCaution/Reading"_json_pointer);
1032 properties.emplace_back(
1033 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1034 "/Thresholds/UpperCritical/Reading"_json_pointer);
1035 properties.emplace_back(
1036 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1037 "/Thresholds/LowerCritical/Reading"_json_pointer);
1038 }
1039 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001040 {
1041 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001042 "WarningHigh",
1043 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001044 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001045 "WarningLow",
1046 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001047 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001048 "CriticalHigh",
1049 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001050 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001051 "CriticalLow",
1052 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001053 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001054
Ed Tanous2474adf2018-09-05 16:31:16 -07001055 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1056
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001057 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001058 {
1059 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001060 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001061 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001062 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001063 }
1064 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065 {
1066 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001067 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001068 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001069 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001070 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001071 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001072 {
1073 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001074 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001075 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001076 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077 }
1078
Anthony Wilson3929aca2019-07-19 15:42:33 -05001079 for (const std::tuple<const char*, const char*,
1080 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001081 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001082 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001083 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001084 if (interface != std::get<0>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001085 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001086 continue;
1087 }
Ed Tanous55f79e62022-01-25 11:26:16 -08001088 for (const auto& [valueName, valueVariant] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001089 {
1090 if (valueName != std::get<1>(p))
1091 {
1092 continue;
1093 }
Anthony Wilson3929aca2019-07-19 15:42:33 -05001094
1095 // The property we want to set may be nested json, so use
1096 // a json_pointer for easy indexing into the json structure.
1097 const nlohmann::json::json_pointer& key = std::get<2>(p);
1098
Ed Tanous1abe55e2018-09-05 08:30:59 -07001099 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001100 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001101
Ed Tanousabf2add2019-01-22 16:40:12 -08001102 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001103 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001104 double temp = 0.0;
1105 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001106 {
Ed Tanous271584a2019-07-09 16:24:22 -07001107 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001108 }
1109 else if (doubleValue != nullptr)
1110 {
1111 temp = *doubleValue;
1112 }
Eddie James028f7eb2019-05-17 21:24:36 +00001113 else if (uValue != nullptr)
1114 {
1115 temp = *uValue;
1116 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001117 else
1118 {
1119 BMCWEB_LOG_ERROR
1120 << "Got value interface that wasn't int or double";
1121 continue;
1122 }
1123 temp = temp * std::pow(10, scaleMultiplier);
1124 if (forceToInt)
1125 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001126 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001127 }
1128 else
1129 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001130 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001131 }
1132 }
1133 }
1134 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001135
Ed Tanous81ce6092020-12-17 16:54:55 +00001136 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001137 "/xyz/openbmc_project/sensors/" + sensorType +
1138 "/" + sensorName);
1139
Ed Tanous1abe55e2018-09-05 08:30:59 -07001140 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001141}
1142
Ed Tanousb5a76932020-09-29 16:16:58 -07001143inline void populateFanRedundancy(
1144 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001145{
1146 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001147 [sensorsAsyncResp](
1148 const boost::system::error_code ec,
1149 const dbus::utility::MapperGetSubTreeResponse& resp) {
James Feist8bd25cc2019-03-15 15:14:00 -07001150 if (ec)
1151 {
1152 return; // don't have to have this interface
1153 }
Ed Tanouse278c182019-03-13 16:23:37 -07001154 for (const std::pair<std::string,
1155 std::vector<std::pair<
1156 std::string, std::vector<std::string>>>>&
1157 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001158 {
Ed Tanouse278c182019-03-13 16:23:37 -07001159 const std::string& path = pathPair.first;
1160 const std::vector<
1161 std::pair<std::string, std::vector<std::string>>>& objDict =
1162 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001163 if (objDict.empty())
1164 {
1165 continue; // this should be impossible
1166 }
1167
1168 const std::string& owner = objDict.begin()->first;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001169 sdbusplus::asio::getProperty<std::vector<std::string>>(
1170 *crow::connections::systemBus,
1171 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1172 "xyz.openbmc_project.Association", "endpoints",
1173 [path, owner, sensorsAsyncResp](
1174 const boost::system::error_code e,
1175 const std::vector<std::string>& endpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001176 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001177 {
1178 return; // if they don't have an association we
1179 // can't tell what chassis is
1180 }
James Feist8bd25cc2019-03-15 15:14:00 -07001181 auto found = std::find_if(
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001182 endpoints.begin(), endpoints.end(),
James Feist8bd25cc2019-03-15 15:14:00 -07001183 [sensorsAsyncResp](const std::string& entry) {
1184 return entry.find(
1185 sensorsAsyncResp->chassisId) !=
1186 std::string::npos;
1187 });
1188
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001189 if (found == endpoints.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001190 {
1191 return;
1192 }
1193 crow::connections::systemBus->async_method_call(
1194 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001195 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001196 const boost::container::flat_map<
1197 std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -08001198 dbus::utility::DbusVariantType>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001199 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001200 {
1201 return; // don't have to have this
1202 // interface
1203 }
1204 auto findFailures = ret.find("AllowedFailures");
1205 auto findCollection = ret.find("Collection");
1206 auto findStatus = ret.find("Status");
1207
1208 if (findFailures == ret.end() ||
1209 findCollection == ret.end() ||
1210 findStatus == ret.end())
1211 {
1212 BMCWEB_LOG_ERROR
1213 << "Invalid redundancy interface";
1214 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001215 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001216 return;
1217 }
1218
Ed Tanous9eb808c2022-01-25 10:19:23 -08001219 const uint8_t* allowedFailures =
1220 std::get_if<uint8_t>(
1221 &(findFailures->second));
1222 const std::vector<std::string>* collection =
James Feist8bd25cc2019-03-15 15:14:00 -07001223 std::get_if<std::vector<std::string>>(
1224 &(findCollection->second));
Ed Tanous9eb808c2022-01-25 10:19:23 -08001225 const std::string* status =
1226 std::get_if<std::string>(
1227 &(findStatus->second));
James Feist8bd25cc2019-03-15 15:14:00 -07001228
1229 if (allowedFailures == nullptr ||
1230 collection == nullptr || status == nullptr)
1231 {
1232
1233 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001234 << "Invalid redundancy interface types";
James Feist8bd25cc2019-03-15 15:14:00 -07001235 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001236 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001237 return;
1238 }
George Liu28aa8de2021-02-01 15:13:30 +08001239 sdbusplus::message::object_path objectPath(
1240 path);
1241 std::string name = objectPath.filename();
1242 if (name.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001243 {
1244 // this should be impossible
1245 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001246 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001247 return;
1248 }
James Feist8bd25cc2019-03-15 15:14:00 -07001249 std::replace(name.begin(), name.end(), '_',
1250 ' ');
1251
1252 std::string health;
1253
1254 if (boost::ends_with(*status, "Full"))
1255 {
1256 health = "OK";
1257 }
1258 else if (boost::ends_with(*status, "Degraded"))
1259 {
1260 health = "Warning";
1261 }
1262 else
1263 {
1264 health = "Critical";
1265 }
Ed Tanous14766872022-03-15 10:44:42 -07001266 nlohmann::json::array_t redfishCollection;
James Feist8bd25cc2019-03-15 15:14:00 -07001267 const auto& fanRedfish =
zhanghch058d1b46d2021-04-01 11:18:24 +08001268 sensorsAsyncResp->asyncResp->res
1269 .jsonValue["Fans"];
James Feist8bd25cc2019-03-15 15:14:00 -07001270 for (const std::string& item : *collection)
1271 {
George Liu28aa8de2021-02-01 15:13:30 +08001272 sdbusplus::message::object_path path(item);
1273 std::string itemName = path.filename();
1274 if (itemName.empty())
1275 {
1276 continue;
1277 }
James Feist8bd25cc2019-03-15 15:14:00 -07001278 /*
1279 todo(ed): merge patch that fixes the names
1280 std::replace(itemName.begin(),
1281 itemName.end(), '_', ' ');*/
1282 auto schemaItem = std::find_if(
1283 fanRedfish.begin(), fanRedfish.end(),
1284 [itemName](const nlohmann::json& fan) {
1285 return fan["MemberId"] == itemName;
1286 });
1287 if (schemaItem != fanRedfish.end())
1288 {
Ed Tanous14766872022-03-15 10:44:42 -07001289 nlohmann::json::object_t collection;
1290 collection["@odata.id"] =
1291 (*schemaItem)["@odata.id"];
1292 redfishCollection.emplace_back(
1293 std::move(collection));
James Feist8bd25cc2019-03-15 15:14:00 -07001294 }
1295 else
1296 {
1297 BMCWEB_LOG_ERROR
1298 << "failed to find fan in schema";
1299 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001300 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001301 return;
1302 }
1303 }
1304
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001305 size_t minNumNeeded =
Ed Tanous26f69762022-01-25 09:49:11 -08001306 collection->empty()
1307 ? 0
1308 : collection->size() - *allowedFailures;
Ed Tanous271584a2019-07-09 16:24:22 -07001309 nlohmann::json& jResp =
zhanghch058d1b46d2021-04-01 11:18:24 +08001310 sensorsAsyncResp->asyncResp->res
Ed Tanous271584a2019-07-09 16:24:22 -07001311 .jsonValue["Redundancy"];
Ed Tanous14766872022-03-15 10:44:42 -07001312
1313 nlohmann::json::object_t redundancy;
1314 redundancy["@odata.id"] =
1315 "/redfish/v1/Chassis/" +
1316 sensorsAsyncResp->chassisId + "/" +
1317 sensorsAsyncResp->chassisSubNode +
1318 "#/Redundancy/" +
1319 std::to_string(jResp.size());
1320 redundancy["@odata.type"] =
1321 "#Redundancy.v1_3_2.Redundancy";
1322 redundancy["MinNumNeeded"] = minNumNeeded;
1323 redundancy["MemberId"] = name;
1324 redundancy["Mode"] = "N+m";
1325 redundancy["Name"] = name;
1326 redundancy["RedundancySet"] = redfishCollection;
1327 redundancy["Status"]["Health"] = health;
1328 redundancy["Status"]["State"] = "Enabled";
1329
1330 jResp.push_back(std::move(redundancy));
James Feist8bd25cc2019-03-15 15:14:00 -07001331 },
1332 owner, path, "org.freedesktop.DBus.Properties",
1333 "GetAll",
1334 "xyz.openbmc_project.Control.FanRedundancy");
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001335 });
James Feist8bd25cc2019-03-15 15:14:00 -07001336 }
1337 },
1338 "xyz.openbmc_project.ObjectMapper",
1339 "/xyz/openbmc_project/object_mapper",
1340 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1341 "/xyz/openbmc_project/control", 2,
1342 std::array<const char*, 1>{
1343 "xyz.openbmc_project.Control.FanRedundancy"});
1344}
1345
Ed Tanousb5a76932020-09-29 16:16:58 -07001346inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001347 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001348{
zhanghch058d1b46d2021-04-01 11:18:24 +08001349 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001350 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001351 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001352 {
1353 sensorHeaders = {"Voltages", "PowerSupplies"};
1354 }
1355 for (const std::string& sensorGroup : sensorHeaders)
1356 {
1357 nlohmann::json::iterator entry = response.find(sensorGroup);
1358 if (entry != response.end())
1359 {
1360 std::sort(entry->begin(), entry->end(),
1361 [](nlohmann::json& c1, nlohmann::json& c2) {
1362 return c1["Name"] < c2["Name"];
1363 });
1364
1365 // add the index counts to the end of each entry
1366 size_t count = 0;
1367 for (nlohmann::json& sensorJson : *entry)
1368 {
1369 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1370 if (odata == sensorJson.end())
1371 {
1372 continue;
1373 }
1374 std::string* value = odata->get_ptr<std::string*>();
1375 if (value != nullptr)
1376 {
1377 *value += std::to_string(count);
1378 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001379 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001380 }
1381 }
1382 }
1383 }
1384}
1385
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001386/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001387 * @brief Finds the inventory item with the specified object path.
1388 * @param inventoryItems D-Bus inventory items associated with sensors.
1389 * @param invItemObjPath D-Bus object path of inventory item.
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* findInventoryItem(
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& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001395{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001396 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001397 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001398 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001399 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001400 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001401 }
1402 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001403 return nullptr;
1404}
1405
1406/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001407 * @brief Finds the inventory item associated with the specified sensor.
1408 * @param inventoryItems D-Bus inventory items associated with sensors.
1409 * @param sensorObjPath D-Bus object path of sensor.
1410 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001411 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001412inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001413 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001414 const std::string& sensorObjPath)
1415{
1416 for (InventoryItem& inventoryItem : *inventoryItems)
1417 {
1418 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1419 {
1420 return &inventoryItem;
1421 }
1422 }
1423 return nullptr;
1424}
1425
1426/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001427 * @brief Finds the inventory item associated with the specified led path.
1428 * @param inventoryItems D-Bus inventory items associated with sensors.
1429 * @param ledObjPath D-Bus object path of led.
1430 * @return Inventory item within vector, or nullptr if no match found.
1431 */
1432inline InventoryItem*
1433 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1434 const std::string& ledObjPath)
1435{
1436 for (InventoryItem& inventoryItem : inventoryItems)
1437 {
1438 if (inventoryItem.ledObjectPath == ledObjPath)
1439 {
1440 return &inventoryItem;
1441 }
1442 }
1443 return nullptr;
1444}
1445
1446/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001447 * @brief Adds inventory item and associated sensor to specified vector.
1448 *
1449 * Adds a new InventoryItem to the vector if necessary. Searches for an
1450 * existing InventoryItem with the specified object path. If not found, one is
1451 * added to the vector.
1452 *
1453 * Next, the specified sensor is added to the set of sensors associated with the
1454 * InventoryItem.
1455 *
1456 * @param inventoryItems D-Bus inventory items associated with sensors.
1457 * @param invItemObjPath D-Bus object path of inventory item.
1458 * @param sensorObjPath D-Bus object path of sensor
1459 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001460inline void addInventoryItem(
1461 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1462 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001463{
1464 // Look for inventory item in vector
1465 InventoryItem* inventoryItem =
1466 findInventoryItem(inventoryItems, invItemObjPath);
1467
1468 // If inventory item doesn't exist in vector, add it
1469 if (inventoryItem == nullptr)
1470 {
1471 inventoryItems->emplace_back(invItemObjPath);
1472 inventoryItem = &(inventoryItems->back());
1473 }
1474
1475 // Add sensor to set of sensors associated with inventory item
1476 inventoryItem->sensors.emplace(sensorObjPath);
1477}
1478
1479/**
1480 * @brief Stores D-Bus data in the specified inventory item.
1481 *
1482 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1483 * specified InventoryItem.
1484 *
1485 * This data is later used to provide sensor property values in the JSON
1486 * response.
1487 *
1488 * @param inventoryItem Inventory item where data will be stored.
1489 * @param interfacesDict Map containing D-Bus interfaces and their properties
1490 * for the specified inventory item.
1491 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001492inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001493 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001494 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001495{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001497
Ed Tanous9eb808c2022-01-25 10:19:23 -08001498 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001499 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001500 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001501 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001502 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001503 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001504 if (name == "Present")
1505 {
1506 const bool* value = std::get_if<bool>(&dbusValue);
1507 if (value != nullptr)
1508 {
1509 inventoryItem.isPresent = *value;
1510 }
1511 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001512 }
1513 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001514 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001515
Ed Tanous711ac7a2021-12-20 09:34:41 -08001516 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001517 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001518 inventoryItem.isPowerSupply = true;
1519 }
1520
1521 // Get properties from Inventory.Decorator.Asset interface
1522 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1523 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001524 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001525 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001526 if (name == "Manufacturer")
1527 {
1528 const std::string* value =
1529 std::get_if<std::string>(&dbusValue);
1530 if (value != nullptr)
1531 {
1532 inventoryItem.manufacturer = *value;
1533 }
1534 }
1535 if (name == "Model")
1536 {
1537 const std::string* value =
1538 std::get_if<std::string>(&dbusValue);
1539 if (value != nullptr)
1540 {
1541 inventoryItem.model = *value;
1542 }
1543 }
1544 if (name == "SerialNumber")
1545 {
1546 const std::string* value =
1547 std::get_if<std::string>(&dbusValue);
1548 if (value != nullptr)
1549 {
1550 inventoryItem.serialNumber = *value;
1551 }
1552 }
1553 if (name == "PartNumber")
1554 {
1555 const std::string* value =
1556 std::get_if<std::string>(&dbusValue);
1557 if (value != nullptr)
1558 {
1559 inventoryItem.partNumber = *value;
1560 }
1561 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001562 }
1563 }
1564
Ed Tanous711ac7a2021-12-20 09:34:41 -08001565 if (interface ==
1566 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001567 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001568 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001569 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001570 if (name == "Functional")
1571 {
1572 const bool* value = std::get_if<bool>(&dbusValue);
1573 if (value != nullptr)
1574 {
1575 inventoryItem.isFunctional = *value;
1576 }
1577 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 }
1579 }
1580 }
1581}
1582
1583/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001584 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001585 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001586 * Uses the specified connections (services) to obtain D-Bus data for inventory
1587 * items associated with sensors. Stores the resulting data in the
1588 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001589 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001590 * This data is later used to provide sensor property values in the JSON
1591 * response.
1592 *
1593 * Finds the inventory item data asynchronously. Invokes callback when data has
1594 * been obtained.
1595 *
1596 * The callback must have the following signature:
1597 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001598 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001599 * @endcode
1600 *
1601 * This function is called recursively, obtaining data asynchronously from one
1602 * connection in each call. This ensures the callback is not invoked until the
1603 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001604 *
1605 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 * @param inventoryItems D-Bus inventory items associated with sensors.
1607 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001608 * @param objectMgrPaths Mappings from connection name to DBus object path that
1609 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001610 * @param callback Callback to invoke when inventory data has been obtained.
1611 * @param invConnectionsIndex Current index in invConnections. Only specified
1612 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001614template <typename Callback>
1615static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001616 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001617 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1619 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001620 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001621 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001622{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001623 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001624
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001625 // If no more connections left, call callback
1626 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001627 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001628 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001629 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1630 return;
1631 }
1632
1633 // Get inventory item data from current connection
1634 auto it = invConnections->nth(invConnectionsIndex);
1635 if (it != invConnections->end())
1636 {
1637 const std::string& invConnection = *it;
1638
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001639 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001640 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001641 objectMgrPaths,
1642 callback{std::forward<Callback>(callback)},
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001643 invConnectionsIndex](
1644 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001645 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001646 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001647 if (ec)
1648 {
1649 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001650 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001651 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001652 return;
1653 }
1654
1655 // Loop through returned object paths
1656 for (const auto& objDictEntry : resp)
1657 {
1658 const std::string& objPath =
1659 static_cast<const std::string&>(objDictEntry.first);
1660
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001661 // If this object path is one of the specified inventory items
1662 InventoryItem* inventoryItem =
1663 findInventoryItem(inventoryItems, objPath);
1664 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001665 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001666 // Store inventory data in InventoryItem
1667 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001668 }
1669 }
1670
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001671 // Recurse to get inventory item data from next connection
1672 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1673 invConnections, objectMgrPaths,
1674 std::move(callback), invConnectionsIndex + 1);
1675
1676 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001677 };
1678
1679 // Find DBus object path that implements ObjectManager for the current
1680 // connection. If no mapping found, default to "/".
1681 auto iter = objectMgrPaths->find(invConnection);
1682 const std::string& objectMgrPath =
1683 (iter != objectMgrPaths->end()) ? iter->second : "/";
1684 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1685 << objectMgrPath;
1686
1687 // Get all object paths and their interfaces for current connection
1688 crow::connections::systemBus->async_method_call(
1689 std::move(respHandler), invConnection, objectMgrPath,
1690 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1691 }
1692
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001693 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001694}
1695
1696/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001697 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001698 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001699 * Gets the D-Bus connections (services) that provide data for the inventory
1700 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001701 *
1702 * Finds the connections asynchronously. Invokes callback when information has
1703 * been obtained.
1704 *
1705 * The callback must have the following signature:
1706 * @code
1707 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1708 * invConnections)
1709 * @endcode
1710 *
1711 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001712 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001713 * @param callback Callback to invoke when connections have been obtained.
1714 */
1715template <typename Callback>
1716static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001717 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1718 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001719 Callback&& callback)
1720{
1721 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1722
1723 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001724 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001726 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1727 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001728 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1729
1730 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001731 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -08001732 sensorsAsyncResp, inventoryItems](
1733 const boost::system::error_code ec,
1734 const dbus::utility::MapperGetSubTreeResponse&
1735 subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001736 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1737 if (ec)
1738 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001739 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001740 BMCWEB_LOG_ERROR
1741 << "getInventoryItemsConnections respHandler DBus error " << ec;
1742 return;
1743 }
1744
1745 // Make unique list of connections for desired inventory items
1746 std::shared_ptr<boost::container::flat_set<std::string>>
1747 invConnections =
1748 std::make_shared<boost::container::flat_set<std::string>>();
1749 invConnections->reserve(8);
1750
1751 // Loop through objects from GetSubTree
1752 for (const std::pair<
1753 std::string,
1754 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1755 object : subtree)
1756 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001757 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001758 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001759 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001760 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001761 // Store all connections to inventory item
1762 for (const std::pair<std::string, std::vector<std::string>>&
1763 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001764 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001765 const std::string& invConnection = objData.first;
1766 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001767 }
1768 }
1769 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001770
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001771 callback(invConnections);
1772 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1773 };
1774
1775 // Make call to ObjectMapper to find all inventory items
1776 crow::connections::systemBus->async_method_call(
1777 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1778 "/xyz/openbmc_project/object_mapper",
1779 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1780 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1781}
1782
1783/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001784 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001785 *
1786 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001787 * inventory items. Then finds the associations from those inventory items to
1788 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001789 *
1790 * Finds the inventory items asynchronously. Invokes callback when information
1791 * has been obtained.
1792 *
1793 * The callback must have the following signature:
1794 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001795 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001796 * @endcode
1797 *
1798 * @param sensorsAsyncResp Pointer to object holding response data.
1799 * @param sensorNames All sensors within the current chassis.
1800 * @param objectMgrPaths Mappings from connection name to DBus object path that
1801 * implements ObjectManager.
1802 * @param callback Callback to invoke when inventory items have been obtained.
1803 */
1804template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001805static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001806 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1807 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1808 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001809 objectMgrPaths,
1810 Callback&& callback)
1811{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001812 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001813
1814 // Response handler for GetManagedObjects
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001815 auto respHandler = [callback{std::forward<Callback>(callback)},
1816 sensorsAsyncResp,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001817 sensorNames](const boost::system::error_code ec,
1818 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001819 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001820 if (ec)
1821 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001822 BMCWEB_LOG_ERROR
1823 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001824 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001825 return;
1826 }
1827
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001828 // Create vector to hold list of inventory items
1829 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1830 std::make_shared<std::vector<InventoryItem>>();
1831
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001832 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001833 std::string sensorAssocPath;
1834 sensorAssocPath.reserve(128); // avoid memory allocations
1835 for (const auto& objDictEntry : resp)
1836 {
1837 const std::string& objPath =
1838 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001839
1840 // If path is inventory association for one of the specified sensors
1841 for (const std::string& sensorName : *sensorNames)
1842 {
1843 sensorAssocPath = sensorName;
1844 sensorAssocPath += "/inventory";
1845 if (objPath == sensorAssocPath)
1846 {
1847 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001848 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001849 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001850 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001851 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001852 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001853 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001854 if (valueName == "endpoints")
1855 {
1856 const std::vector<std::string>* endpoints =
1857 std::get_if<std::vector<std::string>>(
1858 &value);
1859 if ((endpoints != nullptr) &&
1860 !endpoints->empty())
1861 {
1862 // Add inventory item to vector
1863 const std::string& invItemPath =
1864 endpoints->front();
1865 addInventoryItem(inventoryItems,
1866 invItemPath,
1867 sensorName);
1868 }
1869 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001870 }
1871 }
1872 }
1873 break;
1874 }
1875 }
1876 }
1877
Anthony Wilsond5005492019-07-31 16:34:17 -05001878 // Now loop through the returned object paths again, this time to
1879 // find the leds associated with the inventory items we just found
1880 std::string inventoryAssocPath;
1881 inventoryAssocPath.reserve(128); // avoid memory allocations
1882 for (const auto& objDictEntry : resp)
1883 {
1884 const std::string& objPath =
1885 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001886
1887 for (InventoryItem& inventoryItem : *inventoryItems)
1888 {
1889 inventoryAssocPath = inventoryItem.objectPath;
1890 inventoryAssocPath += "/leds";
1891 if (objPath == inventoryAssocPath)
1892 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001893 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001894 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001895 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001896 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001897 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001898 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001899 if (valueName == "endpoints")
1900 {
1901 const std::vector<std::string>* endpoints =
1902 std::get_if<std::vector<std::string>>(
1903 &value);
1904 if ((endpoints != nullptr) &&
1905 !endpoints->empty())
1906 {
1907 // Add inventory item to vector
1908 // Store LED path in inventory item
1909 const std::string& ledPath =
1910 endpoints->front();
1911 inventoryItem.ledObjectPath = ledPath;
1912 }
1913 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001914 }
1915 }
1916 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001917
Anthony Wilsond5005492019-07-31 16:34:17 -05001918 break;
1919 }
1920 }
1921 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001922 callback(inventoryItems);
1923 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001924 };
1925
1926 // Find DBus object path that implements ObjectManager for ObjectMapper
1927 std::string connection = "xyz.openbmc_project.ObjectMapper";
1928 auto iter = objectMgrPaths->find(connection);
1929 const std::string& objectMgrPath =
1930 (iter != objectMgrPaths->end()) ? iter->second : "/";
1931 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1932 << objectMgrPath;
1933
1934 // Call GetManagedObjects on the ObjectMapper to get all associations
1935 crow::connections::systemBus->async_method_call(
1936 std::move(respHandler), connection, objectMgrPath,
1937 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1938
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001939 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001940}
1941
1942/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001943 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1944 *
1945 * Uses the specified connections (services) to obtain D-Bus data for inventory
1946 * item leds associated with sensors. Stores the resulting data in the
1947 * inventoryItems vector.
1948 *
1949 * This data is later used to provide sensor property values in the JSON
1950 * response.
1951 *
1952 * Finds the inventory item led data asynchronously. Invokes callback when data
1953 * has been obtained.
1954 *
1955 * The callback must have the following signature:
1956 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001957 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001958 * @endcode
1959 *
1960 * This function is called recursively, obtaining data asynchronously from one
1961 * connection in each call. This ensures the callback is not invoked until the
1962 * last asynchronous function has completed.
1963 *
1964 * @param sensorsAsyncResp Pointer to object holding response data.
1965 * @param inventoryItems D-Bus inventory items associated with sensors.
1966 * @param ledConnections Connections that provide data for the inventory leds.
1967 * @param callback Callback to invoke when inventory data has been obtained.
1968 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1969 * in recursive calls to this function.
1970 */
1971template <typename Callback>
1972void getInventoryLedData(
1973 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1974 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1975 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1976 ledConnections,
1977 Callback&& callback, size_t ledConnectionsIndex = 0)
1978{
1979 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1980
1981 // If no more connections left, call callback
1982 if (ledConnectionsIndex >= ledConnections->size())
1983 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001984 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001985 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1986 return;
1987 }
1988
1989 // Get inventory item data from current connection
1990 auto it = ledConnections->nth(ledConnectionsIndex);
1991 if (it != ledConnections->end())
1992 {
1993 const std::string& ledPath = (*it).first;
1994 const std::string& ledConnection = (*it).second;
1995 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001996 auto respHandler =
1997 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001998 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001999 const boost::system::error_code ec, const std::string& state) {
2000 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
2001 if (ec)
2002 {
2003 BMCWEB_LOG_ERROR
2004 << "getInventoryLedData respHandler DBus error " << ec;
2005 messages::internalError(sensorsAsyncResp->asyncResp->res);
2006 return;
2007 }
Anthony Wilsond5005492019-07-31 16:34:17 -05002008
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002009 BMCWEB_LOG_DEBUG << "Led state: " << state;
Ed Tanous168e20c2021-12-13 14:39:53 -08002010 // Find inventory item with this LED object path
2011 InventoryItem* inventoryItem =
2012 findInventoryItemForLed(*inventoryItems, ledPath);
2013 if (inventoryItem != nullptr)
Anthony Wilsond5005492019-07-31 16:34:17 -05002014 {
Ed Tanous168e20c2021-12-13 14:39:53 -08002015 // Store LED state in InventoryItem
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002016 if (boost::ends_with(state, "On"))
Anthony Wilsond5005492019-07-31 16:34:17 -05002017 {
Ed Tanous168e20c2021-12-13 14:39:53 -08002018 inventoryItem->ledState = LedState::ON;
2019 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002020 else if (boost::ends_with(state, "Blink"))
Ed Tanous168e20c2021-12-13 14:39:53 -08002021 {
2022 inventoryItem->ledState = LedState::BLINK;
2023 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002024 else if (boost::ends_with(state, "Off"))
Ed Tanous168e20c2021-12-13 14:39:53 -08002025 {
2026 inventoryItem->ledState = LedState::OFF;
2027 }
2028 else
2029 {
2030 inventoryItem->ledState = LedState::UNKNOWN;
Anthony Wilsond5005492019-07-31 16:34:17 -05002031 }
2032 }
Anthony Wilsond5005492019-07-31 16:34:17 -05002033
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002034 // Recurse to get LED data from next connection
2035 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2036 ledConnections, std::move(callback),
2037 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05002038
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002039 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2040 };
Anthony Wilsond5005492019-07-31 16:34:17 -05002041
2042 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002043 sdbusplus::asio::getProperty<std::string>(
2044 *crow::connections::systemBus, ledConnection, ledPath,
2045 "xyz.openbmc_project.Led.Physical", "State",
2046 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05002047 }
2048
2049 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2050}
2051
2052/**
2053 * @brief Gets LED data for LEDs associated with given inventory items.
2054 *
2055 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2056 * associated with the specified inventory items. Then gets the LED data from
2057 * each connection and stores it in the inventory item.
2058 *
2059 * This data is later used to provide sensor property values in the JSON
2060 * response.
2061 *
2062 * Finds the LED data asynchronously. Invokes callback when information has
2063 * been obtained.
2064 *
2065 * The callback must have the following signature:
2066 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002067 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002068 * @endcode
2069 *
2070 * @param sensorsAsyncResp Pointer to object holding response data.
2071 * @param inventoryItems D-Bus inventory items associated with sensors.
2072 * @param callback Callback to invoke when inventory items have been obtained.
2073 */
2074template <typename Callback>
2075void getInventoryLeds(
2076 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2077 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2078 Callback&& callback)
2079{
2080 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2081
2082 const std::string path = "/xyz/openbmc_project";
2083 const std::array<std::string, 1> interfaces = {
2084 "xyz.openbmc_project.Led.Physical"};
2085
2086 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002087 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -08002088 sensorsAsyncResp, inventoryItems](
2089 const boost::system::error_code ec,
2090 const dbus::utility::MapperGetSubTreeResponse&
2091 subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05002092 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2093 if (ec)
2094 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002095 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002096 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2097 << ec;
2098 return;
2099 }
2100
2101 // Build map of LED object paths to connections
2102 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2103 ledConnections = std::make_shared<
2104 boost::container::flat_map<std::string, std::string>>();
2105
2106 // Loop through objects from GetSubTree
2107 for (const std::pair<
2108 std::string,
2109 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2110 object : subtree)
2111 {
2112 // Check if object path is LED for one of the specified inventory
2113 // items
2114 const std::string& ledPath = object.first;
2115 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2116 {
2117 // Add mapping from ledPath to connection
2118 const std::string& connection = object.second.begin()->first;
2119 (*ledConnections)[ledPath] = connection;
2120 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2121 << connection;
2122 }
2123 }
2124
2125 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2126 std::move(callback));
2127 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2128 };
2129 // Make call to ObjectMapper to find all inventory items
2130 crow::connections::systemBus->async_method_call(
2131 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2132 "/xyz/openbmc_project/object_mapper",
2133 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2134 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2135}
2136
2137/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002138 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2139 *
2140 * Uses the specified connections (services) (currently assumes just one) to
2141 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2142 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2143 *
2144 * This data is later used to provide sensor property values in the JSON
2145 * response.
2146 *
2147 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2148 * when data has been obtained.
2149 *
2150 * The callback must have the following signature:
2151 * @code
2152 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2153 * @endcode
2154 *
2155 * @param sensorsAsyncResp Pointer to object holding response data.
2156 * @param inventoryItems D-Bus inventory items associated with sensors.
2157 * @param psAttributesConnections Connections that provide data for the Power
2158 * Supply Attributes
2159 * @param callback Callback to invoke when data has been obtained.
2160 */
2161template <typename Callback>
2162void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002163 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002164 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2165 const boost::container::flat_map<std::string, std::string>&
2166 psAttributesConnections,
2167 Callback&& callback)
2168{
2169 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2170
2171 if (psAttributesConnections.empty())
2172 {
2173 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2174 callback(inventoryItems);
2175 return;
2176 }
2177
2178 // Assuming just one connection (service) for now
2179 auto it = psAttributesConnections.nth(0);
2180
2181 const std::string& psAttributesPath = (*it).first;
2182 const std::string& psAttributesConnection = (*it).second;
2183
2184 // Response handler for Get DeratingFactor property
2185 auto respHandler = [sensorsAsyncResp, inventoryItems,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002186 callback{std::forward<Callback>(callback)}](
Gunnar Mills42cbe532019-08-15 15:26:54 -05002187 const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002188 const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002189 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2190 if (ec)
2191 {
2192 BMCWEB_LOG_ERROR
2193 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002194 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002195 return;
2196 }
2197
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002198 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2199 // Store value in Power Supply Inventory Items
2200 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002201 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002202 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002203 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002204 inventoryItem.powerSupplyEfficiencyPercent =
2205 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002206 }
2207 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002208
2209 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2210 callback(inventoryItems);
2211 };
2212
2213 // Get the DeratingFactor property for the PowerSupplyAttributes
2214 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002215 sdbusplus::asio::getProperty<uint32_t>(
2216 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2217 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2218 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002219
2220 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2221}
2222
2223/**
2224 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2225 *
2226 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2227 * data. Then gets the Power Supply Attributes data from the connection
2228 * (currently just assumes 1 connection) and stores the data in the inventory
2229 * item.
2230 *
2231 * This data is later used to provide sensor property values in the JSON
2232 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2233 *
2234 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2235 * when information has been obtained.
2236 *
2237 * The callback must have the following signature:
2238 * @code
2239 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2240 * @endcode
2241 *
2242 * @param sensorsAsyncResp Pointer to object holding response data.
2243 * @param inventoryItems D-Bus inventory items associated with sensors.
2244 * @param callback Callback to invoke when data has been obtained.
2245 */
2246template <typename Callback>
2247void getPowerSupplyAttributes(
2248 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2249 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2250 Callback&& callback)
2251{
2252 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2253
2254 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002255 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002256 {
2257 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2258 callback(inventoryItems);
2259 return;
2260 }
2261
2262 const std::array<std::string, 1> interfaces = {
2263 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2264
2265 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002266 auto respHandler =
2267 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2268 inventoryItems](
2269 const boost::system::error_code ec,
2270 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2271 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2272 if (ec)
2273 {
2274 messages::internalError(sensorsAsyncResp->asyncResp->res);
2275 BMCWEB_LOG_ERROR
2276 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2277 return;
2278 }
2279 if (subtree.empty())
2280 {
2281 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2282 callback(inventoryItems);
2283 return;
2284 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002285
Ed Tanousb9d36b42022-02-26 21:42:46 -08002286 // Currently we only support 1 power supply attribute, use this for
2287 // all the power supplies. Build map of object path to connection.
2288 // Assume just 1 connection and 1 path for now.
2289 boost::container::flat_map<std::string, std::string>
2290 psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002291
Ed Tanousb9d36b42022-02-26 21:42:46 -08002292 if (subtree[0].first.empty() || subtree[0].second.empty())
2293 {
2294 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2295 callback(inventoryItems);
2296 return;
2297 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002298
Ed Tanousb9d36b42022-02-26 21:42:46 -08002299 const std::string& psAttributesPath = subtree[0].first;
2300 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002301
Ed Tanousb9d36b42022-02-26 21:42:46 -08002302 if (connection.empty())
2303 {
2304 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2305 callback(inventoryItems);
2306 return;
2307 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002308
Ed Tanousb9d36b42022-02-26 21:42:46 -08002309 psAttributesConnections[psAttributesPath] = connection;
2310 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2311 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002312
Ed Tanousb9d36b42022-02-26 21:42:46 -08002313 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2314 psAttributesConnections,
2315 std::move(callback));
2316 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2317 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002318 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2319 crow::connections::systemBus->async_method_call(
2320 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2321 "/xyz/openbmc_project/object_mapper",
2322 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2323 "/xyz/openbmc_project", 0, interfaces);
2324 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2325}
2326
2327/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002328 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002329 *
2330 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002331 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002332 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002333 * This data is later used to provide sensor property values in the JSON
2334 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002335 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002336 * Finds the inventory items asynchronously. Invokes callback when the
2337 * inventory items have been obtained.
2338 *
2339 * The callback must have the following signature:
2340 * @code
2341 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2342 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002343 *
2344 * @param sensorsAsyncResp Pointer to object holding response data.
2345 * @param sensorNames All sensors within the current chassis.
2346 * @param objectMgrPaths Mappings from connection name to DBus object path that
2347 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002348 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002349 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002350template <typename Callback>
2351static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002352 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2353 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2354 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002355 objectMgrPaths,
2356 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002357{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002358 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2359 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002360 [sensorsAsyncResp, objectMgrPaths,
2361 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002362 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2363 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002364 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002365 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002366 callback{std::forward<const Callback>(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002367 std::shared_ptr<boost::container::flat_set<std::string>>
2368 invConnections) {
2369 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002370 auto getInventoryItemsDataCb =
2371 [sensorsAsyncResp, inventoryItems,
2372 callback{std::move(callback)}]() {
2373 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002374
2375 auto getInventoryLedsCb = [sensorsAsyncResp,
2376 inventoryItems,
2377 callback{std::move(
2378 callback)}]() {
2379 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2380 // Find Power Supply Attributes and get the data
2381 getPowerSupplyAttributes(sensorsAsyncResp,
2382 inventoryItems,
2383 std::move(callback));
2384 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2385 };
2386
Anthony Wilsond5005492019-07-31 16:34:17 -05002387 // Find led connections and get the data
2388 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002389 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002390 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2391 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002392
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002393 // Get inventory item data from connections
2394 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2395 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002396 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002397 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2398 };
2399
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002400 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002401 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002402 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002403 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002404 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002405 };
2406
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002407 // Get associations from sensors to inventory items
2408 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2409 std::move(getInventoryItemAssociationsCb));
2410 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2411}
2412
2413/**
2414 * @brief Returns JSON PowerSupply object for the specified inventory item.
2415 *
2416 * Searches for a JSON PowerSupply object that matches the specified inventory
2417 * item. If one is not found, a new PowerSupply object is added to the JSON
2418 * array.
2419 *
2420 * Multiple sensors are often associated with one power supply inventory item.
2421 * As a result, multiple sensor values are stored in one JSON PowerSupply
2422 * object.
2423 *
2424 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2425 * @param inventoryItem Inventory item for the power supply.
2426 * @param chassisId Chassis that contains the power supply.
2427 * @return JSON PowerSupply object for the specified inventory item.
2428 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002429inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002430 const InventoryItem& inventoryItem,
2431 const std::string& chassisId)
2432{
2433 // Check if matching PowerSupply object already exists in JSON array
2434 for (nlohmann::json& powerSupply : powerSupplyArray)
2435 {
2436 if (powerSupply["MemberId"] == inventoryItem.name)
2437 {
2438 return powerSupply;
2439 }
2440 }
2441
2442 // Add new PowerSupply object to JSON array
2443 powerSupplyArray.push_back({});
2444 nlohmann::json& powerSupply = powerSupplyArray.back();
2445 powerSupply["@odata.id"] =
2446 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2447 powerSupply["MemberId"] = inventoryItem.name;
2448 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2449 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2450 powerSupply["Model"] = inventoryItem.model;
2451 powerSupply["PartNumber"] = inventoryItem.partNumber;
2452 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002453 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002454
Gunnar Mills42cbe532019-08-15 15:26:54 -05002455 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2456 {
2457 powerSupply["EfficiencyPercent"] =
2458 inventoryItem.powerSupplyEfficiencyPercent;
2459 }
2460
2461 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002462 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2463 powerSupply["Status"]["Health"] = health;
2464
2465 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002466}
2467
2468/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002469 * @brief Gets the values of the specified sensors.
2470 *
2471 * Stores the results as JSON in the SensorsAsyncResp.
2472 *
2473 * Gets the sensor values asynchronously. Stores the results later when the
2474 * information has been obtained.
2475 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002476 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002477 *
2478 * To minimize the number of DBus calls, the DBus method
2479 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2480 * values of all sensors provided by a connection (service).
2481 *
2482 * The connections set contains all the connections that provide sensor values.
2483 *
2484 * The objectMgrPaths map contains mappings from a connection name to the
2485 * corresponding DBus object path that implements ObjectManager.
2486 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002487 * The InventoryItem vector contains D-Bus inventory items associated with the
2488 * sensors. Inventory item data is needed for some Redfish sensor properties.
2489 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002490 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002491 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002492 * @param connections Connections that provide sensor values.
2493 * @param objectMgrPaths Mappings from connection name to DBus object path that
2494 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002495 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002496 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002497inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002498 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002499 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002500 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002501 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002502 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002503 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002504{
2505 BMCWEB_LOG_DEBUG << "getSensorData enter";
2506 // Get managed objects from all services exposing sensors
2507 for (const std::string& connection : connections)
2508 {
2509 // Response handler to process managed objects
Ed Tanous81ce6092020-12-17 16:54:55 +00002510 auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002511 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002512 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08002513 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002514 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2515 if (ec)
2516 {
2517 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002518 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002519 return;
2520 }
2521 // Go through all objects and update response with sensor data
2522 for (const auto& objDictEntry : resp)
2523 {
2524 const std::string& objPath =
2525 static_cast<const std::string&>(objDictEntry.first);
2526 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2527 << objPath;
2528
Shawn McCarneyde629b62019-03-08 10:42:51 -06002529 std::vector<std::string> split;
2530 // Reserve space for
2531 // /xyz/openbmc_project/sensors/<name>/<subname>
2532 split.reserve(6);
2533 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2534 if (split.size() < 6)
2535 {
2536 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2537 << objPath;
2538 continue;
2539 }
2540 // These indexes aren't intuitive, as boost::split puts an empty
2541 // string at the beginning
2542 const std::string& sensorType = split[4];
2543 const std::string& sensorName = split[5];
2544 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2545 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002546 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002547 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002548 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002549 continue;
2550 }
2551
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002552 // Find inventory item (if any) associated with sensor
2553 InventoryItem* inventoryItem =
2554 findInventoryItemForSensor(inventoryItems, objPath);
2555
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002556 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002557 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002558
2559 nlohmann::json* sensorJson = nullptr;
2560
Nan Zhou928fefb2022-03-28 08:45:00 -07002561 if (sensorSchema == sensors::node::sensors &&
2562 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002563 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002564 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002565 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2566 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002567 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002568 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002569 }
2570 else
2571 {
Ed Tanous271584a2019-07-09 16:24:22 -07002572 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002573 if (sensorsAsyncResp->efficientExpand)
2574 {
2575 fieldName = "Members";
2576 }
2577 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002578 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002579 fieldName = "Temperatures";
2580 }
2581 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2582 sensorType == "fan_pwm")
2583 {
2584 fieldName = "Fans";
2585 }
2586 else if (sensorType == "voltage")
2587 {
2588 fieldName = "Voltages";
2589 }
2590 else if (sensorType == "power")
2591 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002592 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002593 {
2594 fieldName = "PowerControl";
2595 }
2596 else if ((inventoryItem != nullptr) &&
2597 (inventoryItem->isPowerSupply))
2598 {
2599 fieldName = "PowerSupplies";
2600 }
2601 else
2602 {
2603 // Other power sensors are in SensorCollection
2604 continue;
2605 }
2606 }
2607 else
2608 {
2609 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2610 << sensorType;
2611 continue;
2612 }
2613
2614 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002615 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002616 if (fieldName == "PowerControl")
2617 {
2618 if (tempArray.empty())
2619 {
2620 // Put multiple "sensors" into a single
2621 // PowerControl. Follows MemberId naming and
2622 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002623 nlohmann::json::object_t power;
2624 power["@odata.id"] =
2625 "/redfish/v1/Chassis/" +
2626 sensorsAsyncResp->chassisId + "/" +
2627 sensorsAsyncResp->chassisSubNode + "#/" +
2628 fieldName + "/0";
2629 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002630 }
2631 sensorJson = &(tempArray.back());
2632 }
2633 else if (fieldName == "PowerSupplies")
2634 {
2635 if (inventoryItem != nullptr)
2636 {
2637 sensorJson =
2638 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002639 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002640 }
2641 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002642 else if (fieldName == "Members")
2643 {
Ed Tanous14766872022-03-15 10:44:42 -07002644 nlohmann::json::object_t member;
2645 member["@odata.id"] =
2646 "/redfish/v1/Chassis/" +
2647 sensorsAsyncResp->chassisId + "/" +
2648 sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2649 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002650 sensorJson = &(tempArray.back());
2651 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002652 else
2653 {
Ed Tanous14766872022-03-15 10:44:42 -07002654 nlohmann::json::object_t member;
2655 member["@odata.id"] = "/redfish/v1/Chassis/" +
2656 sensorsAsyncResp->chassisId +
2657 "/" +
2658 sensorsAsyncResp->chassisSubNode +
2659 "#/" + fieldName + "/";
2660 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002661 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002662 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002663 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002664
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002665 if (sensorJson != nullptr)
2666 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002667 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002668 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002669 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002670 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002671 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002672 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002673 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002674 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002675 if (sensorsAsyncResp->chassisSubNode ==
2676 sensors::node::sensors &&
2677 sensorsAsyncResp->efficientExpand)
2678 {
2679 sensorsAsyncResp->asyncResp->res
2680 .jsonValue["Members@odata.count"] =
2681 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2682 .size();
2683 }
2684 else if (sensorsAsyncResp->chassisSubNode ==
2685 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002686 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002687 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002688 }
James Feist8bd25cc2019-03-15 15:14:00 -07002689 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002690 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2691 };
2692
2693 // Find DBus object path that implements ObjectManager for the current
2694 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002695 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002696 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002697 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002698 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2699 << objectMgrPath;
2700
2701 crow::connections::systemBus->async_method_call(
2702 getManagedObjectsCb, connection, objectMgrPath,
2703 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002704 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002705 BMCWEB_LOG_DEBUG << "getSensorData exit";
2706}
2707
Ed Tanous23a21a12020-07-25 04:45:05 +00002708inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002709 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002710 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002711{
2712 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002713 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002714 const boost::container::flat_set<std::string>& connections) {
2715 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2716 auto getObjectManagerPathsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002717 [sensorsAsyncResp, sensorNames,
Ed Tanousb5a76932020-09-29 16:16:58 -07002718 connections](const std::shared_ptr<boost::container::flat_map<
2719 std::string, std::string>>& objectMgrPaths) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002720 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2721 auto getInventoryItemsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002722 [sensorsAsyncResp, sensorNames, connections,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002723 objectMgrPaths](
Ed Tanousf23b7292020-10-15 09:41:17 -07002724 const std::shared_ptr<std::vector<InventoryItem>>&
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002725 inventoryItems) {
2726 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2727 // Get sensor data and store results in JSON
Ed Tanous81ce6092020-12-17 16:54:55 +00002728 getSensorData(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002729 connections, objectMgrPaths,
Ed Tanousf23b7292020-10-15 09:41:17 -07002730 inventoryItems);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002731 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2732 };
2733
2734 // Get inventory items associated with sensors
Ed Tanous81ce6092020-12-17 16:54:55 +00002735 getInventoryItems(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002736 objectMgrPaths,
2737 std::move(getInventoryItemsCb));
2738
2739 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2740 };
2741
2742 // Get mapping from connection names to the DBus object
2743 // paths that implement the ObjectManager interface
Ed Tanous81ce6092020-12-17 16:54:55 +00002744 getObjectManagerPaths(sensorsAsyncResp,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002745 std::move(getObjectManagerPathsCb));
2746 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2747 };
2748
2749 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002750 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002751}
2752
Shawn McCarneyde629b62019-03-08 10:42:51 -06002753/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002754 * @brief Entry point for retrieving sensors data related to requested
2755 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002756 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002757 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002758inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002759 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002760{
2761 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002762 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002763 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002764 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002765 sensorNames) {
2766 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Ed Tanous81ce6092020-12-17 16:54:55 +00002767 processSensorList(sensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002768 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2769 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002770 // SensorCollection doesn't contain the Redundancy property
2771 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2772 {
2773 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2774 nlohmann::json::array();
2775 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002776 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002777 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002778 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002779}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002780
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302781/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002782 * @brief Find the requested sensorName in the list of all sensors supplied by
2783 * the chassis node
2784 *
2785 * @param sensorName The sensor name supplied in the PATCH request
2786 * @param sensorsList The list of sensors managed by the chassis node
2787 * @param sensorsModified The list of sensors that were found as a result of
2788 * repeated calls to this function
2789 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002790inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302791 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002792 boost::container::flat_set<std::string>& sensorsList,
2793 boost::container::flat_set<std::string>& sensorsModified)
2794{
George Liu28aa8de2021-02-01 15:13:30 +08002795 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002796 {
George Liu28aa8de2021-02-01 15:13:30 +08002797 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002798 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002799 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002800 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002801 continue;
2802 }
2803 if (thisSensorName == sensorName)
2804 {
2805 sensorsModified.emplace(chassisSensor);
2806 return true;
2807 }
2808 }
2809 return false;
2810}
2811
2812/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302813 * @brief Entry point for overriding sensor values of given sensor
2814 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002815 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002816 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002817 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302818 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002819inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002820 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002821 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002822 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302823{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002824 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002825 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302826
Ed Tanous543f4402022-01-06 13:12:53 -08002827 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302828 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302829 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002830 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302831 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302832 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302833 if (collectionItems.first == "Temperatures")
2834 {
2835 propertyValueName = "ReadingCelsius";
2836 }
2837 else if (collectionItems.first == "Fans")
2838 {
2839 propertyValueName = "Reading";
2840 }
2841 else
2842 {
2843 propertyValueName = "ReadingVolts";
2844 }
2845 for (auto& item : collectionItems.second)
2846 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002847 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2848 "MemberId", memberId, propertyValueName,
2849 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302850 {
2851 return;
2852 }
2853 overrideMap.emplace(memberId,
2854 std::make_pair(value, collectionItems.first));
2855 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302856 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002857
Ed Tanousb5a76932020-09-29 16:16:58 -07002858 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2859 const std::shared_ptr<
2860 boost::container::flat_set<
2861 std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002862 // Match sensor names in the PATCH request to those managed by the
2863 // chassis node
2864 const std::shared_ptr<boost::container::flat_set<std::string>>
2865 sensorNames =
2866 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302867 for (const auto& item : overrideMap)
2868 {
2869 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002870 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2871 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302872 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302873 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002874 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302875 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302876 return;
2877 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302878 }
2879 // Get the connection to which the memberId belongs
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002880 auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
2881 const boost::container::flat_set<
2882 std::string>& /*connections*/,
2883 const std::set<std::pair<
2884 std::string, std::string>>&
2885 objectsWithConnection) {
2886 if (objectsWithConnection.size() != overrideMap.size())
2887 {
2888 BMCWEB_LOG_INFO
2889 << "Unable to find all objects with proper connection "
2890 << objectsWithConnection.size() << " requested "
2891 << overrideMap.size() << "\n";
2892 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2893 sensorAsyncResp->chassisSubNode ==
2894 sensors::node::thermal
2895 ? "Temperatures"
2896 : "Voltages",
2897 "Count");
2898 return;
2899 }
2900 for (const auto& item : objectsWithConnection)
2901 {
2902 sdbusplus::message::object_path path(item.first);
2903 std::string sensorName = path.filename();
2904 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302905 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002906 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302907 return;
2908 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302909
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002910 const auto& iterator = overrideMap.find(sensorName);
2911 if (iterator == overrideMap.end())
2912 {
2913 BMCWEB_LOG_INFO << "Unable to find sensor object"
2914 << item.first << "\n";
2915 messages::internalError(sensorAsyncResp->asyncResp->res);
2916 return;
2917 }
2918 crow::connections::systemBus->async_method_call(
2919 [sensorAsyncResp](const boost::system::error_code ec) {
2920 if (ec)
2921 {
2922 if (ec.value() ==
2923 boost::system::errc::permission_denied)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302924 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002925 BMCWEB_LOG_WARNING
2926 << "Manufacturing mode is not Enabled...can't "
2927 "Override the sensor value. ";
2928
2929 messages::insufficientPrivilege(
zhanghch058d1b46d2021-04-01 11:18:24 +08002930 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302931 return;
2932 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002933 BMCWEB_LOG_DEBUG
2934 << "setOverrideValueStatus DBUS error: " << ec;
2935 messages::internalError(
2936 sensorAsyncResp->asyncResp->res);
2937 }
2938 },
2939 item.second, item.first, "org.freedesktop.DBus.Properties",
2940 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002941 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002942 }
2943 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302944 // Get object with connection for the given sensor name
2945 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2946 std::move(getObjectsWithConnectionCb));
2947 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302948 // get full sensor list for the given chassisId and cross verify the sensor.
2949 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2950}
2951
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002952/**
2953 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2954 * path of the sensor.
2955 *
2956 * Function builds valid Redfish response for sensor query of given chassis and
2957 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2958 * it to caller in a callback.
2959 *
2960 * @param chassis Chassis for which retrieval should be performed
2961 * @param node Node (group) of sensors. See sensors::node for supported values
2962 * @param mapComplete Callback to be called with retrieval result
2963 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002964inline void retrieveUriToDbusMap(const std::string& chassis,
2965 const std::string& node,
2966 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002967{
Ed Tanous02da7c52022-02-27 00:09:02 -08002968 decltype(sensors::paths)::const_iterator pathIt =
2969 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2970 [&node](auto&& val) { return val.first == node; });
2971 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002972 {
2973 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2974 mapComplete(boost::beast::http::status::bad_request, {});
2975 return;
2976 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002977
Nan Zhou72374eb2022-01-27 17:06:51 -08002978 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002979 auto callback =
Nan Zhou72374eb2022-01-27 17:06:51 -08002980 [asyncResp, mapCompleteCb{std::move(mapComplete)}](
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002981 const boost::beast::http::status status,
2982 const boost::container::flat_map<std::string, std::string>&
2983 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2984
2985 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002986 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002987 getChassisData(resp);
2988}
2989
Nan Zhoubacb2162022-04-06 11:28:32 -07002990namespace sensors
2991{
Nan Zhou928fefb2022-03-28 08:45:00 -07002992
Nan Zhoubacb2162022-04-06 11:28:32 -07002993inline void getChassisCallback(
2994 const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2995 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
2996{
2997 BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2998
2999 nlohmann::json& entriesArray =
3000 asyncResp->asyncResp->res.jsonValue["Members"];
3001 for (auto& sensor : *sensorNames)
3002 {
3003 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
3004
3005 sdbusplus::message::object_path path(sensor);
3006 std::string sensorName = path.filename();
3007 if (sensorName.empty())
3008 {
3009 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
3010 messages::internalError(asyncResp->asyncResp->res);
3011 return;
3012 }
Ed Tanous14766872022-03-15 10:44:42 -07003013 nlohmann::json::object_t member;
3014 member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
3015 "/" + asyncResp->chassisSubNode + "/" +
3016 sensorName;
3017 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07003018 }
3019
3020 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
3021 entriesArray.size();
3022 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
3023}
3024} // namespace sensors
3025
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003026inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003027{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003028 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003029 .privileges(redfish::privileges::getSensorCollection)
Nan Zhou928fefb2022-03-28 08:45:00 -07003030 .methods(
3031 boost::beast::http::verb::get)([&app](
3032 const crow::Request& req,
3033 const std::shared_ptr<
3034 bmcweb::AsyncResp>& aResp,
3035 const std::string& chassisId) {
3036 query_param::QueryCapabilities capabilities = {
3037 .canDelegateExpandLevel = 1,
3038 };
3039 query_param::Query delegatedQuery;
3040 if (!redfish::setUpRedfishRouteWithDelegation(
3041 app, req, aResp->res, delegatedQuery, capabilities))
3042 {
3043 return;
3044 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003045
Nan Zhou928fefb2022-03-28 08:45:00 -07003046 if (delegatedQuery.expandType != query_param::ExpandType::None)
3047 {
3048 // we perform efficient expand.
3049 auto asyncResp = std::make_shared<SensorsAsyncResp>(
Ed Tanous02da7c52022-02-27 00:09:02 -08003050 aResp, chassisId, sensors::dbus::sensorPaths,
Nan Zhou928fefb2022-03-28 08:45:00 -07003051 sensors::node::sensors,
3052 /*efficientExpand=*/true);
3053 getChassisData(asyncResp);
zhanghch058d1b46d2021-04-01 11:18:24 +08003054
Nan Zhou928fefb2022-03-28 08:45:00 -07003055 BMCWEB_LOG_DEBUG
3056 << "SensorCollection doGet exit via efficient expand handler";
3057 return;
3058 };
3059
3060 // if there's no efficient expand available, we use the default
3061 // Query Parameters route
3062 auto asyncResp = std::make_shared<SensorsAsyncResp>(
Ed Tanous02da7c52022-02-27 00:09:02 -08003063 aResp, chassisId, sensors::dbus::sensorPaths,
Nan Zhou928fefb2022-03-28 08:45:00 -07003064 sensors::node::sensors);
3065
3066 // We get all sensors as hyperlinkes in the chassis (this
3067 // implies we reply on the default query parameters handler)
3068 getChassis(asyncResp,
3069 std::bind_front(sensors::getChassisCallback, asyncResp));
3070 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
3071 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003072}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003073
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003074inline void requestRoutesSensor(App& app)
3075{
3076 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003077 .privileges(redfish::privileges::getSensor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003078 .methods(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003079 boost::beast::http::verb::get)([&app](
3080 const crow::Request& req,
3081 const std::shared_ptr<
3082 bmcweb::AsyncResp>& aResp,
3083 const std::string& chassisId,
3084 const std::string& sensorName) {
3085 if (!redfish::setUpRedfishRoute(app, req, aResp->res))
3086 {
3087 return;
3088 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003089 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3090 std::shared_ptr<SensorsAsyncResp> asyncResp =
Ed Tanous02da7c52022-02-27 00:09:02 -08003091 std::make_shared<SensorsAsyncResp>(
3092 aResp, chassisId, std::span<std::string_view>(),
3093 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003094
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003095 const std::array<const char*, 1> interfaces = {
3096 "xyz.openbmc_project.Sensor.Value"};
3097
3098 // Get a list of all of the sensors that implement Sensor.Value
3099 // and get the path and service name associated with the sensor
3100 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08003101 [asyncResp, sensorName](
3102 const boost::system::error_code ec,
3103 const dbus::utility::MapperGetSubTreeResponse& subtree) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003104 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3105 if (ec)
3106 {
3107 messages::internalError(asyncResp->asyncResp->res);
3108 BMCWEB_LOG_ERROR
3109 << "Sensor getSensorPaths resp_handler: "
3110 << "Dbus error " << ec;
3111 return;
3112 }
3113
Ed Tanousb9d36b42022-02-26 21:42:46 -08003114 dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3115 std::find_if(
3116 subtree.begin(), subtree.end(),
3117 [sensorName](
3118 const std::pair<std::string,
3119 std::vector<std::pair<
3120 std::string,
3121 std::vector<std::string>>>>&
3122 object) {
3123 sdbusplus::message::object_path path(
3124 object.first);
3125 std::string name = path.filename();
3126 if (name.empty())
3127 {
3128 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3129 << object.first;
3130 return false;
3131 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003132
Ed Tanousb9d36b42022-02-26 21:42:46 -08003133 return name == sensorName;
3134 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003135
3136 if (it == subtree.end())
3137 {
3138 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3139 << sensorName;
3140 messages::resourceNotFound(asyncResp->asyncResp->res,
3141 "Sensor", sensorName);
3142 return;
3143 }
3144 std::string_view sensorPath = (*it).first;
3145 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3146 << sensorName << "': " << sensorPath;
3147
3148 const std::shared_ptr<
3149 boost::container::flat_set<std::string>>
3150 sensorList = std::make_shared<
3151 boost::container::flat_set<std::string>>();
3152
3153 sensorList->emplace(sensorPath);
3154 processSensorList(asyncResp, sensorList);
3155 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3156 },
3157 "xyz.openbmc_project.ObjectMapper",
3158 "/xyz/openbmc_project/object_mapper",
3159 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3160 "/xyz/openbmc_project/sensors", 2, interfaces);
3161 });
3162}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003163
Ed Tanous1abe55e2018-09-05 08:30:59 -07003164} // namespace redfish