blob: ae68a76cf9ecd03c237437e4adc6c46a766b2640 [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 Tanous81ce6092020-12-17 16:54:55 +0000442 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700443 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000444 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445{
Ed Tanous81ce6092020-12-17 16:54:55 +0000446 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447 {
448 return;
449 }
450 if ((allSensors == nullptr) || (activeSensors == nullptr))
451 {
452 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800453 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000454 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200455 ? "Temperatures"
456 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700457
458 return;
459 }
460 if (allSensors->empty())
461 {
462 // Nothing to do, the activeSensors object is also empty
463 return;
464 }
465
Ed Tanous02da7c52022-02-27 00:09:02 -0800466 for (std::string_view type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700467 {
468 for (const std::string& sensor : *allSensors)
469 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700470 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700471 {
472 activeSensors->emplace(sensor);
473 }
474 }
475 }
476}
477
478/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100479 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200480 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100481 * @param callback Callback for next step in gathered sensor processing
482 */
483template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700484void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700485 Callback&& callback)
486{
487 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500488 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700489 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500490 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700491 auto respHandler =
492 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
493 const boost::system::error_code ec,
494 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700495 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
496 if (ec)
497 {
498 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800499 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700500 return;
501 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100502
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700503 const std::string* chassisPath = nullptr;
504 std::string chassisName;
505 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700506 {
George Liu28aa8de2021-02-01 15:13:30 +0800507 sdbusplus::message::object_path path(chassis);
508 chassisName = path.filename();
509 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700510 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700511 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700512 continue;
513 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700514 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700515 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700516 chassisPath = &chassis;
517 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700519 }
520 if (chassisPath == nullptr)
521 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800522 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
523 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700524 return;
525 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700527 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200528 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800530 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700531 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200533 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700534 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800535 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700536 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800537 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
538 nlohmann::json::array();
539 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800540 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200542 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500543 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800544 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500545 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800546 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500547 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800548 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500549 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800550 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
551 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500552 }
553
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200554 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500555 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800556 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500557 }
558
zhanghch058d1b46d2021-04-01 11:18:24 +0800559 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700560 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
561 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800562 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500563 // Get the list of all sensors for this Chassis element
564 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700565 sdbusplus::asio::getProperty<std::vector<std::string>>(
566 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
567 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800568 [sensorsAsyncResp,
569 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700570 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700571 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700572 if (e)
573 {
574 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700575 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700576 messages::internalError(sensorsAsyncResp->asyncResp->res);
577 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700578 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000580 const std::shared_ptr<std::set<std::string>> culledSensorList =
581 std::make_shared<std::set<std::string>>();
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
583 culledSensorList);
584 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700585 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100586 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100587
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700588 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700590 respHandler, "xyz.openbmc_project.ObjectMapper",
591 "/xyz/openbmc_project/object_mapper",
592 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700593 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700594 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100595}
596
597/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600598 * @brief Finds all DBus object paths that implement ObjectManager.
599 *
600 * Creates a mapping from the associated connection name to the object path.
601 *
602 * Finds the object paths asynchronously. Invokes callback when information has
603 * been obtained.
604 *
605 * The callback must have the following signature:
606 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +0000607 * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600608 * @endcode
609 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700610 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600611 * @param callback Callback to invoke when object paths obtained.
612 */
613template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700614void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000615 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700616 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600617{
618 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
619 const std::array<std::string, 1> interfaces = {
620 "org.freedesktop.DBus.ObjectManager"};
621
622 // Response handler for GetSubTree DBus method
Ed Tanous002d39b2022-05-31 08:59:27 -0700623 auto respHandler =
624 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
625 const boost::system::error_code ec,
626 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600627 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
628 if (ec)
629 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800630 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600631 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
632 << ec;
633 return;
634 }
635
636 // Loop over returned object paths
Nan Zhoufe04d492022-06-22 17:10:41 +0000637 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
638 std::make_shared<std::map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600639 for (const std::pair<
640 std::string,
641 std::vector<std::pair<std::string, std::vector<std::string>>>>&
642 object : subtree)
643 {
644 // Loop over connections for current object path
645 const std::string& objectPath = object.first;
646 for (const std::pair<std::string, std::vector<std::string>>&
647 objData : object.second)
648 {
649 // Add mapping from connection to object path
650 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500651 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600652 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
653 << objectPath;
654 }
655 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500656 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600657 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
658 };
659
660 // Query mapper for all DBus object paths that implement ObjectManager
661 crow::connections::systemBus->async_method_call(
662 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
663 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700664 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600665 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
666}
667
668/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500669 * @brief Returns the Redfish State value for the specified inventory item.
670 * @param inventoryItem D-Bus inventory item associated with a sensor.
671 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700672 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000673inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500674{
675 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
676 {
677 return "Absent";
678 }
James Feist34dd1792019-05-17 14:10:54 -0700679
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500680 return "Enabled";
681}
682
683/**
684 * @brief Returns the Redfish Health value for the specified sensor.
685 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700686 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500687 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
688 * be nullptr if no associated inventory item was found.
689 * @return Health value for sensor.
690 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700691inline std::string getHealth(nlohmann::json& sensorJson,
692 const dbus::utility::DBusPropertiesMap& valuesDict,
693 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700694{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500695 // Get current health value (if any) in the sensor JSON object. Some JSON
696 // objects contain multiple sensors (such as PowerSupplies). We want to set
697 // the overall health to be the most severe of any of the sensors.
698 std::string currentHealth;
699 auto statusIt = sensorJson.find("Status");
700 if (statusIt != sensorJson.end())
701 {
702 auto healthIt = statusIt->find("Health");
703 if (healthIt != statusIt->end())
704 {
705 std::string* health = healthIt->get_ptr<std::string*>();
706 if (health != nullptr)
707 {
708 currentHealth = *health;
709 }
710 }
711 }
712
713 // If current health in JSON object is already Critical, return that. This
714 // should override the sensor health, which might be less severe.
715 if (currentHealth == "Critical")
716 {
717 return "Critical";
718 }
719
720 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800721
Ed Tanous1d7c0052022-08-09 12:32:26 -0700722 for (const auto& [valueName, value] : valuesDict)
James Feist34dd1792019-05-17 14:10:54 -0700723 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700724 if (valueName == "CriticalAlarmHigh" || valueName == "CriticalAlarmLow")
James Feist34dd1792019-05-17 14:10:54 -0700725 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700726 const bool* asserted = std::get_if<bool>(&value);
727 if (asserted == nullptr)
James Feist34dd1792019-05-17 14:10:54 -0700728 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700729 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
730 }
731 else if (*asserted)
732 {
733 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700734 }
735 }
736 }
737
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500738 // Check if associated inventory item is not functional
739 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
740 {
741 return "Critical";
742 }
743
744 // If current health in JSON object is already Warning, return that. This
745 // should override the sensor status, which might be less severe.
746 if (currentHealth == "Warning")
747 {
748 return "Warning";
749 }
750
751 // Check if sensor has warning threshold alarm
Ed Tanous1d7c0052022-08-09 12:32:26 -0700752 for (const auto& [valueName, value] : valuesDict)
James Feist34dd1792019-05-17 14:10:54 -0700753 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700754 if (valueName == "WarningAlarmHigh" || valueName == "WarningAlarmLow")
James Feist34dd1792019-05-17 14:10:54 -0700755 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700756 const bool* asserted = std::get_if<bool>(&value);
757 if (asserted == nullptr)
James Feist34dd1792019-05-17 14:10:54 -0700758 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700759 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
760 }
761 else if (*asserted)
762 {
763 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700764 }
765 }
766 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500767
James Feist34dd1792019-05-17 14:10:54 -0700768 return "OK";
769}
770
Ed Tanous23a21a12020-07-25 04:45:05 +0000771inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500772 const InventoryItem* inventoryItem)
773{
774 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
775 {
776 switch (inventoryItem->ledState)
777 {
778 case LedState::OFF:
779 sensorJson["IndicatorLED"] = "Off";
780 break;
781 case LedState::ON:
782 sensorJson["IndicatorLED"] = "Lit";
783 break;
784 case LedState::BLINK:
785 sensorJson["IndicatorLED"] = "Blinking";
786 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000787 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500788 break;
789 }
790 }
791}
792
James Feist34dd1792019-05-17 14:10:54 -0700793/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100794 * @brief Builds a json sensor representation of a sensor.
795 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500796 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100797 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700798 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
799 * @param propertiesDict A dictionary of the properties to build the sensor
800 * from.
801 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500802 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
803 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100804 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700805inline void objectPropertiesToJson(
806 std::string_view sensorName, std::string_view sensorType,
807 std::string_view chassisSubNode,
808 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000809 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700810{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700811 // Assume values exist as is (10^0 == 1) if no scale exists
812 int64_t scaleMultiplier = 0;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700813 for (const auto& [valueName, value] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700814 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700815 if (valueName == "Scale")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700816 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700817 const int64_t* int64Value = std::get_if<int64_t>(&value);
818 if (int64Value != nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800819 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700820 scaleMultiplier = *int64Value;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800821 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100822 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100823 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700824
Ed Tanous1d7c0052022-08-09 12:32:26 -0700825 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500826 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000827 sensorJson["Id"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700828 std::string sensorNameEs(sensorName);
829 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
830 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500831 }
832 else if (sensorType != "power")
833 {
834 // Set MemberId and Name for non-power sensors. For PowerSupplies and
835 // PowerControl, those properties have more general values because
836 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000837 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700838 std::string sensorNameEs(sensorName);
839 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
840 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500841 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700842
Ed Tanous81ce6092020-12-17 16:54:55 +0000843 sensorJson["Status"]["State"] = getState(inventoryItem);
844 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700845 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700846
847 // Parameter to set to override the type we get from dbus, and force it to
848 // int, regardless of what is available. This is used for schemas like fan,
849 // that require integers, not floats.
850 bool forceToInt = false;
851
Anthony Wilson3929aca2019-07-19 15:42:33 -0500852 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700853 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500854 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000855 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000856
Ed Tanous1d7c0052022-08-09 12:32:26 -0700857 std::string_view readingType = sensors::toReadingType(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000858 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500859 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000860 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
861 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500862 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000863 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500864 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000865 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500866 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000867
Ed Tanous1d7c0052022-08-09 12:32:26 -0700868 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000869 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200870 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000871 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
872 << sensorType;
873 }
874 else
875 {
876 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200877 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500878 }
879 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700880 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500881 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000882 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700883 // TODO(ed) Documentation says that path should be type fan_tach,
884 // implementation seems to implement fan
885 }
886 else if (sensorType == "fan" || sensorType == "fan_tach")
887 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500888 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000889 sensorJson["ReadingUnits"] = "RPM";
890 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
891 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700892 forceToInt = true;
893 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700894 else if (sensorType == "fan_pwm")
895 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500896 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000897 sensorJson["ReadingUnits"] = "Percent";
898 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
899 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700900 forceToInt = true;
901 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700902 else if (sensorType == "voltage")
903 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500904 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000905 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700906 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700907 else if (sensorType == "power")
908 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700909 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000910 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000911 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500912 // Put multiple "sensors" into a single PowerControl, so have
913 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000914 sensorJson["MemberId"] = "0";
915 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500916 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000917 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700918 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700919 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500920 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700921 }
922 else
923 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500924 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700925 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700926 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700927 else
928 {
929 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
930 return;
931 }
932 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500933 std::vector<
934 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
935 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700936 properties.reserve(7);
937
938 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600939
Ed Tanous1d7c0052022-08-09 12:32:26 -0700940 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500941 {
942 properties.emplace_back(
943 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
944 "/Thresholds/UpperCaution/Reading"_json_pointer);
945 properties.emplace_back(
946 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
947 "/Thresholds/LowerCaution/Reading"_json_pointer);
948 properties.emplace_back(
949 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
950 "/Thresholds/UpperCritical/Reading"_json_pointer);
951 properties.emplace_back(
952 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
953 "/Thresholds/LowerCritical/Reading"_json_pointer);
954 }
955 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600956 {
957 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500958 "WarningHigh",
959 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600960 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500961 "WarningLow",
962 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600963 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500964 "CriticalHigh",
965 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600966 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500967 "CriticalLow",
968 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600969 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700970
Ed Tanous2474adf2018-09-05 16:31:16 -0700971 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
972
Ed Tanous1d7c0052022-08-09 12:32:26 -0700973 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500974 {
975 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500976 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500977 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500978 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500979 }
980 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 {
982 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500983 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700984 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500985 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700986 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500987 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700988 {
989 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500990 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500992 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700993 }
994
Anthony Wilson3929aca2019-07-19 15:42:33 -0500995 for (const std::tuple<const char*, const char*,
996 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700997 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700998 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700999 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07001000 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001001 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001002 continue;
1003 }
Ed Tanous1d7c0052022-08-09 12:32:26 -07001004
1005 // The property we want to set may be nested json, so use
1006 // a json_pointer for easy indexing into the json structure.
1007 const nlohmann::json::json_pointer& key = std::get<2>(p);
1008
1009 // Attempt to pull the int64 directly
1010 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
1011
1012 const double* doubleValue = std::get_if<double>(&valueVariant);
1013 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
1014 double temp = 0.0;
1015 if (int64Value != nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001016 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07001017 temp = static_cast<double>(*int64Value);
1018 }
1019 else if (doubleValue != nullptr)
1020 {
1021 temp = *doubleValue;
1022 }
1023 else if (uValue != nullptr)
1024 {
1025 temp = *uValue;
1026 }
1027 else
1028 {
1029 BMCWEB_LOG_ERROR
1030 << "Got value interface that wasn't int or double";
1031 continue;
1032 }
1033 temp = temp * std::pow(10, scaleMultiplier);
1034 if (forceToInt)
1035 {
1036 sensorJson[key] = static_cast<int64_t>(temp);
1037 }
1038 else
1039 {
1040 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001041 }
1042 }
1043 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001044
Ed Tanous1abe55e2018-09-05 08:30:59 -07001045 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001046}
1047
Ed Tanous1d7c0052022-08-09 12:32:26 -07001048/**
1049 * @brief Builds a json sensor representation of a sensor.
1050 * @param sensorName The name of the sensor to be built
1051 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
1052 * build
1053 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
1054 * @param interfacesDict A dictionary of the interfaces and properties of said
1055 * interfaces to be built from
1056 * @param sensorJson The json object to fill
1057 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1058 * be nullptr if no associated inventory item was found.
1059 */
1060inline void objectInterfacesToJson(
1061 const std::string& sensorName, const std::string& sensorType,
1062 const std::string& chassisSubNode,
1063 const dbus::utility::DBusInteracesMap& interfacesDict,
1064 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1065{
1066
1067 for (const auto& [interface, valuesDict] : interfacesDict)
1068 {
1069 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1070 valuesDict, sensorJson, inventoryItem);
1071 }
1072}
1073
Ed Tanousb5a76932020-09-29 16:16:58 -07001074inline void populateFanRedundancy(
1075 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001076{
1077 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001078 [sensorsAsyncResp](
1079 const boost::system::error_code ec,
1080 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001081 if (ec)
1082 {
1083 return; // don't have to have this interface
1084 }
1085 for (const std::pair<
1086 std::string,
1087 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1088 pathPair : resp)
1089 {
1090 const std::string& path = pathPair.first;
1091 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1092 objDict = pathPair.second;
1093 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001094 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001096 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001097
1098 const std::string& owner = objDict.begin()->first;
1099 sdbusplus::asio::getProperty<std::vector<std::string>>(
1100 *crow::connections::systemBus,
1101 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1102 "xyz.openbmc_project.Association", "endpoints",
1103 [path, owner,
1104 sensorsAsyncResp](const boost::system::error_code e,
1105 const std::vector<std::string>& endpoints) {
1106 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001107 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001108 return; // if they don't have an association we
1109 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001110 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001111 auto found =
1112 std::find_if(endpoints.begin(), endpoints.end(),
1113 [sensorsAsyncResp](const std::string& entry) {
1114 return entry.find(sensorsAsyncResp->chassisId) !=
1115 std::string::npos;
1116 });
James Feist8bd25cc2019-03-15 15:14:00 -07001117
Ed Tanous002d39b2022-05-31 08:59:27 -07001118 if (found == endpoints.end())
1119 {
1120 return;
1121 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001122 sdbusplus::asio::getAllProperties(
1123 *crow::connections::systemBus, owner, path,
1124 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001125 [path, sensorsAsyncResp](
1126 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001127 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 if (err)
1129 {
1130 return; // don't have to have this
1131 // interface
1132 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001133
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001134 const uint8_t* allowedFailures = nullptr;
1135 const std::vector<std::string>* collection = nullptr;
1136 const std::string* status = nullptr;
1137
1138 const bool success = sdbusplus::unpackPropertiesNoThrow(
1139 dbus_utils::UnpackErrorPrinter(), ret,
1140 "AllowedFailures", allowedFailures, "Collection",
1141 collection, "Status", status);
1142
1143 if (!success)
1144 {
1145 messages::internalError(
1146 sensorsAsyncResp->asyncResp->res);
1147 return;
1148 }
1149
1150 if (allowedFailures == nullptr || collection == nullptr ||
1151 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001152 {
1153 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1154 messages::internalError(
1155 sensorsAsyncResp->asyncResp->res);
1156 return;
1157 }
1158
Ed Tanous002d39b2022-05-31 08:59:27 -07001159 sdbusplus::message::object_path objectPath(path);
1160 std::string name = objectPath.filename();
1161 if (name.empty())
1162 {
1163 // this should be impossible
1164 messages::internalError(
1165 sensorsAsyncResp->asyncResp->res);
1166 return;
1167 }
1168 std::replace(name.begin(), name.end(), '_', ' ');
1169
1170 std::string health;
1171
Ed Tanous11ba3972022-07-11 09:50:41 -07001172 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001173 {
1174 health = "OK";
1175 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001176 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001177 {
1178 health = "Warning";
1179 }
1180 else
1181 {
1182 health = "Critical";
1183 }
1184 nlohmann::json::array_t redfishCollection;
1185 const auto& fanRedfish =
1186 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1187 for (const std::string& item : *collection)
1188 {
Ed Tanous8a592812022-06-04 09:06:59 -07001189 sdbusplus::message::object_path itemPath(item);
1190 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001191 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001192 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001193 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001194 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001195 /*
1196 todo(ed): merge patch that fixes the names
1197 std::replace(itemName.begin(),
1198 itemName.end(), '_', ' ');*/
1199 auto schemaItem =
1200 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1201 [itemName](const nlohmann::json& fan) {
1202 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001203 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001204 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001205 {
Ed Tanous8a592812022-06-04 09:06:59 -07001206 nlohmann::json::object_t collectionId;
1207 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001208 (*schemaItem)["@odata.id"];
1209 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001210 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001211 }
1212 else
1213 {
1214 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1215 messages::internalError(
1216 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001217 return;
1218 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001219 }
James Feist8bd25cc2019-03-15 15:14:00 -07001220
Ed Tanous002d39b2022-05-31 08:59:27 -07001221 size_t minNumNeeded =
1222 collection->empty()
1223 ? 0
1224 : collection->size() - *allowedFailures;
1225 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1226 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001227
Ed Tanous002d39b2022-05-31 08:59:27 -07001228 nlohmann::json::object_t redundancy;
1229 redundancy["@odata.id"] =
1230 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1231 "/" + sensorsAsyncResp->chassisSubNode +
1232 "#/Redundancy/" + std::to_string(jResp.size());
1233 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1234 redundancy["MinNumNeeded"] = minNumNeeded;
1235 redundancy["MemberId"] = name;
1236 redundancy["Mode"] = "N+m";
1237 redundancy["Name"] = name;
1238 redundancy["RedundancySet"] = redfishCollection;
1239 redundancy["Status"]["Health"] = health;
1240 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001241
Ed Tanous002d39b2022-05-31 08:59:27 -07001242 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001243 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001244 });
1245 }
James Feist8bd25cc2019-03-15 15:14:00 -07001246 },
1247 "xyz.openbmc_project.ObjectMapper",
1248 "/xyz/openbmc_project/object_mapper",
1249 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1250 "/xyz/openbmc_project/control", 2,
1251 std::array<const char*, 1>{
1252 "xyz.openbmc_project.Control.FanRedundancy"});
1253}
1254
Ed Tanousb5a76932020-09-29 16:16:58 -07001255inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001256 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001257{
zhanghch058d1b46d2021-04-01 11:18:24 +08001258 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001259 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001260 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001261 {
1262 sensorHeaders = {"Voltages", "PowerSupplies"};
1263 }
1264 for (const std::string& sensorGroup : sensorHeaders)
1265 {
1266 nlohmann::json::iterator entry = response.find(sensorGroup);
1267 if (entry != response.end())
1268 {
1269 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001270 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001271 return c1["Name"] < c2["Name"];
1272 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001273
1274 // add the index counts to the end of each entry
1275 size_t count = 0;
1276 for (nlohmann::json& sensorJson : *entry)
1277 {
1278 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1279 if (odata == sensorJson.end())
1280 {
1281 continue;
1282 }
1283 std::string* value = odata->get_ptr<std::string*>();
1284 if (value != nullptr)
1285 {
1286 *value += std::to_string(count);
1287 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001288 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001289 }
1290 }
1291 }
1292 }
1293}
1294
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001295/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001296 * @brief Finds the inventory item with the specified object path.
1297 * @param inventoryItems D-Bus inventory items associated with sensors.
1298 * @param invItemObjPath D-Bus object path of inventory item.
1299 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001300 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001301inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001302 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001303 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001304{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001305 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001306 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001307 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001308 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001309 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001310 }
1311 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001312 return nullptr;
1313}
1314
1315/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001316 * @brief Finds the inventory item associated with the specified sensor.
1317 * @param inventoryItems D-Bus inventory items associated with sensors.
1318 * @param sensorObjPath D-Bus object path of sensor.
1319 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001320 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001321inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001322 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001323 const std::string& sensorObjPath)
1324{
1325 for (InventoryItem& inventoryItem : *inventoryItems)
1326 {
1327 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1328 {
1329 return &inventoryItem;
1330 }
1331 }
1332 return nullptr;
1333}
1334
1335/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001336 * @brief Finds the inventory item associated with the specified led path.
1337 * @param inventoryItems D-Bus inventory items associated with sensors.
1338 * @param ledObjPath D-Bus object path of led.
1339 * @return Inventory item within vector, or nullptr if no match found.
1340 */
1341inline InventoryItem*
1342 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1343 const std::string& ledObjPath)
1344{
1345 for (InventoryItem& inventoryItem : inventoryItems)
1346 {
1347 if (inventoryItem.ledObjectPath == ledObjPath)
1348 {
1349 return &inventoryItem;
1350 }
1351 }
1352 return nullptr;
1353}
1354
1355/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001356 * @brief Adds inventory item and associated sensor to specified vector.
1357 *
1358 * Adds a new InventoryItem to the vector if necessary. Searches for an
1359 * existing InventoryItem with the specified object path. If not found, one is
1360 * added to the vector.
1361 *
1362 * Next, the specified sensor is added to the set of sensors associated with the
1363 * InventoryItem.
1364 *
1365 * @param inventoryItems D-Bus inventory items associated with sensors.
1366 * @param invItemObjPath D-Bus object path of inventory item.
1367 * @param sensorObjPath D-Bus object path of sensor
1368 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001369inline void addInventoryItem(
1370 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1371 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001372{
1373 // Look for inventory item in vector
1374 InventoryItem* inventoryItem =
1375 findInventoryItem(inventoryItems, invItemObjPath);
1376
1377 // If inventory item doesn't exist in vector, add it
1378 if (inventoryItem == nullptr)
1379 {
1380 inventoryItems->emplace_back(invItemObjPath);
1381 inventoryItem = &(inventoryItems->back());
1382 }
1383
1384 // Add sensor to set of sensors associated with inventory item
1385 inventoryItem->sensors.emplace(sensorObjPath);
1386}
1387
1388/**
1389 * @brief Stores D-Bus data in the specified inventory item.
1390 *
1391 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1392 * specified InventoryItem.
1393 *
1394 * This data is later used to provide sensor property values in the JSON
1395 * response.
1396 *
1397 * @param inventoryItem Inventory item where data will be stored.
1398 * @param interfacesDict Map containing D-Bus interfaces and their properties
1399 * for the specified inventory item.
1400 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001401inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001402 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001403 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001404{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001405 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001406
Ed Tanous9eb808c2022-01-25 10:19:23 -08001407 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001408 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001409 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001410 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001411 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001412 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001413 if (name == "Present")
1414 {
1415 const bool* value = std::get_if<bool>(&dbusValue);
1416 if (value != nullptr)
1417 {
1418 inventoryItem.isPresent = *value;
1419 }
1420 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421 }
1422 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001423 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001424
Ed Tanous711ac7a2021-12-20 09:34:41 -08001425 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001427 inventoryItem.isPowerSupply = true;
1428 }
1429
1430 // Get properties from Inventory.Decorator.Asset interface
1431 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1432 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001433 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001434 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001435 if (name == "Manufacturer")
1436 {
1437 const std::string* value =
1438 std::get_if<std::string>(&dbusValue);
1439 if (value != nullptr)
1440 {
1441 inventoryItem.manufacturer = *value;
1442 }
1443 }
1444 if (name == "Model")
1445 {
1446 const std::string* value =
1447 std::get_if<std::string>(&dbusValue);
1448 if (value != nullptr)
1449 {
1450 inventoryItem.model = *value;
1451 }
1452 }
1453 if (name == "SerialNumber")
1454 {
1455 const std::string* value =
1456 std::get_if<std::string>(&dbusValue);
1457 if (value != nullptr)
1458 {
1459 inventoryItem.serialNumber = *value;
1460 }
1461 }
1462 if (name == "PartNumber")
1463 {
1464 const std::string* value =
1465 std::get_if<std::string>(&dbusValue);
1466 if (value != nullptr)
1467 {
1468 inventoryItem.partNumber = *value;
1469 }
1470 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001471 }
1472 }
1473
Ed Tanous711ac7a2021-12-20 09:34:41 -08001474 if (interface ==
1475 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001476 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001477 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001478 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001479 if (name == "Functional")
1480 {
1481 const bool* value = std::get_if<bool>(&dbusValue);
1482 if (value != nullptr)
1483 {
1484 inventoryItem.isFunctional = *value;
1485 }
1486 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001487 }
1488 }
1489 }
1490}
1491
1492/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001493 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001494 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001495 * Uses the specified connections (services) to obtain D-Bus data for inventory
1496 * items associated with sensors. Stores the resulting data in the
1497 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001499 * This data is later used to provide sensor property values in the JSON
1500 * response.
1501 *
1502 * Finds the inventory item data asynchronously. Invokes callback when data has
1503 * been obtained.
1504 *
1505 * The callback must have the following signature:
1506 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001507 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001508 * @endcode
1509 *
1510 * This function is called recursively, obtaining data asynchronously from one
1511 * connection in each call. This ensures the callback is not invoked until the
1512 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001513 *
1514 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001515 * @param inventoryItems D-Bus inventory items associated with sensors.
1516 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001517 * @param objectMgrPaths Mappings from connection name to DBus object path that
1518 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001519 * @param callback Callback to invoke when inventory data has been obtained.
1520 * @param invConnectionsIndex Current index in invConnections. Only specified
1521 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001522 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001523template <typename Callback>
1524static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001525 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001526 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001527 std::shared_ptr<std::set<std::string>> invConnections,
1528 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001529 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001530{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001531 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001532
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001533 // If no more connections left, call callback
1534 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001535 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001536 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001537 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1538 return;
1539 }
1540
1541 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001542 auto it = invConnections->begin();
1543 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001544 if (it != invConnections->end())
1545 {
1546 const std::string& invConnection = *it;
1547
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001548 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001549 auto respHandler =
1550 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
Ed Tanous02cad962022-06-30 16:50:15 -07001551 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1552 const boost::system::error_code ec,
1553 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001554 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 if (ec)
1556 {
1557 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001558 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001559 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 return;
1561 }
1562
1563 // Loop through returned object paths
1564 for (const auto& objDictEntry : resp)
1565 {
1566 const std::string& objPath =
1567 static_cast<const std::string&>(objDictEntry.first);
1568
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001569 // If this object path is one of the specified inventory items
1570 InventoryItem* inventoryItem =
1571 findInventoryItem(inventoryItems, objPath);
1572 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001573 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001574 // Store inventory data in InventoryItem
1575 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001576 }
1577 }
1578
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579 // Recurse to get inventory item data from next connection
1580 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1581 invConnections, objectMgrPaths,
1582 std::move(callback), invConnectionsIndex + 1);
1583
1584 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001585 };
1586
1587 // Find DBus object path that implements ObjectManager for the current
1588 // connection. If no mapping found, default to "/".
1589 auto iter = objectMgrPaths->find(invConnection);
1590 const std::string& objectMgrPath =
1591 (iter != objectMgrPaths->end()) ? iter->second : "/";
1592 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1593 << objectMgrPath;
1594
1595 // Get all object paths and their interfaces for current connection
1596 crow::connections::systemBus->async_method_call(
1597 std::move(respHandler), invConnection, objectMgrPath,
1598 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1599 }
1600
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001601 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001602}
1603
1604/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001605 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001606 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001607 * Gets the D-Bus connections (services) that provide data for the inventory
1608 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001609 *
1610 * Finds the connections asynchronously. Invokes callback when information has
1611 * been obtained.
1612 *
1613 * The callback must have the following signature:
1614 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001615 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001616 * @endcode
1617 *
1618 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001619 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001620 * @param callback Callback to invoke when connections have been obtained.
1621 */
1622template <typename Callback>
1623static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001624 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1625 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626 Callback&& callback)
1627{
1628 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1629
1630 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001631 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001632 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001633 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1634 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001635 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1636
1637 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001638 auto respHandler =
1639 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1640 inventoryItems](
1641 const boost::system::error_code ec,
1642 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001643 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1644 if (ec)
1645 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001646 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001647 BMCWEB_LOG_ERROR
1648 << "getInventoryItemsConnections respHandler DBus error " << ec;
1649 return;
1650 }
1651
1652 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001653 std::shared_ptr<std::set<std::string>> invConnections =
1654 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001655
1656 // Loop through objects from GetSubTree
1657 for (const std::pair<
1658 std::string,
1659 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1660 object : subtree)
1661 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001662 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001663 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001664 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001665 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001666 // Store all connections to inventory item
1667 for (const std::pair<std::string, std::vector<std::string>>&
1668 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001669 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001670 const std::string& invConnection = objData.first;
1671 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001672 }
1673 }
1674 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001675
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001676 callback(invConnections);
1677 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1678 };
1679
1680 // Make call to ObjectMapper to find all inventory items
1681 crow::connections::systemBus->async_method_call(
1682 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1683 "/xyz/openbmc_project/object_mapper",
1684 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1685 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1686}
1687
1688/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001689 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001690 *
1691 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001692 * inventory items. Then finds the associations from those inventory items to
1693 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001694 *
1695 * Finds the inventory items asynchronously. Invokes callback when information
1696 * has been obtained.
1697 *
1698 * The callback must have the following signature:
1699 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001700 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001701 * @endcode
1702 *
1703 * @param sensorsAsyncResp Pointer to object holding response data.
1704 * @param sensorNames All sensors within the current chassis.
1705 * @param objectMgrPaths Mappings from connection name to DBus object path that
1706 * implements ObjectManager.
1707 * @param callback Callback to invoke when inventory items have been obtained.
1708 */
1709template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001710static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001711 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001712 const std::shared_ptr<std::set<std::string>>& sensorNames,
1713 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001714 Callback&& callback)
1715{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001716 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001717
1718 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001719 auto respHandler =
1720 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1721 sensorNames](const boost::system::error_code ec,
1722 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001723 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001724 if (ec)
1725 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001726 BMCWEB_LOG_ERROR
1727 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001728 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001729 return;
1730 }
1731
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001732 // Create vector to hold list of inventory items
1733 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1734 std::make_shared<std::vector<InventoryItem>>();
1735
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001736 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001737 std::string sensorAssocPath;
1738 sensorAssocPath.reserve(128); // avoid memory allocations
1739 for (const auto& objDictEntry : resp)
1740 {
1741 const std::string& objPath =
1742 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001743
1744 // If path is inventory association for one of the specified sensors
1745 for (const std::string& sensorName : *sensorNames)
1746 {
1747 sensorAssocPath = sensorName;
1748 sensorAssocPath += "/inventory";
1749 if (objPath == sensorAssocPath)
1750 {
1751 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001752 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001753 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001754 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001755 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001756 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001757 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001758 if (valueName == "endpoints")
1759 {
1760 const std::vector<std::string>* endpoints =
1761 std::get_if<std::vector<std::string>>(
1762 &value);
1763 if ((endpoints != nullptr) &&
1764 !endpoints->empty())
1765 {
1766 // Add inventory item to vector
1767 const std::string& invItemPath =
1768 endpoints->front();
1769 addInventoryItem(inventoryItems,
1770 invItemPath,
1771 sensorName);
1772 }
1773 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001774 }
1775 }
1776 }
1777 break;
1778 }
1779 }
1780 }
1781
Anthony Wilsond5005492019-07-31 16:34:17 -05001782 // Now loop through the returned object paths again, this time to
1783 // find the leds associated with the inventory items we just found
1784 std::string inventoryAssocPath;
1785 inventoryAssocPath.reserve(128); // avoid memory allocations
1786 for (const auto& objDictEntry : resp)
1787 {
1788 const std::string& objPath =
1789 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001790
1791 for (InventoryItem& inventoryItem : *inventoryItems)
1792 {
1793 inventoryAssocPath = inventoryItem.objectPath;
1794 inventoryAssocPath += "/leds";
1795 if (objPath == inventoryAssocPath)
1796 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001797 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001798 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001799 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001800 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001801 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001802 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001803 if (valueName == "endpoints")
1804 {
1805 const std::vector<std::string>* endpoints =
1806 std::get_if<std::vector<std::string>>(
1807 &value);
1808 if ((endpoints != nullptr) &&
1809 !endpoints->empty())
1810 {
1811 // Add inventory item to vector
1812 // Store LED path in inventory item
1813 const std::string& ledPath =
1814 endpoints->front();
1815 inventoryItem.ledObjectPath = ledPath;
1816 }
1817 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001818 }
1819 }
1820 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001821
Anthony Wilsond5005492019-07-31 16:34:17 -05001822 break;
1823 }
1824 }
1825 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001826 callback(inventoryItems);
1827 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001828 };
1829
1830 // Find DBus object path that implements ObjectManager for ObjectMapper
1831 std::string connection = "xyz.openbmc_project.ObjectMapper";
1832 auto iter = objectMgrPaths->find(connection);
1833 const std::string& objectMgrPath =
1834 (iter != objectMgrPaths->end()) ? iter->second : "/";
1835 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1836 << objectMgrPath;
1837
1838 // Call GetManagedObjects on the ObjectMapper to get all associations
1839 crow::connections::systemBus->async_method_call(
1840 std::move(respHandler), connection, objectMgrPath,
1841 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1842
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001843 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001844}
1845
1846/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001847 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1848 *
1849 * Uses the specified connections (services) to obtain D-Bus data for inventory
1850 * item leds associated with sensors. Stores the resulting data in the
1851 * inventoryItems vector.
1852 *
1853 * This data is later used to provide sensor property values in the JSON
1854 * response.
1855 *
1856 * Finds the inventory item led data asynchronously. Invokes callback when data
1857 * has been obtained.
1858 *
1859 * The callback must have the following signature:
1860 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001861 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001862 * @endcode
1863 *
1864 * This function is called recursively, obtaining data asynchronously from one
1865 * connection in each call. This ensures the callback is not invoked until the
1866 * last asynchronous function has completed.
1867 *
1868 * @param sensorsAsyncResp Pointer to object holding response data.
1869 * @param inventoryItems D-Bus inventory items associated with sensors.
1870 * @param ledConnections Connections that provide data for the inventory leds.
1871 * @param callback Callback to invoke when inventory data has been obtained.
1872 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1873 * in recursive calls to this function.
1874 */
1875template <typename Callback>
1876void getInventoryLedData(
1877 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1878 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001879 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001880 Callback&& callback, size_t ledConnectionsIndex = 0)
1881{
1882 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1883
1884 // If no more connections left, call callback
1885 if (ledConnectionsIndex >= ledConnections->size())
1886 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001887 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001888 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1889 return;
1890 }
1891
1892 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001893 auto it = ledConnections->begin();
1894 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001895 if (it != ledConnections->end())
1896 {
1897 const std::string& ledPath = (*it).first;
1898 const std::string& ledConnection = (*it).second;
1899 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001900 auto respHandler =
1901 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001902 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001903 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001904 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1905 if (ec)
1906 {
1907 BMCWEB_LOG_ERROR
1908 << "getInventoryLedData respHandler DBus error " << ec;
1909 messages::internalError(sensorsAsyncResp->asyncResp->res);
1910 return;
1911 }
1912
1913 BMCWEB_LOG_DEBUG << "Led state: " << state;
1914 // Find inventory item with this LED object path
1915 InventoryItem* inventoryItem =
1916 findInventoryItemForLed(*inventoryItems, ledPath);
1917 if (inventoryItem != nullptr)
1918 {
1919 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001920 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001921 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001923 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001924 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001925 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001926 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001927 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001928 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001929 {
1930 inventoryItem->ledState = LedState::OFF;
1931 }
1932 else
1933 {
1934 inventoryItem->ledState = LedState::UNKNOWN;
1935 }
1936 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001937
Ed Tanous002d39b2022-05-31 08:59:27 -07001938 // Recurse to get LED data from next connection
1939 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1940 ledConnections, std::move(callback),
1941 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001942
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1944 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001945
1946 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001947 sdbusplus::asio::getProperty<std::string>(
1948 *crow::connections::systemBus, ledConnection, ledPath,
1949 "xyz.openbmc_project.Led.Physical", "State",
1950 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001951 }
1952
1953 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1954}
1955
1956/**
1957 * @brief Gets LED data for LEDs associated with given inventory items.
1958 *
1959 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1960 * associated with the specified inventory items. Then gets the LED data from
1961 * each connection and stores it in the inventory item.
1962 *
1963 * This data is later used to provide sensor property values in the JSON
1964 * response.
1965 *
1966 * Finds the LED data asynchronously. Invokes callback when information has
1967 * been obtained.
1968 *
1969 * The callback must have the following signature:
1970 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001971 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001972 * @endcode
1973 *
1974 * @param sensorsAsyncResp Pointer to object holding response data.
1975 * @param inventoryItems D-Bus inventory items associated with sensors.
1976 * @param callback Callback to invoke when inventory items have been obtained.
1977 */
1978template <typename Callback>
1979void getInventoryLeds(
1980 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1981 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1982 Callback&& callback)
1983{
1984 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1985
1986 const std::string path = "/xyz/openbmc_project";
1987 const std::array<std::string, 1> interfaces = {
1988 "xyz.openbmc_project.Led.Physical"};
1989
1990 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 auto respHandler =
1992 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1993 inventoryItems](
1994 const boost::system::error_code ec,
1995 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001996 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1997 if (ec)
1998 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001999 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002000 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2001 << ec;
2002 return;
2003 }
2004
2005 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00002006 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
2007 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05002008
2009 // Loop through objects from GetSubTree
2010 for (const std::pair<
2011 std::string,
2012 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2013 object : subtree)
2014 {
2015 // Check if object path is LED for one of the specified inventory
2016 // items
2017 const std::string& ledPath = object.first;
2018 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2019 {
2020 // Add mapping from ledPath to connection
2021 const std::string& connection = object.second.begin()->first;
2022 (*ledConnections)[ledPath] = connection;
2023 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2024 << connection;
2025 }
2026 }
2027
2028 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2029 std::move(callback));
2030 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2031 };
2032 // Make call to ObjectMapper to find all inventory items
2033 crow::connections::systemBus->async_method_call(
2034 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2035 "/xyz/openbmc_project/object_mapper",
2036 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2037 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2038}
2039
2040/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002041 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2042 *
2043 * Uses the specified connections (services) (currently assumes just one) to
2044 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2045 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2046 *
2047 * This data is later used to provide sensor property values in the JSON
2048 * response.
2049 *
2050 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2051 * when data has been obtained.
2052 *
2053 * The callback must have the following signature:
2054 * @code
2055 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2056 * @endcode
2057 *
2058 * @param sensorsAsyncResp Pointer to object holding response data.
2059 * @param inventoryItems D-Bus inventory items associated with sensors.
2060 * @param psAttributesConnections Connections that provide data for the Power
2061 * Supply Attributes
2062 * @param callback Callback to invoke when data has been obtained.
2063 */
2064template <typename Callback>
2065void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002066 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002067 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002068 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002069 Callback&& callback)
2070{
2071 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2072
2073 if (psAttributesConnections.empty())
2074 {
2075 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2076 callback(inventoryItems);
2077 return;
2078 }
2079
2080 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002081 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002082
2083 const std::string& psAttributesPath = (*it).first;
2084 const std::string& psAttributesConnection = (*it).second;
2085
2086 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002087 auto respHandler =
2088 [sensorsAsyncResp, inventoryItems,
2089 callback{std::forward<Callback>(callback)}](
2090 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002091 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2092 if (ec)
2093 {
2094 BMCWEB_LOG_ERROR
2095 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002096 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002097 return;
2098 }
2099
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002100 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2101 // Store value in Power Supply Inventory Items
2102 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002103 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002104 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002105 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002106 inventoryItem.powerSupplyEfficiencyPercent =
2107 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002108 }
2109 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002110
2111 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2112 callback(inventoryItems);
2113 };
2114
2115 // Get the DeratingFactor property for the PowerSupplyAttributes
2116 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002117 sdbusplus::asio::getProperty<uint32_t>(
2118 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2119 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2120 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002121
2122 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2123}
2124
2125/**
2126 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2127 *
2128 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2129 * data. Then gets the Power Supply Attributes data from the connection
2130 * (currently just assumes 1 connection) and stores the data in the inventory
2131 * item.
2132 *
2133 * This data is later used to provide sensor property values in the JSON
2134 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2135 *
2136 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2137 * when information has been obtained.
2138 *
2139 * The callback must have the following signature:
2140 * @code
2141 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2142 * @endcode
2143 *
2144 * @param sensorsAsyncResp Pointer to object holding response data.
2145 * @param inventoryItems D-Bus inventory items associated with sensors.
2146 * @param callback Callback to invoke when data has been obtained.
2147 */
2148template <typename Callback>
2149void getPowerSupplyAttributes(
2150 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2151 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2152 Callback&& callback)
2153{
2154 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2155
2156 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002157 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002158 {
2159 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2160 callback(inventoryItems);
2161 return;
2162 }
2163
2164 const std::array<std::string, 1> interfaces = {
2165 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2166
2167 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002168 auto respHandler =
2169 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2170 inventoryItems](
2171 const boost::system::error_code ec,
2172 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2174 if (ec)
2175 {
2176 messages::internalError(sensorsAsyncResp->asyncResp->res);
2177 BMCWEB_LOG_ERROR
2178 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2179 return;
2180 }
2181 if (subtree.empty())
2182 {
2183 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2184 callback(inventoryItems);
2185 return;
2186 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002187
Ed Tanous002d39b2022-05-31 08:59:27 -07002188 // Currently we only support 1 power supply attribute, use this for
2189 // all the power supplies. Build map of object path to connection.
2190 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002191 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002192
Ed Tanous002d39b2022-05-31 08:59:27 -07002193 if (subtree[0].first.empty() || subtree[0].second.empty())
2194 {
2195 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2196 callback(inventoryItems);
2197 return;
2198 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002199
Ed Tanous002d39b2022-05-31 08:59:27 -07002200 const std::string& psAttributesPath = subtree[0].first;
2201 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002202
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 if (connection.empty())
2204 {
2205 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2206 callback(inventoryItems);
2207 return;
2208 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002209
Ed Tanous002d39b2022-05-31 08:59:27 -07002210 psAttributesConnections[psAttributesPath] = connection;
2211 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2212 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002213
Ed Tanous002d39b2022-05-31 08:59:27 -07002214 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2215 psAttributesConnections,
2216 std::move(callback));
2217 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2218 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002219 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2220 crow::connections::systemBus->async_method_call(
2221 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2222 "/xyz/openbmc_project/object_mapper",
2223 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2224 "/xyz/openbmc_project", 0, interfaces);
2225 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2226}
2227
2228/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002229 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002230 *
2231 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002232 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002233 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002234 * This data is later used to provide sensor property values in the JSON
2235 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002236 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002237 * Finds the inventory items asynchronously. Invokes callback when the
2238 * inventory items have been obtained.
2239 *
2240 * The callback must have the following signature:
2241 * @code
2242 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2243 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002244 *
2245 * @param sensorsAsyncResp Pointer to object holding response data.
2246 * @param sensorNames All sensors within the current chassis.
2247 * @param objectMgrPaths Mappings from connection name to DBus object path that
2248 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002249 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002250 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002251template <typename Callback>
2252static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002253 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002254 const std::shared_ptr<std::set<std::string>> sensorNames,
2255 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002257{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002258 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2259 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002260 [sensorsAsyncResp, objectMgrPaths,
2261 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002262 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002263 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2264 auto getInventoryItemsConnectionsCb =
2265 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2266 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002267 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002268 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2269 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2270 callback{std::move(callback)}]() {
2271 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002272
Ed Tanous002d39b2022-05-31 08:59:27 -07002273 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2274 callback{std::move(callback)}]() {
2275 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2276 // Find Power Supply Attributes and get the data
2277 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2278 std::move(callback));
2279 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002280 };
2281
Ed Tanous002d39b2022-05-31 08:59:27 -07002282 // Find led connections and get the data
2283 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2284 std::move(getInventoryLedsCb));
2285 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2286 };
2287
2288 // Get inventory item data from connections
2289 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2290 invConnections, objectMgrPaths,
2291 std::move(getInventoryItemsDataCb));
2292 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002293 };
2294
Ed Tanous002d39b2022-05-31 08:59:27 -07002295 // Get connections that provide inventory item data
2296 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2297 std::move(getInventoryItemsConnectionsCb));
2298 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2299 };
2300
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002301 // Get associations from sensors to inventory items
2302 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2303 std::move(getInventoryItemAssociationsCb));
2304 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2305}
2306
2307/**
2308 * @brief Returns JSON PowerSupply object for the specified inventory item.
2309 *
2310 * Searches for a JSON PowerSupply object that matches the specified inventory
2311 * item. If one is not found, a new PowerSupply object is added to the JSON
2312 * array.
2313 *
2314 * Multiple sensors are often associated with one power supply inventory item.
2315 * As a result, multiple sensor values are stored in one JSON PowerSupply
2316 * object.
2317 *
2318 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2319 * @param inventoryItem Inventory item for the power supply.
2320 * @param chassisId Chassis that contains the power supply.
2321 * @return JSON PowerSupply object for the specified inventory item.
2322 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002323inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002324 const InventoryItem& inventoryItem,
2325 const std::string& chassisId)
2326{
2327 // Check if matching PowerSupply object already exists in JSON array
2328 for (nlohmann::json& powerSupply : powerSupplyArray)
2329 {
2330 if (powerSupply["MemberId"] == inventoryItem.name)
2331 {
2332 return powerSupply;
2333 }
2334 }
2335
2336 // Add new PowerSupply object to JSON array
2337 powerSupplyArray.push_back({});
2338 nlohmann::json& powerSupply = powerSupplyArray.back();
2339 powerSupply["@odata.id"] =
2340 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2341 powerSupply["MemberId"] = inventoryItem.name;
2342 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2343 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2344 powerSupply["Model"] = inventoryItem.model;
2345 powerSupply["PartNumber"] = inventoryItem.partNumber;
2346 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002347 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002348
Gunnar Mills42cbe532019-08-15 15:26:54 -05002349 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2350 {
2351 powerSupply["EfficiencyPercent"] =
2352 inventoryItem.powerSupplyEfficiencyPercent;
2353 }
2354
2355 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002356 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2357 powerSupply["Status"]["Health"] = health;
2358
2359 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002360}
2361
2362/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002363 * @brief Gets the values of the specified sensors.
2364 *
2365 * Stores the results as JSON in the SensorsAsyncResp.
2366 *
2367 * Gets the sensor values asynchronously. Stores the results later when the
2368 * information has been obtained.
2369 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002370 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002371 *
2372 * To minimize the number of DBus calls, the DBus method
2373 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2374 * values of all sensors provided by a connection (service).
2375 *
2376 * The connections set contains all the connections that provide sensor values.
2377 *
2378 * The objectMgrPaths map contains mappings from a connection name to the
2379 * corresponding DBus object path that implements ObjectManager.
2380 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002381 * The InventoryItem vector contains D-Bus inventory items associated with the
2382 * sensors. Inventory item data is needed for some Redfish sensor properties.
2383 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002384 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002385 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002386 * @param connections Connections that provide sensor values.
2387 * @param objectMgrPaths Mappings from connection name to DBus object path that
2388 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002389 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002390 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002391inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002392 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002393 const std::shared_ptr<std::set<std::string>>& sensorNames,
2394 const std::set<std::string>& connections,
2395 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002396 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002397{
2398 BMCWEB_LOG_DEBUG << "getSensorData enter";
2399 // Get managed objects from all services exposing sensors
2400 for (const std::string& connection : connections)
2401 {
2402 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002403 auto getManagedObjectsCb =
2404 [sensorsAsyncResp, sensorNames,
2405 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002406 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002407 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2408 if (ec)
2409 {
2410 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002411 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002412 return;
2413 }
2414 // Go through all objects and update response with sensor data
2415 for (const auto& objDictEntry : resp)
2416 {
2417 const std::string& objPath =
2418 static_cast<const std::string&>(objDictEntry.first);
2419 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2420 << objPath;
2421
Shawn McCarneyde629b62019-03-08 10:42:51 -06002422 std::vector<std::string> split;
2423 // Reserve space for
2424 // /xyz/openbmc_project/sensors/<name>/<subname>
2425 split.reserve(6);
2426 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2427 if (split.size() < 6)
2428 {
2429 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2430 << objPath;
2431 continue;
2432 }
2433 // These indexes aren't intuitive, as boost::split puts an empty
2434 // string at the beginning
2435 const std::string& sensorType = split[4];
2436 const std::string& sensorName = split[5];
2437 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2438 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002439 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002440 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002441 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002442 continue;
2443 }
2444
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002445 // Find inventory item (if any) associated with sensor
2446 InventoryItem* inventoryItem =
2447 findInventoryItemForSensor(inventoryItems, objPath);
2448
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002449 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002450 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002451
2452 nlohmann::json* sensorJson = nullptr;
2453
Nan Zhou928fefb2022-03-28 08:45:00 -07002454 if (sensorSchema == sensors::node::sensors &&
2455 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002456 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002457 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002458 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2459 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002460 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002461 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002462 }
2463 else
2464 {
Ed Tanous271584a2019-07-09 16:24:22 -07002465 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002466 if (sensorsAsyncResp->efficientExpand)
2467 {
2468 fieldName = "Members";
2469 }
2470 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002471 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002472 fieldName = "Temperatures";
2473 }
2474 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2475 sensorType == "fan_pwm")
2476 {
2477 fieldName = "Fans";
2478 }
2479 else if (sensorType == "voltage")
2480 {
2481 fieldName = "Voltages";
2482 }
2483 else if (sensorType == "power")
2484 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002485 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002486 {
2487 fieldName = "PowerControl";
2488 }
2489 else if ((inventoryItem != nullptr) &&
2490 (inventoryItem->isPowerSupply))
2491 {
2492 fieldName = "PowerSupplies";
2493 }
2494 else
2495 {
2496 // Other power sensors are in SensorCollection
2497 continue;
2498 }
2499 }
2500 else
2501 {
2502 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2503 << sensorType;
2504 continue;
2505 }
2506
2507 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002508 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002509 if (fieldName == "PowerControl")
2510 {
2511 if (tempArray.empty())
2512 {
2513 // Put multiple "sensors" into a single
2514 // PowerControl. Follows MemberId naming and
2515 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002516 nlohmann::json::object_t power;
2517 power["@odata.id"] =
2518 "/redfish/v1/Chassis/" +
2519 sensorsAsyncResp->chassisId + "/" +
2520 sensorsAsyncResp->chassisSubNode + "#/" +
2521 fieldName + "/0";
2522 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002523 }
2524 sensorJson = &(tempArray.back());
2525 }
2526 else if (fieldName == "PowerSupplies")
2527 {
2528 if (inventoryItem != nullptr)
2529 {
2530 sensorJson =
2531 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002532 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002533 }
2534 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002535 else if (fieldName == "Members")
2536 {
Ed Tanous14766872022-03-15 10:44:42 -07002537 nlohmann::json::object_t member;
2538 member["@odata.id"] =
2539 "/redfish/v1/Chassis/" +
2540 sensorsAsyncResp->chassisId + "/" +
2541 sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2542 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002543 sensorJson = &(tempArray.back());
2544 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002545 else
2546 {
Ed Tanous14766872022-03-15 10:44:42 -07002547 nlohmann::json::object_t member;
2548 member["@odata.id"] = "/redfish/v1/Chassis/" +
2549 sensorsAsyncResp->chassisId +
2550 "/" +
2551 sensorsAsyncResp->chassisSubNode +
2552 "#/" + fieldName + "/";
2553 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002554 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002555 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002556 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002557
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002558 if (sensorJson != nullptr)
2559 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002560 objectInterfacesToJson(sensorName, sensorType,
2561 sensorsAsyncResp->chassisSubNode,
2562 objDictEntry.second, *sensorJson,
2563 inventoryItem);
2564
2565 std::string path = "/xyz/openbmc_project/sensors/";
2566 path += sensorType;
2567 path += "/";
2568 path += sensorName;
2569 sensorsAsyncResp->addMetadata(*sensorJson, sensorType,
2570 path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002571 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002572 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002573 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002574 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002575 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002576 if (sensorsAsyncResp->chassisSubNode ==
2577 sensors::node::sensors &&
2578 sensorsAsyncResp->efficientExpand)
2579 {
2580 sensorsAsyncResp->asyncResp->res
2581 .jsonValue["Members@odata.count"] =
2582 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2583 .size();
2584 }
2585 else if (sensorsAsyncResp->chassisSubNode ==
2586 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002587 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002588 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002589 }
James Feist8bd25cc2019-03-15 15:14:00 -07002590 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002591 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2592 };
2593
2594 // Find DBus object path that implements ObjectManager for the current
2595 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002596 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002597 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002598 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002599 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2600 << objectMgrPath;
2601
2602 crow::connections::systemBus->async_method_call(
2603 getManagedObjectsCb, connection, objectMgrPath,
2604 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002605 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002606 BMCWEB_LOG_DEBUG << "getSensorData exit";
2607}
2608
Nan Zhoufe04d492022-06-22 17:10:41 +00002609inline void
2610 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2611 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002612{
Nan Zhoufe04d492022-06-22 17:10:41 +00002613 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2614 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002615 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2616 auto getObjectManagerPathsCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002617 [sensorsAsyncResp, sensorNames, connections](
2618 const std::shared_ptr<std::map<std::string, std::string>>&
2619 objectMgrPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002620 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2621 auto getInventoryItemsCb =
2622 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2623 const std::shared_ptr<std::vector<InventoryItem>>&
2624 inventoryItems) {
2625 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2626 // Get sensor data and store results in JSON
2627 getSensorData(sensorsAsyncResp, sensorNames, connections,
2628 objectMgrPaths, inventoryItems);
2629 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2630 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002631
Ed Tanous002d39b2022-05-31 08:59:27 -07002632 // Get inventory items associated with sensors
2633 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2634 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002635
Ed Tanous002d39b2022-05-31 08:59:27 -07002636 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002637 };
2638
Ed Tanous002d39b2022-05-31 08:59:27 -07002639 // Get mapping from connection names to the DBus object
2640 // paths that implement the ObjectManager interface
2641 getObjectManagerPaths(sensorsAsyncResp,
2642 std::move(getObjectManagerPathsCb));
2643 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2644 };
2645
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002646 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002647 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002648}
2649
Shawn McCarneyde629b62019-03-08 10:42:51 -06002650/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002651 * @brief Entry point for retrieving sensors data related to requested
2652 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002653 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002654 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002655inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002656 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002657{
2658 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002659 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002660 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002661 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002662 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2663 processSensorList(sensorsAsyncResp, sensorNames);
2664 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2665 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002666 // SensorCollection doesn't contain the Redundancy property
2667 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2668 {
2669 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2670 nlohmann::json::array();
2671 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002672 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002673 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002674 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002675}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002676
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302677/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002678 * @brief Find the requested sensorName in the list of all sensors supplied by
2679 * the chassis node
2680 *
2681 * @param sensorName The sensor name supplied in the PATCH request
2682 * @param sensorsList The list of sensors managed by the chassis node
2683 * @param sensorsModified The list of sensors that were found as a result of
2684 * repeated calls to this function
2685 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002686inline bool
2687 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002688 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002689 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002690{
Nan Zhoufe04d492022-06-22 17:10:41 +00002691 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002692 {
George Liu28aa8de2021-02-01 15:13:30 +08002693 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002694 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002695 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002696 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002697 continue;
2698 }
2699 if (thisSensorName == sensorName)
2700 {
2701 sensorsModified.emplace(chassisSensor);
2702 return true;
2703 }
2704 }
2705 return false;
2706}
2707
2708/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302709 * @brief Entry point for overriding sensor values of given sensor
2710 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002711 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002712 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002713 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302714 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002715inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002716 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002717 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002718 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302719{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002720 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002721 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302722
Ed Tanous543f4402022-01-06 13:12:53 -08002723 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302724 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302725 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002726 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302727 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302728 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302729 if (collectionItems.first == "Temperatures")
2730 {
2731 propertyValueName = "ReadingCelsius";
2732 }
2733 else if (collectionItems.first == "Fans")
2734 {
2735 propertyValueName = "Reading";
2736 }
2737 else
2738 {
2739 propertyValueName = "ReadingVolts";
2740 }
2741 for (auto& item : collectionItems.second)
2742 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002743 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2744 "MemberId", memberId, propertyValueName,
2745 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302746 {
2747 return;
2748 }
2749 overrideMap.emplace(memberId,
2750 std::make_pair(value, collectionItems.first));
2751 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302752 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002753
Ed Tanous002d39b2022-05-31 08:59:27 -07002754 auto getChassisSensorListCb =
2755 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002756 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002757 // Match sensor names in the PATCH request to those managed by the
2758 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002759 const std::shared_ptr<std::set<std::string>> sensorNames =
2760 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302761 for (const auto& item : overrideMap)
2762 {
2763 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002764 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2765 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302766 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302767 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002768 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302769 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302770 return;
2771 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302772 }
2773 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002774 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002775 [sensorAsyncResp,
2776 overrideMap](const std::set<std::string>& /*connections*/,
2777 const std::set<std::pair<std::string, std::string>>&
2778 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002779 if (objectsWithConnection.size() != overrideMap.size())
2780 {
2781 BMCWEB_LOG_INFO
2782 << "Unable to find all objects with proper connection "
2783 << objectsWithConnection.size() << " requested "
2784 << overrideMap.size() << "\n";
2785 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2786 sensorAsyncResp->chassisSubNode ==
2787 sensors::node::thermal
2788 ? "Temperatures"
2789 : "Voltages",
2790 "Count");
2791 return;
2792 }
2793 for (const auto& item : objectsWithConnection)
2794 {
2795 sdbusplus::message::object_path path(item.first);
2796 std::string sensorName = path.filename();
2797 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302798 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002799 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302800 return;
2801 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302802
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002803 const auto& iterator = overrideMap.find(sensorName);
2804 if (iterator == overrideMap.end())
2805 {
2806 BMCWEB_LOG_INFO << "Unable to find sensor object"
2807 << item.first << "\n";
2808 messages::internalError(sensorAsyncResp->asyncResp->res);
2809 return;
2810 }
2811 crow::connections::systemBus->async_method_call(
2812 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002813 if (ec)
2814 {
2815 if (ec.value() ==
2816 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002817 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002818 BMCWEB_LOG_WARNING
2819 << "Manufacturing mode is not Enabled...can't "
2820 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002821
Ed Tanous002d39b2022-05-31 08:59:27 -07002822 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002823 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002824 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002825 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002826 BMCWEB_LOG_DEBUG
2827 << "setOverrideValueStatus DBUS error: " << ec;
2828 messages::internalError(
2829 sensorAsyncResp->asyncResp->res);
2830 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002831 },
2832 item.second, item.first, "org.freedesktop.DBus.Properties",
2833 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002834 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002835 }
2836 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302837 // Get object with connection for the given sensor name
2838 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2839 std::move(getObjectsWithConnectionCb));
2840 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302841 // get full sensor list for the given chassisId and cross verify the sensor.
2842 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2843}
2844
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002845/**
2846 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2847 * path of the sensor.
2848 *
2849 * Function builds valid Redfish response for sensor query of given chassis and
2850 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2851 * it to caller in a callback.
2852 *
2853 * @param chassis Chassis for which retrieval should be performed
2854 * @param node Node (group) of sensors. See sensors::node for supported values
2855 * @param mapComplete Callback to be called with retrieval result
2856 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002857inline void retrieveUriToDbusMap(const std::string& chassis,
2858 const std::string& node,
2859 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002860{
Ed Tanous02da7c52022-02-27 00:09:02 -08002861 decltype(sensors::paths)::const_iterator pathIt =
2862 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2863 [&node](auto&& val) { return val.first == node; });
2864 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002865 {
2866 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2867 mapComplete(boost::beast::http::status::bad_request, {});
2868 return;
2869 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002870
Nan Zhou72374eb2022-01-27 17:06:51 -08002871 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002872 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2873 const boost::beast::http::status status,
2874 const std::map<std::string, std::string>& uriToDbus) {
2875 mapCompleteCb(status, uriToDbus);
2876 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002877
2878 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002879 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002880 getChassisData(resp);
2881}
2882
Nan Zhoubacb2162022-04-06 11:28:32 -07002883namespace sensors
2884{
Nan Zhou928fefb2022-03-28 08:45:00 -07002885
Nan Zhoubacb2162022-04-06 11:28:32 -07002886inline void getChassisCallback(
2887 const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002888 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002889{
2890 BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2891
2892 nlohmann::json& entriesArray =
2893 asyncResp->asyncResp->res.jsonValue["Members"];
Nan Zhoufe04d492022-06-22 17:10:41 +00002894 for (const auto& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002895 {
2896 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2897
2898 sdbusplus::message::object_path path(sensor);
2899 std::string sensorName = path.filename();
2900 if (sensorName.empty())
2901 {
2902 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2903 messages::internalError(asyncResp->asyncResp->res);
2904 return;
2905 }
Ed Tanous14766872022-03-15 10:44:42 -07002906 nlohmann::json::object_t member;
2907 member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
2908 "/" + asyncResp->chassisSubNode + "/" +
2909 sensorName;
2910 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002911 }
2912
2913 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2914 entriesArray.size();
2915 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2916}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002917
Nan Zhoude167a62022-06-01 04:47:45 +00002918inline void
2919 handleSensorCollectionGet(App& app, const crow::Request& req,
2920 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2921 const std::string& chassisId)
2922{
2923 query_param::QueryCapabilities capabilities = {
2924 .canDelegateExpandLevel = 1,
2925 };
2926 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002927 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002928 delegatedQuery, capabilities))
2929 {
2930 return;
2931 }
2932
2933 if (delegatedQuery.expandType != query_param::ExpandType::None)
2934 {
2935 // we perform efficient expand.
2936 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2937 aResp, chassisId, sensors::dbus::sensorPaths,
2938 sensors::node::sensors,
2939 /*efficientExpand=*/true);
2940 getChassisData(asyncResp);
2941
2942 BMCWEB_LOG_DEBUG
2943 << "SensorCollection doGet exit via efficient expand handler";
2944 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002945 }
Nan Zhoude167a62022-06-01 04:47:45 +00002946
2947 // if there's no efficient expand available, we use the default
2948 // Query Parameters route
2949 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2950 aResp, chassisId, sensors::dbus::sensorPaths, sensors::node::sensors);
2951
2952 // We get all sensors as hyperlinkes in the chassis (this
2953 // implies we reply on the default query parameters handler)
2954 getChassis(asyncResp,
2955 std::bind_front(sensors::getChassisCallback, asyncResp));
2956 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
2957}
2958
Nan Zhoue6bd8462022-06-01 04:35:35 +00002959inline void handleSensorGet(App& app, const crow::Request& req,
2960 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2961 const std::string& chassisId,
2962 const std::string& sensorName)
2963{
Carson Labrado3ba00072022-06-06 19:40:56 +00002964 if (!redfish::setUpRedfishRoute(app, req, aResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002965 {
2966 return;
2967 }
2968 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2969 std::shared_ptr<SensorsAsyncResp> asyncResp =
2970 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
2971 std::span<std::string_view>(),
2972 sensors::node::sensors);
2973
2974 const std::array<const char*, 1> interfaces = {
2975 "xyz.openbmc_project.Sensor.Value"};
2976
2977 // Get a list of all of the sensors that implement Sensor.Value
2978 // and get the path and service name associated with the sensor
2979 crow::connections::systemBus->async_method_call(
2980 [asyncResp,
2981 sensorName](const boost::system::error_code ec,
2982 const ::dbus::utility::MapperGetSubTreeResponse& subtree) {
2983 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2984 if (ec)
2985 {
2986 messages::internalError(asyncResp->asyncResp->res);
2987 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2988 << "Dbus error " << ec;
2989 return;
2990 }
2991
2992 ::dbus::utility::MapperGetSubTreeResponse::const_iterator it =
2993 std::find_if(
2994 subtree.begin(), subtree.end(),
2995 [sensorName](
2996 const std::pair<
2997 std::string,
2998 std::vector<std::pair<
2999 std::string, std::vector<std::string>>>>& object) {
3000 sdbusplus::message::object_path path(object.first);
3001 std::string name = path.filename();
3002 if (name.empty())
3003 {
3004 BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first;
3005 return false;
3006 }
3007
3008 return name == sensorName;
3009 });
3010
3011 if (it == subtree.end())
3012 {
3013 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3014 << sensorName;
3015 messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor",
3016 sensorName);
3017 return;
3018 }
3019 std::string_view sensorPath = (*it).first;
3020 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
3021 << "': " << sensorPath;
3022
Nan Zhoufe04d492022-06-22 17:10:41 +00003023 const std::shared_ptr<std::set<std::string>> sensorList =
3024 std::make_shared<std::set<std::string>>();
Nan Zhoue6bd8462022-06-01 04:35:35 +00003025
3026 sensorList->emplace(sensorPath);
3027 processSensorList(asyncResp, sensorList);
3028 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3029 },
3030 "xyz.openbmc_project.ObjectMapper",
3031 "/xyz/openbmc_project/object_mapper",
3032 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3033 "/xyz/openbmc_project/sensors", 2, interfaces);
3034}
3035
Nan Zhoubacb2162022-04-06 11:28:32 -07003036} // namespace sensors
3037
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003038inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003039{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003040 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003041 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003042 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003043 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003044}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003045
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003046inline void requestRoutesSensor(App& app)
3047{
3048 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003049 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003050 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003051 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003052}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003053
Ed Tanous1abe55e2018-09-05 08:30:59 -07003054} // namespace redfish