blob: 4d045d0b33ae4a02522a16446f00f406d1e523d1 [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080020#include "dbus_utility.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -070021#include "generated/enums/redundancy.hpp"
Matt Simmeringaaf08ac2023-10-04 08:41:01 -070022#include "generated/enums/resource.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070023#include "generated/enums/sensor.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -070024#include "generated/enums/thermal.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "query.hpp"
26#include "registries/privilege_registry.hpp"
Ed Tanous50ebd4a2023-01-19 19:03:17 -080027#include "str_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080028#include "utils/dbus_utils.hpp"
29#include "utils/json_utils.hpp"
30#include "utils/query_param.hpp"
Janet Adkins1516c212024-08-14 13:22:41 -050031#include "utils/sensor_utils.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070032
George Liue99073f2022-12-09 11:06:16 +080033#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070034#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070035#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020036#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050037
George Liu7a1dbc42022-12-07 16:03:22 +080038#include <array>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000040#include <iterator>
Ed Tanous283860f2022-08-29 14:08:50 -070041#include <limits>
Nan Zhoufe04d492022-06-22 17:10:41 +000042#include <map>
Ed Tanous3544d2a2023-08-06 18:12:20 -070043#include <ranges>
Nan Zhoufe04d492022-06-22 17:10:41 +000044#include <set>
Ed Tanous18f8f602023-07-18 10:07:23 -070045#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080046#include <string_view>
Ed Tanousb5a76932020-09-29 16:16:58 -070047#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080048#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010049
Ed Tanous1abe55e2018-09-05 08:30:59 -070050namespace redfish
51{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010052
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020053namespace sensors
54{
55namespace node
56{
57static constexpr std::string_view power = "Power";
58static constexpr std::string_view sensors = "Sensors";
59static constexpr std::string_view thermal = "Thermal";
60} // namespace node
61
Ed Tanous02da7c52022-02-27 00:09:02 -080062// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020063namespace dbus
64{
Ed Tanouscf9e4172022-12-21 09:30:16 -080065constexpr auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080066 "/xyz/openbmc_project/sensors/voltage",
67 "/xyz/openbmc_project/sensors/power"
68});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000069
Ed Tanous25b54db2024-04-17 15:40:31 -070070constexpr auto getSensorPaths(){
71 if constexpr(BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM){
72 return std::to_array<std::string_view>({
73 "/xyz/openbmc_project/sensors/power",
74 "/xyz/openbmc_project/sensors/current",
75 "/xyz/openbmc_project/sensors/airflow",
76 "/xyz/openbmc_project/sensors/humidity",
77 "/xyz/openbmc_project/sensors/voltage",
78 "/xyz/openbmc_project/sensors/fan_tach",
79 "/xyz/openbmc_project/sensors/temperature",
80 "/xyz/openbmc_project/sensors/fan_pwm",
81 "/xyz/openbmc_project/sensors/altitude",
82 "/xyz/openbmc_project/sensors/energy",
83 "/xyz/openbmc_project/sensors/utilization"});
84 } else {
85 return std::to_array<std::string_view>({"/xyz/openbmc_project/sensors/power",
86 "/xyz/openbmc_project/sensors/current",
87 "/xyz/openbmc_project/sensors/airflow",
88 "/xyz/openbmc_project/sensors/humidity",
89 "/xyz/openbmc_project/sensors/utilization"});
90}
91}
92
93constexpr auto sensorPaths = getSensorPaths();
Ed Tanous02da7c52022-02-27 00:09:02 -080094
Ed Tanouscf9e4172022-12-21 09:30:16 -080095constexpr auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080096 "/xyz/openbmc_project/sensors/fan_tach",
97 "/xyz/openbmc_project/sensors/temperature",
98 "/xyz/openbmc_project/sensors/fan_pwm"
99});
100
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000101} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -0800102// clang-format on
103
Ed Tanouscf9e4172022-12-21 09:30:16 -0800104using sensorPair =
105 std::pair<std::string_view, std::span<const std::string_view>>;
Ed Tanous02da7c52022-02-27 00:09:02 -0800106static constexpr std::array<sensorPair, 3> paths = {
Ed Tanouscf9e4172022-12-21 09:30:16 -0800107 {{node::power, dbus::powerPaths},
108 {node::sensors, dbus::sensorPaths},
109 {node::thermal, dbus::thermalPaths}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000110
Ed Tanous0ec8b832022-03-14 14:56:47 -0700111inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000112{
113 if (sensorType == "voltage")
114 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700115 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000116 }
117 if (sensorType == "power")
118 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700119 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000120 }
121 if (sensorType == "current")
122 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700123 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000124 }
125 if (sensorType == "fan_tach")
126 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700127 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000128 }
129 if (sensorType == "temperature")
130 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700131 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000132 }
133 if (sensorType == "fan_pwm" || sensorType == "utilization")
134 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700135 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000136 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600137 if (sensorType == "humidity")
138 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700139 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600140 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000141 if (sensorType == "altitude")
142 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700143 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000144 }
145 if (sensorType == "airflow")
146 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700147 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000148 }
149 if (sensorType == "energy")
150 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700151 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000152 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700153 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000154}
155
Ed Tanous1d7c0052022-08-09 12:32:26 -0700156inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000157{
158 if (sensorType == "voltage")
159 {
160 return "V";
161 }
162 if (sensorType == "power")
163 {
164 return "W";
165 }
166 if (sensorType == "current")
167 {
168 return "A";
169 }
170 if (sensorType == "fan_tach")
171 {
172 return "RPM";
173 }
174 if (sensorType == "temperature")
175 {
176 return "Cel";
177 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600178 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
179 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000180 {
181 return "%";
182 }
183 if (sensorType == "altitude")
184 {
185 return "m";
186 }
187 if (sensorType == "airflow")
188 {
189 return "cft_i/min";
190 }
191 if (sensorType == "energy")
192 {
193 return "J";
194 }
195 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200196}
197} // namespace sensors
198
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100199/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200200 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100201 * Gathers data needed for response processing after async calls are done
202 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700203class SensorsAsyncResp
204{
205 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200206 using DataCompleteCb = std::function<void(
207 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000208 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200209
210 struct SensorData
211 {
212 const std::string name;
213 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200214 const std::string dbusPath;
215 };
216
Ed Tanous8a592812022-06-04 09:06:59 -0700217 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800218 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800219 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800220 std::string_view subNode) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400221 asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
222 chassisSubNode(subNode), efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500223 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200224
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200225 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700226 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800227 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800228 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800229 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200230 DataCompleteCb&& creationComplete) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400231 asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
232 chassisSubNode(subNode), efficientExpand(false),
233 metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200234 dataComplete{std::move(creationComplete)}
235 {}
236
Nan Zhou928fefb2022-03-28 08:45:00 -0700237 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700238 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700239 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800240 std::span<const std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700241 const std::string_view& subNode, bool efficientExpandIn) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400242 asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
243 chassisSubNode(subNode), efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700244 {}
245
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246 ~SensorsAsyncResp()
247 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800248 if (asyncResp->res.result() ==
249 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700250 {
251 // Reset the json object to clear out any data that made it in
252 // before the error happened todo(ed) handle error condition with
253 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800254 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700255 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200256
257 if (dataComplete && metadata)
258 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000259 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800260 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200261 {
262 for (auto& sensor : *metadata)
263 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700264 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200265 }
266 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800267 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200268 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700269 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100270
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800271 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
272 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
273 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
274 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
275
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200276 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700277 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200278 {
279 if (metadata)
280 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700281 metadata->emplace_back(SensorData{
282 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200283 }
284 }
285
286 void updateUri(const std::string& name, const std::string& uri)
287 {
288 if (metadata)
289 {
290 for (auto& sensor : *metadata)
291 {
292 if (sensor.name == name)
293 {
294 sensor.uri = uri;
295 }
296 }
297 }
298 }
299
zhanghch058d1b46d2021-04-01 11:18:24 +0800300 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200301 const std::string chassisId;
Ed Tanouscf9e4172022-12-21 09:30:16 -0800302 const std::span<const std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200303 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700304 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200305
306 private:
307 std::optional<std::vector<SensorData>> metadata;
308 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100309};
310
311/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500312 * Possible states for physical inventory leds
313 */
314enum class LedState
315{
316 OFF,
317 ON,
318 BLINK,
319 UNKNOWN
320};
321
322/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500323 * D-Bus inventory item associated with one or more sensors.
324 */
325class InventoryItem
326{
327 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700328 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500329 {
330 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800331 sdbusplus::message::object_path path(objectPath);
332 name = path.filename();
333 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500334 {
Ed Tanous62598e32023-07-17 17:06:25 -0700335 BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500336 }
337 }
338
339 std::string objectPath;
340 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800341 bool isPresent = true;
342 bool isFunctional = true;
343 bool isPowerSupply = false;
344 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500345 std::string manufacturer;
346 std::string model;
347 std::string partNumber;
348 std::string serialNumber;
349 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500350 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800351 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500352};
353
354/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530355 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200356 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100357 * @param sensorNames Sensors retrieved from chassis
358 * @param callback Callback for processing gathered connections
359 */
360template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530361void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000362 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000363 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530364 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700365{
Ed Tanous62598e32023-07-17 17:06:25 -0700366 BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700367 const std::string path = "/xyz/openbmc_project/sensors";
George Liue99073f2022-12-09 11:06:16 +0800368 constexpr std::array<std::string_view, 1> interfaces = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700369 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100370
George Liue99073f2022-12-09 11:06:16 +0800371 // Make call to ObjectMapper to find all sensors objects
372 dbus::utility::getSubTree(
373 path, 2, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700374 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
George Liue99073f2022-12-09 11:06:16 +0800375 sensorNames](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700376 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400377 // Response handler for parsing objects subtree
378 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
379 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400381 messages::internalError(sensorsAsyncResp->asyncResp->res);
382 BMCWEB_LOG_ERROR(
383 "getObjectsWithConnection resp_handler: Dbus error {}", ec);
384 return;
385 }
386
387 BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
388
389 // Make unique list of connections only for requested sensor types
390 // and found in the chassis
391 std::set<std::string> connections;
392 std::set<std::pair<std::string, std::string>> objectsWithConnection;
393
394 BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
395 for (const std::string& tsensor : *sensorNames)
396 {
397 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
398 }
399
400 for (const std::pair<std::string,
401 std::vector<std::pair<
402 std::string, std::vector<std::string>>>>&
403 object : subtree)
404 {
405 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700406 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400407 for (const std::pair<std::string, std::vector<std::string>>&
408 objData : object.second)
409 {
410 BMCWEB_LOG_DEBUG("Adding connection: {}",
411 objData.first);
412 connections.insert(objData.first);
413 objectsWithConnection.insert(
414 std::make_pair(object.first, objData.first));
415 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700416 }
417 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400418 BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
419 callback(std::move(connections), std::move(objectsWithConnection));
420 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
421 });
Ed Tanous62598e32023-07-17 17:06:25 -0700422 BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530423}
424
425/**
426 * @brief Create connections necessary for sensors
427 * @param SensorsAsyncResp Pointer to object holding response data
428 * @param sensorNames Sensors retrieved from chassis
429 * @param callback Callback for processing gathered connections
430 */
431template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000432void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
433 const std::shared_ptr<std::set<std::string>> sensorNames,
434 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530435{
436 auto objectsWithConnectionCb =
Ed Tanous8cb2c022024-03-27 16:31:46 -0700437 [callback = std::forward<Callback>(callback)](
438 const std::set<std::string>& connections,
439 const std::set<std::pair<std::string, std::string>>&
440 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000441 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530442 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100443}
444
445/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700446 * @brief Shrinks the list of sensors for processing
447 * @param SensorsAysncResp The class holding the Redfish response
448 * @param allSensors A list of all the sensors associated to the
449 * chassis element (i.e. baseboard, front panel, etc...)
450 * @param activeSensors A list that is a reduction of the incoming
451 * allSensors list. Eliminate Thermal sensors when a Power request is
452 * made, and eliminate Power sensors when a Thermal request is made.
453 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000454inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700455 crow::Response& res, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800456 std::span<const std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700457 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000458 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700459{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700460 if ((allSensors == nullptr) || (activeSensors == nullptr))
461 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700462 messages::resourceNotFound(res, chassisSubNode,
463 chassisSubNode == sensors::node::thermal
464 ? "Temperatures"
465 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700466
467 return;
468 }
469 if (allSensors->empty())
470 {
471 // Nothing to do, the activeSensors object is also empty
472 return;
473 }
474
Ed Tanous7f1cc262022-08-09 13:33:57 -0700475 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700476 {
477 for (const std::string& sensor : *allSensors)
478 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700479 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700480 {
481 activeSensors->emplace(sensor);
482 }
483 }
484 }
485}
486
Ed Tanous7f1cc262022-08-09 13:33:57 -0700487/*
488 *Populates the top level collection for a given subnode. Populates
489 *SensorCollection, Power, or Thermal schemas.
490 *
491 * */
492inline void populateChassisNode(nlohmann::json& jsonValue,
493 std::string_view chassisSubNode)
494{
495 if (chassisSubNode == sensors::node::power)
496 {
497 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
498 }
499 else if (chassisSubNode == sensors::node::thermal)
500 {
501 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
502 jsonValue["Fans"] = nlohmann::json::array();
503 jsonValue["Temperatures"] = nlohmann::json::array();
504 }
505 else if (chassisSubNode == sensors::node::sensors)
506 {
507 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
508 jsonValue["Description"] = "Collection of Sensors for this Chassis";
509 jsonValue["Members"] = nlohmann::json::array();
510 jsonValue["Members@odata.count"] = 0;
511 }
512
513 if (chassisSubNode != sensors::node::sensors)
514 {
515 jsonValue["Id"] = chassisSubNode;
516 }
517 jsonValue["Name"] = chassisSubNode;
518}
519
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700520/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100521 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200522 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100523 * @param callback Callback for next step in gathered sensor processing
524 */
525template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700526void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
527 std::string_view chassisId, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800528 std::span<const std::string_view> sensorTypes,
529 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530{
Ed Tanous62598e32023-07-17 17:06:25 -0700531 BMCWEB_LOG_DEBUG("getChassis enter");
George Liu7a1dbc42022-12-07 16:03:22 +0800532 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700533 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500534 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800535
536 // Get the Chassis Collection
537 dbus::utility::getSubTreePaths(
538 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700539 [callback = std::forward<Callback>(callback), asyncResp,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700540 chassisIdStr{std::string(chassisId)},
541 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800542 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400544 BMCWEB_LOG_DEBUG("getChassis respHandler enter");
545 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400547 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
548 messages::internalError(asyncResp->res);
549 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400551 const std::string* chassisPath = nullptr;
552 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400554 sdbusplus::message::object_path path(chassis);
555 std::string chassisName = path.filename();
556 if (chassisName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700557 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400558 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
559 continue;
560 }
561 if (chassisName == chassisIdStr)
562 {
563 chassisPath = &chassis;
564 break;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700565 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700566 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400567 if (chassisPath == nullptr)
568 {
569 messages::resourceNotFound(asyncResp->res, "Chassis",
570 chassisIdStr);
571 return;
572 }
573 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
574
575 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
576 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
577
578 // Get the list of all sensors for this Chassis element
579 std::string sensorPath = *chassisPath + "/all_sensors";
580 dbus::utility::getAssociationEndPoints(
581 sensorPath,
582 [asyncResp, chassisSubNode, sensorTypes,
583 callback = std::forward<const Callback>(callback)](
584 const boost::system::error_code& ec2,
585 const dbus::utility::MapperEndPoints& nodeSensorList) {
586 if (ec2)
587 {
588 if (ec2.value() != EBADR)
589 {
590 messages::internalError(asyncResp->res);
591 return;
592 }
593 }
594 const std::shared_ptr<std::set<std::string>>
595 culledSensorList =
596 std::make_shared<std::set<std::string>>();
597 reduceSensorList(asyncResp->res, chassisSubNode,
598 sensorTypes, &nodeSensorList,
599 culledSensorList);
600 BMCWEB_LOG_DEBUG("Finishing with {}",
601 culledSensorList->size());
602 callback(culledSensorList);
603 });
George Liu7a1dbc42022-12-07 16:03:22 +0800604 });
Ed Tanous62598e32023-07-17 17:06:25 -0700605 BMCWEB_LOG_DEBUG("getChassis exit");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100606}
607
608/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500609 * @brief Returns the Redfish State value for the specified inventory item.
610 * @param inventoryItem D-Bus inventory item associated with a sensor.
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700611 * @param sensorAvailable Boolean representing if D-Bus sensor is marked as
612 * available.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500613 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700614 */
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700615inline resource::State getState(const InventoryItem* inventoryItem,
616 const bool sensorAvailable)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500617{
618 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
619 {
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700620 return resource::State::Absent;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500621 }
James Feist34dd1792019-05-17 14:10:54 -0700622
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700623 if (!sensorAvailable)
624 {
625 return resource::State::UnavailableOffline;
626 }
627
628 return resource::State::Enabled;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500629}
630
631/**
632 * @brief Returns the Redfish Health value for the specified sensor.
633 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700634 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500635 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
636 * be nullptr if no associated inventory item was found.
637 * @return Health value for sensor.
638 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700639inline std::string getHealth(nlohmann::json& sensorJson,
640 const dbus::utility::DBusPropertiesMap& valuesDict,
641 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700642{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500643 // Get current health value (if any) in the sensor JSON object. Some JSON
644 // objects contain multiple sensors (such as PowerSupplies). We want to set
645 // the overall health to be the most severe of any of the sensors.
646 std::string currentHealth;
647 auto statusIt = sensorJson.find("Status");
648 if (statusIt != sensorJson.end())
649 {
650 auto healthIt = statusIt->find("Health");
651 if (healthIt != statusIt->end())
652 {
653 std::string* health = healthIt->get_ptr<std::string*>();
654 if (health != nullptr)
655 {
656 currentHealth = *health;
657 }
658 }
659 }
660
661 // If current health in JSON object is already Critical, return that. This
662 // should override the sensor health, which might be less severe.
663 if (currentHealth == "Critical")
664 {
665 return "Critical";
666 }
667
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200668 const bool* criticalAlarmHigh = nullptr;
669 const bool* criticalAlarmLow = nullptr;
670 const bool* warningAlarmHigh = nullptr;
671 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800672
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200673 const bool success = sdbusplus::unpackPropertiesNoThrow(
674 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
675 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
676 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
677 warningAlarmLow);
678
679 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700680 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200681 // Check if sensor has critical threshold alarm
682 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
683 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700684 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200685 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700686 }
687 }
688
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500689 // Check if associated inventory item is not functional
690 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
691 {
692 return "Critical";
693 }
694
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200695 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500696 // should override the sensor status, which might be less severe.
697 if (currentHealth == "Warning")
698 {
699 return "Warning";
700 }
701
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200702 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700703 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200704 // Check if sensor has warning threshold alarm
705 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
706 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700707 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200708 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700709 }
710 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500711
James Feist34dd1792019-05-17 14:10:54 -0700712 return "OK";
713}
714
Ed Tanous23a21a12020-07-25 04:45:05 +0000715inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500716 const InventoryItem* inventoryItem)
717{
718 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
719 {
720 switch (inventoryItem->ledState)
721 {
722 case LedState::OFF:
Ed Tanous539d8c62024-06-19 14:38:27 -0700723 sensorJson["IndicatorLED"] = resource::IndicatorLED::Off;
Anthony Wilsond5005492019-07-31 16:34:17 -0500724 break;
725 case LedState::ON:
Ed Tanous539d8c62024-06-19 14:38:27 -0700726 sensorJson["IndicatorLED"] = resource::IndicatorLED::Lit;
Anthony Wilsond5005492019-07-31 16:34:17 -0500727 break;
728 case LedState::BLINK:
Ed Tanous539d8c62024-06-19 14:38:27 -0700729 sensorJson["IndicatorLED"] = resource::IndicatorLED::Blinking;
Anthony Wilsond5005492019-07-31 16:34:17 -0500730 break;
Ed Tanous4da04902024-03-19 11:32:44 -0700731 default:
Anthony Wilsond5005492019-07-31 16:34:17 -0500732 break;
733 }
734 }
735}
736
James Feist34dd1792019-05-17 14:10:54 -0700737/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100738 * @brief Builds a json sensor representation of a sensor.
739 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500740 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100741 * build
Ed Tanous8ece0e42024-01-02 13:16:50 -0800742 * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
Ed Tanous1d7c0052022-08-09 12:32:26 -0700743 * @param propertiesDict A dictionary of the properties to build the sensor
744 * from.
745 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500746 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
747 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100748 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700749inline void objectPropertiesToJson(
750 std::string_view sensorName, std::string_view sensorType,
751 std::string_view chassisSubNode,
752 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000753 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700754{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700755 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500756 {
Janet Adkins1516c212024-08-14 13:22:41 -0500757 std::string subNodeEscaped =
758 redfish::sensor_utils::getSensorId(sensorName, sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700759 // For sensors in SensorCollection we set Id instead of MemberId,
760 // including power sensors.
Ed Tanousc1d019a2022-08-06 09:36:06 -0700761 sensorJson["Id"] = std::move(subNodeEscaped);
762
Ed Tanous1d7c0052022-08-09 12:32:26 -0700763 std::string sensorNameEs(sensorName);
764 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
765 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500766 }
767 else if (sensorType != "power")
768 {
769 // Set MemberId and Name for non-power sensors. For PowerSupplies and
770 // PowerControl, those properties have more general values because
771 // multiple sensors can be stored in the same JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700772 std::string sensorNameEs(sensorName);
773 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
774 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500775 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700776
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700777 const bool* checkAvailable = nullptr;
778 bool available = true;
779 const bool success = sdbusplus::unpackPropertiesNoThrow(
780 dbus_utils::UnpackErrorPrinter(), propertiesDict, "Available",
781 checkAvailable);
782 if (!success)
783 {
784 messages::internalError();
785 }
786 if (checkAvailable != nullptr)
787 {
788 available = *checkAvailable;
789 }
790
791 sensorJson["Status"]["State"] = getState(inventoryItem, available);
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400792 sensorJson["Status"]["Health"] =
793 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700794
795 // Parameter to set to override the type we get from dbus, and force it to
796 // int, regardless of what is available. This is used for schemas like fan,
797 // that require integers, not floats.
798 bool forceToInt = false;
799
Anthony Wilson3929aca2019-07-19 15:42:33 -0500800 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700801 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500802 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000803 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000804
Ed Tanous0ec8b832022-03-14 14:56:47 -0700805 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
806 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500807 {
Ed Tanous62598e32023-07-17 17:06:25 -0700808 BMCWEB_LOG_ERROR("Redfish cannot map reading type for {}",
809 sensorType);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500810 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000811 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500812 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000813 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500814 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000815
Ed Tanous1d7c0052022-08-09 12:32:26 -0700816 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000817 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200818 {
Ed Tanous62598e32023-07-17 17:06:25 -0700819 BMCWEB_LOG_ERROR("Redfish cannot map reading unit for {}",
820 sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000821 }
822 else
823 {
824 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200825 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500826 }
827 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700828 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500829 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000830 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700831 // TODO(ed) Documentation says that path should be type fan_tach,
832 // implementation seems to implement fan
833 }
834 else if (sensorType == "fan" || sensorType == "fan_tach")
835 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500836 unit = "/Reading"_json_pointer;
Ed Tanous539d8c62024-06-19 14:38:27 -0700837 sensorJson["ReadingUnits"] = thermal::ReadingUnits::RPM;
Ed Tanous81ce6092020-12-17 16:54:55 +0000838 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
839 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700840 forceToInt = true;
841 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700842 else if (sensorType == "fan_pwm")
843 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500844 unit = "/Reading"_json_pointer;
Ed Tanous539d8c62024-06-19 14:38:27 -0700845 sensorJson["ReadingUnits"] = thermal::ReadingUnits::Percent;
Ed Tanous81ce6092020-12-17 16:54:55 +0000846 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
847 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700848 forceToInt = true;
849 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700850 else if (sensorType == "voltage")
851 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500852 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000853 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700854 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700855 else if (sensorType == "power")
856 {
Ed Tanous18f8f602023-07-18 10:07:23 -0700857 std::string lower;
858 std::ranges::transform(sensorName, std::back_inserter(lower),
859 bmcweb::asciiToLower);
860 if (lower == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000861 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000862 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500863 // Put multiple "sensors" into a single PowerControl, so have
864 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000865 sensorJson["MemberId"] = "0";
866 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500867 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000868 }
Ed Tanous18f8f602023-07-18 10:07:23 -0700869 else if (lower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700870 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500871 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700872 }
873 else
874 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500875 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700876 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700877 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700878 else
879 {
Ed Tanous62598e32023-07-17 17:06:25 -0700880 BMCWEB_LOG_ERROR("Redfish cannot map object type for {}", sensorName);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 return;
882 }
883 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500884 std::vector<
885 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
886 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887 properties.reserve(7);
888
889 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600890
Ed Tanous1d7c0052022-08-09 12:32:26 -0700891 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500892 {
893 properties.emplace_back(
894 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
895 "/Thresholds/UpperCaution/Reading"_json_pointer);
896 properties.emplace_back(
897 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
898 "/Thresholds/LowerCaution/Reading"_json_pointer);
899 properties.emplace_back(
900 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
901 "/Thresholds/UpperCritical/Reading"_json_pointer);
902 properties.emplace_back(
903 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
904 "/Thresholds/LowerCritical/Reading"_json_pointer);
905 }
906 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600907 {
908 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500909 "WarningHigh",
910 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600911 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 "WarningLow",
913 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600914 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500915 "CriticalHigh",
916 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600917 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500918 "CriticalLow",
919 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600920 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700921
Ed Tanous2474adf2018-09-05 16:31:16 -0700922 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
923
Ed Tanous1d7c0052022-08-09 12:32:26 -0700924 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500925 {
926 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500927 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500928 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500929 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800930 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
931 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500932 }
933 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934 {
935 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500936 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700937 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500938 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500940 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 {
942 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500943 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700944 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500945 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700946 }
947
Anthony Wilson3929aca2019-07-19 15:42:33 -0500948 for (const std::tuple<const char*, const char*,
949 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700950 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700951 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700952 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700953 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700954 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800955 continue;
956 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700957
958 // The property we want to set may be nested json, so use
959 // a json_pointer for easy indexing into the json structure.
960 const nlohmann::json::json_pointer& key = std::get<2>(p);
961
Ed Tanous1d7c0052022-08-09 12:32:26 -0700962 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700963 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800964 {
Ed Tanous62598e32023-07-17 17:06:25 -0700965 BMCWEB_LOG_ERROR("Got value interface that wasn't double");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700966 continue;
967 }
Ed Tanous283860f2022-08-29 14:08:50 -0700968 if (!std::isfinite(*doubleValue))
969 {
970 if (valueName == "Value")
971 {
972 // Readings are allowed to be NAN for unavailable; coerce
973 // them to null in the json response.
974 sensorJson[key] = nullptr;
975 continue;
976 }
Ed Tanous62598e32023-07-17 17:06:25 -0700977 BMCWEB_LOG_WARNING("Sensor value for {} was unexpectedly {}",
978 valueName, *doubleValue);
Ed Tanous283860f2022-08-29 14:08:50 -0700979 continue;
980 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700981 if (forceToInt)
982 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700983 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700984 }
985 else
986 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700987 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700988 }
989 }
990 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100991}
992
Ed Tanous1d7c0052022-08-09 12:32:26 -0700993/**
994 * @brief Builds a json sensor representation of a sensor.
995 * @param sensorName The name of the sensor to be built
996 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
997 * build
Ed Tanous8ece0e42024-01-02 13:16:50 -0800998 * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
Ed Tanous1d7c0052022-08-09 12:32:26 -0700999 * @param interfacesDict A dictionary of the interfaces and properties of said
1000 * interfaces to be built from
1001 * @param sensorJson The json object to fill
1002 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1003 * be nullptr if no associated inventory item was found.
1004 */
1005inline void objectInterfacesToJson(
1006 const std::string& sensorName, const std::string& sensorType,
1007 const std::string& chassisSubNode,
Michael Shen80f79a42023-08-24 13:41:53 +00001008 const dbus::utility::DBusInterfacesMap& interfacesDict,
Ed Tanous1d7c0052022-08-09 12:32:26 -07001009 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1010{
Ed Tanous1d7c0052022-08-09 12:32:26 -07001011 for (const auto& [interface, valuesDict] : interfacesDict)
1012 {
1013 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1014 valuesDict, sensorJson, inventoryItem);
1015 }
Ed Tanous62598e32023-07-17 17:06:25 -07001016 BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
Ed Tanous1d7c0052022-08-09 12:32:26 -07001017}
1018
Ed Tanousb5a76932020-09-29 16:16:58 -07001019inline void populateFanRedundancy(
1020 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001021{
George Liue99073f2022-12-09 11:06:16 +08001022 constexpr std::array<std::string_view, 1> interfaces = {
1023 "xyz.openbmc_project.Control.FanRedundancy"};
1024 dbus::utility::getSubTree(
1025 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001026 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +08001027 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001028 const dbus::utility::MapperGetSubTreeResponse& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001029 if (ec)
James Feist8bd25cc2019-03-15 15:14:00 -07001030 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001031 return; // don't have to have this interface
James Feist8bd25cc2019-03-15 15:14:00 -07001032 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001033 for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
1034 pathPair : resp)
1035 {
1036 const std::string& path = pathPair.first;
1037 const dbus::utility::MapperServiceMap& objDict =
1038 pathPair.second;
1039 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001040 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001041 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001042 }
1043
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001044 const std::string& owner = objDict.begin()->first;
1045 dbus::utility::getAssociationEndPoints(
1046 path + "/chassis",
1047 [path, owner, sensorsAsyncResp](
1048 const boost::system::error_code& ec2,
1049 const dbus::utility::MapperEndPoints& endpoints) {
1050 if (ec2)
James Feist8bd25cc2019-03-15 15:14:00 -07001051 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001052 return; // if they don't have an association we
1053 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001054 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001055 auto found = std::ranges::find_if(
1056 endpoints,
1057 [sensorsAsyncResp](const std::string& entry) {
1058 return entry.find(
1059 sensorsAsyncResp->chassisId) !=
1060 std::string::npos;
1061 });
1062
1063 if (found == endpoints.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001064 {
1065 return;
1066 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001067 sdbusplus::asio::getAllProperties(
1068 *crow::connections::systemBus, owner, path,
1069 "xyz.openbmc_project.Control.FanRedundancy",
1070 [path, sensorsAsyncResp](
1071 const boost::system::error_code& ec3,
1072 const dbus::utility::DBusPropertiesMap& ret) {
1073 if (ec3)
1074 {
1075 return; // don't have to have this
1076 // interface
1077 }
James Feist8bd25cc2019-03-15 15:14:00 -07001078
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001079 const uint8_t* allowedFailures = nullptr;
1080 const std::vector<std::string>* collection =
1081 nullptr;
1082 const std::string* status = nullptr;
James Feist8bd25cc2019-03-15 15:14:00 -07001083
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001084 const bool success =
1085 sdbusplus::unpackPropertiesNoThrow(
1086 dbus_utils::UnpackErrorPrinter(), ret,
1087 "AllowedFailures", allowedFailures,
1088 "Collection", collection, "Status",
1089 status);
James Feist8bd25cc2019-03-15 15:14:00 -07001090
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001091 if (!success)
1092 {
1093 messages::internalError(
1094 sensorsAsyncResp->asyncResp->res);
1095 return;
1096 }
1097
1098 if (allowedFailures == nullptr ||
1099 collection == nullptr || status == nullptr)
1100 {
1101 BMCWEB_LOG_ERROR(
1102 "Invalid redundancy interface");
1103 messages::internalError(
1104 sensorsAsyncResp->asyncResp->res);
1105 return;
1106 }
1107
1108 sdbusplus::message::object_path objectPath(
1109 path);
1110 std::string name = objectPath.filename();
1111 if (name.empty())
1112 {
1113 // this should be impossible
1114 messages::internalError(
1115 sensorsAsyncResp->asyncResp->res);
1116 return;
1117 }
1118 std::ranges::replace(name, '_', ' ');
1119
1120 std::string health;
1121
1122 if (status->ends_with("Full"))
1123 {
1124 health = "OK";
1125 }
1126 else if (status->ends_with("Degraded"))
1127 {
1128 health = "Warning";
1129 }
1130 else
1131 {
1132 health = "Critical";
1133 }
1134 nlohmann::json::array_t redfishCollection;
1135 const auto& fanRedfish =
1136 sensorsAsyncResp->asyncResp->res
1137 .jsonValue["Fans"];
1138 for (const std::string& item : *collection)
1139 {
1140 sdbusplus::message::object_path itemPath(
1141 item);
1142 std::string itemName = itemPath.filename();
1143 if (itemName.empty())
1144 {
1145 continue;
1146 }
1147 /*
1148 todo(ed): merge patch that fixes the names
1149 std::replace(itemName.begin(),
1150 itemName.end(), '_', ' ');*/
1151 auto schemaItem = std::ranges::find_if(
1152 fanRedfish,
1153 [itemName](const nlohmann::json& fan) {
1154 return fan["Name"] == itemName;
1155 });
1156 if (schemaItem != fanRedfish.end())
1157 {
1158 nlohmann::json::object_t collectionId;
1159 collectionId["@odata.id"] =
1160 (*schemaItem)["@odata.id"];
1161 redfishCollection.emplace_back(
1162 std::move(collectionId));
1163 }
1164 else
1165 {
1166 BMCWEB_LOG_ERROR(
1167 "failed to find fan in schema");
1168 messages::internalError(
1169 sensorsAsyncResp->asyncResp->res);
1170 return;
1171 }
1172 }
1173
1174 size_t minNumNeeded =
1175 collection->empty()
1176 ? 0
1177 : collection->size() - *allowedFailures;
1178 nlohmann::json& jResp =
1179 sensorsAsyncResp->asyncResp->res
1180 .jsonValue["Redundancy"];
1181
1182 nlohmann::json::object_t redundancy;
1183 boost::urls::url url = boost::urls::format(
1184 "/redfish/v1/Chassis/{}/{}",
1185 sensorsAsyncResp->chassisId,
1186 sensorsAsyncResp->chassisSubNode);
1187 url.set_fragment(
1188 ("/Redundancy"_json_pointer / jResp.size())
1189 .to_string());
1190 redundancy["@odata.id"] = std::move(url);
1191 redundancy["@odata.type"] =
1192 "#Redundancy.v1_3_2.Redundancy";
1193 redundancy["MinNumNeeded"] = minNumNeeded;
1194 redundancy["Mode"] =
1195 redundancy::RedundancyType::NPlusM;
1196 redundancy["Name"] = name;
1197 redundancy["RedundancySet"] = redfishCollection;
1198 redundancy["Status"]["Health"] = health;
1199 redundancy["Status"]["State"] =
1200 resource::State::Enabled;
1201
1202 jResp.emplace_back(std::move(redundancy));
1203 });
1204 });
1205 }
1206 });
James Feist8bd25cc2019-03-15 15:14:00 -07001207}
1208
Ed Tanousb5a76932020-09-29 16:16:58 -07001209inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001210 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001211{
zhanghch058d1b46d2021-04-01 11:18:24 +08001212 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001213 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001214 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001215 {
1216 sensorHeaders = {"Voltages", "PowerSupplies"};
1217 }
1218 for (const std::string& sensorGroup : sensorHeaders)
1219 {
1220 nlohmann::json::iterator entry = response.find(sensorGroup);
1221 if (entry != response.end())
1222 {
1223 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001224 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001225 return c1["Name"] < c2["Name"];
1226 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001227
1228 // add the index counts to the end of each entry
1229 size_t count = 0;
1230 for (nlohmann::json& sensorJson : *entry)
1231 {
1232 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1233 if (odata == sensorJson.end())
1234 {
1235 continue;
1236 }
1237 std::string* value = odata->get_ptr<std::string*>();
1238 if (value != nullptr)
1239 {
Willy Tueddfc432022-09-26 16:46:38 +00001240 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001241 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001242 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001243 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001244 }
1245 }
1246 }
1247 }
1248}
1249
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001250/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001251 * @brief Finds the inventory item with the specified object path.
1252 * @param inventoryItems D-Bus inventory items associated with sensors.
1253 * @param invItemObjPath D-Bus object path of inventory item.
1254 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001255 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001256inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001257 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001258 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001259{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001260 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001261 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001262 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001263 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001264 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001265 }
1266 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001267 return nullptr;
1268}
1269
1270/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001271 * @brief Finds the inventory item associated with the specified sensor.
1272 * @param inventoryItems D-Bus inventory items associated with sensors.
1273 * @param sensorObjPath D-Bus object path of sensor.
1274 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001275 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001276inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001277 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001278 const std::string& sensorObjPath)
1279{
1280 for (InventoryItem& inventoryItem : *inventoryItems)
1281 {
Ed Tanousdb0d36e2023-06-30 10:37:05 -07001282 if (inventoryItem.sensors.contains(sensorObjPath))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001283 {
1284 return &inventoryItem;
1285 }
1286 }
1287 return nullptr;
1288}
1289
1290/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001291 * @brief Finds the inventory item associated with the specified led path.
1292 * @param inventoryItems D-Bus inventory items associated with sensors.
1293 * @param ledObjPath D-Bus object path of led.
1294 * @return Inventory item within vector, or nullptr if no match found.
1295 */
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001296inline InventoryItem* findInventoryItemForLed(
1297 std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath)
Anthony Wilsond5005492019-07-31 16:34:17 -05001298{
1299 for (InventoryItem& inventoryItem : inventoryItems)
1300 {
1301 if (inventoryItem.ledObjectPath == ledObjPath)
1302 {
1303 return &inventoryItem;
1304 }
1305 }
1306 return nullptr;
1307}
1308
1309/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001310 * @brief Adds inventory item and associated sensor to specified vector.
1311 *
1312 * Adds a new InventoryItem to the vector if necessary. Searches for an
1313 * existing InventoryItem with the specified object path. If not found, one is
1314 * added to the vector.
1315 *
1316 * Next, the specified sensor is added to the set of sensors associated with the
1317 * InventoryItem.
1318 *
1319 * @param inventoryItems D-Bus inventory items associated with sensors.
1320 * @param invItemObjPath D-Bus object path of inventory item.
1321 * @param sensorObjPath D-Bus object path of sensor
1322 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001323inline void addInventoryItem(
1324 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1325 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001326{
1327 // Look for inventory item in vector
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001328 InventoryItem* inventoryItem =
1329 findInventoryItem(inventoryItems, invItemObjPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001330
1331 // If inventory item doesn't exist in vector, add it
1332 if (inventoryItem == nullptr)
1333 {
1334 inventoryItems->emplace_back(invItemObjPath);
1335 inventoryItem = &(inventoryItems->back());
1336 }
1337
1338 // Add sensor to set of sensors associated with inventory item
1339 inventoryItem->sensors.emplace(sensorObjPath);
1340}
1341
1342/**
1343 * @brief Stores D-Bus data in the specified inventory item.
1344 *
1345 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1346 * specified InventoryItem.
1347 *
1348 * This data is later used to provide sensor property values in the JSON
1349 * response.
1350 *
1351 * @param inventoryItem Inventory item where data will be stored.
1352 * @param interfacesDict Map containing D-Bus interfaces and their properties
1353 * for the specified inventory item.
1354 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001355inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001356 InventoryItem& inventoryItem,
Michael Shen80f79a42023-08-24 13:41:53 +00001357 const dbus::utility::DBusInterfacesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001358{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001359 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001360
Ed Tanous9eb808c2022-01-25 10:19:23 -08001361 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001362 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001363 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001364 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001365 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001366 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001367 if (name == "Present")
1368 {
1369 const bool* value = std::get_if<bool>(&dbusValue);
1370 if (value != nullptr)
1371 {
1372 inventoryItem.isPresent = *value;
1373 }
1374 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001375 }
1376 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001377 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001378
Ed Tanous711ac7a2021-12-20 09:34:41 -08001379 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001380 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001381 inventoryItem.isPowerSupply = true;
1382 }
1383
1384 // Get properties from Inventory.Decorator.Asset interface
1385 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1386 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001387 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001388 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001389 if (name == "Manufacturer")
1390 {
1391 const std::string* value =
1392 std::get_if<std::string>(&dbusValue);
1393 if (value != nullptr)
1394 {
1395 inventoryItem.manufacturer = *value;
1396 }
1397 }
1398 if (name == "Model")
1399 {
1400 const std::string* value =
1401 std::get_if<std::string>(&dbusValue);
1402 if (value != nullptr)
1403 {
1404 inventoryItem.model = *value;
1405 }
1406 }
1407 if (name == "SerialNumber")
1408 {
1409 const std::string* value =
1410 std::get_if<std::string>(&dbusValue);
1411 if (value != nullptr)
1412 {
1413 inventoryItem.serialNumber = *value;
1414 }
1415 }
1416 if (name == "PartNumber")
1417 {
1418 const std::string* value =
1419 std::get_if<std::string>(&dbusValue);
1420 if (value != nullptr)
1421 {
1422 inventoryItem.partNumber = *value;
1423 }
1424 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001425 }
1426 }
1427
Ed Tanous711ac7a2021-12-20 09:34:41 -08001428 if (interface ==
1429 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001430 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001431 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001432 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001433 if (name == "Functional")
1434 {
1435 const bool* value = std::get_if<bool>(&dbusValue);
1436 if (value != nullptr)
1437 {
1438 inventoryItem.isFunctional = *value;
1439 }
1440 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001441 }
1442 }
1443 }
1444}
1445
1446/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001447 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001448 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001449 * Uses the specified connections (services) to obtain D-Bus data for inventory
1450 * items associated with sensors. Stores the resulting data in the
1451 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001452 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001453 * This data is later used to provide sensor property values in the JSON
1454 * response.
1455 *
1456 * Finds the inventory item data asynchronously. Invokes callback when data has
1457 * been obtained.
1458 *
1459 * The callback must have the following signature:
1460 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001461 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001462 * @endcode
1463 *
1464 * This function is called recursively, obtaining data asynchronously from one
1465 * connection in each call. This ensures the callback is not invoked until the
1466 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001467 *
1468 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001469 * @param inventoryItems D-Bus inventory items associated with sensors.
1470 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001471 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001472 * @param callback Callback to invoke when inventory data has been obtained.
1473 * @param invConnectionsIndex Current index in invConnections. Only specified
1474 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001475 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001476template <typename Callback>
1477static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001478 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001479 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001480 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1481 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001482{
Ed Tanous62598e32023-07-17 17:06:25 -07001483 BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001484
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001485 // If no more connections left, call callback
1486 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001487 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001488 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001489 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001490 return;
1491 }
1492
1493 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001494 auto it = invConnections->begin();
1495 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 if (it != invConnections->end())
1497 {
1498 const std::string& invConnection = *it;
1499
George Liu5eb468d2023-06-20 17:03:24 +08001500 // Get all object paths and their interfaces for current connection
1501 sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
1502 dbus::utility::getManagedObjects(
1503 invConnection, path,
1504 [sensorsAsyncResp, inventoryItems, invConnections,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001505 callback = std::forward<Callback>(callback), invConnectionsIndex](
George Liu5eb468d2023-06-20 17:03:24 +08001506 const boost::system::error_code& ec,
1507 const dbus::utility::ManagedObjectType& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001508 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
1509 if (ec)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001510 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001511 BMCWEB_LOG_ERROR(
1512 "getInventoryItemsData respHandler DBus error {}", ec);
1513 messages::internalError(sensorsAsyncResp->asyncResp->res);
1514 return;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001515 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001516
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001517 // Loop through returned object paths
1518 for (const auto& objDictEntry : resp)
1519 {
1520 const std::string& objPath =
1521 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001522
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001523 // If this object path is one of the specified inventory
1524 // items
1525 InventoryItem* inventoryItem =
1526 findInventoryItem(inventoryItems, objPath);
1527 if (inventoryItem != nullptr)
1528 {
1529 // Store inventory data in InventoryItem
1530 storeInventoryItemData(*inventoryItem,
1531 objDictEntry.second);
1532 }
1533 }
1534
1535 // Recurse to get inventory item data from next connection
1536 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1537 invConnections, std::move(callback),
1538 invConnectionsIndex + 1);
1539
1540 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
1541 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001542 }
1543
Ed Tanous62598e32023-07-17 17:06:25 -07001544 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001545}
1546
1547/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001548 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001549 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001550 * Gets the D-Bus connections (services) that provide data for the inventory
1551 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001552 *
1553 * Finds the connections asynchronously. Invokes callback when information has
1554 * been obtained.
1555 *
1556 * The callback must have the following signature:
1557 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001558 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 * @endcode
1560 *
1561 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001562 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001563 * @param callback Callback to invoke when connections have been obtained.
1564 */
1565template <typename Callback>
1566static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001567 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1568 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001569 Callback&& callback)
1570{
Ed Tanous62598e32023-07-17 17:06:25 -07001571 BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001572
1573 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001574 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001575 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001576 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1577 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1579
George Liue99073f2022-12-09 11:06:16 +08001580 // Make call to ObjectMapper to find all inventory items
1581 dbus::utility::getSubTree(
1582 path, 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001583 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous002d39b2022-05-31 08:59:27 -07001584 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001585 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001586 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001587 // Response handler for parsing output from GetSubTree
1588 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
1589 if (ec)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001591 messages::internalError(sensorsAsyncResp->asyncResp->res);
1592 BMCWEB_LOG_ERROR(
1593 "getInventoryItemsConnections respHandler DBus error {}",
1594 ec);
1595 return;
1596 }
1597
1598 // Make unique list of connections for desired inventory items
1599 std::shared_ptr<std::set<std::string>> invConnections =
1600 std::make_shared<std::set<std::string>>();
1601
1602 // Loop through objects from GetSubTree
1603 for (const std::pair<std::string,
1604 std::vector<std::pair<
1605 std::string, std::vector<std::string>>>>&
1606 object : subtree)
1607 {
1608 // Check if object path is one of the specified inventory items
1609 const std::string& objPath = object.first;
1610 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001612 // Store all connections to inventory item
1613 for (const std::pair<std::string, std::vector<std::string>>&
1614 objData : object.second)
1615 {
1616 const std::string& invConnection = objData.first;
1617 invConnections->insert(invConnection);
1618 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001619 }
1620 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001621
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001622 callback(invConnections);
1623 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1624 });
Ed Tanous62598e32023-07-17 17:06:25 -07001625 BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626}
1627
1628/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001629 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001630 *
1631 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001632 * inventory items. Then finds the associations from those inventory items to
1633 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001634 *
1635 * Finds the inventory items asynchronously. Invokes callback when information
1636 * has been obtained.
1637 *
1638 * The callback must have the following signature:
1639 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001640 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001641 * @endcode
1642 *
1643 * @param sensorsAsyncResp Pointer to object holding response data.
1644 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001645 * implements ObjectManager.
1646 * @param callback Callback to invoke when inventory items have been obtained.
1647 */
1648template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001649static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001650 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001651 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001652 Callback&& callback)
1653{
Ed Tanous62598e32023-07-17 17:06:25 -07001654 BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001655
George Liu5eb468d2023-06-20 17:03:24 +08001656 // Call GetManagedObjects on the ObjectMapper to get all associations
1657 sdbusplus::message::object_path path("/");
1658 dbus::utility::getManagedObjects(
1659 "xyz.openbmc_project.ObjectMapper", path,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001660 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001661 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001662 const dbus::utility::ManagedObjectType& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001663 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
1664 if (ec)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001665 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001666 BMCWEB_LOG_ERROR(
1667 "getInventoryItemAssociations respHandler DBus error {}",
1668 ec);
1669 messages::internalError(sensorsAsyncResp->asyncResp->res);
1670 return;
1671 }
1672
1673 // Create vector to hold list of inventory items
1674 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1675 std::make_shared<std::vector<InventoryItem>>();
1676
1677 // Loop through returned object paths
1678 std::string sensorAssocPath;
1679 sensorAssocPath.reserve(128); // avoid memory allocations
1680 for (const auto& objDictEntry : resp)
1681 {
1682 const std::string& objPath =
1683 static_cast<const std::string&>(objDictEntry.first);
1684
1685 // If path is inventory association for one of the specified
1686 // sensors
1687 for (const std::string& sensorName : *sensorNames)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001688 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001689 sensorAssocPath = sensorName;
1690 sensorAssocPath += "/inventory";
1691 if (objPath == sensorAssocPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001692 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001693 // Get Association interface for object path
1694 for (const auto& [interface, values] :
1695 objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001696 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001697 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001698 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001699 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001700 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001701 if (valueName == "endpoints")
Ed Tanous711ac7a2021-12-20 09:34:41 -08001702 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001703 const std::vector<std::string>*
1704 endpoints = std::get_if<
1705 std::vector<std::string>>(
1706 &value);
1707 if ((endpoints != nullptr) &&
1708 !endpoints->empty())
1709 {
1710 // Add inventory item to vector
1711 const std::string& invItemPath =
1712 endpoints->front();
1713 addInventoryItem(inventoryItems,
1714 invItemPath,
1715 sensorName);
1716 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001717 }
1718 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001719 }
1720 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001721 break;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001722 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001723 }
1724 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001726 // Now loop through the returned object paths again, this time to
1727 // find the leds associated with the inventory items we just found
1728 std::string inventoryAssocPath;
1729 inventoryAssocPath.reserve(128); // avoid memory allocations
1730 for (const auto& objDictEntry : resp)
Anthony Wilsond5005492019-07-31 16:34:17 -05001731 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001732 const std::string& objPath =
1733 static_cast<const std::string&>(objDictEntry.first);
1734
1735 for (InventoryItem& inventoryItem : *inventoryItems)
Anthony Wilsond5005492019-07-31 16:34:17 -05001736 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001737 inventoryAssocPath = inventoryItem.objectPath;
1738 inventoryAssocPath += "/leds";
1739 if (objPath == inventoryAssocPath)
Anthony Wilsond5005492019-07-31 16:34:17 -05001740 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001741 for (const auto& [interface, values] :
1742 objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001743 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001744 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001745 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001746 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001747 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001748 if (valueName == "endpoints")
Ed Tanous711ac7a2021-12-20 09:34:41 -08001749 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001750 const std::vector<std::string>*
1751 endpoints = std::get_if<
1752 std::vector<std::string>>(
1753 &value);
1754 if ((endpoints != nullptr) &&
1755 !endpoints->empty())
1756 {
1757 // Add inventory item to vector
1758 // Store LED path in inventory item
1759 const std::string& ledPath =
1760 endpoints->front();
1761 inventoryItem.ledObjectPath =
1762 ledPath;
1763 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001764 }
1765 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001766 }
1767 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001768
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001769 break;
1770 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001771 }
1772 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001773 callback(inventoryItems);
1774 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
1775 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001776
Ed Tanous62598e32023-07-17 17:06:25 -07001777 BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001778}
1779
1780/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001781 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1782 *
1783 * Uses the specified connections (services) to obtain D-Bus data for inventory
1784 * item leds associated with sensors. Stores the resulting data in the
1785 * inventoryItems vector.
1786 *
1787 * This data is later used to provide sensor property values in the JSON
1788 * response.
1789 *
1790 * Finds the inventory item led data asynchronously. Invokes callback when data
1791 * has been obtained.
1792 *
1793 * The callback must have the following signature:
1794 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001795 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001796 * @endcode
1797 *
1798 * This function is called recursively, obtaining data asynchronously from one
1799 * connection in each call. This ensures the callback is not invoked until the
1800 * last asynchronous function has completed.
1801 *
1802 * @param sensorsAsyncResp Pointer to object holding response data.
1803 * @param inventoryItems D-Bus inventory items associated with sensors.
1804 * @param ledConnections Connections that provide data for the inventory leds.
1805 * @param callback Callback to invoke when inventory data has been obtained.
1806 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1807 * in recursive calls to this function.
1808 */
1809template <typename Callback>
1810void getInventoryLedData(
1811 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1812 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001813 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001814 Callback&& callback, size_t ledConnectionsIndex = 0)
1815{
Ed Tanous62598e32023-07-17 17:06:25 -07001816 BMCWEB_LOG_DEBUG("getInventoryLedData enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001817
1818 // If no more connections left, call callback
1819 if (ledConnectionsIndex >= ledConnections->size())
1820 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001821 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001822 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001823 return;
1824 }
1825
1826 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001827 auto it = ledConnections->begin();
1828 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001829 if (it != ledConnections->end())
1830 {
1831 const std::string& ledPath = (*it).first;
1832 const std::string& ledConnection = (*it).second;
1833 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001834 auto respHandler =
1835 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001836 callback = std::forward<Callback>(callback), ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001837 const boost::system::error_code& ec, const std::string& state) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001838 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1839 if (ec)
1840 {
1841 BMCWEB_LOG_ERROR(
1842 "getInventoryLedData respHandler DBus error {}", ec);
1843 messages::internalError(sensorsAsyncResp->asyncResp->res);
1844 return;
1845 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001846
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001847 BMCWEB_LOG_DEBUG("Led state: {}", state);
1848 // Find inventory item with this LED object path
1849 InventoryItem* inventoryItem =
1850 findInventoryItemForLed(*inventoryItems, ledPath);
1851 if (inventoryItem != nullptr)
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001852 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001853 // Store LED state in InventoryItem
1854 if (state.ends_with("On"))
1855 {
1856 inventoryItem->ledState = LedState::ON;
1857 }
1858 else if (state.ends_with("Blink"))
1859 {
1860 inventoryItem->ledState = LedState::BLINK;
1861 }
1862 else if (state.ends_with("Off"))
1863 {
1864 inventoryItem->ledState = LedState::OFF;
1865 }
1866 else
1867 {
1868 inventoryItem->ledState = LedState::UNKNOWN;
1869 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001870 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001871
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001872 // Recurse to get LED data from next connection
1873 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1874 ledConnections, std::move(callback),
1875 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001876
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001877 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1878 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001879
1880 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001881 sdbusplus::asio::getProperty<std::string>(
1882 *crow::connections::systemBus, ledConnection, ledPath,
1883 "xyz.openbmc_project.Led.Physical", "State",
1884 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001885 }
1886
Ed Tanous62598e32023-07-17 17:06:25 -07001887 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001888}
1889
1890/**
1891 * @brief Gets LED data for LEDs associated with given inventory items.
1892 *
1893 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1894 * associated with the specified inventory items. Then gets the LED data from
1895 * each connection and stores it in the inventory item.
1896 *
1897 * This data is later used to provide sensor property values in the JSON
1898 * response.
1899 *
1900 * Finds the LED data asynchronously. Invokes callback when information has
1901 * been obtained.
1902 *
1903 * The callback must have the following signature:
1904 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001905 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001906 * @endcode
1907 *
1908 * @param sensorsAsyncResp Pointer to object holding response data.
1909 * @param inventoryItems D-Bus inventory items associated with sensors.
1910 * @param callback Callback to invoke when inventory items have been obtained.
1911 */
1912template <typename Callback>
1913void getInventoryLeds(
1914 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1915 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1916 Callback&& callback)
1917{
Ed Tanous62598e32023-07-17 17:06:25 -07001918 BMCWEB_LOG_DEBUG("getInventoryLeds enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001919
1920 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001921 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001922 "xyz.openbmc_project.Led.Physical"};
1923
George Liue99073f2022-12-09 11:06:16 +08001924 // Make call to ObjectMapper to find all inventory items
1925 dbus::utility::getSubTree(
1926 path, 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001927 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous002d39b2022-05-31 08:59:27 -07001928 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001929 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001930 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001931 // Response handler for parsing output from GetSubTree
1932 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1933 if (ec)
Anthony Wilsond5005492019-07-31 16:34:17 -05001934 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001935 messages::internalError(sensorsAsyncResp->asyncResp->res);
1936 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}",
1937 ec);
1938 return;
Anthony Wilsond5005492019-07-31 16:34:17 -05001939 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001940
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001941 // Build map of LED object paths to connections
1942 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1943 std::make_shared<std::map<std::string, std::string>>();
1944
1945 // Loop through objects from GetSubTree
1946 for (const std::pair<std::string,
1947 std::vector<std::pair<
1948 std::string, std::vector<std::string>>>>&
1949 object : subtree)
1950 {
1951 // Check if object path is LED for one of the specified
1952 // inventory items
1953 const std::string& ledPath = object.first;
1954 if (findInventoryItemForLed(*inventoryItems, ledPath) !=
1955 nullptr)
1956 {
1957 // Add mapping from ledPath to connection
1958 const std::string& connection =
1959 object.second.begin()->first;
1960 (*ledConnections)[ledPath] = connection;
1961 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath,
1962 connection);
1963 }
1964 }
1965
1966 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1967 ledConnections, std::move(callback));
1968 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1969 });
Ed Tanous62598e32023-07-17 17:06:25 -07001970 BMCWEB_LOG_DEBUG("getInventoryLeds exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001971}
1972
1973/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001974 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1975 *
1976 * Uses the specified connections (services) (currently assumes just one) to
1977 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1978 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1979 *
1980 * This data is later used to provide sensor property values in the JSON
1981 * response.
1982 *
1983 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1984 * when data has been obtained.
1985 *
1986 * The callback must have the following signature:
1987 * @code
1988 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1989 * @endcode
1990 *
1991 * @param sensorsAsyncResp Pointer to object holding response data.
1992 * @param inventoryItems D-Bus inventory items associated with sensors.
1993 * @param psAttributesConnections Connections that provide data for the Power
1994 * Supply Attributes
1995 * @param callback Callback to invoke when data has been obtained.
1996 */
1997template <typename Callback>
1998void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001999 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002000 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002001 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002002 Callback&& callback)
2003{
Ed Tanous62598e32023-07-17 17:06:25 -07002004 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002005
2006 if (psAttributesConnections.empty())
2007 {
Ed Tanous62598e32023-07-17 17:06:25 -07002008 BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002009 callback(inventoryItems);
2010 return;
2011 }
2012
2013 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002014 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002015
2016 const std::string& psAttributesPath = (*it).first;
2017 const std::string& psAttributesConnection = (*it).second;
2018
2019 // Response handler for Get DeratingFactor property
Patrick Williams5a39f772023-10-20 11:20:21 -05002020 auto respHandler = [sensorsAsyncResp, inventoryItems,
Ed Tanous8cb2c022024-03-27 16:31:46 -07002021 callback = std::forward<Callback>(callback)](
Patrick Williams5a39f772023-10-20 11:20:21 -05002022 const boost::system::error_code& ec,
2023 const uint32_t value) {
Ed Tanous62598e32023-07-17 17:06:25 -07002024 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002025 if (ec)
2026 {
Ed Tanous62598e32023-07-17 17:06:25 -07002027 BMCWEB_LOG_ERROR(
2028 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08002029 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002030 return;
2031 }
2032
Ed Tanous62598e32023-07-17 17:06:25 -07002033 BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002034 // Store value in Power Supply Inventory Items
2035 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002036 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002037 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002038 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002039 inventoryItem.powerSupplyEfficiencyPercent =
2040 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002041 }
2042 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002043
Ed Tanous62598e32023-07-17 17:06:25 -07002044 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002045 callback(inventoryItems);
2046 };
2047
2048 // Get the DeratingFactor property for the PowerSupplyAttributes
2049 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002050 sdbusplus::asio::getProperty<uint32_t>(
2051 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2052 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2053 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002054
Ed Tanous62598e32023-07-17 17:06:25 -07002055 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002056}
2057
2058/**
2059 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2060 *
2061 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2062 * data. Then gets the Power Supply Attributes data from the connection
2063 * (currently just assumes 1 connection) and stores the data in the inventory
2064 * item.
2065 *
2066 * This data is later used to provide sensor property values in the JSON
2067 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2068 *
2069 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2070 * when information has been obtained.
2071 *
2072 * The callback must have the following signature:
2073 * @code
2074 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2075 * @endcode
2076 *
2077 * @param sensorsAsyncResp Pointer to object holding response data.
2078 * @param inventoryItems D-Bus inventory items associated with sensors.
2079 * @param callback Callback to invoke when data has been obtained.
2080 */
2081template <typename Callback>
2082void getPowerSupplyAttributes(
2083 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2084 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2085 Callback&& callback)
2086{
Ed Tanous62598e32023-07-17 17:06:25 -07002087 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002088
2089 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002090 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002091 {
Ed Tanous62598e32023-07-17 17:06:25 -07002092 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002093 callback(inventoryItems);
2094 return;
2095 }
2096
George Liue99073f2022-12-09 11:06:16 +08002097 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002098 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2099
George Liue99073f2022-12-09 11:06:16 +08002100 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2101 dbus::utility::getSubTree(
2102 "/xyz/openbmc_project", 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07002103 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002104 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002105 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002106 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002107 // Response handler for parsing output from GetSubTree
2108 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
2109 if (ec)
2110 {
2111 messages::internalError(sensorsAsyncResp->asyncResp->res);
2112 BMCWEB_LOG_ERROR(
2113 "getPowerSupplyAttributes respHandler DBus error {}", ec);
2114 return;
2115 }
2116 if (subtree.empty())
2117 {
2118 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
2119 callback(inventoryItems);
2120 return;
2121 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002122
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002123 // Currently we only support 1 power supply attribute, use this for
2124 // all the power supplies. Build map of object path to connection.
2125 // Assume just 1 connection and 1 path for now.
2126 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002127
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002128 if (subtree[0].first.empty() || subtree[0].second.empty())
2129 {
2130 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
2131 callback(inventoryItems);
2132 return;
2133 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002134
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002135 const std::string& psAttributesPath = subtree[0].first;
2136 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002137
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002138 if (connection.empty())
2139 {
2140 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
2141 callback(inventoryItems);
2142 return;
2143 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002144
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002145 psAttributesConnections[psAttributesPath] = connection;
2146 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
2147 connection);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002148
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002149 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2150 psAttributesConnections,
2151 std::move(callback));
2152 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
2153 });
Ed Tanous62598e32023-07-17 17:06:25 -07002154 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002155}
2156
2157/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002158 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002159 *
2160 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002161 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002162 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002163 * This data is later used to provide sensor property values in the JSON
2164 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002165 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002166 * Finds the inventory items asynchronously. Invokes callback when the
2167 * inventory items have been obtained.
2168 *
2169 * The callback must have the following signature:
2170 * @code
2171 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2172 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002173 *
2174 * @param sensorsAsyncResp Pointer to object holding response data.
2175 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002176 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002177 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002178 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002179template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002180static void
2181 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2182 const std::shared_ptr<std::set<std::string>> sensorNames,
2183 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002184{
Ed Tanous62598e32023-07-17 17:06:25 -07002185 BMCWEB_LOG_DEBUG("getInventoryItems enter");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002186 auto getInventoryItemAssociationsCb =
Ed Tanous8cb2c022024-03-27 16:31:46 -07002187 [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002188 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002189 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
2190 auto getInventoryItemsConnectionsCb =
2191 [sensorsAsyncResp, inventoryItems,
2192 callback = std::forward<const Callback>(callback)](
2193 std::shared_ptr<std::set<std::string>> invConnections) {
2194 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
2195 auto getInventoryItemsDataCb =
2196 [sensorsAsyncResp, inventoryItems,
2197 callback{std::move(callback)}]() {
2198 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002199
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002200 auto getInventoryLedsCb =
2201 [sensorsAsyncResp, inventoryItems,
2202 callback{std::move(callback)}]() {
2203 BMCWEB_LOG_DEBUG(
2204 "getInventoryLedsCb enter");
2205 // Find Power Supply Attributes and get the
2206 // data
2207 getPowerSupplyAttributes(
2208 sensorsAsyncResp, inventoryItems,
2209 std::move(callback));
2210 BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
2211 };
2212
2213 // Find led connections and get the data
2214 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2215 std::move(getInventoryLedsCb));
2216 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
2217 };
2218
2219 // Get inventory item data from connections
2220 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2221 invConnections,
2222 std::move(getInventoryItemsDataCb));
2223 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002224 };
2225
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002226 // Get connections that provide inventory item data
2227 getInventoryItemsConnections(
2228 sensorsAsyncResp, inventoryItems,
2229 std::move(getInventoryItemsConnectionsCb));
2230 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002231 };
2232
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002233 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002234 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002235 std::move(getInventoryItemAssociationsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002236 BMCWEB_LOG_DEBUG("getInventoryItems exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002237}
2238
2239/**
2240 * @brief Returns JSON PowerSupply object for the specified inventory item.
2241 *
2242 * Searches for a JSON PowerSupply object that matches the specified inventory
2243 * item. If one is not found, a new PowerSupply object is added to the JSON
2244 * array.
2245 *
2246 * Multiple sensors are often associated with one power supply inventory item.
2247 * As a result, multiple sensor values are stored in one JSON PowerSupply
2248 * object.
2249 *
2250 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2251 * @param inventoryItem Inventory item for the power supply.
2252 * @param chassisId Chassis that contains the power supply.
2253 * @return JSON PowerSupply object for the specified inventory item.
2254 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002255inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 const InventoryItem& inventoryItem,
2257 const std::string& chassisId)
2258{
Ed Tanous18f8f602023-07-18 10:07:23 -07002259 std::string nameS;
Alexander Hansen6f4bd292024-03-08 17:04:54 +01002260 nameS.resize(inventoryItem.name.size());
Ed Tanous18f8f602023-07-18 10:07:23 -07002261 std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002262 // Check if matching PowerSupply object already exists in JSON array
2263 for (nlohmann::json& powerSupply : powerSupplyArray)
2264 {
Ed Tanous18f8f602023-07-18 10:07:23 -07002265 nlohmann::json::iterator nameIt = powerSupply.find("Name");
2266 if (nameIt == powerSupply.end())
2267 {
2268 continue;
2269 }
2270 const std::string* name = nameIt->get_ptr<std::string*>();
2271 if (name == nullptr)
2272 {
2273 continue;
2274 }
2275 if (nameS == *name)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002276 {
2277 return powerSupply;
2278 }
2279 }
2280
2281 // Add new PowerSupply object to JSON array
2282 powerSupplyArray.push_back({});
2283 nlohmann::json& powerSupply = powerSupplyArray.back();
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002284 boost::urls::url url =
2285 boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002286 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2287 powerSupply["@odata.id"] = std::move(url);
Ed Tanous18f8f602023-07-18 10:07:23 -07002288 std::string escaped;
Alexander Hansen6f4bd292024-03-08 17:04:54 +01002289 escaped.resize(inventoryItem.name.size());
Ed Tanous18f8f602023-07-18 10:07:23 -07002290 std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
2291 powerSupply["Name"] = std::move(escaped);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002292 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2293 powerSupply["Model"] = inventoryItem.model;
2294 powerSupply["PartNumber"] = inventoryItem.partNumber;
2295 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002296 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002297
Gunnar Mills42cbe532019-08-15 15:26:54 -05002298 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2299 {
2300 powerSupply["EfficiencyPercent"] =
2301 inventoryItem.powerSupplyEfficiencyPercent;
2302 }
2303
Matt Simmeringaaf08ac2023-10-04 08:41:01 -07002304 powerSupply["Status"]["State"] = getState(&inventoryItem, true);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002305 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2306 powerSupply["Status"]["Health"] = health;
2307
2308 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002309}
2310
2311/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002312 * @brief Gets the values of the specified sensors.
2313 *
2314 * Stores the results as JSON in the SensorsAsyncResp.
2315 *
2316 * Gets the sensor values asynchronously. Stores the results later when the
2317 * information has been obtained.
2318 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002319 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002320 *
2321 * To minimize the number of DBus calls, the DBus method
2322 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2323 * values of all sensors provided by a connection (service).
2324 *
2325 * The connections set contains all the connections that provide sensor values.
2326 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002327 * The InventoryItem vector contains D-Bus inventory items associated with the
2328 * sensors. Inventory item data is needed for some Redfish sensor properties.
2329 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002330 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002331 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002332 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002333 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002334 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002335 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002336inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002337 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002338 const std::shared_ptr<std::set<std::string>>& sensorNames,
2339 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002340 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002341{
Ed Tanous62598e32023-07-17 17:06:25 -07002342 BMCWEB_LOG_DEBUG("getSensorData enter");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002343 // Get managed objects from all services exposing sensors
2344 for (const std::string& connection : connections)
2345 {
George Liu5eb468d2023-06-20 17:03:24 +08002346 sdbusplus::message::object_path sensorPath(
2347 "/xyz/openbmc_project/sensors");
2348 dbus::utility::getManagedObjects(
2349 connection, sensorPath,
Ed Tanous002d39b2022-05-31 08:59:27 -07002350 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002351 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002352 const dbus::utility::ManagedObjectType& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002353 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
2354 if (ec)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002355 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002356 BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
2357 messages::internalError(sensorsAsyncResp->asyncResp->res);
2358 return;
2359 }
2360 // Go through all objects and update response with sensor data
2361 for (const auto& objDictEntry : resp)
2362 {
2363 const std::string& objPath =
2364 static_cast<const std::string&>(objDictEntry.first);
2365 BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002366 objPath);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002367
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002368 std::vector<std::string> split;
2369 // Reserve space for
2370 // /xyz/openbmc_project/sensors/<name>/<subname>
2371 split.reserve(6);
2372 // NOLINTNEXTLINE
2373 bmcweb::split(split, objPath, '/');
2374 if (split.size() < 6)
Nan Zhou928fefb2022-03-28 08:45:00 -07002375 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002376 BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
2377 objPath);
2378 continue;
Nan Zhou928fefb2022-03-28 08:45:00 -07002379 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002380 // These indexes aren't intuitive, as split puts an empty
2381 // string at the beginning
2382 const std::string& sensorType = split[4];
2383 const std::string& sensorName = split[5];
2384 BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
2385 sensorType);
2386 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002387 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002388 BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002389 continue;
2390 }
2391
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002392 // Find inventory item (if any) associated with sensor
2393 InventoryItem* inventoryItem =
2394 findInventoryItemForSensor(inventoryItems, objPath);
2395
2396 const std::string& sensorSchema =
2397 sensorsAsyncResp->chassisSubNode;
2398
2399 nlohmann::json* sensorJson = nullptr;
2400
2401 if (sensorSchema == sensors::node::sensors &&
2402 !sensorsAsyncResp->efficientExpand)
Nan Zhou928fefb2022-03-28 08:45:00 -07002403 {
Janet Adkins1516c212024-08-14 13:22:41 -05002404 std::string sensorId =
2405 redfish::sensor_utils::getSensorId(sensorName,
2406 sensorType);
Ed Tanous677bb752022-09-15 10:52:19 -07002407
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002408 sensorsAsyncResp->asyncResp->res
2409 .jsonValue["@odata.id"] = boost::urls::format(
Ed Tanousef4c65b2023-04-24 15:28:50 -07002410 "/redfish/v1/Chassis/{}/{}/{}",
Ed Tanous677bb752022-09-15 10:52:19 -07002411 sensorsAsyncResp->chassisId,
2412 sensorsAsyncResp->chassisSubNode, sensorId);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002413 sensorJson =
2414 &(sensorsAsyncResp->asyncResp->res.jsonValue);
Nan Zhou928fefb2022-03-28 08:45:00 -07002415 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002416 else
2417 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002418 std::string fieldName;
2419 if (sensorsAsyncResp->efficientExpand)
2420 {
2421 fieldName = "Members";
2422 }
2423 else if (sensorType == "temperature")
2424 {
2425 fieldName = "Temperatures";
2426 }
2427 else if (sensorType == "fan" ||
2428 sensorType == "fan_tach" ||
2429 sensorType == "fan_pwm")
2430 {
2431 fieldName = "Fans";
2432 }
2433 else if (sensorType == "voltage")
2434 {
2435 fieldName = "Voltages";
2436 }
2437 else if (sensorType == "power")
2438 {
2439 if (sensorName == "total_power")
2440 {
2441 fieldName = "PowerControl";
2442 }
2443 else if ((inventoryItem != nullptr) &&
2444 (inventoryItem->isPowerSupply))
2445 {
2446 fieldName = "PowerSupplies";
2447 }
2448 else
2449 {
2450 // Other power sensors are in SensorCollection
2451 continue;
2452 }
2453 }
2454 else
2455 {
2456 BMCWEB_LOG_ERROR(
2457 "Unsure how to handle sensorType {}",
2458 sensorType);
2459 continue;
2460 }
2461
2462 nlohmann::json& tempArray =
2463 sensorsAsyncResp->asyncResp->res
2464 .jsonValue[fieldName];
2465 if (fieldName == "PowerControl")
2466 {
2467 if (tempArray.empty())
2468 {
2469 // Put multiple "sensors" into a single
2470 // PowerControl. Follows MemberId naming and
2471 // naming in power.hpp.
2472 nlohmann::json::object_t power;
2473 boost::urls::url url = boost::urls::format(
2474 "/redfish/v1/Chassis/{}/{}",
2475 sensorsAsyncResp->chassisId,
2476 sensorsAsyncResp->chassisSubNode);
2477 url.set_fragment(
2478 (""_json_pointer / fieldName / "0")
2479 .to_string());
2480 power["@odata.id"] = std::move(url);
2481 tempArray.emplace_back(std::move(power));
2482 }
2483 sensorJson = &(tempArray.back());
2484 }
2485 else if (fieldName == "PowerSupplies")
2486 {
2487 if (inventoryItem != nullptr)
2488 {
2489 sensorJson = &(getPowerSupply(
2490 tempArray, *inventoryItem,
2491 sensorsAsyncResp->chassisId));
2492 }
2493 }
2494 else if (fieldName == "Members")
2495 {
Janet Adkins1516c212024-08-14 13:22:41 -05002496 std::string sensorId =
2497 redfish::sensor_utils::getSensorId(sensorName,
2498 sensorType);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002499
2500 nlohmann::json::object_t member;
2501 member["@odata.id"] = boost::urls::format(
2502 "/redfish/v1/Chassis/{}/{}/{}",
2503 sensorsAsyncResp->chassisId,
2504 sensorsAsyncResp->chassisSubNode, sensorId);
2505 tempArray.emplace_back(std::move(member));
2506 sensorJson = &(tempArray.back());
2507 }
2508 else
2509 {
2510 nlohmann::json::object_t member;
2511 boost::urls::url url = boost::urls::format(
2512 "/redfish/v1/Chassis/{}/{}",
2513 sensorsAsyncResp->chassisId,
2514 sensorsAsyncResp->chassisSubNode);
2515 url.set_fragment(
2516 (""_json_pointer / fieldName).to_string());
2517 member["@odata.id"] = std::move(url);
2518 tempArray.emplace_back(std::move(member));
2519 sensorJson = &(tempArray.back());
2520 }
2521 }
2522
2523 if (sensorJson != nullptr)
2524 {
2525 objectInterfacesToJson(sensorName, sensorType,
2526 sensorsAsyncResp->chassisSubNode,
2527 objDictEntry.second, *sensorJson,
2528 inventoryItem);
2529
2530 std::string path = "/xyz/openbmc_project/sensors/";
2531 path += sensorType;
2532 path += "/";
2533 path += sensorName;
2534 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002535 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002536 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002537 if (sensorsAsyncResp.use_count() == 1)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002538 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002539 sortJSONResponse(sensorsAsyncResp);
2540 if (sensorsAsyncResp->chassisSubNode ==
2541 sensors::node::sensors &&
2542 sensorsAsyncResp->efficientExpand)
2543 {
2544 sensorsAsyncResp->asyncResp->res
2545 .jsonValue["Members@odata.count"] =
2546 sensorsAsyncResp->asyncResp->res
2547 .jsonValue["Members"]
2548 .size();
2549 }
2550 else if (sensorsAsyncResp->chassisSubNode ==
2551 sensors::node::thermal)
2552 {
2553 populateFanRedundancy(sensorsAsyncResp);
2554 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002555 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002556 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
2557 });
Ed Tanous23a21a12020-07-25 04:45:05 +00002558 }
Ed Tanous62598e32023-07-17 17:06:25 -07002559 BMCWEB_LOG_DEBUG("getSensorData exit");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002560}
2561
Nan Zhoufe04d492022-06-22 17:10:41 +00002562inline void
2563 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2564 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002565{
Nan Zhoufe04d492022-06-22 17:10:41 +00002566 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2567 const std::set<std::string>& connections) {
Ed Tanous62598e32023-07-17 17:06:25 -07002568 BMCWEB_LOG_DEBUG("getConnectionCb enter");
Ed Tanousd0090732022-10-04 17:22:56 -07002569 auto getInventoryItemsCb =
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002570 [sensorsAsyncResp, sensorNames, connections](
2571 const std::shared_ptr<std::vector<InventoryItem>>&
2572 inventoryItems) {
2573 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
2574 // Get sensor data and store results in JSON
2575 getSensorData(sensorsAsyncResp, sensorNames, connections,
2576 inventoryItems);
2577 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2578 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002579
Ed Tanousd0090732022-10-04 17:22:56 -07002580 // Get inventory items associated with sensors
2581 getInventoryItems(sensorsAsyncResp, sensorNames,
2582 std::move(getInventoryItemsCb));
2583
Ed Tanous62598e32023-07-17 17:06:25 -07002584 BMCWEB_LOG_DEBUG("getConnectionCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002585 };
2586
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002587 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002588 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002589}
2590
Shawn McCarneyde629b62019-03-08 10:42:51 -06002591/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002592 * @brief Entry point for retrieving sensors data related to requested
2593 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002594 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002595 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002596inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002597 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002598{
Ed Tanous62598e32023-07-17 17:06:25 -07002599 BMCWEB_LOG_DEBUG("getChassisData enter");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002600 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002601 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002602 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002603 BMCWEB_LOG_DEBUG("getChassisCb enter");
2604 processSensorList(sensorsAsyncResp, sensorNames);
2605 BMCWEB_LOG_DEBUG("getChassisCb exit");
2606 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002607 // SensorCollection doesn't contain the Redundancy property
2608 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2609 {
2610 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2611 nlohmann::json::array();
2612 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002613 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002614 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2615 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2616 std::move(getChassisCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002617 BMCWEB_LOG_DEBUG("getChassisData exit");
Ed Tanous271584a2019-07-09 16:24:22 -07002618}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002619
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302620/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002621 * @brief Find the requested sensorName in the list of all sensors supplied by
2622 * the chassis node
2623 *
2624 * @param sensorName The sensor name supplied in the PATCH request
2625 * @param sensorsList The list of sensors managed by the chassis node
2626 * @param sensorsModified The list of sensors that were found as a result of
2627 * repeated calls to this function
2628 */
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002629inline bool findSensorNameUsingSensorPath(
2630 std::string_view sensorName, const std::set<std::string>& sensorsList,
2631 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002632{
Nan Zhoufe04d492022-06-22 17:10:41 +00002633 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002634 {
George Liu28aa8de2021-02-01 15:13:30 +08002635 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002636 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002637 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002638 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002639 continue;
2640 }
2641 if (thisSensorName == sensorName)
2642 {
2643 sensorsModified.emplace(chassisSensor);
2644 return true;
2645 }
2646 }
2647 return false;
2648}
2649
2650/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302651 * @brief Entry point for overriding sensor values of given sensor
2652 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002653 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002654 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002655 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302656 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002657inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002658 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Ed Tanous08850572024-03-06 15:09:17 -08002659 std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002660 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302661{
Ed Tanous62598e32023-07-17 17:06:25 -07002662 BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
2663 sensorAsyncResp->chassisSubNode);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302664
Ed Tanousd02aad32024-02-13 14:43:34 -08002665 std::string_view propertyValueName;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302666 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302667 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002668 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302669 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302670 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302671 if (collectionItems.first == "Temperatures")
2672 {
2673 propertyValueName = "ReadingCelsius";
2674 }
2675 else if (collectionItems.first == "Fans")
2676 {
2677 propertyValueName = "Reading";
2678 }
2679 else
2680 {
2681 propertyValueName = "ReadingVolts";
2682 }
2683 for (auto& item : collectionItems.second)
2684 {
Ed Tanous08850572024-03-06 15:09:17 -08002685 if (!json_util::readJsonObject(
2686 item, sensorAsyncResp->asyncResp->res, "MemberId", memberId,
2687 propertyValueName, value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302688 {
2689 return;
2690 }
2691 overrideMap.emplace(memberId,
2692 std::make_pair(value, collectionItems.first));
2693 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302694 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002695
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002696 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap,
2697 propertyValueNameStr =
2698 std::string(propertyValueName)](
2699 const std::shared_ptr<
2700 std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002701 // Match sensor names in the PATCH request to those managed by the
2702 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002703 const std::shared_ptr<std::set<std::string>> sensorNames =
2704 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302705 for (const auto& item : overrideMap)
2706 {
2707 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002708 std::pair<std::string, std::string> sensorNameType =
Janet Adkins1516c212024-08-14 13:22:41 -05002709 redfish::sensor_utils::splitSensorNameAndType(sensor);
Ed Tanousc71d6122022-11-29 14:10:32 -08002710 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2711 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302712 {
Ed Tanous62598e32023-07-17 17:06:25 -07002713 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
zhanghch058d1b46d2021-04-01 11:18:24 +08002714 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302715 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302716 return;
2717 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302718 }
2719 // Get the connection to which the memberId belongs
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002720 auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap,
2721 propertyValueNameStr](
2722 const std::set<
2723 std::string>& /*connections*/,
2724 const std::set<std::pair<
2725 std::string, std::string>>&
2726 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002727 if (objectsWithConnection.size() != overrideMap.size())
2728 {
Ed Tanous62598e32023-07-17 17:06:25 -07002729 BMCWEB_LOG_INFO(
2730 "Unable to find all objects with proper connection {} requested {}",
2731 objectsWithConnection.size(), overrideMap.size());
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002732 messages::resourceNotFound(
2733 sensorAsyncResp->asyncResp->res,
2734 sensorAsyncResp->chassisSubNode == sensors::node::thermal
2735 ? "Temperatures"
2736 : "Voltages",
2737 "Count");
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002738 return;
2739 }
2740 for (const auto& item : objectsWithConnection)
2741 {
2742 sdbusplus::message::object_path path(item.first);
2743 std::string sensorName = path.filename();
2744 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302745 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002746 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302747 return;
2748 }
Janet Adkins1516c212024-08-14 13:22:41 -05002749 std::string id = redfish::sensor_utils::getSensorId(
2750 sensorName, path.parent_path().filename());
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302751
Ban Feng3f5eb752023-06-29 09:19:20 +08002752 const auto& iterator = overrideMap.find(id);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002753 if (iterator == overrideMap.end())
2754 {
Ed Tanous62598e32023-07-17 17:06:25 -07002755 BMCWEB_LOG_INFO("Unable to find sensor object{}",
2756 item.first);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002757 messages::internalError(sensorAsyncResp->asyncResp->res);
2758 return;
2759 }
Ginu Georgee93abac2024-06-14 17:35:27 +05302760 setDbusProperty(sensorAsyncResp->asyncResp,
2761 propertyValueNameStr, item.second, item.first,
2762 "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanousd02aad32024-02-13 14:43:34 -08002763 iterator->second.first);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002764 }
2765 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302766 // Get object with connection for the given sensor name
2767 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2768 std::move(getObjectsWithConnectionCb));
2769 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302770 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002771 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2772 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2773 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302774}
2775
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002776/**
2777 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2778 * path of the sensor.
2779 *
2780 * Function builds valid Redfish response for sensor query of given chassis and
2781 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2782 * it to caller in a callback.
2783 *
2784 * @param chassis Chassis for which retrieval should be performed
2785 * @param node Node (group) of sensors. See sensors::node for supported values
2786 * @param mapComplete Callback to be called with retrieval result
2787 */
Ed Tanous931edc72023-11-01 12:09:07 -07002788template <typename Callback>
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002789inline void retrieveUriToDbusMap(
2790 const std::string& chassis, const std::string& node, Callback&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002791{
Ed Tanous02da7c52022-02-27 00:09:02 -08002792 decltype(sensors::paths)::const_iterator pathIt =
2793 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2794 [&node](auto&& val) { return val.first == node; });
2795 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002796 {
Ed Tanous62598e32023-07-17 17:06:25 -07002797 BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
Ed Tanous6804b5c2023-10-31 14:50:03 -07002798 std::map<std::string, std::string> noop;
2799 mapComplete(boost::beast::http::status::bad_request, noop);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002800 return;
2801 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002802
Nan Zhou72374eb2022-01-27 17:06:51 -08002803 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002804 auto callback =
2805 [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)](
2806 const boost::beast::http::status status,
2807 const std::map<std::string, std::string>& uriToDbus) {
2808 mapCompleteCb(status, uriToDbus);
2809 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002810
2811 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002812 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002813 getChassisData(resp);
2814}
2815
Nan Zhoubacb2162022-04-06 11:28:32 -07002816namespace sensors
2817{
Nan Zhou928fefb2022-03-28 08:45:00 -07002818
Nan Zhoubacb2162022-04-06 11:28:32 -07002819inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002820 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2821 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002822 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002823{
Ed Tanous62598e32023-07-17 17:06:25 -07002824 BMCWEB_LOG_DEBUG("getChassisCallback enter ");
Nan Zhoubacb2162022-04-06 11:28:32 -07002825
Ed Tanousc1d019a2022-08-06 09:36:06 -07002826 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2827 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002828 {
Ed Tanous62598e32023-07-17 17:06:25 -07002829 BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
Nan Zhoubacb2162022-04-06 11:28:32 -07002830
2831 sdbusplus::message::object_path path(sensor);
2832 std::string sensorName = path.filename();
2833 if (sensorName.empty())
2834 {
Ed Tanous62598e32023-07-17 17:06:25 -07002835 BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002836 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002837 return;
2838 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002839 std::string type = path.parent_path().filename();
Janet Adkins1516c212024-08-14 13:22:41 -05002840 std::string id = redfish::sensor_utils::getSensorId(sensorName, type);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002841
Ed Tanous14766872022-03-15 10:44:42 -07002842 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002843 member["@odata.id"] = boost::urls::format(
2844 "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002845
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002846 entriesArray.emplace_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002847 }
2848
Ed Tanousc1d019a2022-08-06 09:36:06 -07002849 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous62598e32023-07-17 17:06:25 -07002850 BMCWEB_LOG_DEBUG("getChassisCallback exit");
Nan Zhoubacb2162022-04-06 11:28:32 -07002851}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002852
Ed Tanousac106bf2023-06-07 09:24:59 -07002853inline void handleSensorCollectionGet(
2854 App& app, const crow::Request& req,
2855 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2856 const std::string& chassisId)
Nan Zhoude167a62022-06-01 04:47:45 +00002857{
2858 query_param::QueryCapabilities capabilities = {
2859 .canDelegateExpandLevel = 1,
2860 };
2861 query_param::Query delegatedQuery;
Ed Tanousac106bf2023-06-07 09:24:59 -07002862 if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002863 delegatedQuery, capabilities))
2864 {
2865 return;
2866 }
2867
2868 if (delegatedQuery.expandType != query_param::ExpandType::None)
2869 {
2870 // we perform efficient expand.
Ed Tanousac106bf2023-06-07 09:24:59 -07002871 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2872 asyncResp, chassisId, sensors::dbus::sensorPaths,
Nan Zhoude167a62022-06-01 04:47:45 +00002873 sensors::node::sensors,
2874 /*efficientExpand=*/true);
Ed Tanousac106bf2023-06-07 09:24:59 -07002875 getChassisData(sensorsAsyncResp);
Nan Zhoude167a62022-06-01 04:47:45 +00002876
Ed Tanous62598e32023-07-17 17:06:25 -07002877 BMCWEB_LOG_DEBUG(
2878 "SensorCollection doGet exit via efficient expand handler");
Nan Zhoude167a62022-06-01 04:47:45 +00002879 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002880 }
Nan Zhoude167a62022-06-01 04:47:45 +00002881
Nan Zhoude167a62022-06-01 04:47:45 +00002882 // We get all sensors as hyperlinkes in the chassis (this
2883 // implies we reply on the default query parameters handler)
Ed Tanousac106bf2023-06-07 09:24:59 -07002884 getChassis(asyncResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2885 std::bind_front(sensors::getChassisCallback, asyncResp,
2886 chassisId, sensors::node::sensors));
Ed Tanousc1d019a2022-08-06 09:36:06 -07002887}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002888
Ed Tanousc1d019a2022-08-06 09:36:06 -07002889inline void
2890 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2891 const std::string& sensorPath,
2892 const ::dbus::utility::MapperGetObject& mapperResponse)
2893{
2894 if (mapperResponse.size() != 1)
2895 {
2896 messages::internalError(asyncResp->res);
2897 return;
2898 }
2899 const auto& valueIface = *mapperResponse.begin();
2900 const std::string& connectionName = valueIface.first;
Ed Tanous62598e32023-07-17 17:06:25 -07002901 BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
2902 BMCWEB_LOG_DEBUG("Path {}", sensorPath);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002903
2904 sdbusplus::asio::getAllProperties(
2905 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002906 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002907 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002908 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002909 if (ec)
2910 {
2911 messages::internalError(asyncResp->res);
2912 return;
2913 }
2914 sdbusplus::message::object_path path(sensorPath);
2915 std::string name = path.filename();
2916 path = path.parent_path();
2917 std::string type = path.filename();
2918 objectPropertiesToJson(name, type, sensors::node::sensors,
2919 valuesDict, asyncResp->res.jsonValue,
2920 nullptr);
2921 });
Nan Zhoude167a62022-06-01 04:47:45 +00002922}
2923
Nan Zhoue6bd8462022-06-01 04:35:35 +00002924inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002925 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002926 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002927 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002928{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002929 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002930 {
2931 return;
2932 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002933 std::pair<std::string, std::string> nameType =
Janet Adkins1516c212024-08-14 13:22:41 -05002934 redfish::sensor_utils::splitSensorNameAndType(sensorId);
Ed Tanousc71d6122022-11-29 14:10:32 -08002935 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002936 {
2937 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2938 return;
2939 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002940
Ed Tanousef4c65b2023-04-24 15:28:50 -07002941 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2942 "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002943
Ed Tanous62598e32023-07-17 17:06:25 -07002944 BMCWEB_LOG_DEBUG("Sensor doGet enter");
Nan Zhoue6bd8462022-06-01 04:35:35 +00002945
George Liu2b731192023-01-11 16:27:13 +08002946 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002947 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002948 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2949 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002950 // Get a list of all of the sensors that implement Sensor.Value
2951 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002952 ::dbus::utility::getDbusObject(
2953 sensorPath, interfaces,
Myung Baeaec0ec32023-05-31 15:10:01 -05002954 [asyncResp, sensorId,
George Liu2b731192023-01-11 16:27:13 +08002955 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002956 const ::dbus::utility::MapperGetObject& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002957 BMCWEB_LOG_DEBUG("respHandler1 enter");
2958 if (ec == boost::system::errc::io_error)
2959 {
2960 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
2961 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2962 return;
2963 }
2964 if (ec)
2965 {
2966 messages::internalError(asyncResp->res);
2967 BMCWEB_LOG_ERROR(
2968 "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
2969 return;
2970 }
2971 getSensorFromDbus(asyncResp, sensorPath, subtree);
2972 BMCWEB_LOG_DEBUG("respHandler1 exit");
2973 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002974}
2975
Nan Zhoubacb2162022-04-06 11:28:32 -07002976} // namespace sensors
2977
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002978inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002979{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002980 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002981 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002982 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002983 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002984}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002985
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002986inline void requestRoutesSensor(App& app)
2987{
2988 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002989 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002990 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002991 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002992}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002993
Ed Tanous1abe55e2018-09-05 08:30:59 -07002994} // namespace redfish