blob: 2b1a3c8fba5c007c5d3bcc1cba96d3533e03414f [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>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010020#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010021#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070022#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080023#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070024#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070025#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070026#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020027#include <sdbusplus/unpack_properties.hpp>
28#include <utils/dbus_utils.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053029#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070030#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031
32#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000033#include <iterator>
34#include <map>
35#include <set>
Ed Tanousb5a76932020-09-29 16:16:58 -070036#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080037#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010038
Ed Tanous1abe55e2018-09-05 08:30:59 -070039namespace redfish
40{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010041
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020042namespace sensors
43{
44namespace node
45{
46static constexpr std::string_view power = "Power";
47static constexpr std::string_view sensors = "Sensors";
48static constexpr std::string_view thermal = "Thermal";
49} // namespace node
50
Ed Tanous02da7c52022-02-27 00:09:02 -080051// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020052namespace dbus
53{
Ed Tanous4ee8e212022-05-28 09:42:51 -070054static auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080055 "/xyz/openbmc_project/sensors/voltage",
56 "/xyz/openbmc_project/sensors/power"
57});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000058
Ed Tanous4ee8e212022-05-28 09:42:51 -070059static auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080060 "/xyz/openbmc_project/sensors/power",
61 "/xyz/openbmc_project/sensors/current",
62 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070063 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080064#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080065 "/xyz/openbmc_project/sensors/voltage",
66 "/xyz/openbmc_project/sensors/fan_tach",
67 "/xyz/openbmc_project/sensors/temperature",
68 "/xyz/openbmc_project/sensors/fan_pwm",
69 "/xyz/openbmc_project/sensors/altitude",
70 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080071#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080072 "/xyz/openbmc_project/sensors/utilization"
73});
74
Ed Tanous4ee8e212022-05-28 09:42:51 -070075static auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080076 "/xyz/openbmc_project/sensors/fan_tach",
77 "/xyz/openbmc_project/sensors/temperature",
78 "/xyz/openbmc_project/sensors/fan_pwm"
79});
80
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000081} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080082// clang-format on
83
84using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
85static constexpr std::array<sensorPair, 3> paths = {
86 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
87 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
88 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000089
90inline const char* toReadingType(const std::string& sensorType)
91{
92 if (sensorType == "voltage")
93 {
94 return "Voltage";
95 }
96 if (sensorType == "power")
97 {
98 return "Power";
99 }
100 if (sensorType == "current")
101 {
102 return "Current";
103 }
104 if (sensorType == "fan_tach")
105 {
106 return "Rotational";
107 }
108 if (sensorType == "temperature")
109 {
110 return "Temperature";
111 }
112 if (sensorType == "fan_pwm" || sensorType == "utilization")
113 {
114 return "Percent";
115 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600116 if (sensorType == "humidity")
117 {
118 return "Humidity";
119 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000120 if (sensorType == "altitude")
121 {
122 return "Altitude";
123 }
124 if (sensorType == "airflow")
125 {
126 return "AirFlow";
127 }
128 if (sensorType == "energy")
129 {
130 return "EnergyJoules";
131 }
132 return "";
133}
134
135inline const char* toReadingUnits(const std::string& sensorType)
136{
137 if (sensorType == "voltage")
138 {
139 return "V";
140 }
141 if (sensorType == "power")
142 {
143 return "W";
144 }
145 if (sensorType == "current")
146 {
147 return "A";
148 }
149 if (sensorType == "fan_tach")
150 {
151 return "RPM";
152 }
153 if (sensorType == "temperature")
154 {
155 return "Cel";
156 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600157 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
158 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000159 {
160 return "%";
161 }
162 if (sensorType == "altitude")
163 {
164 return "m";
165 }
166 if (sensorType == "airflow")
167 {
168 return "cft_i/min";
169 }
170 if (sensorType == "energy")
171 {
172 return "J";
173 }
174 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200175}
176} // namespace sensors
177
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100178/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200179 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100180 * Gathers data needed for response processing after async calls are done
181 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700182class SensorsAsyncResp
183{
184 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200185 using DataCompleteCb = std::function<void(
186 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000187 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200188
189 struct SensorData
190 {
191 const std::string name;
192 std::string uri;
193 const std::string valueKey;
194 const std::string dbusPath;
195 };
196
Ed Tanous8a592812022-06-04 09:06:59 -0700197 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800198 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800199 std::span<std::string_view> typesIn,
200 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700201 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700202 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
203 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500204 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200205
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200206 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700207 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800208 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800209 std::span<std::string_view> typesIn,
210 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200211 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700212 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700213 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
214 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200215 dataComplete{std::move(creationComplete)}
216 {}
217
Nan Zhou928fefb2022-03-28 08:45:00 -0700218 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700219 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700220 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800221 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700222 const std::string_view& subNode, bool efficientExpandIn) :
223 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700224 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700225 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700226 {}
227
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 ~SensorsAsyncResp()
229 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800230 if (asyncResp->res.result() ==
231 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 {
233 // Reset the json object to clear out any data that made it in
234 // before the error happened todo(ed) handle error condition with
235 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800236 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200238
239 if (dataComplete && metadata)
240 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000241 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800242 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200243 {
244 for (auto& sensor : *metadata)
245 {
246 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
247 sensor.dbusPath));
248 }
249 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200251 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100253
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800254 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
255 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
256 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
257 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
258
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200259 void addMetadata(const nlohmann::json& sensorObject,
260 const std::string& valueKey, const std::string& dbusPath)
261 {
262 if (metadata)
263 {
264 metadata->emplace_back(SensorData{sensorObject["Name"],
265 sensorObject["@odata.id"],
266 valueKey, dbusPath});
267 }
268 }
269
270 void updateUri(const std::string& name, const std::string& uri)
271 {
272 if (metadata)
273 {
274 for (auto& sensor : *metadata)
275 {
276 if (sensor.name == name)
277 {
278 sensor.uri = uri;
279 }
280 }
281 }
282 }
283
zhanghch058d1b46d2021-04-01 11:18:24 +0800284 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200285 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800286 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200287 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700288 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200289
290 private:
291 std::optional<std::vector<SensorData>> metadata;
292 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100293};
294
295/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500296 * Possible states for physical inventory leds
297 */
298enum class LedState
299{
300 OFF,
301 ON,
302 BLINK,
303 UNKNOWN
304};
305
306/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500307 * D-Bus inventory item associated with one or more sensors.
308 */
309class InventoryItem
310{
311 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700312 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500313 {
314 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800315 sdbusplus::message::object_path path(objectPath);
316 name = path.filename();
317 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500318 {
George Liu28aa8de2021-02-01 15:13:30 +0800319 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500320 }
321 }
322
323 std::string objectPath;
324 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800325 bool isPresent = true;
326 bool isFunctional = true;
327 bool isPowerSupply = false;
328 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500329 std::string manufacturer;
330 std::string model;
331 std::string partNumber;
332 std::string serialNumber;
333 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500334 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800335 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500336};
337
338/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530339 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200340 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100341 * @param sensorNames Sensors retrieved from chassis
342 * @param callback Callback for processing gathered connections
343 */
344template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530345void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000346 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000347 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530348 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700349{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530350 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700351 const std::string path = "/xyz/openbmc_project/sensors";
352 const std::array<std::string, 1> interfaces = {
353 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100354
Ed Tanous1abe55e2018-09-05 08:30:59 -0700355 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700356 auto respHandler =
357 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
358 sensorNames](const boost::system::error_code ec,
359 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530360 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700361 if (ec)
362 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800363 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530364 BMCWEB_LOG_ERROR
365 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700366 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100367 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100368
Ed Tanous1abe55e2018-09-05 08:30:59 -0700369 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
370
371 // Make unique list of connections only for requested sensor types and
372 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000373 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530374 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700375
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700376 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
377 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700378 {
379 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
380 }
381
382 for (const std::pair<
383 std::string,
384 std::vector<std::pair<std::string, std::vector<std::string>>>>&
385 object : subtree)
386 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700387 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700388 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700389 for (const std::pair<std::string, std::vector<std::string>>&
390 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700391 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700392 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
393 connections.insert(objData.first);
394 objectsWithConnection.insert(
395 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700396 }
397 }
398 }
399 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530400 callback(std::move(connections), std::move(objectsWithConnection));
401 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700403 // Make call to ObjectMapper to find all sensors objects
404 crow::connections::systemBus->async_method_call(
405 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
406 "/xyz/openbmc_project/object_mapper",
407 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530408 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
409}
410
411/**
412 * @brief Create connections necessary for sensors
413 * @param SensorsAsyncResp Pointer to object holding response data
414 * @param sensorNames Sensors retrieved from chassis
415 * @param callback Callback for processing gathered connections
416 */
417template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000418void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
419 const std::shared_ptr<std::set<std::string>> sensorNames,
420 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530421{
422 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000423 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530424 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700425 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000426 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530427 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100428}
429
430/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700431 * @brief Shrinks the list of sensors for processing
432 * @param SensorsAysncResp The class holding the Redfish response
433 * @param allSensors A list of all the sensors associated to the
434 * chassis element (i.e. baseboard, front panel, etc...)
435 * @param activeSensors A list that is a reduction of the incoming
436 * allSensors list. Eliminate Thermal sensors when a Power request is
437 * made, and eliminate Power sensors when a Thermal request is made.
438 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000439inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000440 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700441 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000442 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700443{
Ed Tanous81ce6092020-12-17 16:54:55 +0000444 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445 {
446 return;
447 }
448 if ((allSensors == nullptr) || (activeSensors == nullptr))
449 {
450 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800451 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000452 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200453 ? "Temperatures"
454 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700455
456 return;
457 }
458 if (allSensors->empty())
459 {
460 // Nothing to do, the activeSensors object is also empty
461 return;
462 }
463
Ed Tanous02da7c52022-02-27 00:09:02 -0800464 for (std::string_view type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700465 {
466 for (const std::string& sensor : *allSensors)
467 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700468 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700469 {
470 activeSensors->emplace(sensor);
471 }
472 }
473 }
474}
475
476/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100477 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200478 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100479 * @param callback Callback for next step in gathered sensor processing
480 */
481template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700482void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700483 Callback&& callback)
484{
485 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500486 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700487 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500488 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700489 auto respHandler =
490 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
491 const boost::system::error_code ec,
492 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700493 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
494 if (ec)
495 {
496 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800497 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700498 return;
499 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100500
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700501 const std::string* chassisPath = nullptr;
502 std::string chassisName;
503 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700504 {
George Liu28aa8de2021-02-01 15:13:30 +0800505 sdbusplus::message::object_path path(chassis);
506 chassisName = path.filename();
507 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700508 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700509 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700510 continue;
511 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700512 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700513 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700514 chassisPath = &chassis;
515 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700516 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700517 }
518 if (chassisPath == nullptr)
519 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800520 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
521 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700522 return;
523 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700524
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700525 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200526 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700527 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800528 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700529 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200531 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800533 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700534 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800535 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
536 nlohmann::json::array();
537 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800538 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700539 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200540 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500541 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800542 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500543 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800544 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500545 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800546 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500547 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800548 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
549 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500550 }
551
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200552 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500553 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800554 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500555 }
556
zhanghch058d1b46d2021-04-01 11:18:24 +0800557 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700558 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
559 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800560 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500561 // Get the list of all sensors for this Chassis element
562 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700563 sdbusplus::asio::getProperty<std::vector<std::string>>(
564 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
565 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800566 [sensorsAsyncResp,
567 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700568 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700569 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700570 if (e)
571 {
572 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700573 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 messages::internalError(sensorsAsyncResp->asyncResp->res);
575 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700576 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700577 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000578 const std::shared_ptr<std::set<std::string>> culledSensorList =
579 std::make_shared<std::set<std::string>>();
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
581 culledSensorList);
582 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700583 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100584 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100585
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700586 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700587 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700588 respHandler, "xyz.openbmc_project.ObjectMapper",
589 "/xyz/openbmc_project/object_mapper",
590 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700591 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700592 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100593}
594
595/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600596 * @brief Finds all DBus object paths that implement ObjectManager.
597 *
598 * Creates a mapping from the associated connection name to the object path.
599 *
600 * Finds the object paths asynchronously. Invokes callback when information has
601 * been obtained.
602 *
603 * The callback must have the following signature:
604 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +0000605 * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600606 * @endcode
607 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700608 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600609 * @param callback Callback to invoke when object paths obtained.
610 */
611template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700612void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000613 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700614 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600615{
616 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
617 const std::array<std::string, 1> interfaces = {
618 "org.freedesktop.DBus.ObjectManager"};
619
620 // Response handler for GetSubTree DBus method
Ed Tanous002d39b2022-05-31 08:59:27 -0700621 auto respHandler =
622 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
623 const boost::system::error_code ec,
624 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600625 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
626 if (ec)
627 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800628 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600629 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
630 << ec;
631 return;
632 }
633
634 // Loop over returned object paths
Nan Zhoufe04d492022-06-22 17:10:41 +0000635 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
636 std::make_shared<std::map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600637 for (const std::pair<
638 std::string,
639 std::vector<std::pair<std::string, std::vector<std::string>>>>&
640 object : subtree)
641 {
642 // Loop over connections for current object path
643 const std::string& objectPath = object.first;
644 for (const std::pair<std::string, std::vector<std::string>>&
645 objData : object.second)
646 {
647 // Add mapping from connection to object path
648 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500649 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600650 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
651 << objectPath;
652 }
653 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500654 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600655 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
656 };
657
658 // Query mapper for all DBus object paths that implement ObjectManager
659 crow::connections::systemBus->async_method_call(
660 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
661 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700662 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600663 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
664}
665
666/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500667 * @brief Returns the Redfish State value for the specified inventory item.
668 * @param inventoryItem D-Bus inventory item associated with a sensor.
669 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700670 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000671inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500672{
673 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
674 {
675 return "Absent";
676 }
James Feist34dd1792019-05-17 14:10:54 -0700677
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500678 return "Enabled";
679}
680
681/**
682 * @brief Returns the Redfish Health value for the specified sensor.
683 * @param sensorJson Sensor JSON object.
684 * @param interfacesDict Map of all sensor interfaces.
685 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
686 * be nullptr if no associated inventory item was found.
687 * @return Health value for sensor.
688 */
Ed Tanous711ac7a2021-12-20 09:34:41 -0800689inline std::string
690 getHealth(nlohmann::json& sensorJson,
691 const dbus::utility::DBusInteracesMap& interfacesDict,
692 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700693{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500694 // Get current health value (if any) in the sensor JSON object. Some JSON
695 // objects contain multiple sensors (such as PowerSupplies). We want to set
696 // the overall health to be the most severe of any of the sensors.
697 std::string currentHealth;
698 auto statusIt = sensorJson.find("Status");
699 if (statusIt != sensorJson.end())
700 {
701 auto healthIt = statusIt->find("Health");
702 if (healthIt != statusIt->end())
703 {
704 std::string* health = healthIt->get_ptr<std::string*>();
705 if (health != nullptr)
706 {
707 currentHealth = *health;
708 }
709 }
710 }
711
712 // If current health in JSON object is already Critical, return that. This
713 // should override the sensor health, which might be less severe.
714 if (currentHealth == "Critical")
715 {
716 return "Critical";
717 }
718
719 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800720
Ed Tanous9eb808c2022-01-25 10:19:23 -0800721 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700722 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800723 if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
James Feist34dd1792019-05-17 14:10:54 -0700724 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800725 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700726 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800727 if (valueName == "CriticalAlarmHigh" ||
728 valueName == "CriticalAlarmLow")
729 {
730 const bool* asserted = std::get_if<bool>(&value);
731 if (asserted == nullptr)
732 {
733 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
734 }
735 else if (*asserted)
736 {
737 return "Critical";
738 }
739 }
James Feist34dd1792019-05-17 14:10:54 -0700740 }
741 }
742 }
743
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500744 // Check if associated inventory item is not functional
745 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
746 {
747 return "Critical";
748 }
749
750 // If current health in JSON object is already Warning, return that. This
751 // should override the sensor status, which might be less severe.
752 if (currentHealth == "Warning")
753 {
754 return "Warning";
755 }
756
757 // Check if sensor has warning threshold alarm
Ed Tanous9eb808c2022-01-25 10:19:23 -0800758 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700759 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800760 if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
James Feist34dd1792019-05-17 14:10:54 -0700761 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800762 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700763 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800764 if (valueName == "WarningAlarmHigh" ||
765 valueName == "WarningAlarmLow")
766 {
767 const bool* asserted = std::get_if<bool>(&value);
768 if (asserted == nullptr)
769 {
770 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
771 }
772 else if (*asserted)
773 {
Ed Tanousebe4d912022-01-12 12:38:17 -0800774 return "Warning";
Ed Tanous711ac7a2021-12-20 09:34:41 -0800775 }
776 }
James Feist34dd1792019-05-17 14:10:54 -0700777 }
778 }
779 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500780
James Feist34dd1792019-05-17 14:10:54 -0700781 return "OK";
782}
783
Ed Tanous23a21a12020-07-25 04:45:05 +0000784inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500785 const InventoryItem* inventoryItem)
786{
787 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
788 {
789 switch (inventoryItem->ledState)
790 {
791 case LedState::OFF:
792 sensorJson["IndicatorLED"] = "Off";
793 break;
794 case LedState::ON:
795 sensorJson["IndicatorLED"] = "Lit";
796 break;
797 case LedState::BLINK:
798 sensorJson["IndicatorLED"] = "Blinking";
799 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000800 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500801 break;
802 }
803 }
804}
805
James Feist34dd1792019-05-17 14:10:54 -0700806/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100807 * @brief Builds a json sensor representation of a sensor.
808 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500809 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100810 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200811 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100812 * @param interfacesDict A dictionary of the interfaces and properties of said
813 * interfaces to be built from
814 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500815 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
816 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100817 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000818inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100819 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700820 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800821 const dbus::utility::DBusInteracesMap& interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000822 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700824 // Assume values exist as is (10^0 == 1) if no scale exists
825 int64_t scaleMultiplier = 0;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800826 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700827 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800828 if (interface == "xyz.openbmc_project.Sensor.Value")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700829 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800830 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800831 {
832 if (valueName == "Scale")
833 {
834 const int64_t* int64Value = std::get_if<int64_t>(&value);
835 if (int64Value != nullptr)
836 {
837 scaleMultiplier = *int64Value;
838 }
839 }
840 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100841 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100842 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700843
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200844 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500845 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500846 // For sensors in SensorCollection we set Id instead of MemberId,
847 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000848 sensorJson["Id"] = sensorName;
849 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500850 }
851 else if (sensorType != "power")
852 {
853 // Set MemberId and Name for non-power sensors. For PowerSupplies and
854 // PowerControl, those properties have more general values because
855 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000856 sensorJson["MemberId"] = sensorName;
857 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500858 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700859
Ed Tanous81ce6092020-12-17 16:54:55 +0000860 sensorJson["Status"]["State"] = getState(inventoryItem);
861 sensorJson["Status"]["Health"] =
862 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700863
864 // Parameter to set to override the type we get from dbus, and force it to
865 // int, regardless of what is available. This is used for schemas like fan,
866 // that require integers, not floats.
867 bool forceToInt = false;
868
Anthony Wilson3929aca2019-07-19 15:42:33 -0500869 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200870 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500871 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000872 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000873
874 const std::string& readingType = sensors::toReadingType(sensorType);
875 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500876 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000877 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
878 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500879 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000880 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500881 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000882 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500883 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000884
885 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
886 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200887 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000888 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
889 << sensorType;
890 }
891 else
892 {
893 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200894 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500895 }
896 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700897 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500898 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000899 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700900 // TODO(ed) Documentation says that path should be type fan_tach,
901 // implementation seems to implement fan
902 }
903 else if (sensorType == "fan" || sensorType == "fan_tach")
904 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500905 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000906 sensorJson["ReadingUnits"] = "RPM";
907 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
908 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 forceToInt = true;
910 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700911 else if (sensorType == "fan_pwm")
912 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500913 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000914 sensorJson["ReadingUnits"] = "Percent";
915 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
916 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700917 forceToInt = true;
918 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 else if (sensorType == "voltage")
920 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500921 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000922 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700924 else if (sensorType == "power")
925 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700926 std::string sensorNameLower =
927 boost::algorithm::to_lower_copy(sensorName);
928
Ed Tanous55f79e62022-01-25 11:26:16 -0800929 if (sensorName == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000930 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000931 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500932 // Put multiple "sensors" into a single PowerControl, so have
933 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000934 sensorJson["MemberId"] = "0";
935 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500936 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000937 }
938 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700939 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500940 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700941 }
942 else
943 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500944 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700945 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700946 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700947 else
948 {
949 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
950 return;
951 }
952 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500953 std::vector<
954 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
955 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700956 properties.reserve(7);
957
958 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600959
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200960 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500961 {
962 properties.emplace_back(
963 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
964 "/Thresholds/UpperCaution/Reading"_json_pointer);
965 properties.emplace_back(
966 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
967 "/Thresholds/LowerCaution/Reading"_json_pointer);
968 properties.emplace_back(
969 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
970 "/Thresholds/UpperCritical/Reading"_json_pointer);
971 properties.emplace_back(
972 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
973 "/Thresholds/LowerCritical/Reading"_json_pointer);
974 }
975 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600976 {
977 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500978 "WarningHigh",
979 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600980 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500981 "WarningLow",
982 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600983 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500984 "CriticalHigh",
985 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600986 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500987 "CriticalLow",
988 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600989 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990
Ed Tanous2474adf2018-09-05 16:31:16 -0700991 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
992
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200993 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500994 {
995 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500996 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500997 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500998 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500999 }
1000 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001001 {
1002 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001003 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001004 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001005 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001006 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001007 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001008 {
1009 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001010 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001012 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001013 }
1014
Anthony Wilson3929aca2019-07-19 15:42:33 -05001015 for (const std::tuple<const char*, const char*,
1016 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001017 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001018 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001019 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001020 if (interface != std::get<0>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001021 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001022 continue;
1023 }
Ed Tanous55f79e62022-01-25 11:26:16 -08001024 for (const auto& [valueName, valueVariant] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001025 {
1026 if (valueName != std::get<1>(p))
1027 {
1028 continue;
1029 }
Anthony Wilson3929aca2019-07-19 15:42:33 -05001030
1031 // The property we want to set may be nested json, so use
1032 // a json_pointer for easy indexing into the json structure.
1033 const nlohmann::json::json_pointer& key = std::get<2>(p);
1034
Ed Tanous1abe55e2018-09-05 08:30:59 -07001035 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001036 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001037
Ed Tanousabf2add2019-01-22 16:40:12 -08001038 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001039 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001040 double temp = 0.0;
1041 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001042 {
Ed Tanous271584a2019-07-09 16:24:22 -07001043 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001044 }
1045 else if (doubleValue != nullptr)
1046 {
1047 temp = *doubleValue;
1048 }
Eddie James028f7eb2019-05-17 21:24:36 +00001049 else if (uValue != nullptr)
1050 {
1051 temp = *uValue;
1052 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001053 else
1054 {
1055 BMCWEB_LOG_ERROR
1056 << "Got value interface that wasn't int or double";
1057 continue;
1058 }
1059 temp = temp * std::pow(10, scaleMultiplier);
1060 if (forceToInt)
1061 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001062 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001063 }
1064 else
1065 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001066 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001067 }
1068 }
1069 }
1070 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001071
Ed Tanous81ce6092020-12-17 16:54:55 +00001072 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001073 "/xyz/openbmc_project/sensors/" + sensorType +
1074 "/" + sensorName);
1075
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001077}
1078
Ed Tanousb5a76932020-09-29 16:16:58 -07001079inline void populateFanRedundancy(
1080 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001081{
1082 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001083 [sensorsAsyncResp](
1084 const boost::system::error_code ec,
1085 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001086 if (ec)
1087 {
1088 return; // don't have to have this interface
1089 }
1090 for (const std::pair<
1091 std::string,
1092 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1093 pathPair : resp)
1094 {
1095 const std::string& path = pathPair.first;
1096 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1097 objDict = pathPair.second;
1098 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001099 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001100 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001101 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001102
1103 const std::string& owner = objDict.begin()->first;
1104 sdbusplus::asio::getProperty<std::vector<std::string>>(
1105 *crow::connections::systemBus,
1106 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1107 "xyz.openbmc_project.Association", "endpoints",
1108 [path, owner,
1109 sensorsAsyncResp](const boost::system::error_code e,
1110 const std::vector<std::string>& endpoints) {
1111 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001112 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 return; // if they don't have an association we
1114 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001115 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001116 auto found =
1117 std::find_if(endpoints.begin(), endpoints.end(),
1118 [sensorsAsyncResp](const std::string& entry) {
1119 return entry.find(sensorsAsyncResp->chassisId) !=
1120 std::string::npos;
1121 });
James Feist8bd25cc2019-03-15 15:14:00 -07001122
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 if (found == endpoints.end())
1124 {
1125 return;
1126 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001127 sdbusplus::asio::getAllProperties(
1128 *crow::connections::systemBus, owner, path,
1129 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001130 [path, sensorsAsyncResp](
1131 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001132 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001133 if (err)
1134 {
1135 return; // don't have to have this
1136 // interface
1137 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001138
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001139 const uint8_t* allowedFailures = nullptr;
1140 const std::vector<std::string>* collection = nullptr;
1141 const std::string* status = nullptr;
1142
1143 const bool success = sdbusplus::unpackPropertiesNoThrow(
1144 dbus_utils::UnpackErrorPrinter(), ret,
1145 "AllowedFailures", allowedFailures, "Collection",
1146 collection, "Status", status);
1147
1148 if (!success)
1149 {
1150 messages::internalError(
1151 sensorsAsyncResp->asyncResp->res);
1152 return;
1153 }
1154
1155 if (allowedFailures == nullptr || collection == nullptr ||
1156 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001157 {
1158 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1159 messages::internalError(
1160 sensorsAsyncResp->asyncResp->res);
1161 return;
1162 }
1163
Ed Tanous002d39b2022-05-31 08:59:27 -07001164 sdbusplus::message::object_path objectPath(path);
1165 std::string name = objectPath.filename();
1166 if (name.empty())
1167 {
1168 // this should be impossible
1169 messages::internalError(
1170 sensorsAsyncResp->asyncResp->res);
1171 return;
1172 }
1173 std::replace(name.begin(), name.end(), '_', ' ');
1174
1175 std::string health;
1176
Ed Tanous11ba3972022-07-11 09:50:41 -07001177 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001178 {
1179 health = "OK";
1180 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001181 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001182 {
1183 health = "Warning";
1184 }
1185 else
1186 {
1187 health = "Critical";
1188 }
1189 nlohmann::json::array_t redfishCollection;
1190 const auto& fanRedfish =
1191 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1192 for (const std::string& item : *collection)
1193 {
Ed Tanous8a592812022-06-04 09:06:59 -07001194 sdbusplus::message::object_path itemPath(item);
1195 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001196 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001197 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001198 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001199 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001200 /*
1201 todo(ed): merge patch that fixes the names
1202 std::replace(itemName.begin(),
1203 itemName.end(), '_', ' ');*/
1204 auto schemaItem =
1205 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1206 [itemName](const nlohmann::json& fan) {
1207 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001208 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001209 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001210 {
Ed Tanous8a592812022-06-04 09:06:59 -07001211 nlohmann::json::object_t collectionId;
1212 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001213 (*schemaItem)["@odata.id"];
1214 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001215 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001216 }
1217 else
1218 {
1219 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1220 messages::internalError(
1221 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001222 return;
1223 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001224 }
James Feist8bd25cc2019-03-15 15:14:00 -07001225
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 size_t minNumNeeded =
1227 collection->empty()
1228 ? 0
1229 : collection->size() - *allowedFailures;
1230 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1231 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001232
Ed Tanous002d39b2022-05-31 08:59:27 -07001233 nlohmann::json::object_t redundancy;
1234 redundancy["@odata.id"] =
1235 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1236 "/" + sensorsAsyncResp->chassisSubNode +
1237 "#/Redundancy/" + std::to_string(jResp.size());
1238 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1239 redundancy["MinNumNeeded"] = minNumNeeded;
1240 redundancy["MemberId"] = name;
1241 redundancy["Mode"] = "N+m";
1242 redundancy["Name"] = name;
1243 redundancy["RedundancySet"] = redfishCollection;
1244 redundancy["Status"]["Health"] = health;
1245 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001246
Ed Tanous002d39b2022-05-31 08:59:27 -07001247 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001248 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001249 });
1250 }
James Feist8bd25cc2019-03-15 15:14:00 -07001251 },
1252 "xyz.openbmc_project.ObjectMapper",
1253 "/xyz/openbmc_project/object_mapper",
1254 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1255 "/xyz/openbmc_project/control", 2,
1256 std::array<const char*, 1>{
1257 "xyz.openbmc_project.Control.FanRedundancy"});
1258}
1259
Ed Tanousb5a76932020-09-29 16:16:58 -07001260inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001261 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001262{
zhanghch058d1b46d2021-04-01 11:18:24 +08001263 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001264 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001265 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001266 {
1267 sensorHeaders = {"Voltages", "PowerSupplies"};
1268 }
1269 for (const std::string& sensorGroup : sensorHeaders)
1270 {
1271 nlohmann::json::iterator entry = response.find(sensorGroup);
1272 if (entry != response.end())
1273 {
1274 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001275 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001276 return c1["Name"] < c2["Name"];
1277 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001278
1279 // add the index counts to the end of each entry
1280 size_t count = 0;
1281 for (nlohmann::json& sensorJson : *entry)
1282 {
1283 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1284 if (odata == sensorJson.end())
1285 {
1286 continue;
1287 }
1288 std::string* value = odata->get_ptr<std::string*>();
1289 if (value != nullptr)
1290 {
1291 *value += std::to_string(count);
1292 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001293 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001294 }
1295 }
1296 }
1297 }
1298}
1299
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001300/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001301 * @brief Finds the inventory item with the specified object path.
1302 * @param inventoryItems D-Bus inventory items associated with sensors.
1303 * @param invItemObjPath D-Bus object path of inventory item.
1304 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001305 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001306inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001307 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001308 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001309{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001310 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001311 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001312 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001313 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001314 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001315 }
1316 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001317 return nullptr;
1318}
1319
1320/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001321 * @brief Finds the inventory item associated with the specified sensor.
1322 * @param inventoryItems D-Bus inventory items associated with sensors.
1323 * @param sensorObjPath D-Bus object path of sensor.
1324 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001325 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001326inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001327 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001328 const std::string& sensorObjPath)
1329{
1330 for (InventoryItem& inventoryItem : *inventoryItems)
1331 {
1332 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1333 {
1334 return &inventoryItem;
1335 }
1336 }
1337 return nullptr;
1338}
1339
1340/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001341 * @brief Finds the inventory item associated with the specified led path.
1342 * @param inventoryItems D-Bus inventory items associated with sensors.
1343 * @param ledObjPath D-Bus object path of led.
1344 * @return Inventory item within vector, or nullptr if no match found.
1345 */
1346inline InventoryItem*
1347 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1348 const std::string& ledObjPath)
1349{
1350 for (InventoryItem& inventoryItem : inventoryItems)
1351 {
1352 if (inventoryItem.ledObjectPath == ledObjPath)
1353 {
1354 return &inventoryItem;
1355 }
1356 }
1357 return nullptr;
1358}
1359
1360/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001361 * @brief Adds inventory item and associated sensor to specified vector.
1362 *
1363 * Adds a new InventoryItem to the vector if necessary. Searches for an
1364 * existing InventoryItem with the specified object path. If not found, one is
1365 * added to the vector.
1366 *
1367 * Next, the specified sensor is added to the set of sensors associated with the
1368 * InventoryItem.
1369 *
1370 * @param inventoryItems D-Bus inventory items associated with sensors.
1371 * @param invItemObjPath D-Bus object path of inventory item.
1372 * @param sensorObjPath D-Bus object path of sensor
1373 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001374inline void addInventoryItem(
1375 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1376 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001377{
1378 // Look for inventory item in vector
1379 InventoryItem* inventoryItem =
1380 findInventoryItem(inventoryItems, invItemObjPath);
1381
1382 // If inventory item doesn't exist in vector, add it
1383 if (inventoryItem == nullptr)
1384 {
1385 inventoryItems->emplace_back(invItemObjPath);
1386 inventoryItem = &(inventoryItems->back());
1387 }
1388
1389 // Add sensor to set of sensors associated with inventory item
1390 inventoryItem->sensors.emplace(sensorObjPath);
1391}
1392
1393/**
1394 * @brief Stores D-Bus data in the specified inventory item.
1395 *
1396 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1397 * specified InventoryItem.
1398 *
1399 * This data is later used to provide sensor property values in the JSON
1400 * response.
1401 *
1402 * @param inventoryItem Inventory item where data will be stored.
1403 * @param interfacesDict Map containing D-Bus interfaces and their properties
1404 * for the specified inventory item.
1405 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001406inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001407 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001408 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001409{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001410 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001411
Ed Tanous9eb808c2022-01-25 10:19:23 -08001412 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001413 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001414 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001415 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001416 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001417 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001418 if (name == "Present")
1419 {
1420 const bool* value = std::get_if<bool>(&dbusValue);
1421 if (value != nullptr)
1422 {
1423 inventoryItem.isPresent = *value;
1424 }
1425 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426 }
1427 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001428 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001429
Ed Tanous711ac7a2021-12-20 09:34:41 -08001430 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001431 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001432 inventoryItem.isPowerSupply = true;
1433 }
1434
1435 // Get properties from Inventory.Decorator.Asset interface
1436 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1437 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001438 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001439 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001440 if (name == "Manufacturer")
1441 {
1442 const std::string* value =
1443 std::get_if<std::string>(&dbusValue);
1444 if (value != nullptr)
1445 {
1446 inventoryItem.manufacturer = *value;
1447 }
1448 }
1449 if (name == "Model")
1450 {
1451 const std::string* value =
1452 std::get_if<std::string>(&dbusValue);
1453 if (value != nullptr)
1454 {
1455 inventoryItem.model = *value;
1456 }
1457 }
1458 if (name == "SerialNumber")
1459 {
1460 const std::string* value =
1461 std::get_if<std::string>(&dbusValue);
1462 if (value != nullptr)
1463 {
1464 inventoryItem.serialNumber = *value;
1465 }
1466 }
1467 if (name == "PartNumber")
1468 {
1469 const std::string* value =
1470 std::get_if<std::string>(&dbusValue);
1471 if (value != nullptr)
1472 {
1473 inventoryItem.partNumber = *value;
1474 }
1475 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001476 }
1477 }
1478
Ed Tanous711ac7a2021-12-20 09:34:41 -08001479 if (interface ==
1480 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001481 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001482 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001484 if (name == "Functional")
1485 {
1486 const bool* value = std::get_if<bool>(&dbusValue);
1487 if (value != nullptr)
1488 {
1489 inventoryItem.isFunctional = *value;
1490 }
1491 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001492 }
1493 }
1494 }
1495}
1496
1497/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001498 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001499 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001500 * Uses the specified connections (services) to obtain D-Bus data for inventory
1501 * items associated with sensors. Stores the resulting data in the
1502 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001503 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001504 * This data is later used to provide sensor property values in the JSON
1505 * response.
1506 *
1507 * Finds the inventory item data asynchronously. Invokes callback when data has
1508 * been obtained.
1509 *
1510 * The callback must have the following signature:
1511 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001512 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001513 * @endcode
1514 *
1515 * This function is called recursively, obtaining data asynchronously from one
1516 * connection in each call. This ensures the callback is not invoked until the
1517 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001518 *
1519 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001520 * @param inventoryItems D-Bus inventory items associated with sensors.
1521 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001522 * @param objectMgrPaths Mappings from connection name to DBus object path that
1523 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001524 * @param callback Callback to invoke when inventory data has been obtained.
1525 * @param invConnectionsIndex Current index in invConnections. Only specified
1526 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001527 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001528template <typename Callback>
1529static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001530 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001531 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001532 std::shared_ptr<std::set<std::string>> invConnections,
1533 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001534 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001535{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001536 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001537
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001538 // If no more connections left, call callback
1539 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001540 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001541 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001542 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1543 return;
1544 }
1545
1546 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001547 auto it = invConnections->begin();
1548 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001549 if (it != invConnections->end())
1550 {
1551 const std::string& invConnection = *it;
1552
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001553 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001554 auto respHandler =
1555 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
Ed Tanous02cad962022-06-30 16:50:15 -07001556 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1557 const boost::system::error_code ec,
1558 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001559 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 if (ec)
1561 {
1562 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001563 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001564 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001565 return;
1566 }
1567
1568 // Loop through returned object paths
1569 for (const auto& objDictEntry : resp)
1570 {
1571 const std::string& objPath =
1572 static_cast<const std::string&>(objDictEntry.first);
1573
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001574 // If this object path is one of the specified inventory items
1575 InventoryItem* inventoryItem =
1576 findInventoryItem(inventoryItems, objPath);
1577 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579 // Store inventory data in InventoryItem
1580 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001581 }
1582 }
1583
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001584 // Recurse to get inventory item data from next connection
1585 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1586 invConnections, objectMgrPaths,
1587 std::move(callback), invConnectionsIndex + 1);
1588
1589 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 };
1591
1592 // Find DBus object path that implements ObjectManager for the current
1593 // connection. If no mapping found, default to "/".
1594 auto iter = objectMgrPaths->find(invConnection);
1595 const std::string& objectMgrPath =
1596 (iter != objectMgrPaths->end()) ? iter->second : "/";
1597 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1598 << objectMgrPath;
1599
1600 // Get all object paths and their interfaces for current connection
1601 crow::connections::systemBus->async_method_call(
1602 std::move(respHandler), invConnection, objectMgrPath,
1603 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1604 }
1605
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001607}
1608
1609/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001610 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001612 * Gets the D-Bus connections (services) that provide data for the inventory
1613 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001614 *
1615 * Finds the connections asynchronously. Invokes callback when information has
1616 * been obtained.
1617 *
1618 * The callback must have the following signature:
1619 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001620 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001621 * @endcode
1622 *
1623 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001624 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 * @param callback Callback to invoke when connections have been obtained.
1626 */
1627template <typename Callback>
1628static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001629 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1630 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631 Callback&& callback)
1632{
1633 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1634
1635 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001636 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001637 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001638 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1639 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001640 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1641
1642 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001643 auto respHandler =
1644 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1645 inventoryItems](
1646 const boost::system::error_code ec,
1647 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001648 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1649 if (ec)
1650 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001651 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001652 BMCWEB_LOG_ERROR
1653 << "getInventoryItemsConnections respHandler DBus error " << ec;
1654 return;
1655 }
1656
1657 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001658 std::shared_ptr<std::set<std::string>> invConnections =
1659 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001660
1661 // Loop through objects from GetSubTree
1662 for (const std::pair<
1663 std::string,
1664 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1665 object : subtree)
1666 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001667 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001668 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001669 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001670 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001671 // Store all connections to inventory item
1672 for (const std::pair<std::string, std::vector<std::string>>&
1673 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001674 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001675 const std::string& invConnection = objData.first;
1676 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001677 }
1678 }
1679 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001680
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001681 callback(invConnections);
1682 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1683 };
1684
1685 // Make call to ObjectMapper to find all inventory items
1686 crow::connections::systemBus->async_method_call(
1687 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1688 "/xyz/openbmc_project/object_mapper",
1689 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1690 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1691}
1692
1693/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001694 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001695 *
1696 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001697 * inventory items. Then finds the associations from those inventory items to
1698 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001699 *
1700 * Finds the inventory items asynchronously. Invokes callback when information
1701 * has been obtained.
1702 *
1703 * The callback must have the following signature:
1704 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001705 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001706 * @endcode
1707 *
1708 * @param sensorsAsyncResp Pointer to object holding response data.
1709 * @param sensorNames All sensors within the current chassis.
1710 * @param objectMgrPaths Mappings from connection name to DBus object path that
1711 * implements ObjectManager.
1712 * @param callback Callback to invoke when inventory items have been obtained.
1713 */
1714template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001715static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001716 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001717 const std::shared_ptr<std::set<std::string>>& sensorNames,
1718 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001719 Callback&& callback)
1720{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001721 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001722
1723 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001724 auto respHandler =
1725 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1726 sensorNames](const boost::system::error_code ec,
1727 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001728 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001729 if (ec)
1730 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001731 BMCWEB_LOG_ERROR
1732 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001733 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001734 return;
1735 }
1736
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001737 // Create vector to hold list of inventory items
1738 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1739 std::make_shared<std::vector<InventoryItem>>();
1740
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001741 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001742 std::string sensorAssocPath;
1743 sensorAssocPath.reserve(128); // avoid memory allocations
1744 for (const auto& objDictEntry : resp)
1745 {
1746 const std::string& objPath =
1747 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001748
1749 // If path is inventory association for one of the specified sensors
1750 for (const std::string& sensorName : *sensorNames)
1751 {
1752 sensorAssocPath = sensorName;
1753 sensorAssocPath += "/inventory";
1754 if (objPath == sensorAssocPath)
1755 {
1756 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001757 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001758 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001759 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001760 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001761 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001762 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001763 if (valueName == "endpoints")
1764 {
1765 const std::vector<std::string>* endpoints =
1766 std::get_if<std::vector<std::string>>(
1767 &value);
1768 if ((endpoints != nullptr) &&
1769 !endpoints->empty())
1770 {
1771 // Add inventory item to vector
1772 const std::string& invItemPath =
1773 endpoints->front();
1774 addInventoryItem(inventoryItems,
1775 invItemPath,
1776 sensorName);
1777 }
1778 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001779 }
1780 }
1781 }
1782 break;
1783 }
1784 }
1785 }
1786
Anthony Wilsond5005492019-07-31 16:34:17 -05001787 // Now loop through the returned object paths again, this time to
1788 // find the leds associated with the inventory items we just found
1789 std::string inventoryAssocPath;
1790 inventoryAssocPath.reserve(128); // avoid memory allocations
1791 for (const auto& objDictEntry : resp)
1792 {
1793 const std::string& objPath =
1794 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001795
1796 for (InventoryItem& inventoryItem : *inventoryItems)
1797 {
1798 inventoryAssocPath = inventoryItem.objectPath;
1799 inventoryAssocPath += "/leds";
1800 if (objPath == inventoryAssocPath)
1801 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001802 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001803 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001804 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001805 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001806 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001807 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001808 if (valueName == "endpoints")
1809 {
1810 const std::vector<std::string>* endpoints =
1811 std::get_if<std::vector<std::string>>(
1812 &value);
1813 if ((endpoints != nullptr) &&
1814 !endpoints->empty())
1815 {
1816 // Add inventory item to vector
1817 // Store LED path in inventory item
1818 const std::string& ledPath =
1819 endpoints->front();
1820 inventoryItem.ledObjectPath = ledPath;
1821 }
1822 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001823 }
1824 }
1825 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001826
Anthony Wilsond5005492019-07-31 16:34:17 -05001827 break;
1828 }
1829 }
1830 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001831 callback(inventoryItems);
1832 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001833 };
1834
1835 // Find DBus object path that implements ObjectManager for ObjectMapper
1836 std::string connection = "xyz.openbmc_project.ObjectMapper";
1837 auto iter = objectMgrPaths->find(connection);
1838 const std::string& objectMgrPath =
1839 (iter != objectMgrPaths->end()) ? iter->second : "/";
1840 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1841 << objectMgrPath;
1842
1843 // Call GetManagedObjects on the ObjectMapper to get all associations
1844 crow::connections::systemBus->async_method_call(
1845 std::move(respHandler), connection, objectMgrPath,
1846 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1847
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001848 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001849}
1850
1851/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001852 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1853 *
1854 * Uses the specified connections (services) to obtain D-Bus data for inventory
1855 * item leds associated with sensors. Stores the resulting data in the
1856 * inventoryItems vector.
1857 *
1858 * This data is later used to provide sensor property values in the JSON
1859 * response.
1860 *
1861 * Finds the inventory item led data asynchronously. Invokes callback when data
1862 * has been obtained.
1863 *
1864 * The callback must have the following signature:
1865 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001866 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001867 * @endcode
1868 *
1869 * This function is called recursively, obtaining data asynchronously from one
1870 * connection in each call. This ensures the callback is not invoked until the
1871 * last asynchronous function has completed.
1872 *
1873 * @param sensorsAsyncResp Pointer to object holding response data.
1874 * @param inventoryItems D-Bus inventory items associated with sensors.
1875 * @param ledConnections Connections that provide data for the inventory leds.
1876 * @param callback Callback to invoke when inventory data has been obtained.
1877 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1878 * in recursive calls to this function.
1879 */
1880template <typename Callback>
1881void getInventoryLedData(
1882 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1883 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001884 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001885 Callback&& callback, size_t ledConnectionsIndex = 0)
1886{
1887 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1888
1889 // If no more connections left, call callback
1890 if (ledConnectionsIndex >= ledConnections->size())
1891 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001892 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001893 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1894 return;
1895 }
1896
1897 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001898 auto it = ledConnections->begin();
1899 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001900 if (it != ledConnections->end())
1901 {
1902 const std::string& ledPath = (*it).first;
1903 const std::string& ledConnection = (*it).second;
1904 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001905 auto respHandler =
1906 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001907 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001908 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1910 if (ec)
1911 {
1912 BMCWEB_LOG_ERROR
1913 << "getInventoryLedData respHandler DBus error " << ec;
1914 messages::internalError(sensorsAsyncResp->asyncResp->res);
1915 return;
1916 }
1917
1918 BMCWEB_LOG_DEBUG << "Led state: " << state;
1919 // Find inventory item with this LED object path
1920 InventoryItem* inventoryItem =
1921 findInventoryItemForLed(*inventoryItems, ledPath);
1922 if (inventoryItem != nullptr)
1923 {
1924 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001925 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001926 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001927 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001928 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001929 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001930 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001931 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001932 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001933 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001934 {
1935 inventoryItem->ledState = LedState::OFF;
1936 }
1937 else
1938 {
1939 inventoryItem->ledState = LedState::UNKNOWN;
1940 }
1941 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001942
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 // Recurse to get LED data from next connection
1944 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1945 ledConnections, std::move(callback),
1946 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001947
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1949 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001950
1951 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001952 sdbusplus::asio::getProperty<std::string>(
1953 *crow::connections::systemBus, ledConnection, ledPath,
1954 "xyz.openbmc_project.Led.Physical", "State",
1955 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001956 }
1957
1958 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1959}
1960
1961/**
1962 * @brief Gets LED data for LEDs associated with given inventory items.
1963 *
1964 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1965 * associated with the specified inventory items. Then gets the LED data from
1966 * each connection and stores it in the inventory item.
1967 *
1968 * This data is later used to provide sensor property values in the JSON
1969 * response.
1970 *
1971 * Finds the LED data asynchronously. Invokes callback when information has
1972 * been obtained.
1973 *
1974 * The callback must have the following signature:
1975 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001976 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001977 * @endcode
1978 *
1979 * @param sensorsAsyncResp Pointer to object holding response data.
1980 * @param inventoryItems D-Bus inventory items associated with sensors.
1981 * @param callback Callback to invoke when inventory items have been obtained.
1982 */
1983template <typename Callback>
1984void getInventoryLeds(
1985 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1986 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1987 Callback&& callback)
1988{
1989 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1990
1991 const std::string path = "/xyz/openbmc_project";
1992 const std::array<std::string, 1> interfaces = {
1993 "xyz.openbmc_project.Led.Physical"};
1994
1995 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001996 auto respHandler =
1997 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1998 inventoryItems](
1999 const boost::system::error_code ec,
2000 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05002001 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2002 if (ec)
2003 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002004 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002005 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2006 << ec;
2007 return;
2008 }
2009
2010 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00002011 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
2012 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05002013
2014 // Loop through objects from GetSubTree
2015 for (const std::pair<
2016 std::string,
2017 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2018 object : subtree)
2019 {
2020 // Check if object path is LED for one of the specified inventory
2021 // items
2022 const std::string& ledPath = object.first;
2023 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2024 {
2025 // Add mapping from ledPath to connection
2026 const std::string& connection = object.second.begin()->first;
2027 (*ledConnections)[ledPath] = connection;
2028 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2029 << connection;
2030 }
2031 }
2032
2033 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2034 std::move(callback));
2035 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2036 };
2037 // Make call to ObjectMapper to find all inventory items
2038 crow::connections::systemBus->async_method_call(
2039 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2040 "/xyz/openbmc_project/object_mapper",
2041 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2042 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2043}
2044
2045/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002046 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2047 *
2048 * Uses the specified connections (services) (currently assumes just one) to
2049 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2050 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2051 *
2052 * This data is later used to provide sensor property values in the JSON
2053 * response.
2054 *
2055 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2056 * when data has been obtained.
2057 *
2058 * The callback must have the following signature:
2059 * @code
2060 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2061 * @endcode
2062 *
2063 * @param sensorsAsyncResp Pointer to object holding response data.
2064 * @param inventoryItems D-Bus inventory items associated with sensors.
2065 * @param psAttributesConnections Connections that provide data for the Power
2066 * Supply Attributes
2067 * @param callback Callback to invoke when data has been obtained.
2068 */
2069template <typename Callback>
2070void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002071 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002072 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002073 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002074 Callback&& callback)
2075{
2076 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2077
2078 if (psAttributesConnections.empty())
2079 {
2080 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2081 callback(inventoryItems);
2082 return;
2083 }
2084
2085 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002086 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002087
2088 const std::string& psAttributesPath = (*it).first;
2089 const std::string& psAttributesConnection = (*it).second;
2090
2091 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002092 auto respHandler =
2093 [sensorsAsyncResp, inventoryItems,
2094 callback{std::forward<Callback>(callback)}](
2095 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002096 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2097 if (ec)
2098 {
2099 BMCWEB_LOG_ERROR
2100 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002101 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002102 return;
2103 }
2104
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002105 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2106 // Store value in Power Supply Inventory Items
2107 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002108 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002109 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002110 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002111 inventoryItem.powerSupplyEfficiencyPercent =
2112 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002113 }
2114 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002115
2116 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2117 callback(inventoryItems);
2118 };
2119
2120 // Get the DeratingFactor property for the PowerSupplyAttributes
2121 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002122 sdbusplus::asio::getProperty<uint32_t>(
2123 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2124 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2125 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002126
2127 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2128}
2129
2130/**
2131 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2132 *
2133 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2134 * data. Then gets the Power Supply Attributes data from the connection
2135 * (currently just assumes 1 connection) and stores the data in the inventory
2136 * item.
2137 *
2138 * This data is later used to provide sensor property values in the JSON
2139 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2140 *
2141 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2142 * when information has been obtained.
2143 *
2144 * The callback must have the following signature:
2145 * @code
2146 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2147 * @endcode
2148 *
2149 * @param sensorsAsyncResp Pointer to object holding response data.
2150 * @param inventoryItems D-Bus inventory items associated with sensors.
2151 * @param callback Callback to invoke when data has been obtained.
2152 */
2153template <typename Callback>
2154void getPowerSupplyAttributes(
2155 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2156 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2157 Callback&& callback)
2158{
2159 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2160
2161 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002162 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002163 {
2164 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2165 callback(inventoryItems);
2166 return;
2167 }
2168
2169 const std::array<std::string, 1> interfaces = {
2170 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2171
2172 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002173 auto respHandler =
2174 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2175 inventoryItems](
2176 const boost::system::error_code ec,
2177 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002178 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2179 if (ec)
2180 {
2181 messages::internalError(sensorsAsyncResp->asyncResp->res);
2182 BMCWEB_LOG_ERROR
2183 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2184 return;
2185 }
2186 if (subtree.empty())
2187 {
2188 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2189 callback(inventoryItems);
2190 return;
2191 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002192
Ed Tanous002d39b2022-05-31 08:59:27 -07002193 // Currently we only support 1 power supply attribute, use this for
2194 // all the power supplies. Build map of object path to connection.
2195 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002196 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002197
Ed Tanous002d39b2022-05-31 08:59:27 -07002198 if (subtree[0].first.empty() || subtree[0].second.empty())
2199 {
2200 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2201 callback(inventoryItems);
2202 return;
2203 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002204
Ed Tanous002d39b2022-05-31 08:59:27 -07002205 const std::string& psAttributesPath = subtree[0].first;
2206 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002207
Ed Tanous002d39b2022-05-31 08:59:27 -07002208 if (connection.empty())
2209 {
2210 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2211 callback(inventoryItems);
2212 return;
2213 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002214
Ed Tanous002d39b2022-05-31 08:59:27 -07002215 psAttributesConnections[psAttributesPath] = connection;
2216 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2217 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002218
Ed Tanous002d39b2022-05-31 08:59:27 -07002219 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2220 psAttributesConnections,
2221 std::move(callback));
2222 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2223 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002224 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2225 crow::connections::systemBus->async_method_call(
2226 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2227 "/xyz/openbmc_project/object_mapper",
2228 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2229 "/xyz/openbmc_project", 0, interfaces);
2230 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2231}
2232
2233/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002234 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002235 *
2236 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002237 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002238 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002239 * This data is later used to provide sensor property values in the JSON
2240 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002241 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002242 * Finds the inventory items asynchronously. Invokes callback when the
2243 * inventory items have been obtained.
2244 *
2245 * The callback must have the following signature:
2246 * @code
2247 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2248 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002249 *
2250 * @param sensorsAsyncResp Pointer to object holding response data.
2251 * @param sensorNames All sensors within the current chassis.
2252 * @param objectMgrPaths Mappings from connection name to DBus object path that
2253 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002254 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002255 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256template <typename Callback>
2257static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002258 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002259 const std::shared_ptr<std::set<std::string>> sensorNames,
2260 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002261 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002262{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002263 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2264 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002265 [sensorsAsyncResp, objectMgrPaths,
2266 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002267 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002268 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2269 auto getInventoryItemsConnectionsCb =
2270 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2271 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002272 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002273 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2274 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2275 callback{std::move(callback)}]() {
2276 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002277
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2279 callback{std::move(callback)}]() {
2280 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2281 // Find Power Supply Attributes and get the data
2282 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2283 std::move(callback));
2284 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002285 };
2286
Ed Tanous002d39b2022-05-31 08:59:27 -07002287 // Find led connections and get the data
2288 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2289 std::move(getInventoryLedsCb));
2290 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2291 };
2292
2293 // Get inventory item data from connections
2294 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2295 invConnections, objectMgrPaths,
2296 std::move(getInventoryItemsDataCb));
2297 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002298 };
2299
Ed Tanous002d39b2022-05-31 08:59:27 -07002300 // Get connections that provide inventory item data
2301 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2302 std::move(getInventoryItemsConnectionsCb));
2303 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2304 };
2305
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002306 // Get associations from sensors to inventory items
2307 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2308 std::move(getInventoryItemAssociationsCb));
2309 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2310}
2311
2312/**
2313 * @brief Returns JSON PowerSupply object for the specified inventory item.
2314 *
2315 * Searches for a JSON PowerSupply object that matches the specified inventory
2316 * item. If one is not found, a new PowerSupply object is added to the JSON
2317 * array.
2318 *
2319 * Multiple sensors are often associated with one power supply inventory item.
2320 * As a result, multiple sensor values are stored in one JSON PowerSupply
2321 * object.
2322 *
2323 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2324 * @param inventoryItem Inventory item for the power supply.
2325 * @param chassisId Chassis that contains the power supply.
2326 * @return JSON PowerSupply object for the specified inventory item.
2327 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002328inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002329 const InventoryItem& inventoryItem,
2330 const std::string& chassisId)
2331{
2332 // Check if matching PowerSupply object already exists in JSON array
2333 for (nlohmann::json& powerSupply : powerSupplyArray)
2334 {
2335 if (powerSupply["MemberId"] == inventoryItem.name)
2336 {
2337 return powerSupply;
2338 }
2339 }
2340
2341 // Add new PowerSupply object to JSON array
2342 powerSupplyArray.push_back({});
2343 nlohmann::json& powerSupply = powerSupplyArray.back();
2344 powerSupply["@odata.id"] =
2345 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2346 powerSupply["MemberId"] = inventoryItem.name;
2347 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2348 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2349 powerSupply["Model"] = inventoryItem.model;
2350 powerSupply["PartNumber"] = inventoryItem.partNumber;
2351 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002352 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002353
Gunnar Mills42cbe532019-08-15 15:26:54 -05002354 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2355 {
2356 powerSupply["EfficiencyPercent"] =
2357 inventoryItem.powerSupplyEfficiencyPercent;
2358 }
2359
2360 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002361 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2362 powerSupply["Status"]["Health"] = health;
2363
2364 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002365}
2366
2367/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002368 * @brief Gets the values of the specified sensors.
2369 *
2370 * Stores the results as JSON in the SensorsAsyncResp.
2371 *
2372 * Gets the sensor values asynchronously. Stores the results later when the
2373 * information has been obtained.
2374 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002375 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002376 *
2377 * To minimize the number of DBus calls, the DBus method
2378 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2379 * values of all sensors provided by a connection (service).
2380 *
2381 * The connections set contains all the connections that provide sensor values.
2382 *
2383 * The objectMgrPaths map contains mappings from a connection name to the
2384 * corresponding DBus object path that implements ObjectManager.
2385 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002386 * The InventoryItem vector contains D-Bus inventory items associated with the
2387 * sensors. Inventory item data is needed for some Redfish sensor properties.
2388 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002389 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002390 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002391 * @param connections Connections that provide sensor values.
2392 * @param objectMgrPaths Mappings from connection name to DBus object path that
2393 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002394 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002395 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002396inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002397 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002398 const std::shared_ptr<std::set<std::string>>& sensorNames,
2399 const std::set<std::string>& connections,
2400 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002401 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002402{
2403 BMCWEB_LOG_DEBUG << "getSensorData enter";
2404 // Get managed objects from all services exposing sensors
2405 for (const std::string& connection : connections)
2406 {
2407 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002408 auto getManagedObjectsCb =
2409 [sensorsAsyncResp, sensorNames,
2410 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002411 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002412 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2413 if (ec)
2414 {
2415 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002416 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002417 return;
2418 }
2419 // Go through all objects and update response with sensor data
2420 for (const auto& objDictEntry : resp)
2421 {
2422 const std::string& objPath =
2423 static_cast<const std::string&>(objDictEntry.first);
2424 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2425 << objPath;
2426
Shawn McCarneyde629b62019-03-08 10:42:51 -06002427 std::vector<std::string> split;
2428 // Reserve space for
2429 // /xyz/openbmc_project/sensors/<name>/<subname>
2430 split.reserve(6);
2431 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2432 if (split.size() < 6)
2433 {
2434 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2435 << objPath;
2436 continue;
2437 }
2438 // These indexes aren't intuitive, as boost::split puts an empty
2439 // string at the beginning
2440 const std::string& sensorType = split[4];
2441 const std::string& sensorName = split[5];
2442 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2443 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002444 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002445 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002446 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002447 continue;
2448 }
2449
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002450 // Find inventory item (if any) associated with sensor
2451 InventoryItem* inventoryItem =
2452 findInventoryItemForSensor(inventoryItems, objPath);
2453
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002454 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002455 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002456
2457 nlohmann::json* sensorJson = nullptr;
2458
Nan Zhou928fefb2022-03-28 08:45:00 -07002459 if (sensorSchema == sensors::node::sensors &&
2460 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002461 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002462 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002463 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2464 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002465 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002466 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002467 }
2468 else
2469 {
Ed Tanous271584a2019-07-09 16:24:22 -07002470 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002471 if (sensorsAsyncResp->efficientExpand)
2472 {
2473 fieldName = "Members";
2474 }
2475 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002476 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002477 fieldName = "Temperatures";
2478 }
2479 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2480 sensorType == "fan_pwm")
2481 {
2482 fieldName = "Fans";
2483 }
2484 else if (sensorType == "voltage")
2485 {
2486 fieldName = "Voltages";
2487 }
2488 else if (sensorType == "power")
2489 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002490 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002491 {
2492 fieldName = "PowerControl";
2493 }
2494 else if ((inventoryItem != nullptr) &&
2495 (inventoryItem->isPowerSupply))
2496 {
2497 fieldName = "PowerSupplies";
2498 }
2499 else
2500 {
2501 // Other power sensors are in SensorCollection
2502 continue;
2503 }
2504 }
2505 else
2506 {
2507 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2508 << sensorType;
2509 continue;
2510 }
2511
2512 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002513 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002514 if (fieldName == "PowerControl")
2515 {
2516 if (tempArray.empty())
2517 {
2518 // Put multiple "sensors" into a single
2519 // PowerControl. Follows MemberId naming and
2520 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002521 nlohmann::json::object_t power;
2522 power["@odata.id"] =
2523 "/redfish/v1/Chassis/" +
2524 sensorsAsyncResp->chassisId + "/" +
2525 sensorsAsyncResp->chassisSubNode + "#/" +
2526 fieldName + "/0";
2527 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002528 }
2529 sensorJson = &(tempArray.back());
2530 }
2531 else if (fieldName == "PowerSupplies")
2532 {
2533 if (inventoryItem != nullptr)
2534 {
2535 sensorJson =
2536 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002537 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002538 }
2539 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002540 else if (fieldName == "Members")
2541 {
Ed Tanous14766872022-03-15 10:44:42 -07002542 nlohmann::json::object_t member;
2543 member["@odata.id"] =
2544 "/redfish/v1/Chassis/" +
2545 sensorsAsyncResp->chassisId + "/" +
2546 sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2547 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002548 sensorJson = &(tempArray.back());
2549 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002550 else
2551 {
Ed Tanous14766872022-03-15 10:44:42 -07002552 nlohmann::json::object_t member;
2553 member["@odata.id"] = "/redfish/v1/Chassis/" +
2554 sensorsAsyncResp->chassisId +
2555 "/" +
2556 sensorsAsyncResp->chassisSubNode +
2557 "#/" + fieldName + "/";
2558 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002559 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002560 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002561 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002562
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002563 if (sensorJson != nullptr)
2564 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002565 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002566 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002567 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002568 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002569 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002570 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002571 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002572 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002573 if (sensorsAsyncResp->chassisSubNode ==
2574 sensors::node::sensors &&
2575 sensorsAsyncResp->efficientExpand)
2576 {
2577 sensorsAsyncResp->asyncResp->res
2578 .jsonValue["Members@odata.count"] =
2579 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2580 .size();
2581 }
2582 else if (sensorsAsyncResp->chassisSubNode ==
2583 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002584 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002585 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002586 }
James Feist8bd25cc2019-03-15 15:14:00 -07002587 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002588 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2589 };
2590
2591 // Find DBus object path that implements ObjectManager for the current
2592 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002593 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002594 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002595 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002596 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2597 << objectMgrPath;
2598
2599 crow::connections::systemBus->async_method_call(
2600 getManagedObjectsCb, connection, objectMgrPath,
2601 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002602 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002603 BMCWEB_LOG_DEBUG << "getSensorData exit";
2604}
2605
Nan Zhoufe04d492022-06-22 17:10:41 +00002606inline void
2607 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2608 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002609{
Nan Zhoufe04d492022-06-22 17:10:41 +00002610 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2611 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002612 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2613 auto getObjectManagerPathsCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002614 [sensorsAsyncResp, sensorNames, connections](
2615 const std::shared_ptr<std::map<std::string, std::string>>&
2616 objectMgrPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002617 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2618 auto getInventoryItemsCb =
2619 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2620 const std::shared_ptr<std::vector<InventoryItem>>&
2621 inventoryItems) {
2622 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2623 // Get sensor data and store results in JSON
2624 getSensorData(sensorsAsyncResp, sensorNames, connections,
2625 objectMgrPaths, inventoryItems);
2626 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2627 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002628
Ed Tanous002d39b2022-05-31 08:59:27 -07002629 // Get inventory items associated with sensors
2630 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2631 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002632
Ed Tanous002d39b2022-05-31 08:59:27 -07002633 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002634 };
2635
Ed Tanous002d39b2022-05-31 08:59:27 -07002636 // Get mapping from connection names to the DBus object
2637 // paths that implement the ObjectManager interface
2638 getObjectManagerPaths(sensorsAsyncResp,
2639 std::move(getObjectManagerPathsCb));
2640 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2641 };
2642
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002643 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002644 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002645}
2646
Shawn McCarneyde629b62019-03-08 10:42:51 -06002647/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002648 * @brief Entry point for retrieving sensors data related to requested
2649 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002650 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002651 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002652inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002653 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002654{
2655 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002656 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002657 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002658 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002659 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2660 processSensorList(sensorsAsyncResp, sensorNames);
2661 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2662 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002663 // SensorCollection doesn't contain the Redundancy property
2664 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2665 {
2666 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2667 nlohmann::json::array();
2668 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002669 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002670 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002671 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002672}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002673
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302674/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002675 * @brief Find the requested sensorName in the list of all sensors supplied by
2676 * the chassis node
2677 *
2678 * @param sensorName The sensor name supplied in the PATCH request
2679 * @param sensorsList The list of sensors managed by the chassis node
2680 * @param sensorsModified The list of sensors that were found as a result of
2681 * repeated calls to this function
2682 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002683inline bool
2684 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002685 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002686 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002687{
Nan Zhoufe04d492022-06-22 17:10:41 +00002688 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002689 {
George Liu28aa8de2021-02-01 15:13:30 +08002690 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002691 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002692 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002693 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002694 continue;
2695 }
2696 if (thisSensorName == sensorName)
2697 {
2698 sensorsModified.emplace(chassisSensor);
2699 return true;
2700 }
2701 }
2702 return false;
2703}
2704
2705/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302706 * @brief Entry point for overriding sensor values of given sensor
2707 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002708 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002709 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002710 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302711 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002712inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002713 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002714 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002715 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302716{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002717 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002718 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302719
Ed Tanous543f4402022-01-06 13:12:53 -08002720 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302721 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302722 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002723 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302724 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302725 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302726 if (collectionItems.first == "Temperatures")
2727 {
2728 propertyValueName = "ReadingCelsius";
2729 }
2730 else if (collectionItems.first == "Fans")
2731 {
2732 propertyValueName = "Reading";
2733 }
2734 else
2735 {
2736 propertyValueName = "ReadingVolts";
2737 }
2738 for (auto& item : collectionItems.second)
2739 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002740 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2741 "MemberId", memberId, propertyValueName,
2742 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302743 {
2744 return;
2745 }
2746 overrideMap.emplace(memberId,
2747 std::make_pair(value, collectionItems.first));
2748 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302749 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002750
Ed Tanous002d39b2022-05-31 08:59:27 -07002751 auto getChassisSensorListCb =
2752 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002753 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002754 // Match sensor names in the PATCH request to those managed by the
2755 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002756 const std::shared_ptr<std::set<std::string>> sensorNames =
2757 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302758 for (const auto& item : overrideMap)
2759 {
2760 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002761 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2762 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302763 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302764 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002765 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302766 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302767 return;
2768 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302769 }
2770 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002771 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002772 [sensorAsyncResp,
2773 overrideMap](const std::set<std::string>& /*connections*/,
2774 const std::set<std::pair<std::string, std::string>>&
2775 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002776 if (objectsWithConnection.size() != overrideMap.size())
2777 {
2778 BMCWEB_LOG_INFO
2779 << "Unable to find all objects with proper connection "
2780 << objectsWithConnection.size() << " requested "
2781 << overrideMap.size() << "\n";
2782 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2783 sensorAsyncResp->chassisSubNode ==
2784 sensors::node::thermal
2785 ? "Temperatures"
2786 : "Voltages",
2787 "Count");
2788 return;
2789 }
2790 for (const auto& item : objectsWithConnection)
2791 {
2792 sdbusplus::message::object_path path(item.first);
2793 std::string sensorName = path.filename();
2794 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302795 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002796 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302797 return;
2798 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302799
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002800 const auto& iterator = overrideMap.find(sensorName);
2801 if (iterator == overrideMap.end())
2802 {
2803 BMCWEB_LOG_INFO << "Unable to find sensor object"
2804 << item.first << "\n";
2805 messages::internalError(sensorAsyncResp->asyncResp->res);
2806 return;
2807 }
2808 crow::connections::systemBus->async_method_call(
2809 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002810 if (ec)
2811 {
2812 if (ec.value() ==
2813 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002814 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002815 BMCWEB_LOG_WARNING
2816 << "Manufacturing mode is not Enabled...can't "
2817 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002818
Ed Tanous002d39b2022-05-31 08:59:27 -07002819 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002820 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002821 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002822 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002823 BMCWEB_LOG_DEBUG
2824 << "setOverrideValueStatus DBUS error: " << ec;
2825 messages::internalError(
2826 sensorAsyncResp->asyncResp->res);
2827 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002828 },
2829 item.second, item.first, "org.freedesktop.DBus.Properties",
2830 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002831 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002832 }
2833 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302834 // Get object with connection for the given sensor name
2835 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2836 std::move(getObjectsWithConnectionCb));
2837 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302838 // get full sensor list for the given chassisId and cross verify the sensor.
2839 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2840}
2841
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002842/**
2843 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2844 * path of the sensor.
2845 *
2846 * Function builds valid Redfish response for sensor query of given chassis and
2847 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2848 * it to caller in a callback.
2849 *
2850 * @param chassis Chassis for which retrieval should be performed
2851 * @param node Node (group) of sensors. See sensors::node for supported values
2852 * @param mapComplete Callback to be called with retrieval result
2853 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002854inline void retrieveUriToDbusMap(const std::string& chassis,
2855 const std::string& node,
2856 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002857{
Ed Tanous02da7c52022-02-27 00:09:02 -08002858 decltype(sensors::paths)::const_iterator pathIt =
2859 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2860 [&node](auto&& val) { return val.first == node; });
2861 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002862 {
2863 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2864 mapComplete(boost::beast::http::status::bad_request, {});
2865 return;
2866 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002867
Nan Zhou72374eb2022-01-27 17:06:51 -08002868 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002869 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2870 const boost::beast::http::status status,
2871 const std::map<std::string, std::string>& uriToDbus) {
2872 mapCompleteCb(status, uriToDbus);
2873 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002874
2875 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002876 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002877 getChassisData(resp);
2878}
2879
Nan Zhoubacb2162022-04-06 11:28:32 -07002880namespace sensors
2881{
Nan Zhou928fefb2022-03-28 08:45:00 -07002882
Nan Zhoubacb2162022-04-06 11:28:32 -07002883inline void getChassisCallback(
2884 const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002885 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002886{
2887 BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2888
2889 nlohmann::json& entriesArray =
2890 asyncResp->asyncResp->res.jsonValue["Members"];
Nan Zhoufe04d492022-06-22 17:10:41 +00002891 for (const auto& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002892 {
2893 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2894
2895 sdbusplus::message::object_path path(sensor);
2896 std::string sensorName = path.filename();
2897 if (sensorName.empty())
2898 {
2899 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2900 messages::internalError(asyncResp->asyncResp->res);
2901 return;
2902 }
Ed Tanous14766872022-03-15 10:44:42 -07002903 nlohmann::json::object_t member;
2904 member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
2905 "/" + asyncResp->chassisSubNode + "/" +
2906 sensorName;
2907 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002908 }
2909
2910 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2911 entriesArray.size();
2912 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2913}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002914
Nan Zhoude167a62022-06-01 04:47:45 +00002915inline void
2916 handleSensorCollectionGet(App& app, const crow::Request& req,
2917 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2918 const std::string& chassisId)
2919{
2920 query_param::QueryCapabilities capabilities = {
2921 .canDelegateExpandLevel = 1,
2922 };
2923 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002924 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002925 delegatedQuery, capabilities))
2926 {
2927 return;
2928 }
2929
2930 if (delegatedQuery.expandType != query_param::ExpandType::None)
2931 {
2932 // we perform efficient expand.
2933 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2934 aResp, chassisId, sensors::dbus::sensorPaths,
2935 sensors::node::sensors,
2936 /*efficientExpand=*/true);
2937 getChassisData(asyncResp);
2938
2939 BMCWEB_LOG_DEBUG
2940 << "SensorCollection doGet exit via efficient expand handler";
2941 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002942 }
Nan Zhoude167a62022-06-01 04:47:45 +00002943
2944 // if there's no efficient expand available, we use the default
2945 // Query Parameters route
2946 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2947 aResp, chassisId, sensors::dbus::sensorPaths, sensors::node::sensors);
2948
2949 // We get all sensors as hyperlinkes in the chassis (this
2950 // implies we reply on the default query parameters handler)
2951 getChassis(asyncResp,
2952 std::bind_front(sensors::getChassisCallback, asyncResp));
2953 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
2954}
2955
Nan Zhoue6bd8462022-06-01 04:35:35 +00002956inline void handleSensorGet(App& app, const crow::Request& req,
2957 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2958 const std::string& chassisId,
2959 const std::string& sensorName)
2960{
Carson Labrado3ba00072022-06-06 19:40:56 +00002961 if (!redfish::setUpRedfishRoute(app, req, aResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002962 {
2963 return;
2964 }
2965 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2966 std::shared_ptr<SensorsAsyncResp> asyncResp =
2967 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
2968 std::span<std::string_view>(),
2969 sensors::node::sensors);
2970
2971 const std::array<const char*, 1> interfaces = {
2972 "xyz.openbmc_project.Sensor.Value"};
2973
2974 // Get a list of all of the sensors that implement Sensor.Value
2975 // and get the path and service name associated with the sensor
2976 crow::connections::systemBus->async_method_call(
2977 [asyncResp,
2978 sensorName](const boost::system::error_code ec,
2979 const ::dbus::utility::MapperGetSubTreeResponse& subtree) {
2980 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2981 if (ec)
2982 {
2983 messages::internalError(asyncResp->asyncResp->res);
2984 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2985 << "Dbus error " << ec;
2986 return;
2987 }
2988
2989 ::dbus::utility::MapperGetSubTreeResponse::const_iterator it =
2990 std::find_if(
2991 subtree.begin(), subtree.end(),
2992 [sensorName](
2993 const std::pair<
2994 std::string,
2995 std::vector<std::pair<
2996 std::string, std::vector<std::string>>>>& object) {
2997 sdbusplus::message::object_path path(object.first);
2998 std::string name = path.filename();
2999 if (name.empty())
3000 {
3001 BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first;
3002 return false;
3003 }
3004
3005 return name == sensorName;
3006 });
3007
3008 if (it == subtree.end())
3009 {
3010 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3011 << sensorName;
3012 messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor",
3013 sensorName);
3014 return;
3015 }
3016 std::string_view sensorPath = (*it).first;
3017 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
3018 << "': " << sensorPath;
3019
Nan Zhoufe04d492022-06-22 17:10:41 +00003020 const std::shared_ptr<std::set<std::string>> sensorList =
3021 std::make_shared<std::set<std::string>>();
Nan Zhoue6bd8462022-06-01 04:35:35 +00003022
3023 sensorList->emplace(sensorPath);
3024 processSensorList(asyncResp, sensorList);
3025 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3026 },
3027 "xyz.openbmc_project.ObjectMapper",
3028 "/xyz/openbmc_project/object_mapper",
3029 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3030 "/xyz/openbmc_project/sensors", 2, interfaces);
3031}
3032
Nan Zhoubacb2162022-04-06 11:28:32 -07003033} // namespace sensors
3034
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003035inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003036{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003037 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003038 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003039 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003040 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003041}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003042
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003043inline void requestRoutesSensor(App& app)
3044{
3045 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003046 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003047 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003048 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003049}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003050
Ed Tanous1abe55e2018-09-05 08:30:59 -07003051} // namespace redfish