blob: 7d09ce1904f1c79e276311a87ed3ce47f3b07282 [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>
Ed Tanous11ba3972022-07-11 09:50:41 -070019#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070020#include <boost/algorithm/string/find.hpp>
21#include <boost/algorithm/string/predicate.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010022#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010023#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070024#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080025#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070026#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070027#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070028#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020029#include <sdbusplus/unpack_properties.hpp>
30#include <utils/dbus_utils.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053031#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070032#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033
34#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000035#include <iterator>
36#include <map>
37#include <set>
Ed Tanousb5a76932020-09-29 16:16:58 -070038#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080039#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010040
Ed Tanous1abe55e2018-09-05 08:30:59 -070041namespace redfish
42{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010043
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020044namespace sensors
45{
46namespace node
47{
48static constexpr std::string_view power = "Power";
49static constexpr std::string_view sensors = "Sensors";
50static constexpr std::string_view thermal = "Thermal";
51} // namespace node
52
Ed Tanous02da7c52022-02-27 00:09:02 -080053// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020054namespace dbus
55{
Ed Tanous4ee8e212022-05-28 09:42:51 -070056static auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080057 "/xyz/openbmc_project/sensors/voltage",
58 "/xyz/openbmc_project/sensors/power"
59});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000060
Ed Tanous4ee8e212022-05-28 09:42:51 -070061static auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080062 "/xyz/openbmc_project/sensors/power",
63 "/xyz/openbmc_project/sensors/current",
64 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070065 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080066#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080067 "/xyz/openbmc_project/sensors/voltage",
68 "/xyz/openbmc_project/sensors/fan_tach",
69 "/xyz/openbmc_project/sensors/temperature",
70 "/xyz/openbmc_project/sensors/fan_pwm",
71 "/xyz/openbmc_project/sensors/altitude",
72 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080073#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080074 "/xyz/openbmc_project/sensors/utilization"
75});
76
Ed Tanous4ee8e212022-05-28 09:42:51 -070077static auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080078 "/xyz/openbmc_project/sensors/fan_tach",
79 "/xyz/openbmc_project/sensors/temperature",
80 "/xyz/openbmc_project/sensors/fan_pwm"
81});
82
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000083} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080084// clang-format on
85
86using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
87static constexpr std::array<sensorPair, 3> paths = {
88 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
89 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
90 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000091
Ed Tanous1d7c0052022-08-09 12:32:26 -070092inline std::string_view toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000093{
94 if (sensorType == "voltage")
95 {
96 return "Voltage";
97 }
98 if (sensorType == "power")
99 {
100 return "Power";
101 }
102 if (sensorType == "current")
103 {
104 return "Current";
105 }
106 if (sensorType == "fan_tach")
107 {
108 return "Rotational";
109 }
110 if (sensorType == "temperature")
111 {
112 return "Temperature";
113 }
114 if (sensorType == "fan_pwm" || sensorType == "utilization")
115 {
116 return "Percent";
117 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600118 if (sensorType == "humidity")
119 {
120 return "Humidity";
121 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000122 if (sensorType == "altitude")
123 {
124 return "Altitude";
125 }
126 if (sensorType == "airflow")
127 {
128 return "AirFlow";
129 }
130 if (sensorType == "energy")
131 {
132 return "EnergyJoules";
133 }
134 return "";
135}
136
Ed Tanous1d7c0052022-08-09 12:32:26 -0700137inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000138{
139 if (sensorType == "voltage")
140 {
141 return "V";
142 }
143 if (sensorType == "power")
144 {
145 return "W";
146 }
147 if (sensorType == "current")
148 {
149 return "A";
150 }
151 if (sensorType == "fan_tach")
152 {
153 return "RPM";
154 }
155 if (sensorType == "temperature")
156 {
157 return "Cel";
158 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600159 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
160 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000161 {
162 return "%";
163 }
164 if (sensorType == "altitude")
165 {
166 return "m";
167 }
168 if (sensorType == "airflow")
169 {
170 return "cft_i/min";
171 }
172 if (sensorType == "energy")
173 {
174 return "J";
175 }
176 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200177}
178} // namespace sensors
179
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100180/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200181 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100182 * Gathers data needed for response processing after async calls are done
183 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184class SensorsAsyncResp
185{
186 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200187 using DataCompleteCb = std::function<void(
188 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000189 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200190
191 struct SensorData
192 {
193 const std::string name;
194 std::string uri;
195 const std::string valueKey;
196 const std::string dbusPath;
197 };
198
Ed Tanous8a592812022-06-04 09:06:59 -0700199 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800200 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800201 std::span<std::string_view> typesIn,
202 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700203 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700204 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
205 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500206 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200207
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200208 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700209 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800210 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800211 std::span<std::string_view> typesIn,
212 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200213 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700214 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700215 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
216 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200217 dataComplete{std::move(creationComplete)}
218 {}
219
Nan Zhou928fefb2022-03-28 08:45:00 -0700220 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700221 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700222 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800223 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700224 const std::string_view& subNode, bool efficientExpandIn) :
225 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700226 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700227 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700228 {}
229
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 ~SensorsAsyncResp()
231 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800232 if (asyncResp->res.result() ==
233 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 {
235 // Reset the json object to clear out any data that made it in
236 // before the error happened todo(ed) handle error condition with
237 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800238 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200240
241 if (dataComplete && metadata)
242 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000243 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800244 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200245 {
246 for (auto& sensor : *metadata)
247 {
248 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
249 sensor.dbusPath));
250 }
251 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800252 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200253 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100255
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800256 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
257 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
258 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
259 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
260
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200261 void addMetadata(const nlohmann::json& sensorObject,
262 const std::string& valueKey, const std::string& dbusPath)
263 {
264 if (metadata)
265 {
266 metadata->emplace_back(SensorData{sensorObject["Name"],
267 sensorObject["@odata.id"],
268 valueKey, dbusPath});
269 }
270 }
271
272 void updateUri(const std::string& name, const std::string& uri)
273 {
274 if (metadata)
275 {
276 for (auto& sensor : *metadata)
277 {
278 if (sensor.name == name)
279 {
280 sensor.uri = uri;
281 }
282 }
283 }
284 }
285
zhanghch058d1b46d2021-04-01 11:18:24 +0800286 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200287 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800288 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200289 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700290 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200291
292 private:
293 std::optional<std::vector<SensorData>> metadata;
294 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100295};
296
297/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500298 * Possible states for physical inventory leds
299 */
300enum class LedState
301{
302 OFF,
303 ON,
304 BLINK,
305 UNKNOWN
306};
307
308/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500309 * D-Bus inventory item associated with one or more sensors.
310 */
311class InventoryItem
312{
313 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700314 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500315 {
316 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800317 sdbusplus::message::object_path path(objectPath);
318 name = path.filename();
319 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500320 {
George Liu28aa8de2021-02-01 15:13:30 +0800321 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500322 }
323 }
324
325 std::string objectPath;
326 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800327 bool isPresent = true;
328 bool isFunctional = true;
329 bool isPowerSupply = false;
330 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500331 std::string manufacturer;
332 std::string model;
333 std::string partNumber;
334 std::string serialNumber;
335 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500336 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800337 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500338};
339
340/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530341 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200342 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100343 * @param sensorNames Sensors retrieved from chassis
344 * @param callback Callback for processing gathered connections
345 */
346template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530347void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000348 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000349 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530350 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700351{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530352 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700353 const std::string path = "/xyz/openbmc_project/sensors";
354 const std::array<std::string, 1> interfaces = {
355 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100356
Ed Tanous1abe55e2018-09-05 08:30:59 -0700357 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700358 auto respHandler =
359 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
360 sensorNames](const boost::system::error_code ec,
361 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530362 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700363 if (ec)
364 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800365 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530366 BMCWEB_LOG_ERROR
367 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700368 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100369 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100370
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
372
373 // Make unique list of connections only for requested sensor types and
374 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000375 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530376 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700378 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
379 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380 {
381 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
382 }
383
384 for (const std::pair<
385 std::string,
386 std::vector<std::pair<std::string, std::vector<std::string>>>>&
387 object : subtree)
388 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700389 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700391 for (const std::pair<std::string, std::vector<std::string>>&
392 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700393 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700394 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
395 connections.insert(objData.first);
396 objectsWithConnection.insert(
397 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700398 }
399 }
400 }
401 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530402 callback(std::move(connections), std::move(objectsWithConnection));
403 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700405 // Make call to ObjectMapper to find all sensors objects
406 crow::connections::systemBus->async_method_call(
407 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
408 "/xyz/openbmc_project/object_mapper",
409 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530410 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
411}
412
413/**
414 * @brief Create connections necessary for sensors
415 * @param SensorsAsyncResp Pointer to object holding response data
416 * @param sensorNames Sensors retrieved from chassis
417 * @param callback Callback for processing gathered connections
418 */
419template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000420void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
421 const std::shared_ptr<std::set<std::string>> sensorNames,
422 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530423{
424 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000425 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530426 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700427 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000428 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530429 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100430}
431
432/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700433 * @brief Shrinks the list of sensors for processing
434 * @param SensorsAysncResp The class holding the Redfish response
435 * @param allSensors A list of all the sensors associated to the
436 * chassis element (i.e. baseboard, front panel, etc...)
437 * @param activeSensors A list that is a reduction of the incoming
438 * allSensors list. Eliminate Thermal sensors when a Power request is
439 * made, and eliminate Power sensors when a Thermal request is made.
440 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000441inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700442 crow::Response& res, std::string_view chassisSubNode,
443 std::span<std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700444 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000445 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700446{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447 if ((allSensors == nullptr) || (activeSensors == nullptr))
448 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700449 messages::resourceNotFound(res, chassisSubNode,
450 chassisSubNode == sensors::node::thermal
451 ? "Temperatures"
452 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700453
454 return;
455 }
456 if (allSensors->empty())
457 {
458 // Nothing to do, the activeSensors object is also empty
459 return;
460 }
461
Ed Tanous7f1cc262022-08-09 13:33:57 -0700462 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700463 {
464 for (const std::string& sensor : *allSensors)
465 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700466 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700467 {
468 activeSensors->emplace(sensor);
469 }
470 }
471 }
472}
473
Ed Tanous7f1cc262022-08-09 13:33:57 -0700474/*
475 *Populates the top level collection for a given subnode. Populates
476 *SensorCollection, Power, or Thermal schemas.
477 *
478 * */
479inline void populateChassisNode(nlohmann::json& jsonValue,
480 std::string_view chassisSubNode)
481{
482 if (chassisSubNode == sensors::node::power)
483 {
484 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
485 }
486 else if (chassisSubNode == sensors::node::thermal)
487 {
488 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
489 jsonValue["Fans"] = nlohmann::json::array();
490 jsonValue["Temperatures"] = nlohmann::json::array();
491 }
492 else if (chassisSubNode == sensors::node::sensors)
493 {
494 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
495 jsonValue["Description"] = "Collection of Sensors for this Chassis";
496 jsonValue["Members"] = nlohmann::json::array();
497 jsonValue["Members@odata.count"] = 0;
498 }
499
500 if (chassisSubNode != sensors::node::sensors)
501 {
502 jsonValue["Id"] = chassisSubNode;
503 }
504 jsonValue["Name"] = chassisSubNode;
505}
506
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700507/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100508 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200509 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100510 * @param callback Callback for next step in gathered sensor processing
511 */
512template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700513void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
514 std::string_view chassisId, std::string_view chassisSubNode,
515 std::span<std::string_view> sensorTypes, Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700516{
517 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500518 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700519 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500520 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700521 auto respHandler =
Ed Tanous7f1cc262022-08-09 13:33:57 -0700522 [callback{std::forward<Callback>(callback)}, asyncResp,
523 chassisIdStr{std::string(chassisId)},
524 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
Ed Tanous002d39b2022-05-31 08:59:27 -0700525 const boost::system::error_code ec,
526 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700527 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
528 if (ec)
529 {
530 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700531 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 return;
533 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700534 const std::string* chassisPath = nullptr;
535 std::string chassisName;
536 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700537 {
George Liu28aa8de2021-02-01 15:13:30 +0800538 sdbusplus::message::object_path path(chassis);
539 chassisName = path.filename();
540 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700542 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543 continue;
544 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700545 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700547 chassisPath = &chassis;
548 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700549 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700550 }
551 if (chassisPath == nullptr)
552 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700553 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700554 return;
555 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700556 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700557
Ed Tanous7f1cc262022-08-09 13:33:57 -0700558 asyncResp->res.jsonValue["@odata.id"] =
559 "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500560
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500561 // Get the list of all sensors for this Chassis element
562 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700563 sdbusplus::asio::getProperty<std::vector<std::string>>(
564 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
565 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700566 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800567 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700568 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700569 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700570 if (e)
571 {
572 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700573 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700574 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700576 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700577 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000578 const std::shared_ptr<std::set<std::string>> culledSensorList =
579 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700580 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
581 &nodeSensorList, culledSensorList);
582 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700583 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700584 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100585 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100586
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700587 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700588 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700589 respHandler, "xyz.openbmc_project.ObjectMapper",
590 "/xyz/openbmc_project/object_mapper",
591 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700592 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700593 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100594}
595
596/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600597 * @brief Finds all DBus object paths that implement ObjectManager.
598 *
599 * Creates a mapping from the associated connection name to the object path.
600 *
601 * Finds the object paths asynchronously. Invokes callback when information has
602 * been obtained.
603 *
604 * The callback must have the following signature:
605 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +0000606 * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600607 * @endcode
608 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700609 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600610 * @param callback Callback to invoke when object paths obtained.
611 */
612template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700613void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000614 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700615 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600616{
617 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
618 const std::array<std::string, 1> interfaces = {
619 "org.freedesktop.DBus.ObjectManager"};
620
621 // Response handler for GetSubTree DBus method
Ed Tanous002d39b2022-05-31 08:59:27 -0700622 auto respHandler =
623 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
624 const boost::system::error_code ec,
625 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600626 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
627 if (ec)
628 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800629 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600630 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
631 << ec;
632 return;
633 }
634
635 // Loop over returned object paths
Nan Zhoufe04d492022-06-22 17:10:41 +0000636 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
637 std::make_shared<std::map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600638 for (const std::pair<
639 std::string,
640 std::vector<std::pair<std::string, std::vector<std::string>>>>&
641 object : subtree)
642 {
643 // Loop over connections for current object path
644 const std::string& objectPath = object.first;
645 for (const std::pair<std::string, std::vector<std::string>>&
646 objData : object.second)
647 {
648 // Add mapping from connection to object path
649 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500650 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600651 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
652 << objectPath;
653 }
654 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500655 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600656 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
657 };
658
659 // Query mapper for all DBus object paths that implement ObjectManager
660 crow::connections::systemBus->async_method_call(
661 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
662 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700663 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600664 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
665}
666
667/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500668 * @brief Returns the Redfish State value for the specified inventory item.
669 * @param inventoryItem D-Bus inventory item associated with a sensor.
670 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700671 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000672inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500673{
674 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
675 {
676 return "Absent";
677 }
James Feist34dd1792019-05-17 14:10:54 -0700678
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500679 return "Enabled";
680}
681
682/**
683 * @brief Returns the Redfish Health value for the specified sensor.
684 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700685 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500686 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
687 * be nullptr if no associated inventory item was found.
688 * @return Health value for sensor.
689 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700690inline std::string getHealth(nlohmann::json& sensorJson,
691 const dbus::utility::DBusPropertiesMap& valuesDict,
692 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700693{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500694 // Get current health value (if any) in the sensor JSON object. Some JSON
695 // objects contain multiple sensors (such as PowerSupplies). We want to set
696 // the overall health to be the most severe of any of the sensors.
697 std::string currentHealth;
698 auto statusIt = sensorJson.find("Status");
699 if (statusIt != sensorJson.end())
700 {
701 auto healthIt = statusIt->find("Health");
702 if (healthIt != statusIt->end())
703 {
704 std::string* health = healthIt->get_ptr<std::string*>();
705 if (health != nullptr)
706 {
707 currentHealth = *health;
708 }
709 }
710 }
711
712 // If current health in JSON object is already Critical, return that. This
713 // should override the sensor health, which might be less severe.
714 if (currentHealth == "Critical")
715 {
716 return "Critical";
717 }
718
719 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800720
Ed Tanous1d7c0052022-08-09 12:32:26 -0700721 for (const auto& [valueName, value] : valuesDict)
James Feist34dd1792019-05-17 14:10:54 -0700722 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700723 if (valueName == "CriticalAlarmHigh" || valueName == "CriticalAlarmLow")
James Feist34dd1792019-05-17 14:10:54 -0700724 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700725 const bool* asserted = std::get_if<bool>(&value);
726 if (asserted == nullptr)
James Feist34dd1792019-05-17 14:10:54 -0700727 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700728 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
729 }
730 else if (*asserted)
731 {
732 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700733 }
734 }
735 }
736
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500737 // Check if associated inventory item is not functional
738 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
739 {
740 return "Critical";
741 }
742
743 // If current health in JSON object is already Warning, return that. This
744 // should override the sensor status, which might be less severe.
745 if (currentHealth == "Warning")
746 {
747 return "Warning";
748 }
749
750 // Check if sensor has warning threshold alarm
Ed Tanous1d7c0052022-08-09 12:32:26 -0700751 for (const auto& [valueName, value] : valuesDict)
James Feist34dd1792019-05-17 14:10:54 -0700752 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700753 if (valueName == "WarningAlarmHigh" || valueName == "WarningAlarmLow")
James Feist34dd1792019-05-17 14:10:54 -0700754 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700755 const bool* asserted = std::get_if<bool>(&value);
756 if (asserted == nullptr)
James Feist34dd1792019-05-17 14:10:54 -0700757 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700758 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
759 }
760 else if (*asserted)
761 {
762 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700763 }
764 }
765 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500766
James Feist34dd1792019-05-17 14:10:54 -0700767 return "OK";
768}
769
Ed Tanous23a21a12020-07-25 04:45:05 +0000770inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500771 const InventoryItem* inventoryItem)
772{
773 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
774 {
775 switch (inventoryItem->ledState)
776 {
777 case LedState::OFF:
778 sensorJson["IndicatorLED"] = "Off";
779 break;
780 case LedState::ON:
781 sensorJson["IndicatorLED"] = "Lit";
782 break;
783 case LedState::BLINK:
784 sensorJson["IndicatorLED"] = "Blinking";
785 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000786 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500787 break;
788 }
789 }
790}
791
James Feist34dd1792019-05-17 14:10:54 -0700792/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100793 * @brief Builds a json sensor representation of a sensor.
794 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500795 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100796 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700797 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
798 * @param propertiesDict A dictionary of the properties to build the sensor
799 * from.
800 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500801 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
802 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100803 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700804inline void objectPropertiesToJson(
805 std::string_view sensorName, std::string_view sensorType,
806 std::string_view chassisSubNode,
807 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000808 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700809{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700810 // Assume values exist as is (10^0 == 1) if no scale exists
811 int64_t scaleMultiplier = 0;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700812 for (const auto& [valueName, value] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700813 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700814 if (valueName == "Scale")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700815 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700816 const int64_t* int64Value = std::get_if<int64_t>(&value);
817 if (int64Value != nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800818 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700819 scaleMultiplier = *int64Value;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800820 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100821 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100822 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823
Ed Tanous1d7c0052022-08-09 12:32:26 -0700824 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500825 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000826 sensorJson["Id"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700827 std::string sensorNameEs(sensorName);
828 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
829 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500830 }
831 else if (sensorType != "power")
832 {
833 // Set MemberId and Name for non-power sensors. For PowerSupplies and
834 // PowerControl, those properties have more general values because
835 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000836 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700837 std::string sensorNameEs(sensorName);
838 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
839 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500840 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700841
Ed Tanous81ce6092020-12-17 16:54:55 +0000842 sensorJson["Status"]["State"] = getState(inventoryItem);
843 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700844 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845
846 // Parameter to set to override the type we get from dbus, and force it to
847 // int, regardless of what is available. This is used for schemas like fan,
848 // that require integers, not floats.
849 bool forceToInt = false;
850
Anthony Wilson3929aca2019-07-19 15:42:33 -0500851 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700852 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500853 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000854 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000855
Ed Tanous1d7c0052022-08-09 12:32:26 -0700856 std::string_view readingType = sensors::toReadingType(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000857 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500858 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000859 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
860 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500861 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000862 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500863 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000864 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500865 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000866
Ed Tanous1d7c0052022-08-09 12:32:26 -0700867 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000868 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200869 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000870 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
871 << sensorType;
872 }
873 else
874 {
875 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200876 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500877 }
878 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700879 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500880 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000881 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700882 // TODO(ed) Documentation says that path should be type fan_tach,
883 // implementation seems to implement fan
884 }
885 else if (sensorType == "fan" || sensorType == "fan_tach")
886 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500887 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000888 sensorJson["ReadingUnits"] = "RPM";
889 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
890 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700891 forceToInt = true;
892 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700893 else if (sensorType == "fan_pwm")
894 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500895 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000896 sensorJson["ReadingUnits"] = "Percent";
897 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
898 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700899 forceToInt = true;
900 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 else if (sensorType == "voltage")
902 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500903 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000904 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700906 else if (sensorType == "power")
907 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700908 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000909 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000910 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500911 // Put multiple "sensors" into a single PowerControl, so have
912 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000913 sensorJson["MemberId"] = "0";
914 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500915 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000916 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700917 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700918 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500919 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700920 }
921 else
922 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500923 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700924 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700925 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700926 else
927 {
928 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
929 return;
930 }
931 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500932 std::vector<
933 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
934 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700935 properties.reserve(7);
936
937 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600938
Ed Tanous1d7c0052022-08-09 12:32:26 -0700939 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500940 {
941 properties.emplace_back(
942 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
943 "/Thresholds/UpperCaution/Reading"_json_pointer);
944 properties.emplace_back(
945 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
946 "/Thresholds/LowerCaution/Reading"_json_pointer);
947 properties.emplace_back(
948 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
949 "/Thresholds/UpperCritical/Reading"_json_pointer);
950 properties.emplace_back(
951 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
952 "/Thresholds/LowerCritical/Reading"_json_pointer);
953 }
954 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600955 {
956 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500957 "WarningHigh",
958 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600959 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500960 "WarningLow",
961 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600962 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500963 "CriticalHigh",
964 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600965 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500966 "CriticalLow",
967 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600968 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700969
Ed Tanous2474adf2018-09-05 16:31:16 -0700970 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
971
Ed Tanous1d7c0052022-08-09 12:32:26 -0700972 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500973 {
974 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500975 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500976 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500977 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500978 }
979 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 {
981 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500982 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500984 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700985 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500986 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 {
988 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500989 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500991 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700992 }
993
Anthony Wilson3929aca2019-07-19 15:42:33 -0500994 for (const std::tuple<const char*, const char*,
995 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700996 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700997 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700998 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700999 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001000 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001001 continue;
1002 }
Ed Tanous1d7c0052022-08-09 12:32:26 -07001003
1004 // The property we want to set may be nested json, so use
1005 // a json_pointer for easy indexing into the json structure.
1006 const nlohmann::json::json_pointer& key = std::get<2>(p);
1007
1008 // Attempt to pull the int64 directly
1009 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
1010
1011 const double* doubleValue = std::get_if<double>(&valueVariant);
1012 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
1013 double temp = 0.0;
1014 if (int64Value != nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001015 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07001016 temp = static_cast<double>(*int64Value);
1017 }
1018 else if (doubleValue != nullptr)
1019 {
1020 temp = *doubleValue;
1021 }
1022 else if (uValue != nullptr)
1023 {
1024 temp = *uValue;
1025 }
1026 else
1027 {
1028 BMCWEB_LOG_ERROR
1029 << "Got value interface that wasn't int or double";
1030 continue;
1031 }
1032 temp = temp * std::pow(10, scaleMultiplier);
1033 if (forceToInt)
1034 {
1035 sensorJson[key] = static_cast<int64_t>(temp);
1036 }
1037 else
1038 {
1039 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001040 }
1041 }
1042 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001043
Ed Tanous1abe55e2018-09-05 08:30:59 -07001044 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001045}
1046
Ed Tanous1d7c0052022-08-09 12:32:26 -07001047/**
1048 * @brief Builds a json sensor representation of a sensor.
1049 * @param sensorName The name of the sensor to be built
1050 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
1051 * build
1052 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
1053 * @param interfacesDict A dictionary of the interfaces and properties of said
1054 * interfaces to be built from
1055 * @param sensorJson The json object to fill
1056 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1057 * be nullptr if no associated inventory item was found.
1058 */
1059inline void objectInterfacesToJson(
1060 const std::string& sensorName, const std::string& sensorType,
1061 const std::string& chassisSubNode,
1062 const dbus::utility::DBusInteracesMap& interfacesDict,
1063 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1064{
1065
1066 for (const auto& [interface, valuesDict] : interfacesDict)
1067 {
1068 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1069 valuesDict, sensorJson, inventoryItem);
1070 }
1071}
1072
Ed Tanousb5a76932020-09-29 16:16:58 -07001073inline void populateFanRedundancy(
1074 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001075{
1076 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001077 [sensorsAsyncResp](
1078 const boost::system::error_code ec,
1079 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001080 if (ec)
1081 {
1082 return; // don't have to have this interface
1083 }
1084 for (const std::pair<
1085 std::string,
1086 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1087 pathPair : resp)
1088 {
1089 const std::string& path = pathPair.first;
1090 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1091 objDict = pathPair.second;
1092 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001093 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001094 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001095 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001096
1097 const std::string& owner = objDict.begin()->first;
1098 sdbusplus::asio::getProperty<std::vector<std::string>>(
1099 *crow::connections::systemBus,
1100 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1101 "xyz.openbmc_project.Association", "endpoints",
1102 [path, owner,
1103 sensorsAsyncResp](const boost::system::error_code e,
1104 const std::vector<std::string>& endpoints) {
1105 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001106 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001107 return; // if they don't have an association we
1108 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001109 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001110 auto found =
1111 std::find_if(endpoints.begin(), endpoints.end(),
1112 [sensorsAsyncResp](const std::string& entry) {
1113 return entry.find(sensorsAsyncResp->chassisId) !=
1114 std::string::npos;
1115 });
James Feist8bd25cc2019-03-15 15:14:00 -07001116
Ed Tanous002d39b2022-05-31 08:59:27 -07001117 if (found == endpoints.end())
1118 {
1119 return;
1120 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001121 sdbusplus::asio::getAllProperties(
1122 *crow::connections::systemBus, owner, path,
1123 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001124 [path, sensorsAsyncResp](
1125 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001126 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001127 if (err)
1128 {
1129 return; // don't have to have this
1130 // interface
1131 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001132
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001133 const uint8_t* allowedFailures = nullptr;
1134 const std::vector<std::string>* collection = nullptr;
1135 const std::string* status = nullptr;
1136
1137 const bool success = sdbusplus::unpackPropertiesNoThrow(
1138 dbus_utils::UnpackErrorPrinter(), ret,
1139 "AllowedFailures", allowedFailures, "Collection",
1140 collection, "Status", status);
1141
1142 if (!success)
1143 {
1144 messages::internalError(
1145 sensorsAsyncResp->asyncResp->res);
1146 return;
1147 }
1148
1149 if (allowedFailures == nullptr || collection == nullptr ||
1150 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001151 {
1152 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1153 messages::internalError(
1154 sensorsAsyncResp->asyncResp->res);
1155 return;
1156 }
1157
Ed Tanous002d39b2022-05-31 08:59:27 -07001158 sdbusplus::message::object_path objectPath(path);
1159 std::string name = objectPath.filename();
1160 if (name.empty())
1161 {
1162 // this should be impossible
1163 messages::internalError(
1164 sensorsAsyncResp->asyncResp->res);
1165 return;
1166 }
1167 std::replace(name.begin(), name.end(), '_', ' ');
1168
1169 std::string health;
1170
Ed Tanous11ba3972022-07-11 09:50:41 -07001171 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 {
1173 health = "OK";
1174 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001175 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001176 {
1177 health = "Warning";
1178 }
1179 else
1180 {
1181 health = "Critical";
1182 }
1183 nlohmann::json::array_t redfishCollection;
1184 const auto& fanRedfish =
1185 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1186 for (const std::string& item : *collection)
1187 {
Ed Tanous8a592812022-06-04 09:06:59 -07001188 sdbusplus::message::object_path itemPath(item);
1189 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001190 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001191 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001192 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001193 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001194 /*
1195 todo(ed): merge patch that fixes the names
1196 std::replace(itemName.begin(),
1197 itemName.end(), '_', ' ');*/
1198 auto schemaItem =
1199 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1200 [itemName](const nlohmann::json& fan) {
1201 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001202 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001203 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001204 {
Ed Tanous8a592812022-06-04 09:06:59 -07001205 nlohmann::json::object_t collectionId;
1206 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001207 (*schemaItem)["@odata.id"];
1208 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001209 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001210 }
1211 else
1212 {
1213 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1214 messages::internalError(
1215 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001216 return;
1217 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001218 }
James Feist8bd25cc2019-03-15 15:14:00 -07001219
Ed Tanous002d39b2022-05-31 08:59:27 -07001220 size_t minNumNeeded =
1221 collection->empty()
1222 ? 0
1223 : collection->size() - *allowedFailures;
1224 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1225 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001226
Ed Tanous002d39b2022-05-31 08:59:27 -07001227 nlohmann::json::object_t redundancy;
1228 redundancy["@odata.id"] =
1229 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1230 "/" + sensorsAsyncResp->chassisSubNode +
1231 "#/Redundancy/" + std::to_string(jResp.size());
1232 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1233 redundancy["MinNumNeeded"] = minNumNeeded;
1234 redundancy["MemberId"] = name;
1235 redundancy["Mode"] = "N+m";
1236 redundancy["Name"] = name;
1237 redundancy["RedundancySet"] = redfishCollection;
1238 redundancy["Status"]["Health"] = health;
1239 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001240
Ed Tanous002d39b2022-05-31 08:59:27 -07001241 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001242 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001243 });
1244 }
James Feist8bd25cc2019-03-15 15:14:00 -07001245 },
1246 "xyz.openbmc_project.ObjectMapper",
1247 "/xyz/openbmc_project/object_mapper",
1248 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1249 "/xyz/openbmc_project/control", 2,
1250 std::array<const char*, 1>{
1251 "xyz.openbmc_project.Control.FanRedundancy"});
1252}
1253
Ed Tanousb5a76932020-09-29 16:16:58 -07001254inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001255 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001256{
zhanghch058d1b46d2021-04-01 11:18:24 +08001257 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001258 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001259 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001260 {
1261 sensorHeaders = {"Voltages", "PowerSupplies"};
1262 }
1263 for (const std::string& sensorGroup : sensorHeaders)
1264 {
1265 nlohmann::json::iterator entry = response.find(sensorGroup);
1266 if (entry != response.end())
1267 {
1268 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001269 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001270 return c1["Name"] < c2["Name"];
1271 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001272
1273 // add the index counts to the end of each entry
1274 size_t count = 0;
1275 for (nlohmann::json& sensorJson : *entry)
1276 {
1277 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1278 if (odata == sensorJson.end())
1279 {
1280 continue;
1281 }
1282 std::string* value = odata->get_ptr<std::string*>();
1283 if (value != nullptr)
1284 {
1285 *value += std::to_string(count);
1286 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001287 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001288 }
1289 }
1290 }
1291 }
1292}
1293
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001294/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001295 * @brief Finds the inventory item with the specified object path.
1296 * @param inventoryItems D-Bus inventory items associated with sensors.
1297 * @param invItemObjPath D-Bus object path of inventory item.
1298 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001299 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001300inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001301 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001302 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001303{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001304 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001305 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001306 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001307 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001308 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001309 }
1310 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001311 return nullptr;
1312}
1313
1314/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001315 * @brief Finds the inventory item associated with the specified sensor.
1316 * @param inventoryItems D-Bus inventory items associated with sensors.
1317 * @param sensorObjPath D-Bus object path of sensor.
1318 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001319 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001320inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001321 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001322 const std::string& sensorObjPath)
1323{
1324 for (InventoryItem& inventoryItem : *inventoryItems)
1325 {
1326 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1327 {
1328 return &inventoryItem;
1329 }
1330 }
1331 return nullptr;
1332}
1333
1334/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001335 * @brief Finds the inventory item associated with the specified led path.
1336 * @param inventoryItems D-Bus inventory items associated with sensors.
1337 * @param ledObjPath D-Bus object path of led.
1338 * @return Inventory item within vector, or nullptr if no match found.
1339 */
1340inline InventoryItem*
1341 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1342 const std::string& ledObjPath)
1343{
1344 for (InventoryItem& inventoryItem : inventoryItems)
1345 {
1346 if (inventoryItem.ledObjectPath == ledObjPath)
1347 {
1348 return &inventoryItem;
1349 }
1350 }
1351 return nullptr;
1352}
1353
1354/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001355 * @brief Adds inventory item and associated sensor to specified vector.
1356 *
1357 * Adds a new InventoryItem to the vector if necessary. Searches for an
1358 * existing InventoryItem with the specified object path. If not found, one is
1359 * added to the vector.
1360 *
1361 * Next, the specified sensor is added to the set of sensors associated with the
1362 * InventoryItem.
1363 *
1364 * @param inventoryItems D-Bus inventory items associated with sensors.
1365 * @param invItemObjPath D-Bus object path of inventory item.
1366 * @param sensorObjPath D-Bus object path of sensor
1367 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001368inline void addInventoryItem(
1369 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1370 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001371{
1372 // Look for inventory item in vector
1373 InventoryItem* inventoryItem =
1374 findInventoryItem(inventoryItems, invItemObjPath);
1375
1376 // If inventory item doesn't exist in vector, add it
1377 if (inventoryItem == nullptr)
1378 {
1379 inventoryItems->emplace_back(invItemObjPath);
1380 inventoryItem = &(inventoryItems->back());
1381 }
1382
1383 // Add sensor to set of sensors associated with inventory item
1384 inventoryItem->sensors.emplace(sensorObjPath);
1385}
1386
1387/**
1388 * @brief Stores D-Bus data in the specified inventory item.
1389 *
1390 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1391 * specified InventoryItem.
1392 *
1393 * This data is later used to provide sensor property values in the JSON
1394 * response.
1395 *
1396 * @param inventoryItem Inventory item where data will be stored.
1397 * @param interfacesDict Map containing D-Bus interfaces and their properties
1398 * for the specified inventory item.
1399 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001400inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001401 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001402 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001403{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001404 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001405
Ed Tanous9eb808c2022-01-25 10:19:23 -08001406 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001407 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001408 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001409 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001410 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001411 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001412 if (name == "Present")
1413 {
1414 const bool* value = std::get_if<bool>(&dbusValue);
1415 if (value != nullptr)
1416 {
1417 inventoryItem.isPresent = *value;
1418 }
1419 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001420 }
1421 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001422 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001423
Ed Tanous711ac7a2021-12-20 09:34:41 -08001424 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001425 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001426 inventoryItem.isPowerSupply = true;
1427 }
1428
1429 // Get properties from Inventory.Decorator.Asset interface
1430 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1431 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001432 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001433 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001434 if (name == "Manufacturer")
1435 {
1436 const std::string* value =
1437 std::get_if<std::string>(&dbusValue);
1438 if (value != nullptr)
1439 {
1440 inventoryItem.manufacturer = *value;
1441 }
1442 }
1443 if (name == "Model")
1444 {
1445 const std::string* value =
1446 std::get_if<std::string>(&dbusValue);
1447 if (value != nullptr)
1448 {
1449 inventoryItem.model = *value;
1450 }
1451 }
1452 if (name == "SerialNumber")
1453 {
1454 const std::string* value =
1455 std::get_if<std::string>(&dbusValue);
1456 if (value != nullptr)
1457 {
1458 inventoryItem.serialNumber = *value;
1459 }
1460 }
1461 if (name == "PartNumber")
1462 {
1463 const std::string* value =
1464 std::get_if<std::string>(&dbusValue);
1465 if (value != nullptr)
1466 {
1467 inventoryItem.partNumber = *value;
1468 }
1469 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001470 }
1471 }
1472
Ed Tanous711ac7a2021-12-20 09:34:41 -08001473 if (interface ==
1474 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001475 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001476 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001477 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001478 if (name == "Functional")
1479 {
1480 const bool* value = std::get_if<bool>(&dbusValue);
1481 if (value != nullptr)
1482 {
1483 inventoryItem.isFunctional = *value;
1484 }
1485 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001486 }
1487 }
1488 }
1489}
1490
1491/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001492 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001493 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001494 * Uses the specified connections (services) to obtain D-Bus data for inventory
1495 * items associated with sensors. Stores the resulting data in the
1496 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001497 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001498 * This data is later used to provide sensor property values in the JSON
1499 * response.
1500 *
1501 * Finds the inventory item data asynchronously. Invokes callback when data has
1502 * been obtained.
1503 *
1504 * The callback must have the following signature:
1505 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001506 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001507 * @endcode
1508 *
1509 * This function is called recursively, obtaining data asynchronously from one
1510 * connection in each call. This ensures the callback is not invoked until the
1511 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001512 *
1513 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001514 * @param inventoryItems D-Bus inventory items associated with sensors.
1515 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001516 * @param objectMgrPaths Mappings from connection name to DBus object path that
1517 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001518 * @param callback Callback to invoke when inventory data has been obtained.
1519 * @param invConnectionsIndex Current index in invConnections. Only specified
1520 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001521 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001522template <typename Callback>
1523static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001524 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001525 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001526 std::shared_ptr<std::set<std::string>> invConnections,
1527 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001528 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001529{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001530 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001531
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001532 // If no more connections left, call callback
1533 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001535 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001536 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1537 return;
1538 }
1539
1540 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001541 auto it = invConnections->begin();
1542 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001543 if (it != invConnections->end())
1544 {
1545 const std::string& invConnection = *it;
1546
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001547 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001548 auto respHandler =
1549 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
Ed Tanous02cad962022-06-30 16:50:15 -07001550 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1551 const boost::system::error_code ec,
1552 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001553 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554 if (ec)
1555 {
1556 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001557 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001558 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 return;
1560 }
1561
1562 // Loop through returned object paths
1563 for (const auto& objDictEntry : resp)
1564 {
1565 const std::string& objPath =
1566 static_cast<const std::string&>(objDictEntry.first);
1567
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001568 // If this object path is one of the specified inventory items
1569 InventoryItem* inventoryItem =
1570 findInventoryItem(inventoryItems, objPath);
1571 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001572 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001573 // Store inventory data in InventoryItem
1574 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001575 }
1576 }
1577
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001578 // Recurse to get inventory item data from next connection
1579 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1580 invConnections, objectMgrPaths,
1581 std::move(callback), invConnectionsIndex + 1);
1582
1583 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001584 };
1585
1586 // Find DBus object path that implements ObjectManager for the current
1587 // connection. If no mapping found, default to "/".
1588 auto iter = objectMgrPaths->find(invConnection);
1589 const std::string& objectMgrPath =
1590 (iter != objectMgrPaths->end()) ? iter->second : "/";
1591 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1592 << objectMgrPath;
1593
1594 // Get all object paths and their interfaces for current connection
1595 crow::connections::systemBus->async_method_call(
1596 std::move(respHandler), invConnection, objectMgrPath,
1597 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1598 }
1599
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001600 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001601}
1602
1603/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001604 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001605 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 * Gets the D-Bus connections (services) that provide data for the inventory
1607 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001608 *
1609 * Finds the connections asynchronously. Invokes callback when information has
1610 * been obtained.
1611 *
1612 * The callback must have the following signature:
1613 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001614 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001615 * @endcode
1616 *
1617 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001618 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001619 * @param callback Callback to invoke when connections have been obtained.
1620 */
1621template <typename Callback>
1622static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001623 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1624 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 Callback&& callback)
1626{
1627 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1628
1629 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001630 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001632 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1633 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001634 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1635
1636 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001637 auto respHandler =
1638 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1639 inventoryItems](
1640 const boost::system::error_code ec,
1641 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1643 if (ec)
1644 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001645 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001646 BMCWEB_LOG_ERROR
1647 << "getInventoryItemsConnections respHandler DBus error " << ec;
1648 return;
1649 }
1650
1651 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001652 std::shared_ptr<std::set<std::string>> invConnections =
1653 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001654
1655 // Loop through objects from GetSubTree
1656 for (const std::pair<
1657 std::string,
1658 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1659 object : subtree)
1660 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001661 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001662 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001663 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001664 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001665 // Store all connections to inventory item
1666 for (const std::pair<std::string, std::vector<std::string>>&
1667 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001668 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001669 const std::string& invConnection = objData.first;
1670 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001671 }
1672 }
1673 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001674
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001675 callback(invConnections);
1676 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1677 };
1678
1679 // Make call to ObjectMapper to find all inventory items
1680 crow::connections::systemBus->async_method_call(
1681 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1682 "/xyz/openbmc_project/object_mapper",
1683 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1684 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1685}
1686
1687/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001688 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001689 *
1690 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001691 * inventory items. Then finds the associations from those inventory items to
1692 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001693 *
1694 * Finds the inventory items asynchronously. Invokes callback when information
1695 * has been obtained.
1696 *
1697 * The callback must have the following signature:
1698 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001699 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001700 * @endcode
1701 *
1702 * @param sensorsAsyncResp Pointer to object holding response data.
1703 * @param sensorNames All sensors within the current chassis.
1704 * @param objectMgrPaths Mappings from connection name to DBus object path that
1705 * implements ObjectManager.
1706 * @param callback Callback to invoke when inventory items have been obtained.
1707 */
1708template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001709static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001710 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001711 const std::shared_ptr<std::set<std::string>>& sensorNames,
1712 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001713 Callback&& callback)
1714{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001715 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001716
1717 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001718 auto respHandler =
1719 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1720 sensorNames](const boost::system::error_code ec,
1721 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001722 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001723 if (ec)
1724 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001725 BMCWEB_LOG_ERROR
1726 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001727 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001728 return;
1729 }
1730
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001731 // Create vector to hold list of inventory items
1732 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1733 std::make_shared<std::vector<InventoryItem>>();
1734
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001735 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001736 std::string sensorAssocPath;
1737 sensorAssocPath.reserve(128); // avoid memory allocations
1738 for (const auto& objDictEntry : resp)
1739 {
1740 const std::string& objPath =
1741 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001742
1743 // If path is inventory association for one of the specified sensors
1744 for (const std::string& sensorName : *sensorNames)
1745 {
1746 sensorAssocPath = sensorName;
1747 sensorAssocPath += "/inventory";
1748 if (objPath == sensorAssocPath)
1749 {
1750 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001751 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001752 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001753 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001754 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001755 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001756 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001757 if (valueName == "endpoints")
1758 {
1759 const std::vector<std::string>* endpoints =
1760 std::get_if<std::vector<std::string>>(
1761 &value);
1762 if ((endpoints != nullptr) &&
1763 !endpoints->empty())
1764 {
1765 // Add inventory item to vector
1766 const std::string& invItemPath =
1767 endpoints->front();
1768 addInventoryItem(inventoryItems,
1769 invItemPath,
1770 sensorName);
1771 }
1772 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001773 }
1774 }
1775 }
1776 break;
1777 }
1778 }
1779 }
1780
Anthony Wilsond5005492019-07-31 16:34:17 -05001781 // Now loop through the returned object paths again, this time to
1782 // find the leds associated with the inventory items we just found
1783 std::string inventoryAssocPath;
1784 inventoryAssocPath.reserve(128); // avoid memory allocations
1785 for (const auto& objDictEntry : resp)
1786 {
1787 const std::string& objPath =
1788 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001789
1790 for (InventoryItem& inventoryItem : *inventoryItems)
1791 {
1792 inventoryAssocPath = inventoryItem.objectPath;
1793 inventoryAssocPath += "/leds";
1794 if (objPath == inventoryAssocPath)
1795 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001796 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001797 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001798 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001799 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001800 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001801 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001802 if (valueName == "endpoints")
1803 {
1804 const std::vector<std::string>* endpoints =
1805 std::get_if<std::vector<std::string>>(
1806 &value);
1807 if ((endpoints != nullptr) &&
1808 !endpoints->empty())
1809 {
1810 // Add inventory item to vector
1811 // Store LED path in inventory item
1812 const std::string& ledPath =
1813 endpoints->front();
1814 inventoryItem.ledObjectPath = ledPath;
1815 }
1816 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001817 }
1818 }
1819 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001820
Anthony Wilsond5005492019-07-31 16:34:17 -05001821 break;
1822 }
1823 }
1824 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001825 callback(inventoryItems);
1826 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001827 };
1828
1829 // Find DBus object path that implements ObjectManager for ObjectMapper
1830 std::string connection = "xyz.openbmc_project.ObjectMapper";
1831 auto iter = objectMgrPaths->find(connection);
1832 const std::string& objectMgrPath =
1833 (iter != objectMgrPaths->end()) ? iter->second : "/";
1834 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1835 << objectMgrPath;
1836
1837 // Call GetManagedObjects on the ObjectMapper to get all associations
1838 crow::connections::systemBus->async_method_call(
1839 std::move(respHandler), connection, objectMgrPath,
1840 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1841
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001842 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001843}
1844
1845/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001846 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1847 *
1848 * Uses the specified connections (services) to obtain D-Bus data for inventory
1849 * item leds associated with sensors. Stores the resulting data in the
1850 * inventoryItems vector.
1851 *
1852 * This data is later used to provide sensor property values in the JSON
1853 * response.
1854 *
1855 * Finds the inventory item led data asynchronously. Invokes callback when data
1856 * has been obtained.
1857 *
1858 * The callback must have the following signature:
1859 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001860 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001861 * @endcode
1862 *
1863 * This function is called recursively, obtaining data asynchronously from one
1864 * connection in each call. This ensures the callback is not invoked until the
1865 * last asynchronous function has completed.
1866 *
1867 * @param sensorsAsyncResp Pointer to object holding response data.
1868 * @param inventoryItems D-Bus inventory items associated with sensors.
1869 * @param ledConnections Connections that provide data for the inventory leds.
1870 * @param callback Callback to invoke when inventory data has been obtained.
1871 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1872 * in recursive calls to this function.
1873 */
1874template <typename Callback>
1875void getInventoryLedData(
1876 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1877 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001878 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001879 Callback&& callback, size_t ledConnectionsIndex = 0)
1880{
1881 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1882
1883 // If no more connections left, call callback
1884 if (ledConnectionsIndex >= ledConnections->size())
1885 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001886 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001887 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1888 return;
1889 }
1890
1891 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001892 auto it = ledConnections->begin();
1893 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001894 if (it != ledConnections->end())
1895 {
1896 const std::string& ledPath = (*it).first;
1897 const std::string& ledConnection = (*it).second;
1898 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001899 auto respHandler =
1900 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001901 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001902 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1904 if (ec)
1905 {
1906 BMCWEB_LOG_ERROR
1907 << "getInventoryLedData respHandler DBus error " << ec;
1908 messages::internalError(sensorsAsyncResp->asyncResp->res);
1909 return;
1910 }
1911
1912 BMCWEB_LOG_DEBUG << "Led state: " << state;
1913 // Find inventory item with this LED object path
1914 InventoryItem* inventoryItem =
1915 findInventoryItemForLed(*inventoryItems, ledPath);
1916 if (inventoryItem != nullptr)
1917 {
1918 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001919 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001920 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001921 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001922 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001923 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001924 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001925 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001926 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001927 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001928 {
1929 inventoryItem->ledState = LedState::OFF;
1930 }
1931 else
1932 {
1933 inventoryItem->ledState = LedState::UNKNOWN;
1934 }
1935 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001936
Ed Tanous002d39b2022-05-31 08:59:27 -07001937 // Recurse to get LED data from next connection
1938 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1939 ledConnections, std::move(callback),
1940 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001941
Ed Tanous002d39b2022-05-31 08:59:27 -07001942 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1943 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001944
1945 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001946 sdbusplus::asio::getProperty<std::string>(
1947 *crow::connections::systemBus, ledConnection, ledPath,
1948 "xyz.openbmc_project.Led.Physical", "State",
1949 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001950 }
1951
1952 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1953}
1954
1955/**
1956 * @brief Gets LED data for LEDs associated with given inventory items.
1957 *
1958 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1959 * associated with the specified inventory items. Then gets the LED data from
1960 * each connection and stores it in the inventory item.
1961 *
1962 * This data is later used to provide sensor property values in the JSON
1963 * response.
1964 *
1965 * Finds the LED data asynchronously. Invokes callback when information has
1966 * been obtained.
1967 *
1968 * The callback must have the following signature:
1969 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001970 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001971 * @endcode
1972 *
1973 * @param sensorsAsyncResp Pointer to object holding response data.
1974 * @param inventoryItems D-Bus inventory items associated with sensors.
1975 * @param callback Callback to invoke when inventory items have been obtained.
1976 */
1977template <typename Callback>
1978void getInventoryLeds(
1979 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1980 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1981 Callback&& callback)
1982{
1983 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1984
1985 const std::string path = "/xyz/openbmc_project";
1986 const std::array<std::string, 1> interfaces = {
1987 "xyz.openbmc_project.Led.Physical"};
1988
1989 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001990 auto respHandler =
1991 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1992 inventoryItems](
1993 const boost::system::error_code ec,
1994 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001995 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1996 if (ec)
1997 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001998 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001999 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2000 << ec;
2001 return;
2002 }
2003
2004 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00002005 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
2006 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05002007
2008 // Loop through objects from GetSubTree
2009 for (const std::pair<
2010 std::string,
2011 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2012 object : subtree)
2013 {
2014 // Check if object path is LED for one of the specified inventory
2015 // items
2016 const std::string& ledPath = object.first;
2017 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2018 {
2019 // Add mapping from ledPath to connection
2020 const std::string& connection = object.second.begin()->first;
2021 (*ledConnections)[ledPath] = connection;
2022 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2023 << connection;
2024 }
2025 }
2026
2027 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2028 std::move(callback));
2029 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2030 };
2031 // Make call to ObjectMapper to find all inventory items
2032 crow::connections::systemBus->async_method_call(
2033 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2034 "/xyz/openbmc_project/object_mapper",
2035 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2036 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2037}
2038
2039/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002040 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2041 *
2042 * Uses the specified connections (services) (currently assumes just one) to
2043 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2044 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2045 *
2046 * This data is later used to provide sensor property values in the JSON
2047 * response.
2048 *
2049 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2050 * when data has been obtained.
2051 *
2052 * The callback must have the following signature:
2053 * @code
2054 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2055 * @endcode
2056 *
2057 * @param sensorsAsyncResp Pointer to object holding response data.
2058 * @param inventoryItems D-Bus inventory items associated with sensors.
2059 * @param psAttributesConnections Connections that provide data for the Power
2060 * Supply Attributes
2061 * @param callback Callback to invoke when data has been obtained.
2062 */
2063template <typename Callback>
2064void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002065 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002066 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002067 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002068 Callback&& callback)
2069{
2070 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2071
2072 if (psAttributesConnections.empty())
2073 {
2074 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2075 callback(inventoryItems);
2076 return;
2077 }
2078
2079 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002080 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002081
2082 const std::string& psAttributesPath = (*it).first;
2083 const std::string& psAttributesConnection = (*it).second;
2084
2085 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002086 auto respHandler =
2087 [sensorsAsyncResp, inventoryItems,
2088 callback{std::forward<Callback>(callback)}](
2089 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002090 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2091 if (ec)
2092 {
2093 BMCWEB_LOG_ERROR
2094 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002095 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002096 return;
2097 }
2098
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002099 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2100 // Store value in Power Supply Inventory Items
2101 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002102 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002103 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002104 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002105 inventoryItem.powerSupplyEfficiencyPercent =
2106 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002107 }
2108 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002109
2110 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2111 callback(inventoryItems);
2112 };
2113
2114 // Get the DeratingFactor property for the PowerSupplyAttributes
2115 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002116 sdbusplus::asio::getProperty<uint32_t>(
2117 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2118 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2119 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002120
2121 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2122}
2123
2124/**
2125 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2126 *
2127 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2128 * data. Then gets the Power Supply Attributes data from the connection
2129 * (currently just assumes 1 connection) and stores the data in the inventory
2130 * item.
2131 *
2132 * This data is later used to provide sensor property values in the JSON
2133 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2134 *
2135 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2136 * when information has been obtained.
2137 *
2138 * The callback must have the following signature:
2139 * @code
2140 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2141 * @endcode
2142 *
2143 * @param sensorsAsyncResp Pointer to object holding response data.
2144 * @param inventoryItems D-Bus inventory items associated with sensors.
2145 * @param callback Callback to invoke when data has been obtained.
2146 */
2147template <typename Callback>
2148void getPowerSupplyAttributes(
2149 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2150 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2151 Callback&& callback)
2152{
2153 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2154
2155 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002156 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002157 {
2158 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2159 callback(inventoryItems);
2160 return;
2161 }
2162
2163 const std::array<std::string, 1> interfaces = {
2164 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2165
2166 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002167 auto respHandler =
2168 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2169 inventoryItems](
2170 const boost::system::error_code ec,
2171 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002172 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2173 if (ec)
2174 {
2175 messages::internalError(sensorsAsyncResp->asyncResp->res);
2176 BMCWEB_LOG_ERROR
2177 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2178 return;
2179 }
2180 if (subtree.empty())
2181 {
2182 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2183 callback(inventoryItems);
2184 return;
2185 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002186
Ed Tanous002d39b2022-05-31 08:59:27 -07002187 // Currently we only support 1 power supply attribute, use this for
2188 // all the power supplies. Build map of object path to connection.
2189 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002190 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002191
Ed Tanous002d39b2022-05-31 08:59:27 -07002192 if (subtree[0].first.empty() || subtree[0].second.empty())
2193 {
2194 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2195 callback(inventoryItems);
2196 return;
2197 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002198
Ed Tanous002d39b2022-05-31 08:59:27 -07002199 const std::string& psAttributesPath = subtree[0].first;
2200 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002201
Ed Tanous002d39b2022-05-31 08:59:27 -07002202 if (connection.empty())
2203 {
2204 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2205 callback(inventoryItems);
2206 return;
2207 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002208
Ed Tanous002d39b2022-05-31 08:59:27 -07002209 psAttributesConnections[psAttributesPath] = connection;
2210 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2211 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002212
Ed Tanous002d39b2022-05-31 08:59:27 -07002213 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2214 psAttributesConnections,
2215 std::move(callback));
2216 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2217 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002218 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2219 crow::connections::systemBus->async_method_call(
2220 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2221 "/xyz/openbmc_project/object_mapper",
2222 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2223 "/xyz/openbmc_project", 0, interfaces);
2224 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2225}
2226
2227/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002228 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002229 *
2230 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002231 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002232 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002233 * This data is later used to provide sensor property values in the JSON
2234 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002235 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002236 * Finds the inventory items asynchronously. Invokes callback when the
2237 * inventory items have been obtained.
2238 *
2239 * The callback must have the following signature:
2240 * @code
2241 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2242 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002243 *
2244 * @param sensorsAsyncResp Pointer to object holding response data.
2245 * @param sensorNames All sensors within the current chassis.
2246 * @param objectMgrPaths Mappings from connection name to DBus object path that
2247 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002248 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002249 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002250template <typename Callback>
2251static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002252 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002253 const std::shared_ptr<std::set<std::string>> sensorNames,
2254 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002255 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002256{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002257 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2258 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002259 [sensorsAsyncResp, objectMgrPaths,
2260 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002261 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002262 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2263 auto getInventoryItemsConnectionsCb =
2264 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2265 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002266 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002267 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2268 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2269 callback{std::move(callback)}]() {
2270 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002271
Ed Tanous002d39b2022-05-31 08:59:27 -07002272 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2273 callback{std::move(callback)}]() {
2274 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2275 // Find Power Supply Attributes and get the data
2276 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2277 std::move(callback));
2278 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002279 };
2280
Ed Tanous002d39b2022-05-31 08:59:27 -07002281 // Find led connections and get the data
2282 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2283 std::move(getInventoryLedsCb));
2284 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2285 };
2286
2287 // Get inventory item data from connections
2288 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2289 invConnections, objectMgrPaths,
2290 std::move(getInventoryItemsDataCb));
2291 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002292 };
2293
Ed Tanous002d39b2022-05-31 08:59:27 -07002294 // Get connections that provide inventory item data
2295 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2296 std::move(getInventoryItemsConnectionsCb));
2297 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2298 };
2299
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002300 // Get associations from sensors to inventory items
2301 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2302 std::move(getInventoryItemAssociationsCb));
2303 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2304}
2305
2306/**
2307 * @brief Returns JSON PowerSupply object for the specified inventory item.
2308 *
2309 * Searches for a JSON PowerSupply object that matches the specified inventory
2310 * item. If one is not found, a new PowerSupply object is added to the JSON
2311 * array.
2312 *
2313 * Multiple sensors are often associated with one power supply inventory item.
2314 * As a result, multiple sensor values are stored in one JSON PowerSupply
2315 * object.
2316 *
2317 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2318 * @param inventoryItem Inventory item for the power supply.
2319 * @param chassisId Chassis that contains the power supply.
2320 * @return JSON PowerSupply object for the specified inventory item.
2321 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002322inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002323 const InventoryItem& inventoryItem,
2324 const std::string& chassisId)
2325{
2326 // Check if matching PowerSupply object already exists in JSON array
2327 for (nlohmann::json& powerSupply : powerSupplyArray)
2328 {
2329 if (powerSupply["MemberId"] == inventoryItem.name)
2330 {
2331 return powerSupply;
2332 }
2333 }
2334
2335 // Add new PowerSupply object to JSON array
2336 powerSupplyArray.push_back({});
2337 nlohmann::json& powerSupply = powerSupplyArray.back();
2338 powerSupply["@odata.id"] =
2339 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2340 powerSupply["MemberId"] = inventoryItem.name;
2341 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2342 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2343 powerSupply["Model"] = inventoryItem.model;
2344 powerSupply["PartNumber"] = inventoryItem.partNumber;
2345 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002346 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002347
Gunnar Mills42cbe532019-08-15 15:26:54 -05002348 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2349 {
2350 powerSupply["EfficiencyPercent"] =
2351 inventoryItem.powerSupplyEfficiencyPercent;
2352 }
2353
2354 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002355 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2356 powerSupply["Status"]["Health"] = health;
2357
2358 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002359}
2360
2361/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002362 * @brief Gets the values of the specified sensors.
2363 *
2364 * Stores the results as JSON in the SensorsAsyncResp.
2365 *
2366 * Gets the sensor values asynchronously. Stores the results later when the
2367 * information has been obtained.
2368 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002369 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002370 *
2371 * To minimize the number of DBus calls, the DBus method
2372 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2373 * values of all sensors provided by a connection (service).
2374 *
2375 * The connections set contains all the connections that provide sensor values.
2376 *
2377 * The objectMgrPaths map contains mappings from a connection name to the
2378 * corresponding DBus object path that implements ObjectManager.
2379 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002380 * The InventoryItem vector contains D-Bus inventory items associated with the
2381 * sensors. Inventory item data is needed for some Redfish sensor properties.
2382 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002383 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002384 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002385 * @param connections Connections that provide sensor values.
2386 * @param objectMgrPaths Mappings from connection name to DBus object path that
2387 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002388 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002389 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002390inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002391 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002392 const std::shared_ptr<std::set<std::string>>& sensorNames,
2393 const std::set<std::string>& connections,
2394 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002395 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002396{
2397 BMCWEB_LOG_DEBUG << "getSensorData enter";
2398 // Get managed objects from all services exposing sensors
2399 for (const std::string& connection : connections)
2400 {
2401 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002402 auto getManagedObjectsCb =
2403 [sensorsAsyncResp, sensorNames,
2404 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002405 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002406 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2407 if (ec)
2408 {
2409 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002410 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002411 return;
2412 }
2413 // Go through all objects and update response with sensor data
2414 for (const auto& objDictEntry : resp)
2415 {
2416 const std::string& objPath =
2417 static_cast<const std::string&>(objDictEntry.first);
2418 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2419 << objPath;
2420
Shawn McCarneyde629b62019-03-08 10:42:51 -06002421 std::vector<std::string> split;
2422 // Reserve space for
2423 // /xyz/openbmc_project/sensors/<name>/<subname>
2424 split.reserve(6);
2425 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2426 if (split.size() < 6)
2427 {
2428 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2429 << objPath;
2430 continue;
2431 }
2432 // These indexes aren't intuitive, as boost::split puts an empty
2433 // string at the beginning
2434 const std::string& sensorType = split[4];
2435 const std::string& sensorName = split[5];
2436 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2437 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002438 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002439 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002440 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002441 continue;
2442 }
2443
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002444 // Find inventory item (if any) associated with sensor
2445 InventoryItem* inventoryItem =
2446 findInventoryItemForSensor(inventoryItems, objPath);
2447
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002448 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002449 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002450
2451 nlohmann::json* sensorJson = nullptr;
2452
Nan Zhou928fefb2022-03-28 08:45:00 -07002453 if (sensorSchema == sensors::node::sensors &&
2454 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002455 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002456 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002457 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2458 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002459 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002460 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002461 }
2462 else
2463 {
Ed Tanous271584a2019-07-09 16:24:22 -07002464 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002465 if (sensorsAsyncResp->efficientExpand)
2466 {
2467 fieldName = "Members";
2468 }
2469 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002470 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002471 fieldName = "Temperatures";
2472 }
2473 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2474 sensorType == "fan_pwm")
2475 {
2476 fieldName = "Fans";
2477 }
2478 else if (sensorType == "voltage")
2479 {
2480 fieldName = "Voltages";
2481 }
2482 else if (sensorType == "power")
2483 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002484 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002485 {
2486 fieldName = "PowerControl";
2487 }
2488 else if ((inventoryItem != nullptr) &&
2489 (inventoryItem->isPowerSupply))
2490 {
2491 fieldName = "PowerSupplies";
2492 }
2493 else
2494 {
2495 // Other power sensors are in SensorCollection
2496 continue;
2497 }
2498 }
2499 else
2500 {
2501 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2502 << sensorType;
2503 continue;
2504 }
2505
2506 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002507 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002508 if (fieldName == "PowerControl")
2509 {
2510 if (tempArray.empty())
2511 {
2512 // Put multiple "sensors" into a single
2513 // PowerControl. Follows MemberId naming and
2514 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002515 nlohmann::json::object_t power;
2516 power["@odata.id"] =
2517 "/redfish/v1/Chassis/" +
2518 sensorsAsyncResp->chassisId + "/" +
2519 sensorsAsyncResp->chassisSubNode + "#/" +
2520 fieldName + "/0";
2521 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002522 }
2523 sensorJson = &(tempArray.back());
2524 }
2525 else if (fieldName == "PowerSupplies")
2526 {
2527 if (inventoryItem != nullptr)
2528 {
2529 sensorJson =
2530 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002531 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002532 }
2533 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002534 else if (fieldName == "Members")
2535 {
Ed Tanous14766872022-03-15 10:44:42 -07002536 nlohmann::json::object_t member;
2537 member["@odata.id"] =
2538 "/redfish/v1/Chassis/" +
2539 sensorsAsyncResp->chassisId + "/" +
2540 sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2541 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002542 sensorJson = &(tempArray.back());
2543 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002544 else
2545 {
Ed Tanous14766872022-03-15 10:44:42 -07002546 nlohmann::json::object_t member;
2547 member["@odata.id"] = "/redfish/v1/Chassis/" +
2548 sensorsAsyncResp->chassisId +
2549 "/" +
2550 sensorsAsyncResp->chassisSubNode +
2551 "#/" + fieldName + "/";
2552 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002553 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002554 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002555 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002556
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002557 if (sensorJson != nullptr)
2558 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002559 objectInterfacesToJson(sensorName, sensorType,
2560 sensorsAsyncResp->chassisSubNode,
2561 objDictEntry.second, *sensorJson,
2562 inventoryItem);
2563
2564 std::string path = "/xyz/openbmc_project/sensors/";
2565 path += sensorType;
2566 path += "/";
2567 path += sensorName;
2568 sensorsAsyncResp->addMetadata(*sensorJson, sensorType,
2569 path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002570 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002571 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002572 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002573 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002574 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002575 if (sensorsAsyncResp->chassisSubNode ==
2576 sensors::node::sensors &&
2577 sensorsAsyncResp->efficientExpand)
2578 {
2579 sensorsAsyncResp->asyncResp->res
2580 .jsonValue["Members@odata.count"] =
2581 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2582 .size();
2583 }
2584 else if (sensorsAsyncResp->chassisSubNode ==
2585 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002586 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002587 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002588 }
James Feist8bd25cc2019-03-15 15:14:00 -07002589 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002590 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2591 };
2592
2593 // Find DBus object path that implements ObjectManager for the current
2594 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002595 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002596 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002597 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002598 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2599 << objectMgrPath;
2600
2601 crow::connections::systemBus->async_method_call(
2602 getManagedObjectsCb, connection, objectMgrPath,
2603 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002604 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002605 BMCWEB_LOG_DEBUG << "getSensorData exit";
2606}
2607
Nan Zhoufe04d492022-06-22 17:10:41 +00002608inline void
2609 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2610 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002611{
Nan Zhoufe04d492022-06-22 17:10:41 +00002612 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2613 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002614 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2615 auto getObjectManagerPathsCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002616 [sensorsAsyncResp, sensorNames, connections](
2617 const std::shared_ptr<std::map<std::string, std::string>>&
2618 objectMgrPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002619 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2620 auto getInventoryItemsCb =
2621 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2622 const std::shared_ptr<std::vector<InventoryItem>>&
2623 inventoryItems) {
2624 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2625 // Get sensor data and store results in JSON
2626 getSensorData(sensorsAsyncResp, sensorNames, connections,
2627 objectMgrPaths, inventoryItems);
2628 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2629 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002630
Ed Tanous002d39b2022-05-31 08:59:27 -07002631 // Get inventory items associated with sensors
2632 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2633 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002634
Ed Tanous002d39b2022-05-31 08:59:27 -07002635 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002636 };
2637
Ed Tanous002d39b2022-05-31 08:59:27 -07002638 // Get mapping from connection names to the DBus object
2639 // paths that implement the ObjectManager interface
2640 getObjectManagerPaths(sensorsAsyncResp,
2641 std::move(getObjectManagerPathsCb));
2642 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2643 };
2644
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002645 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002646 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002647}
2648
Shawn McCarneyde629b62019-03-08 10:42:51 -06002649/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002650 * @brief Entry point for retrieving sensors data related to requested
2651 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002652 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002653 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002654inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002655 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002656{
2657 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002658 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002659 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002660 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002661 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2662 processSensorList(sensorsAsyncResp, sensorNames);
2663 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2664 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002665 // SensorCollection doesn't contain the Redundancy property
2666 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2667 {
2668 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2669 nlohmann::json::array();
2670 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002671 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002672 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2673 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2674 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002675 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002676}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002677
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302678/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002679 * @brief Find the requested sensorName in the list of all sensors supplied by
2680 * the chassis node
2681 *
2682 * @param sensorName The sensor name supplied in the PATCH request
2683 * @param sensorsList The list of sensors managed by the chassis node
2684 * @param sensorsModified The list of sensors that were found as a result of
2685 * repeated calls to this function
2686 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002687inline bool
2688 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002689 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002690 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002691{
Nan Zhoufe04d492022-06-22 17:10:41 +00002692 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002693 {
George Liu28aa8de2021-02-01 15:13:30 +08002694 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002695 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002696 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002697 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002698 continue;
2699 }
2700 if (thisSensorName == sensorName)
2701 {
2702 sensorsModified.emplace(chassisSensor);
2703 return true;
2704 }
2705 }
2706 return false;
2707}
2708
2709/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302710 * @brief Entry point for overriding sensor values of given sensor
2711 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002712 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002713 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002714 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302715 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002716inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002717 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002718 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002719 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302720{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002721 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002722 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302723
Ed Tanous543f4402022-01-06 13:12:53 -08002724 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302725 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302726 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002727 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302728 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302729 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302730 if (collectionItems.first == "Temperatures")
2731 {
2732 propertyValueName = "ReadingCelsius";
2733 }
2734 else if (collectionItems.first == "Fans")
2735 {
2736 propertyValueName = "Reading";
2737 }
2738 else
2739 {
2740 propertyValueName = "ReadingVolts";
2741 }
2742 for (auto& item : collectionItems.second)
2743 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002744 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2745 "MemberId", memberId, propertyValueName,
2746 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302747 {
2748 return;
2749 }
2750 overrideMap.emplace(memberId,
2751 std::make_pair(value, collectionItems.first));
2752 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302753 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002754
Ed Tanous002d39b2022-05-31 08:59:27 -07002755 auto getChassisSensorListCb =
2756 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002757 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002758 // Match sensor names in the PATCH request to those managed by the
2759 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002760 const std::shared_ptr<std::set<std::string>> sensorNames =
2761 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302762 for (const auto& item : overrideMap)
2763 {
2764 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002765 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2766 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302767 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302768 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002769 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302770 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302771 return;
2772 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302773 }
2774 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002775 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002776 [sensorAsyncResp,
2777 overrideMap](const std::set<std::string>& /*connections*/,
2778 const std::set<std::pair<std::string, std::string>>&
2779 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002780 if (objectsWithConnection.size() != overrideMap.size())
2781 {
2782 BMCWEB_LOG_INFO
2783 << "Unable to find all objects with proper connection "
2784 << objectsWithConnection.size() << " requested "
2785 << overrideMap.size() << "\n";
2786 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2787 sensorAsyncResp->chassisSubNode ==
2788 sensors::node::thermal
2789 ? "Temperatures"
2790 : "Voltages",
2791 "Count");
2792 return;
2793 }
2794 for (const auto& item : objectsWithConnection)
2795 {
2796 sdbusplus::message::object_path path(item.first);
2797 std::string sensorName = path.filename();
2798 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302799 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002800 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302801 return;
2802 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302803
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002804 const auto& iterator = overrideMap.find(sensorName);
2805 if (iterator == overrideMap.end())
2806 {
2807 BMCWEB_LOG_INFO << "Unable to find sensor object"
2808 << item.first << "\n";
2809 messages::internalError(sensorAsyncResp->asyncResp->res);
2810 return;
2811 }
2812 crow::connections::systemBus->async_method_call(
2813 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002814 if (ec)
2815 {
2816 if (ec.value() ==
2817 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002818 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002819 BMCWEB_LOG_WARNING
2820 << "Manufacturing mode is not Enabled...can't "
2821 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002822
Ed Tanous002d39b2022-05-31 08:59:27 -07002823 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002824 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002825 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002826 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002827 BMCWEB_LOG_DEBUG
2828 << "setOverrideValueStatus DBUS error: " << ec;
2829 messages::internalError(
2830 sensorAsyncResp->asyncResp->res);
2831 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002832 },
2833 item.second, item.first, "org.freedesktop.DBus.Properties",
2834 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002835 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002836 }
2837 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302838 // Get object with connection for the given sensor name
2839 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2840 std::move(getObjectsWithConnectionCb));
2841 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302842 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002843 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2844 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2845 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302846}
2847
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002848/**
2849 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2850 * path of the sensor.
2851 *
2852 * Function builds valid Redfish response for sensor query of given chassis and
2853 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2854 * it to caller in a callback.
2855 *
2856 * @param chassis Chassis for which retrieval should be performed
2857 * @param node Node (group) of sensors. See sensors::node for supported values
2858 * @param mapComplete Callback to be called with retrieval result
2859 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002860inline void retrieveUriToDbusMap(const std::string& chassis,
2861 const std::string& node,
2862 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002863{
Ed Tanous02da7c52022-02-27 00:09:02 -08002864 decltype(sensors::paths)::const_iterator pathIt =
2865 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2866 [&node](auto&& val) { return val.first == node; });
2867 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002868 {
2869 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2870 mapComplete(boost::beast::http::status::bad_request, {});
2871 return;
2872 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002873
Nan Zhou72374eb2022-01-27 17:06:51 -08002874 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002875 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2876 const boost::beast::http::status status,
2877 const std::map<std::string, std::string>& uriToDbus) {
2878 mapCompleteCb(status, uriToDbus);
2879 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002880
2881 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002882 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002883 getChassisData(resp);
2884}
2885
Nan Zhoubacb2162022-04-06 11:28:32 -07002886namespace sensors
2887{
Nan Zhou928fefb2022-03-28 08:45:00 -07002888
Nan Zhoubacb2162022-04-06 11:28:32 -07002889inline void getChassisCallback(
2890 const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002891 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002892{
2893 BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2894
2895 nlohmann::json& entriesArray =
2896 asyncResp->asyncResp->res.jsonValue["Members"];
Nan Zhoufe04d492022-06-22 17:10:41 +00002897 for (const auto& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002898 {
2899 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2900
2901 sdbusplus::message::object_path path(sensor);
2902 std::string sensorName = path.filename();
2903 if (sensorName.empty())
2904 {
2905 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2906 messages::internalError(asyncResp->asyncResp->res);
2907 return;
2908 }
Ed Tanous14766872022-03-15 10:44:42 -07002909 nlohmann::json::object_t member;
2910 member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
2911 "/" + asyncResp->chassisSubNode + "/" +
2912 sensorName;
2913 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002914 }
2915
2916 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2917 entriesArray.size();
2918 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2919}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002920
Nan Zhoude167a62022-06-01 04:47:45 +00002921inline void
2922 handleSensorCollectionGet(App& app, const crow::Request& req,
2923 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2924 const std::string& chassisId)
2925{
2926 query_param::QueryCapabilities capabilities = {
2927 .canDelegateExpandLevel = 1,
2928 };
2929 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002930 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002931 delegatedQuery, capabilities))
2932 {
2933 return;
2934 }
2935
2936 if (delegatedQuery.expandType != query_param::ExpandType::None)
2937 {
2938 // we perform efficient expand.
2939 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2940 aResp, chassisId, sensors::dbus::sensorPaths,
2941 sensors::node::sensors,
2942 /*efficientExpand=*/true);
2943 getChassisData(asyncResp);
2944
2945 BMCWEB_LOG_DEBUG
2946 << "SensorCollection doGet exit via efficient expand handler";
2947 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002948 }
Nan Zhoude167a62022-06-01 04:47:45 +00002949
2950 // if there's no efficient expand available, we use the default
2951 // Query Parameters route
2952 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2953 aResp, chassisId, sensors::dbus::sensorPaths, sensors::node::sensors);
2954
2955 // We get all sensors as hyperlinkes in the chassis (this
2956 // implies we reply on the default query parameters handler)
Ed Tanous7f1cc262022-08-09 13:33:57 -07002957 getChassis(asyncResp->asyncResp, asyncResp->chassisId,
2958 asyncResp->chassisSubNode, asyncResp->types,
2959
Nan Zhoude167a62022-06-01 04:47:45 +00002960 std::bind_front(sensors::getChassisCallback, asyncResp));
2961 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
2962}
2963
Nan Zhoue6bd8462022-06-01 04:35:35 +00002964inline void handleSensorGet(App& app, const crow::Request& req,
2965 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2966 const std::string& chassisId,
2967 const std::string& sensorName)
2968{
Carson Labrado3ba00072022-06-06 19:40:56 +00002969 if (!redfish::setUpRedfishRoute(app, req, aResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002970 {
2971 return;
2972 }
2973 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2974 std::shared_ptr<SensorsAsyncResp> asyncResp =
2975 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
2976 std::span<std::string_view>(),
2977 sensors::node::sensors);
2978
2979 const std::array<const char*, 1> interfaces = {
2980 "xyz.openbmc_project.Sensor.Value"};
2981
2982 // Get a list of all of the sensors that implement Sensor.Value
2983 // and get the path and service name associated with the sensor
2984 crow::connections::systemBus->async_method_call(
2985 [asyncResp,
2986 sensorName](const boost::system::error_code ec,
2987 const ::dbus::utility::MapperGetSubTreeResponse& subtree) {
2988 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2989 if (ec)
2990 {
2991 messages::internalError(asyncResp->asyncResp->res);
2992 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2993 << "Dbus error " << ec;
2994 return;
2995 }
2996
2997 ::dbus::utility::MapperGetSubTreeResponse::const_iterator it =
2998 std::find_if(
2999 subtree.begin(), subtree.end(),
3000 [sensorName](
3001 const std::pair<
3002 std::string,
3003 std::vector<std::pair<
3004 std::string, std::vector<std::string>>>>& object) {
3005 sdbusplus::message::object_path path(object.first);
3006 std::string name = path.filename();
3007 if (name.empty())
3008 {
3009 BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first;
3010 return false;
3011 }
3012
3013 return name == sensorName;
3014 });
3015
3016 if (it == subtree.end())
3017 {
3018 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3019 << sensorName;
3020 messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor",
3021 sensorName);
3022 return;
3023 }
3024 std::string_view sensorPath = (*it).first;
3025 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
3026 << "': " << sensorPath;
3027
Nan Zhoufe04d492022-06-22 17:10:41 +00003028 const std::shared_ptr<std::set<std::string>> sensorList =
3029 std::make_shared<std::set<std::string>>();
Nan Zhoue6bd8462022-06-01 04:35:35 +00003030
3031 sensorList->emplace(sensorPath);
3032 processSensorList(asyncResp, sensorList);
3033 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3034 },
3035 "xyz.openbmc_project.ObjectMapper",
3036 "/xyz/openbmc_project/object_mapper",
3037 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3038 "/xyz/openbmc_project/sensors", 2, interfaces);
3039}
3040
Nan Zhoubacb2162022-04-06 11:28:32 -07003041} // namespace sensors
3042
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003043inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003044{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003045 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003046 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003047 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003048 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003049}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003050
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003051inline void requestRoutesSensor(App& app)
3052{
3053 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003054 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003055 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003056 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003057}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003058
Ed Tanous1abe55e2018-09-05 08:30:59 -07003059} // namespace redfish