blob: 86271948490696704dcc861702d61d9f15716ecc [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"
Ed Tanous0ec8b832022-03-14 14:56:47 -070031
George Liue99073f2022-12-09 11:06:16 +080032#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070033#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070034#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020035#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050036
George Liu7a1dbc42022-12-07 16:03:22 +080037#include <array>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000039#include <iterator>
Ed Tanous283860f2022-08-29 14:08:50 -070040#include <limits>
Nan Zhoufe04d492022-06-22 17:10:41 +000041#include <map>
Ed Tanous3544d2a2023-08-06 18:12:20 -070042#include <ranges>
Nan Zhoufe04d492022-06-22 17:10:41 +000043#include <set>
Ed Tanous18f8f602023-07-18 10:07:23 -070044#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080045#include <string_view>
Ed Tanousb5a76932020-09-29 16:16:58 -070046#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080047#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010048
Ed Tanous1abe55e2018-09-05 08:30:59 -070049namespace redfish
50{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010051
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020052namespace sensors
53{
54namespace node
55{
56static constexpr std::string_view power = "Power";
57static constexpr std::string_view sensors = "Sensors";
58static constexpr std::string_view thermal = "Thermal";
59} // namespace node
60
Ed Tanous02da7c52022-02-27 00:09:02 -080061// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020062namespace dbus
63{
Ed Tanouscf9e4172022-12-21 09:30:16 -080064constexpr auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080065 "/xyz/openbmc_project/sensors/voltage",
66 "/xyz/openbmc_project/sensors/power"
67});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000068
Ed Tanous25b54db2024-04-17 15:40:31 -070069constexpr auto getSensorPaths(){
70 if constexpr(BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM){
71 return std::to_array<std::string_view>({
72 "/xyz/openbmc_project/sensors/power",
73 "/xyz/openbmc_project/sensors/current",
74 "/xyz/openbmc_project/sensors/airflow",
75 "/xyz/openbmc_project/sensors/humidity",
76 "/xyz/openbmc_project/sensors/voltage",
77 "/xyz/openbmc_project/sensors/fan_tach",
78 "/xyz/openbmc_project/sensors/temperature",
79 "/xyz/openbmc_project/sensors/fan_pwm",
80 "/xyz/openbmc_project/sensors/altitude",
81 "/xyz/openbmc_project/sensors/energy",
82 "/xyz/openbmc_project/sensors/utilization"});
83 } else {
84 return std::to_array<std::string_view>({"/xyz/openbmc_project/sensors/power",
85 "/xyz/openbmc_project/sensors/current",
86 "/xyz/openbmc_project/sensors/airflow",
87 "/xyz/openbmc_project/sensors/humidity",
88 "/xyz/openbmc_project/sensors/utilization"});
89}
90}
91
92constexpr auto sensorPaths = getSensorPaths();
Ed Tanous02da7c52022-02-27 00:09:02 -080093
Ed Tanouscf9e4172022-12-21 09:30:16 -080094constexpr auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080095 "/xyz/openbmc_project/sensors/fan_tach",
96 "/xyz/openbmc_project/sensors/temperature",
97 "/xyz/openbmc_project/sensors/fan_pwm"
98});
99
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000100} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -0800101// clang-format on
102
Ed Tanouscf9e4172022-12-21 09:30:16 -0800103using sensorPair =
104 std::pair<std::string_view, std::span<const std::string_view>>;
Ed Tanous02da7c52022-02-27 00:09:02 -0800105static constexpr std::array<sensorPair, 3> paths = {
Ed Tanouscf9e4172022-12-21 09:30:16 -0800106 {{node::power, dbus::powerPaths},
107 {node::sensors, dbus::sensorPaths},
108 {node::thermal, dbus::thermalPaths}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000109
Ed Tanous0ec8b832022-03-14 14:56:47 -0700110inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000111{
112 if (sensorType == "voltage")
113 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700114 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000115 }
116 if (sensorType == "power")
117 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700118 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000119 }
120 if (sensorType == "current")
121 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700122 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000123 }
124 if (sensorType == "fan_tach")
125 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700126 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000127 }
128 if (sensorType == "temperature")
129 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700130 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000131 }
132 if (sensorType == "fan_pwm" || sensorType == "utilization")
133 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700134 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000135 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600136 if (sensorType == "humidity")
137 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700138 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600139 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000140 if (sensorType == "altitude")
141 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700142 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000143 }
144 if (sensorType == "airflow")
145 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700146 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000147 }
148 if (sensorType == "energy")
149 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700150 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000151 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700152 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000153}
154
Ed Tanous1d7c0052022-08-09 12:32:26 -0700155inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000156{
157 if (sensorType == "voltage")
158 {
159 return "V";
160 }
161 if (sensorType == "power")
162 {
163 return "W";
164 }
165 if (sensorType == "current")
166 {
167 return "A";
168 }
169 if (sensorType == "fan_tach")
170 {
171 return "RPM";
172 }
173 if (sensorType == "temperature")
174 {
175 return "Cel";
176 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600177 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
178 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000179 {
180 return "%";
181 }
182 if (sensorType == "altitude")
183 {
184 return "m";
185 }
186 if (sensorType == "airflow")
187 {
188 return "cft_i/min";
189 }
190 if (sensorType == "energy")
191 {
192 return "J";
193 }
194 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200195}
196} // namespace sensors
197
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100198/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200199 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100200 * Gathers data needed for response processing after async calls are done
201 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202class SensorsAsyncResp
203{
204 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200205 using DataCompleteCb = std::function<void(
206 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000207 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200208
209 struct SensorData
210 {
211 const std::string name;
212 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200213 const std::string dbusPath;
214 };
215
Ed Tanous8a592812022-06-04 09:06:59 -0700216 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800217 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800218 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800219 std::string_view subNode) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400220 asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
221 chassisSubNode(subNode), efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500222 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200223
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200224 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700225 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800226 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800227 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800228 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200229 DataCompleteCb&& creationComplete) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400230 asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
231 chassisSubNode(subNode), efficientExpand(false),
232 metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200233 dataComplete{std::move(creationComplete)}
234 {}
235
Nan Zhou928fefb2022-03-28 08:45:00 -0700236 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700237 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700238 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800239 std::span<const std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700240 const std::string_view& subNode, bool efficientExpandIn) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400241 asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
242 chassisSubNode(subNode), efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700243 {}
244
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 ~SensorsAsyncResp()
246 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800247 if (asyncResp->res.result() ==
248 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249 {
250 // Reset the json object to clear out any data that made it in
251 // before the error happened todo(ed) handle error condition with
252 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800253 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200255
256 if (dataComplete && metadata)
257 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000258 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800259 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200260 {
261 for (auto& sensor : *metadata)
262 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700263 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200264 }
265 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800266 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200267 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100269
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800270 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
271 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
272 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
273 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
274
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200275 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700276 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200277 {
278 if (metadata)
279 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700280 metadata->emplace_back(SensorData{
281 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200282 }
283 }
284
285 void updateUri(const std::string& name, const std::string& uri)
286 {
287 if (metadata)
288 {
289 for (auto& sensor : *metadata)
290 {
291 if (sensor.name == name)
292 {
293 sensor.uri = uri;
294 }
295 }
296 }
297 }
298
zhanghch058d1b46d2021-04-01 11:18:24 +0800299 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200300 const std::string chassisId;
Ed Tanouscf9e4172022-12-21 09:30:16 -0800301 const std::span<const std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200302 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700303 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200304
305 private:
306 std::optional<std::vector<SensorData>> metadata;
307 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100308};
309
310/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500311 * Possible states for physical inventory leds
312 */
313enum class LedState
314{
315 OFF,
316 ON,
317 BLINK,
318 UNKNOWN
319};
320
321/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500322 * D-Bus inventory item associated with one or more sensors.
323 */
324class InventoryItem
325{
326 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700327 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500328 {
329 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800330 sdbusplus::message::object_path path(objectPath);
331 name = path.filename();
332 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500333 {
Ed Tanous62598e32023-07-17 17:06:25 -0700334 BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500335 }
336 }
337
338 std::string objectPath;
339 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800340 bool isPresent = true;
341 bool isFunctional = true;
342 bool isPowerSupply = false;
343 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500344 std::string manufacturer;
345 std::string model;
346 std::string partNumber;
347 std::string serialNumber;
348 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500349 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800350 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500351};
352
353/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530354 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200355 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100356 * @param sensorNames Sensors retrieved from chassis
357 * @param callback Callback for processing gathered connections
358 */
359template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530360void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000361 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000362 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530363 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700364{
Ed Tanous62598e32023-07-17 17:06:25 -0700365 BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700366 const std::string path = "/xyz/openbmc_project/sensors";
George Liue99073f2022-12-09 11:06:16 +0800367 constexpr std::array<std::string_view, 1> interfaces = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700368 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100369
George Liue99073f2022-12-09 11:06:16 +0800370 // Make call to ObjectMapper to find all sensors objects
371 dbus::utility::getSubTree(
372 path, 2, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700373 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
George Liue99073f2022-12-09 11:06:16 +0800374 sensorNames](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700375 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400376 // Response handler for parsing objects subtree
377 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
378 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700379 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400380 messages::internalError(sensorsAsyncResp->asyncResp->res);
381 BMCWEB_LOG_ERROR(
382 "getObjectsWithConnection resp_handler: Dbus error {}", ec);
383 return;
384 }
385
386 BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
387
388 // Make unique list of connections only for requested sensor types
389 // and found in the chassis
390 std::set<std::string> connections;
391 std::set<std::pair<std::string, std::string>> objectsWithConnection;
392
393 BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
394 for (const std::string& tsensor : *sensorNames)
395 {
396 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
397 }
398
399 for (const std::pair<std::string,
400 std::vector<std::pair<
401 std::string, std::vector<std::string>>>>&
402 object : subtree)
403 {
404 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700405 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400406 for (const std::pair<std::string, std::vector<std::string>>&
407 objData : object.second)
408 {
409 BMCWEB_LOG_DEBUG("Adding connection: {}",
410 objData.first);
411 connections.insert(objData.first);
412 objectsWithConnection.insert(
413 std::make_pair(object.first, objData.first));
414 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700415 }
416 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400417 BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
418 callback(std::move(connections), std::move(objectsWithConnection));
419 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
420 });
Ed Tanous62598e32023-07-17 17:06:25 -0700421 BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530422}
423
424/**
425 * @brief Create connections necessary for sensors
426 * @param SensorsAsyncResp Pointer to object holding response data
427 * @param sensorNames Sensors retrieved from chassis
428 * @param callback Callback for processing gathered connections
429 */
430template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000431void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
432 const std::shared_ptr<std::set<std::string>> sensorNames,
433 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530434{
435 auto objectsWithConnectionCb =
Ed Tanous8cb2c022024-03-27 16:31:46 -0700436 [callback = std::forward<Callback>(callback)](
437 const std::set<std::string>& connections,
438 const std::set<std::pair<std::string, std::string>>&
439 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000440 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530441 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100442}
443
444/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445 * @brief Shrinks the list of sensors for processing
446 * @param SensorsAysncResp The class holding the Redfish response
447 * @param allSensors A list of all the sensors associated to the
448 * chassis element (i.e. baseboard, front panel, etc...)
449 * @param activeSensors A list that is a reduction of the incoming
450 * allSensors list. Eliminate Thermal sensors when a Power request is
451 * made, and eliminate Power sensors when a Thermal request is made.
452 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000453inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700454 crow::Response& res, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800455 std::span<const std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700456 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000457 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700458{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700459 if ((allSensors == nullptr) || (activeSensors == nullptr))
460 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700461 messages::resourceNotFound(res, chassisSubNode,
462 chassisSubNode == sensors::node::thermal
463 ? "Temperatures"
464 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700465
466 return;
467 }
468 if (allSensors->empty())
469 {
470 // Nothing to do, the activeSensors object is also empty
471 return;
472 }
473
Ed Tanous7f1cc262022-08-09 13:33:57 -0700474 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700475 {
476 for (const std::string& sensor : *allSensors)
477 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700478 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700479 {
480 activeSensors->emplace(sensor);
481 }
482 }
483 }
484}
485
Ed Tanous7f1cc262022-08-09 13:33:57 -0700486/*
487 *Populates the top level collection for a given subnode. Populates
488 *SensorCollection, Power, or Thermal schemas.
489 *
490 * */
491inline void populateChassisNode(nlohmann::json& jsonValue,
492 std::string_view chassisSubNode)
493{
494 if (chassisSubNode == sensors::node::power)
495 {
496 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
497 }
498 else if (chassisSubNode == sensors::node::thermal)
499 {
500 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
501 jsonValue["Fans"] = nlohmann::json::array();
502 jsonValue["Temperatures"] = nlohmann::json::array();
503 }
504 else if (chassisSubNode == sensors::node::sensors)
505 {
506 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
507 jsonValue["Description"] = "Collection of Sensors for this Chassis";
508 jsonValue["Members"] = nlohmann::json::array();
509 jsonValue["Members@odata.count"] = 0;
510 }
511
512 if (chassisSubNode != sensors::node::sensors)
513 {
514 jsonValue["Id"] = chassisSubNode;
515 }
516 jsonValue["Name"] = chassisSubNode;
517}
518
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700519/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100520 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200521 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100522 * @param callback Callback for next step in gathered sensor processing
523 */
524template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700525void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
526 std::string_view chassisId, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800527 std::span<const std::string_view> sensorTypes,
528 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529{
Ed Tanous62598e32023-07-17 17:06:25 -0700530 BMCWEB_LOG_DEBUG("getChassis enter");
George Liu7a1dbc42022-12-07 16:03:22 +0800531 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700532 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500533 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800534
535 // Get the Chassis Collection
536 dbus::utility::getSubTreePaths(
537 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700538 [callback = std::forward<Callback>(callback), asyncResp,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700539 chassisIdStr{std::string(chassisId)},
540 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800541 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700542 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400543 BMCWEB_LOG_DEBUG("getChassis respHandler enter");
544 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400546 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
547 messages::internalError(asyncResp->res);
548 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700549 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400550 const std::string* chassisPath = nullptr;
551 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400553 sdbusplus::message::object_path path(chassis);
554 std::string chassisName = path.filename();
555 if (chassisName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700556 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400557 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
558 continue;
559 }
560 if (chassisName == chassisIdStr)
561 {
562 chassisPath = &chassis;
563 break;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700564 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700565 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400566 if (chassisPath == nullptr)
567 {
568 messages::resourceNotFound(asyncResp->res, "Chassis",
569 chassisIdStr);
570 return;
571 }
572 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
573
574 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
575 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
576
577 // Get the list of all sensors for this Chassis element
578 std::string sensorPath = *chassisPath + "/all_sensors";
579 dbus::utility::getAssociationEndPoints(
580 sensorPath,
581 [asyncResp, chassisSubNode, sensorTypes,
582 callback = std::forward<const Callback>(callback)](
583 const boost::system::error_code& ec2,
584 const dbus::utility::MapperEndPoints& nodeSensorList) {
585 if (ec2)
586 {
587 if (ec2.value() != EBADR)
588 {
589 messages::internalError(asyncResp->res);
590 return;
591 }
592 }
593 const std::shared_ptr<std::set<std::string>>
594 culledSensorList =
595 std::make_shared<std::set<std::string>>();
596 reduceSensorList(asyncResp->res, chassisSubNode,
597 sensorTypes, &nodeSensorList,
598 culledSensorList);
599 BMCWEB_LOG_DEBUG("Finishing with {}",
600 culledSensorList->size());
601 callback(culledSensorList);
602 });
George Liu7a1dbc42022-12-07 16:03:22 +0800603 });
Ed Tanous62598e32023-07-17 17:06:25 -0700604 BMCWEB_LOG_DEBUG("getChassis exit");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100605}
606
607/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500608 * @brief Returns the Redfish State value for the specified inventory item.
609 * @param inventoryItem D-Bus inventory item associated with a sensor.
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700610 * @param sensorAvailable Boolean representing if D-Bus sensor is marked as
611 * available.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500612 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700613 */
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700614inline resource::State getState(const InventoryItem* inventoryItem,
615 const bool sensorAvailable)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500616{
617 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
618 {
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700619 return resource::State::Absent;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500620 }
James Feist34dd1792019-05-17 14:10:54 -0700621
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700622 if (!sensorAvailable)
623 {
624 return resource::State::UnavailableOffline;
625 }
626
627 return resource::State::Enabled;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500628}
629
630/**
631 * @brief Returns the Redfish Health value for the specified sensor.
632 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700633 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500634 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
635 * be nullptr if no associated inventory item was found.
636 * @return Health value for sensor.
637 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700638inline std::string getHealth(nlohmann::json& sensorJson,
639 const dbus::utility::DBusPropertiesMap& valuesDict,
640 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700641{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500642 // Get current health value (if any) in the sensor JSON object. Some JSON
643 // objects contain multiple sensors (such as PowerSupplies). We want to set
644 // the overall health to be the most severe of any of the sensors.
645 std::string currentHealth;
646 auto statusIt = sensorJson.find("Status");
647 if (statusIt != sensorJson.end())
648 {
649 auto healthIt = statusIt->find("Health");
650 if (healthIt != statusIt->end())
651 {
652 std::string* health = healthIt->get_ptr<std::string*>();
653 if (health != nullptr)
654 {
655 currentHealth = *health;
656 }
657 }
658 }
659
660 // If current health in JSON object is already Critical, return that. This
661 // should override the sensor health, which might be less severe.
662 if (currentHealth == "Critical")
663 {
664 return "Critical";
665 }
666
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200667 const bool* criticalAlarmHigh = nullptr;
668 const bool* criticalAlarmLow = nullptr;
669 const bool* warningAlarmHigh = nullptr;
670 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800671
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200672 const bool success = sdbusplus::unpackPropertiesNoThrow(
673 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
674 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
675 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
676 warningAlarmLow);
677
678 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700679 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200680 // Check if sensor has critical threshold alarm
681 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
682 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700683 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200684 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700685 }
686 }
687
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500688 // Check if associated inventory item is not functional
689 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
690 {
691 return "Critical";
692 }
693
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200694 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500695 // should override the sensor status, which might be less severe.
696 if (currentHealth == "Warning")
697 {
698 return "Warning";
699 }
700
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200701 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700702 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200703 // Check if sensor has warning threshold alarm
704 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
705 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700706 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200707 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700708 }
709 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500710
James Feist34dd1792019-05-17 14:10:54 -0700711 return "OK";
712}
713
Ed Tanous23a21a12020-07-25 04:45:05 +0000714inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500715 const InventoryItem* inventoryItem)
716{
717 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
718 {
719 switch (inventoryItem->ledState)
720 {
721 case LedState::OFF:
Ed Tanous539d8c62024-06-19 14:38:27 -0700722 sensorJson["IndicatorLED"] = resource::IndicatorLED::Off;
Anthony Wilsond5005492019-07-31 16:34:17 -0500723 break;
724 case LedState::ON:
Ed Tanous539d8c62024-06-19 14:38:27 -0700725 sensorJson["IndicatorLED"] = resource::IndicatorLED::Lit;
Anthony Wilsond5005492019-07-31 16:34:17 -0500726 break;
727 case LedState::BLINK:
Ed Tanous539d8c62024-06-19 14:38:27 -0700728 sensorJson["IndicatorLED"] = resource::IndicatorLED::Blinking;
Anthony Wilsond5005492019-07-31 16:34:17 -0500729 break;
Ed Tanous4da04902024-03-19 11:32:44 -0700730 default:
Anthony Wilsond5005492019-07-31 16:34:17 -0500731 break;
732 }
733 }
734}
735
James Feist34dd1792019-05-17 14:10:54 -0700736/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100737 * @brief Builds a json sensor representation of a sensor.
738 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500739 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100740 * build
Ed Tanous8ece0e42024-01-02 13:16:50 -0800741 * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
Ed Tanous1d7c0052022-08-09 12:32:26 -0700742 * @param propertiesDict A dictionary of the properties to build the sensor
743 * from.
744 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500745 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
746 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100747 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700748inline void objectPropertiesToJson(
749 std::string_view sensorName, std::string_view sensorType,
750 std::string_view chassisSubNode,
751 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000752 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700753{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700754 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500755 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800756 std::string subNodeEscaped(sensorType);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700757 auto remove = std::ranges::remove(subNodeEscaped, '_');
758 subNodeEscaped.erase(std::ranges::begin(remove), subNodeEscaped.end());
Ed Tanousc1d019a2022-08-06 09:36:06 -0700759
760 // For sensors in SensorCollection we set Id instead of MemberId,
761 // including power sensors.
762 subNodeEscaped += '_';
763 subNodeEscaped += sensorName;
764 sensorJson["Id"] = std::move(subNodeEscaped);
765
Ed Tanous1d7c0052022-08-09 12:32:26 -0700766 std::string sensorNameEs(sensorName);
767 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
768 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500769 }
770 else if (sensorType != "power")
771 {
772 // Set MemberId and Name for non-power sensors. For PowerSupplies and
773 // PowerControl, those properties have more general values because
774 // multiple sensors can be stored in the same JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700775 std::string sensorNameEs(sensorName);
776 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
777 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500778 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700779
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700780 const bool* checkAvailable = nullptr;
781 bool available = true;
782 const bool success = sdbusplus::unpackPropertiesNoThrow(
783 dbus_utils::UnpackErrorPrinter(), propertiesDict, "Available",
784 checkAvailable);
785 if (!success)
786 {
787 messages::internalError();
788 }
789 if (checkAvailable != nullptr)
790 {
791 available = *checkAvailable;
792 }
793
794 sensorJson["Status"]["State"] = getState(inventoryItem, available);
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400795 sensorJson["Status"]["Health"] =
796 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700797
798 // Parameter to set to override the type we get from dbus, and force it to
799 // int, regardless of what is available. This is used for schemas like fan,
800 // that require integers, not floats.
801 bool forceToInt = false;
802
Anthony Wilson3929aca2019-07-19 15:42:33 -0500803 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700804 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500805 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000806 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000807
Ed Tanous0ec8b832022-03-14 14:56:47 -0700808 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
809 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500810 {
Ed Tanous62598e32023-07-17 17:06:25 -0700811 BMCWEB_LOG_ERROR("Redfish cannot map reading type for {}",
812 sensorType);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500813 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000814 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500815 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000816 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500817 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000818
Ed Tanous1d7c0052022-08-09 12:32:26 -0700819 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000820 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200821 {
Ed Tanous62598e32023-07-17 17:06:25 -0700822 BMCWEB_LOG_ERROR("Redfish cannot map reading unit for {}",
823 sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000824 }
825 else
826 {
827 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200828 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500829 }
830 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700831 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500832 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000833 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700834 // TODO(ed) Documentation says that path should be type fan_tach,
835 // implementation seems to implement fan
836 }
837 else if (sensorType == "fan" || sensorType == "fan_tach")
838 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500839 unit = "/Reading"_json_pointer;
Ed Tanous539d8c62024-06-19 14:38:27 -0700840 sensorJson["ReadingUnits"] = thermal::ReadingUnits::RPM;
Ed Tanous81ce6092020-12-17 16:54:55 +0000841 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
842 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700843 forceToInt = true;
844 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700845 else if (sensorType == "fan_pwm")
846 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500847 unit = "/Reading"_json_pointer;
Ed Tanous539d8c62024-06-19 14:38:27 -0700848 sensorJson["ReadingUnits"] = thermal::ReadingUnits::Percent;
Ed Tanous81ce6092020-12-17 16:54:55 +0000849 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
850 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700851 forceToInt = true;
852 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 else if (sensorType == "voltage")
854 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500855 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000856 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700858 else if (sensorType == "power")
859 {
Ed Tanous18f8f602023-07-18 10:07:23 -0700860 std::string lower;
861 std::ranges::transform(sensorName, std::back_inserter(lower),
862 bmcweb::asciiToLower);
863 if (lower == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000864 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000865 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500866 // Put multiple "sensors" into a single PowerControl, so have
867 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000868 sensorJson["MemberId"] = "0";
869 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500870 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000871 }
Ed Tanous18f8f602023-07-18 10:07:23 -0700872 else if (lower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700873 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500874 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700875 }
876 else
877 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500878 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700879 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700880 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 else
882 {
Ed Tanous62598e32023-07-17 17:06:25 -0700883 BMCWEB_LOG_ERROR("Redfish cannot map object type for {}", sensorName);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700884 return;
885 }
886 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500887 std::vector<
888 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
889 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700890 properties.reserve(7);
891
892 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600893
Ed Tanous1d7c0052022-08-09 12:32:26 -0700894 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500895 {
896 properties.emplace_back(
897 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
898 "/Thresholds/UpperCaution/Reading"_json_pointer);
899 properties.emplace_back(
900 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
901 "/Thresholds/LowerCaution/Reading"_json_pointer);
902 properties.emplace_back(
903 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
904 "/Thresholds/UpperCritical/Reading"_json_pointer);
905 properties.emplace_back(
906 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
907 "/Thresholds/LowerCritical/Reading"_json_pointer);
908 }
909 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600910 {
911 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 "WarningHigh",
913 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600914 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500915 "WarningLow",
916 "/LowerThresholdNonCritical"_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 "CriticalHigh",
919 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600920 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500921 "CriticalLow",
922 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600923 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700924
Ed Tanous2474adf2018-09-05 16:31:16 -0700925 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
926
Ed Tanous1d7c0052022-08-09 12:32:26 -0700927 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500928 {
929 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500930 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500931 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500932 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800933 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
934 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500935 }
936 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700937 {
938 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500939 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500941 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500943 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700944 {
945 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500946 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700947 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500948 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700949 }
950
Anthony Wilson3929aca2019-07-19 15:42:33 -0500951 for (const std::tuple<const char*, const char*,
952 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700953 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700954 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700955 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700956 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800958 continue;
959 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700960
961 // The property we want to set may be nested json, so use
962 // a json_pointer for easy indexing into the json structure.
963 const nlohmann::json::json_pointer& key = std::get<2>(p);
964
Ed Tanous1d7c0052022-08-09 12:32:26 -0700965 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700966 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800967 {
Ed Tanous62598e32023-07-17 17:06:25 -0700968 BMCWEB_LOG_ERROR("Got value interface that wasn't double");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700969 continue;
970 }
Ed Tanous283860f2022-08-29 14:08:50 -0700971 if (!std::isfinite(*doubleValue))
972 {
973 if (valueName == "Value")
974 {
975 // Readings are allowed to be NAN for unavailable; coerce
976 // them to null in the json response.
977 sensorJson[key] = nullptr;
978 continue;
979 }
Ed Tanous62598e32023-07-17 17:06:25 -0700980 BMCWEB_LOG_WARNING("Sensor value for {} was unexpectedly {}",
981 valueName, *doubleValue);
Ed Tanous283860f2022-08-29 14:08:50 -0700982 continue;
983 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700984 if (forceToInt)
985 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700986 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700987 }
988 else
989 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700990 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 }
992 }
993 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100994}
995
Ed Tanous1d7c0052022-08-09 12:32:26 -0700996/**
997 * @brief Builds a json sensor representation of a sensor.
998 * @param sensorName The name of the sensor to be built
999 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
1000 * build
Ed Tanous8ece0e42024-01-02 13:16:50 -08001001 * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
Ed Tanous1d7c0052022-08-09 12:32:26 -07001002 * @param interfacesDict A dictionary of the interfaces and properties of said
1003 * interfaces to be built from
1004 * @param sensorJson The json object to fill
1005 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1006 * be nullptr if no associated inventory item was found.
1007 */
1008inline void objectInterfacesToJson(
1009 const std::string& sensorName, const std::string& sensorType,
1010 const std::string& chassisSubNode,
Michael Shen80f79a42023-08-24 13:41:53 +00001011 const dbus::utility::DBusInterfacesMap& interfacesDict,
Ed Tanous1d7c0052022-08-09 12:32:26 -07001012 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1013{
Ed Tanous1d7c0052022-08-09 12:32:26 -07001014 for (const auto& [interface, valuesDict] : interfacesDict)
1015 {
1016 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1017 valuesDict, sensorJson, inventoryItem);
1018 }
Ed Tanous62598e32023-07-17 17:06:25 -07001019 BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
Ed Tanous1d7c0052022-08-09 12:32:26 -07001020}
1021
Ed Tanousb5a76932020-09-29 16:16:58 -07001022inline void populateFanRedundancy(
1023 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001024{
George Liue99073f2022-12-09 11:06:16 +08001025 constexpr std::array<std::string_view, 1> interfaces = {
1026 "xyz.openbmc_project.Control.FanRedundancy"};
1027 dbus::utility::getSubTree(
1028 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001029 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +08001030 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001031 const dbus::utility::MapperGetSubTreeResponse& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001032 if (ec)
James Feist8bd25cc2019-03-15 15:14:00 -07001033 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001034 return; // don't have to have this interface
James Feist8bd25cc2019-03-15 15:14:00 -07001035 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001036 for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
1037 pathPair : resp)
1038 {
1039 const std::string& path = pathPair.first;
1040 const dbus::utility::MapperServiceMap& objDict =
1041 pathPair.second;
1042 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001043 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001044 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001045 }
1046
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001047 const std::string& owner = objDict.begin()->first;
1048 dbus::utility::getAssociationEndPoints(
1049 path + "/chassis",
1050 [path, owner, sensorsAsyncResp](
1051 const boost::system::error_code& ec2,
1052 const dbus::utility::MapperEndPoints& endpoints) {
1053 if (ec2)
James Feist8bd25cc2019-03-15 15:14:00 -07001054 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001055 return; // if they don't have an association we
1056 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001057 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001058 auto found = std::ranges::find_if(
1059 endpoints,
1060 [sensorsAsyncResp](const std::string& entry) {
1061 return entry.find(
1062 sensorsAsyncResp->chassisId) !=
1063 std::string::npos;
1064 });
1065
1066 if (found == endpoints.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001067 {
1068 return;
1069 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001070 sdbusplus::asio::getAllProperties(
1071 *crow::connections::systemBus, owner, path,
1072 "xyz.openbmc_project.Control.FanRedundancy",
1073 [path, sensorsAsyncResp](
1074 const boost::system::error_code& ec3,
1075 const dbus::utility::DBusPropertiesMap& ret) {
1076 if (ec3)
1077 {
1078 return; // don't have to have this
1079 // interface
1080 }
James Feist8bd25cc2019-03-15 15:14:00 -07001081
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001082 const uint8_t* allowedFailures = nullptr;
1083 const std::vector<std::string>* collection =
1084 nullptr;
1085 const std::string* status = nullptr;
James Feist8bd25cc2019-03-15 15:14:00 -07001086
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001087 const bool success =
1088 sdbusplus::unpackPropertiesNoThrow(
1089 dbus_utils::UnpackErrorPrinter(), ret,
1090 "AllowedFailures", allowedFailures,
1091 "Collection", collection, "Status",
1092 status);
James Feist8bd25cc2019-03-15 15:14:00 -07001093
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001094 if (!success)
1095 {
1096 messages::internalError(
1097 sensorsAsyncResp->asyncResp->res);
1098 return;
1099 }
1100
1101 if (allowedFailures == nullptr ||
1102 collection == nullptr || status == nullptr)
1103 {
1104 BMCWEB_LOG_ERROR(
1105 "Invalid redundancy interface");
1106 messages::internalError(
1107 sensorsAsyncResp->asyncResp->res);
1108 return;
1109 }
1110
1111 sdbusplus::message::object_path objectPath(
1112 path);
1113 std::string name = objectPath.filename();
1114 if (name.empty())
1115 {
1116 // this should be impossible
1117 messages::internalError(
1118 sensorsAsyncResp->asyncResp->res);
1119 return;
1120 }
1121 std::ranges::replace(name, '_', ' ');
1122
1123 std::string health;
1124
1125 if (status->ends_with("Full"))
1126 {
1127 health = "OK";
1128 }
1129 else if (status->ends_with("Degraded"))
1130 {
1131 health = "Warning";
1132 }
1133 else
1134 {
1135 health = "Critical";
1136 }
1137 nlohmann::json::array_t redfishCollection;
1138 const auto& fanRedfish =
1139 sensorsAsyncResp->asyncResp->res
1140 .jsonValue["Fans"];
1141 for (const std::string& item : *collection)
1142 {
1143 sdbusplus::message::object_path itemPath(
1144 item);
1145 std::string itemName = itemPath.filename();
1146 if (itemName.empty())
1147 {
1148 continue;
1149 }
1150 /*
1151 todo(ed): merge patch that fixes the names
1152 std::replace(itemName.begin(),
1153 itemName.end(), '_', ' ');*/
1154 auto schemaItem = std::ranges::find_if(
1155 fanRedfish,
1156 [itemName](const nlohmann::json& fan) {
1157 return fan["Name"] == itemName;
1158 });
1159 if (schemaItem != fanRedfish.end())
1160 {
1161 nlohmann::json::object_t collectionId;
1162 collectionId["@odata.id"] =
1163 (*schemaItem)["@odata.id"];
1164 redfishCollection.emplace_back(
1165 std::move(collectionId));
1166 }
1167 else
1168 {
1169 BMCWEB_LOG_ERROR(
1170 "failed to find fan in schema");
1171 messages::internalError(
1172 sensorsAsyncResp->asyncResp->res);
1173 return;
1174 }
1175 }
1176
1177 size_t minNumNeeded =
1178 collection->empty()
1179 ? 0
1180 : collection->size() - *allowedFailures;
1181 nlohmann::json& jResp =
1182 sensorsAsyncResp->asyncResp->res
1183 .jsonValue["Redundancy"];
1184
1185 nlohmann::json::object_t redundancy;
1186 boost::urls::url url = boost::urls::format(
1187 "/redfish/v1/Chassis/{}/{}",
1188 sensorsAsyncResp->chassisId,
1189 sensorsAsyncResp->chassisSubNode);
1190 url.set_fragment(
1191 ("/Redundancy"_json_pointer / jResp.size())
1192 .to_string());
1193 redundancy["@odata.id"] = std::move(url);
1194 redundancy["@odata.type"] =
1195 "#Redundancy.v1_3_2.Redundancy";
1196 redundancy["MinNumNeeded"] = minNumNeeded;
1197 redundancy["Mode"] =
1198 redundancy::RedundancyType::NPlusM;
1199 redundancy["Name"] = name;
1200 redundancy["RedundancySet"] = redfishCollection;
1201 redundancy["Status"]["Health"] = health;
1202 redundancy["Status"]["State"] =
1203 resource::State::Enabled;
1204
1205 jResp.emplace_back(std::move(redundancy));
1206 });
1207 });
1208 }
1209 });
James Feist8bd25cc2019-03-15 15:14:00 -07001210}
1211
Ed Tanousb5a76932020-09-29 16:16:58 -07001212inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001213 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001214{
zhanghch058d1b46d2021-04-01 11:18:24 +08001215 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001216 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001217 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001218 {
1219 sensorHeaders = {"Voltages", "PowerSupplies"};
1220 }
1221 for (const std::string& sensorGroup : sensorHeaders)
1222 {
1223 nlohmann::json::iterator entry = response.find(sensorGroup);
1224 if (entry != response.end())
1225 {
1226 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001227 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001228 return c1["Name"] < c2["Name"];
1229 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001230
1231 // add the index counts to the end of each entry
1232 size_t count = 0;
1233 for (nlohmann::json& sensorJson : *entry)
1234 {
1235 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1236 if (odata == sensorJson.end())
1237 {
1238 continue;
1239 }
1240 std::string* value = odata->get_ptr<std::string*>();
1241 if (value != nullptr)
1242 {
Willy Tueddfc432022-09-26 16:46:38 +00001243 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001244 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001245 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001246 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001247 }
1248 }
1249 }
1250 }
1251}
1252
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001253/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001254 * @brief Finds the inventory item with the specified object path.
1255 * @param inventoryItems D-Bus inventory items associated with sensors.
1256 * @param invItemObjPath D-Bus object path of inventory item.
1257 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001258 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001259inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001260 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001261 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001262{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001263 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001264 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001265 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001266 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001267 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001268 }
1269 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001270 return nullptr;
1271}
1272
1273/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001274 * @brief Finds the inventory item associated with the specified sensor.
1275 * @param inventoryItems D-Bus inventory items associated with sensors.
1276 * @param sensorObjPath D-Bus object path of sensor.
1277 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001278 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001279inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001280 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001281 const std::string& sensorObjPath)
1282{
1283 for (InventoryItem& inventoryItem : *inventoryItems)
1284 {
Ed Tanousdb0d36e2023-06-30 10:37:05 -07001285 if (inventoryItem.sensors.contains(sensorObjPath))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001286 {
1287 return &inventoryItem;
1288 }
1289 }
1290 return nullptr;
1291}
1292
1293/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001294 * @brief Finds the inventory item associated with the specified led path.
1295 * @param inventoryItems D-Bus inventory items associated with sensors.
1296 * @param ledObjPath D-Bus object path of led.
1297 * @return Inventory item within vector, or nullptr if no match found.
1298 */
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001299inline InventoryItem* findInventoryItemForLed(
1300 std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath)
Anthony Wilsond5005492019-07-31 16:34:17 -05001301{
1302 for (InventoryItem& inventoryItem : inventoryItems)
1303 {
1304 if (inventoryItem.ledObjectPath == ledObjPath)
1305 {
1306 return &inventoryItem;
1307 }
1308 }
1309 return nullptr;
1310}
1311
1312/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001313 * @brief Adds inventory item and associated sensor to specified vector.
1314 *
1315 * Adds a new InventoryItem to the vector if necessary. Searches for an
1316 * existing InventoryItem with the specified object path. If not found, one is
1317 * added to the vector.
1318 *
1319 * Next, the specified sensor is added to the set of sensors associated with the
1320 * InventoryItem.
1321 *
1322 * @param inventoryItems D-Bus inventory items associated with sensors.
1323 * @param invItemObjPath D-Bus object path of inventory item.
1324 * @param sensorObjPath D-Bus object path of sensor
1325 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001326inline void addInventoryItem(
1327 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1328 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001329{
1330 // Look for inventory item in vector
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001331 InventoryItem* inventoryItem =
1332 findInventoryItem(inventoryItems, invItemObjPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001333
1334 // If inventory item doesn't exist in vector, add it
1335 if (inventoryItem == nullptr)
1336 {
1337 inventoryItems->emplace_back(invItemObjPath);
1338 inventoryItem = &(inventoryItems->back());
1339 }
1340
1341 // Add sensor to set of sensors associated with inventory item
1342 inventoryItem->sensors.emplace(sensorObjPath);
1343}
1344
1345/**
1346 * @brief Stores D-Bus data in the specified inventory item.
1347 *
1348 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1349 * specified InventoryItem.
1350 *
1351 * This data is later used to provide sensor property values in the JSON
1352 * response.
1353 *
1354 * @param inventoryItem Inventory item where data will be stored.
1355 * @param interfacesDict Map containing D-Bus interfaces and their properties
1356 * for the specified inventory item.
1357 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001358inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001359 InventoryItem& inventoryItem,
Michael Shen80f79a42023-08-24 13:41:53 +00001360 const dbus::utility::DBusInterfacesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001361{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001362 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001363
Ed Tanous9eb808c2022-01-25 10:19:23 -08001364 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001365 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001366 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001367 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001368 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001369 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001370 if (name == "Present")
1371 {
1372 const bool* value = std::get_if<bool>(&dbusValue);
1373 if (value != nullptr)
1374 {
1375 inventoryItem.isPresent = *value;
1376 }
1377 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001378 }
1379 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001380 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001381
Ed Tanous711ac7a2021-12-20 09:34:41 -08001382 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001383 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001384 inventoryItem.isPowerSupply = true;
1385 }
1386
1387 // Get properties from Inventory.Decorator.Asset interface
1388 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1389 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001390 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001391 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001392 if (name == "Manufacturer")
1393 {
1394 const std::string* value =
1395 std::get_if<std::string>(&dbusValue);
1396 if (value != nullptr)
1397 {
1398 inventoryItem.manufacturer = *value;
1399 }
1400 }
1401 if (name == "Model")
1402 {
1403 const std::string* value =
1404 std::get_if<std::string>(&dbusValue);
1405 if (value != nullptr)
1406 {
1407 inventoryItem.model = *value;
1408 }
1409 }
1410 if (name == "SerialNumber")
1411 {
1412 const std::string* value =
1413 std::get_if<std::string>(&dbusValue);
1414 if (value != nullptr)
1415 {
1416 inventoryItem.serialNumber = *value;
1417 }
1418 }
1419 if (name == "PartNumber")
1420 {
1421 const std::string* value =
1422 std::get_if<std::string>(&dbusValue);
1423 if (value != nullptr)
1424 {
1425 inventoryItem.partNumber = *value;
1426 }
1427 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001428 }
1429 }
1430
Ed Tanous711ac7a2021-12-20 09:34:41 -08001431 if (interface ==
1432 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001433 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001434 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001435 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001436 if (name == "Functional")
1437 {
1438 const bool* value = std::get_if<bool>(&dbusValue);
1439 if (value != nullptr)
1440 {
1441 inventoryItem.isFunctional = *value;
1442 }
1443 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001444 }
1445 }
1446 }
1447}
1448
1449/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001450 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001451 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001452 * Uses the specified connections (services) to obtain D-Bus data for inventory
1453 * items associated with sensors. Stores the resulting data in the
1454 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001455 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001456 * This data is later used to provide sensor property values in the JSON
1457 * response.
1458 *
1459 * Finds the inventory item data asynchronously. Invokes callback when data has
1460 * been obtained.
1461 *
1462 * The callback must have the following signature:
1463 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001464 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001465 * @endcode
1466 *
1467 * This function is called recursively, obtaining data asynchronously from one
1468 * connection in each call. This ensures the callback is not invoked until the
1469 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001470 *
1471 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001472 * @param inventoryItems D-Bus inventory items associated with sensors.
1473 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001474 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001475 * @param callback Callback to invoke when inventory data has been obtained.
1476 * @param invConnectionsIndex Current index in invConnections. Only specified
1477 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001478 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001479template <typename Callback>
1480static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001481 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001482 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001483 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1484 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001485{
Ed Tanous62598e32023-07-17 17:06:25 -07001486 BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001487
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001488 // If no more connections left, call callback
1489 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001490 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001491 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001492 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001493 return;
1494 }
1495
1496 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001497 auto it = invConnections->begin();
1498 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001499 if (it != invConnections->end())
1500 {
1501 const std::string& invConnection = *it;
1502
George Liu5eb468d2023-06-20 17:03:24 +08001503 // Get all object paths and their interfaces for current connection
1504 sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
1505 dbus::utility::getManagedObjects(
1506 invConnection, path,
1507 [sensorsAsyncResp, inventoryItems, invConnections,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001508 callback = std::forward<Callback>(callback), invConnectionsIndex](
George Liu5eb468d2023-06-20 17:03:24 +08001509 const boost::system::error_code& ec,
1510 const dbus::utility::ManagedObjectType& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001511 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
1512 if (ec)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001513 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001514 BMCWEB_LOG_ERROR(
1515 "getInventoryItemsData respHandler DBus error {}", ec);
1516 messages::internalError(sensorsAsyncResp->asyncResp->res);
1517 return;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001518 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001519
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001520 // Loop through returned object paths
1521 for (const auto& objDictEntry : resp)
1522 {
1523 const std::string& objPath =
1524 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001525
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001526 // If this object path is one of the specified inventory
1527 // items
1528 InventoryItem* inventoryItem =
1529 findInventoryItem(inventoryItems, objPath);
1530 if (inventoryItem != nullptr)
1531 {
1532 // Store inventory data in InventoryItem
1533 storeInventoryItemData(*inventoryItem,
1534 objDictEntry.second);
1535 }
1536 }
1537
1538 // Recurse to get inventory item data from next connection
1539 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1540 invConnections, std::move(callback),
1541 invConnectionsIndex + 1);
1542
1543 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
1544 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001545 }
1546
Ed Tanous62598e32023-07-17 17:06:25 -07001547 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001548}
1549
1550/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001551 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001552 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001553 * Gets the D-Bus connections (services) that provide data for the inventory
1554 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 *
1556 * Finds the connections asynchronously. Invokes callback when information has
1557 * been obtained.
1558 *
1559 * The callback must have the following signature:
1560 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001561 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001562 * @endcode
1563 *
1564 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001565 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001566 * @param callback Callback to invoke when connections have been obtained.
1567 */
1568template <typename Callback>
1569static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001570 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1571 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001572 Callback&& callback)
1573{
Ed Tanous62598e32023-07-17 17:06:25 -07001574 BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001575
1576 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001577 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1580 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001581 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1582
George Liue99073f2022-12-09 11:06:16 +08001583 // Make call to ObjectMapper to find all inventory items
1584 dbus::utility::getSubTree(
1585 path, 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001586 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous002d39b2022-05-31 08:59:27 -07001587 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001588 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001589 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001590 // Response handler for parsing output from GetSubTree
1591 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
1592 if (ec)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001593 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001594 messages::internalError(sensorsAsyncResp->asyncResp->res);
1595 BMCWEB_LOG_ERROR(
1596 "getInventoryItemsConnections respHandler DBus error {}",
1597 ec);
1598 return;
1599 }
1600
1601 // Make unique list of connections for desired inventory items
1602 std::shared_ptr<std::set<std::string>> invConnections =
1603 std::make_shared<std::set<std::string>>();
1604
1605 // Loop through objects from GetSubTree
1606 for (const std::pair<std::string,
1607 std::vector<std::pair<
1608 std::string, std::vector<std::string>>>>&
1609 object : subtree)
1610 {
1611 // Check if object path is one of the specified inventory items
1612 const std::string& objPath = object.first;
1613 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001614 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001615 // Store all connections to inventory item
1616 for (const std::pair<std::string, std::vector<std::string>>&
1617 objData : object.second)
1618 {
1619 const std::string& invConnection = objData.first;
1620 invConnections->insert(invConnection);
1621 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001622 }
1623 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001624
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001625 callback(invConnections);
1626 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1627 });
Ed Tanous62598e32023-07-17 17:06:25 -07001628 BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001629}
1630
1631/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001632 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633 *
1634 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001635 * inventory items. Then finds the associations from those inventory items to
1636 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001637 *
1638 * Finds the inventory items asynchronously. Invokes callback when information
1639 * has been obtained.
1640 *
1641 * The callback must have the following signature:
1642 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001643 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001644 * @endcode
1645 *
1646 * @param sensorsAsyncResp Pointer to object holding response data.
1647 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001648 * implements ObjectManager.
1649 * @param callback Callback to invoke when inventory items have been obtained.
1650 */
1651template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001652static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001653 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001654 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001655 Callback&& callback)
1656{
Ed Tanous62598e32023-07-17 17:06:25 -07001657 BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001658
George Liu5eb468d2023-06-20 17:03:24 +08001659 // Call GetManagedObjects on the ObjectMapper to get all associations
1660 sdbusplus::message::object_path path("/");
1661 dbus::utility::getManagedObjects(
1662 "xyz.openbmc_project.ObjectMapper", path,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001663 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001664 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001665 const dbus::utility::ManagedObjectType& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001666 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
1667 if (ec)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001668 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001669 BMCWEB_LOG_ERROR(
1670 "getInventoryItemAssociations respHandler DBus error {}",
1671 ec);
1672 messages::internalError(sensorsAsyncResp->asyncResp->res);
1673 return;
1674 }
1675
1676 // Create vector to hold list of inventory items
1677 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1678 std::make_shared<std::vector<InventoryItem>>();
1679
1680 // Loop through returned object paths
1681 std::string sensorAssocPath;
1682 sensorAssocPath.reserve(128); // avoid memory allocations
1683 for (const auto& objDictEntry : resp)
1684 {
1685 const std::string& objPath =
1686 static_cast<const std::string&>(objDictEntry.first);
1687
1688 // If path is inventory association for one of the specified
1689 // sensors
1690 for (const std::string& sensorName : *sensorNames)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001691 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001692 sensorAssocPath = sensorName;
1693 sensorAssocPath += "/inventory";
1694 if (objPath == sensorAssocPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001695 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001696 // Get Association interface for object path
1697 for (const auto& [interface, values] :
1698 objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001699 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001700 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001701 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001702 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001703 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001704 if (valueName == "endpoints")
Ed Tanous711ac7a2021-12-20 09:34:41 -08001705 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001706 const std::vector<std::string>*
1707 endpoints = std::get_if<
1708 std::vector<std::string>>(
1709 &value);
1710 if ((endpoints != nullptr) &&
1711 !endpoints->empty())
1712 {
1713 // Add inventory item to vector
1714 const std::string& invItemPath =
1715 endpoints->front();
1716 addInventoryItem(inventoryItems,
1717 invItemPath,
1718 sensorName);
1719 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001720 }
1721 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001722 }
1723 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001724 break;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001726 }
1727 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001728
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001729 // Now loop through the returned object paths again, this time to
1730 // find the leds associated with the inventory items we just found
1731 std::string inventoryAssocPath;
1732 inventoryAssocPath.reserve(128); // avoid memory allocations
1733 for (const auto& objDictEntry : resp)
Anthony Wilsond5005492019-07-31 16:34:17 -05001734 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001735 const std::string& objPath =
1736 static_cast<const std::string&>(objDictEntry.first);
1737
1738 for (InventoryItem& inventoryItem : *inventoryItems)
Anthony Wilsond5005492019-07-31 16:34:17 -05001739 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001740 inventoryAssocPath = inventoryItem.objectPath;
1741 inventoryAssocPath += "/leds";
1742 if (objPath == inventoryAssocPath)
Anthony Wilsond5005492019-07-31 16:34:17 -05001743 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001744 for (const auto& [interface, values] :
1745 objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001746 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001747 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001748 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001749 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001750 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001751 if (valueName == "endpoints")
Ed Tanous711ac7a2021-12-20 09:34:41 -08001752 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001753 const std::vector<std::string>*
1754 endpoints = std::get_if<
1755 std::vector<std::string>>(
1756 &value);
1757 if ((endpoints != nullptr) &&
1758 !endpoints->empty())
1759 {
1760 // Add inventory item to vector
1761 // Store LED path in inventory item
1762 const std::string& ledPath =
1763 endpoints->front();
1764 inventoryItem.ledObjectPath =
1765 ledPath;
1766 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001767 }
1768 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001769 }
1770 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001771
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001772 break;
1773 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001774 }
1775 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001776 callback(inventoryItems);
1777 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
1778 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001779
Ed Tanous62598e32023-07-17 17:06:25 -07001780 BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001781}
1782
1783/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001784 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1785 *
1786 * Uses the specified connections (services) to obtain D-Bus data for inventory
1787 * item leds associated with sensors. Stores the resulting data in the
1788 * inventoryItems vector.
1789 *
1790 * This data is later used to provide sensor property values in the JSON
1791 * response.
1792 *
1793 * Finds the inventory item led data asynchronously. Invokes callback when data
1794 * has been obtained.
1795 *
1796 * The callback must have the following signature:
1797 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001798 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001799 * @endcode
1800 *
1801 * This function is called recursively, obtaining data asynchronously from one
1802 * connection in each call. This ensures the callback is not invoked until the
1803 * last asynchronous function has completed.
1804 *
1805 * @param sensorsAsyncResp Pointer to object holding response data.
1806 * @param inventoryItems D-Bus inventory items associated with sensors.
1807 * @param ledConnections Connections that provide data for the inventory leds.
1808 * @param callback Callback to invoke when inventory data has been obtained.
1809 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1810 * in recursive calls to this function.
1811 */
1812template <typename Callback>
1813void getInventoryLedData(
1814 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1815 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001816 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001817 Callback&& callback, size_t ledConnectionsIndex = 0)
1818{
Ed Tanous62598e32023-07-17 17:06:25 -07001819 BMCWEB_LOG_DEBUG("getInventoryLedData enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001820
1821 // If no more connections left, call callback
1822 if (ledConnectionsIndex >= ledConnections->size())
1823 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001824 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001825 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001826 return;
1827 }
1828
1829 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001830 auto it = ledConnections->begin();
1831 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001832 if (it != ledConnections->end())
1833 {
1834 const std::string& ledPath = (*it).first;
1835 const std::string& ledConnection = (*it).second;
1836 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001837 auto respHandler =
1838 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001839 callback = std::forward<Callback>(callback), ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001840 const boost::system::error_code& ec, const std::string& state) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001841 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1842 if (ec)
1843 {
1844 BMCWEB_LOG_ERROR(
1845 "getInventoryLedData respHandler DBus error {}", ec);
1846 messages::internalError(sensorsAsyncResp->asyncResp->res);
1847 return;
1848 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001849
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001850 BMCWEB_LOG_DEBUG("Led state: {}", state);
1851 // Find inventory item with this LED object path
1852 InventoryItem* inventoryItem =
1853 findInventoryItemForLed(*inventoryItems, ledPath);
1854 if (inventoryItem != nullptr)
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001855 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001856 // Store LED state in InventoryItem
1857 if (state.ends_with("On"))
1858 {
1859 inventoryItem->ledState = LedState::ON;
1860 }
1861 else if (state.ends_with("Blink"))
1862 {
1863 inventoryItem->ledState = LedState::BLINK;
1864 }
1865 else if (state.ends_with("Off"))
1866 {
1867 inventoryItem->ledState = LedState::OFF;
1868 }
1869 else
1870 {
1871 inventoryItem->ledState = LedState::UNKNOWN;
1872 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001873 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001874
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001875 // Recurse to get LED data from next connection
1876 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1877 ledConnections, std::move(callback),
1878 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001879
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001880 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1881 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001882
1883 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001884 sdbusplus::asio::getProperty<std::string>(
1885 *crow::connections::systemBus, ledConnection, ledPath,
1886 "xyz.openbmc_project.Led.Physical", "State",
1887 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001888 }
1889
Ed Tanous62598e32023-07-17 17:06:25 -07001890 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001891}
1892
1893/**
1894 * @brief Gets LED data for LEDs associated with given inventory items.
1895 *
1896 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1897 * associated with the specified inventory items. Then gets the LED data from
1898 * each connection and stores it in the inventory item.
1899 *
1900 * This data is later used to provide sensor property values in the JSON
1901 * response.
1902 *
1903 * Finds the LED data asynchronously. Invokes callback when information has
1904 * been obtained.
1905 *
1906 * The callback must have the following signature:
1907 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001908 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001909 * @endcode
1910 *
1911 * @param sensorsAsyncResp Pointer to object holding response data.
1912 * @param inventoryItems D-Bus inventory items associated with sensors.
1913 * @param callback Callback to invoke when inventory items have been obtained.
1914 */
1915template <typename Callback>
1916void getInventoryLeds(
1917 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1918 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1919 Callback&& callback)
1920{
Ed Tanous62598e32023-07-17 17:06:25 -07001921 BMCWEB_LOG_DEBUG("getInventoryLeds enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001922
1923 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001924 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001925 "xyz.openbmc_project.Led.Physical"};
1926
George Liue99073f2022-12-09 11:06:16 +08001927 // Make call to ObjectMapper to find all inventory items
1928 dbus::utility::getSubTree(
1929 path, 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001930 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous002d39b2022-05-31 08:59:27 -07001931 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001932 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001933 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001934 // Response handler for parsing output from GetSubTree
1935 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1936 if (ec)
Anthony Wilsond5005492019-07-31 16:34:17 -05001937 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001938 messages::internalError(sensorsAsyncResp->asyncResp->res);
1939 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}",
1940 ec);
1941 return;
Anthony Wilsond5005492019-07-31 16:34:17 -05001942 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001943
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001944 // Build map of LED object paths to connections
1945 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1946 std::make_shared<std::map<std::string, std::string>>();
1947
1948 // Loop through objects from GetSubTree
1949 for (const std::pair<std::string,
1950 std::vector<std::pair<
1951 std::string, std::vector<std::string>>>>&
1952 object : subtree)
1953 {
1954 // Check if object path is LED for one of the specified
1955 // inventory items
1956 const std::string& ledPath = object.first;
1957 if (findInventoryItemForLed(*inventoryItems, ledPath) !=
1958 nullptr)
1959 {
1960 // Add mapping from ledPath to connection
1961 const std::string& connection =
1962 object.second.begin()->first;
1963 (*ledConnections)[ledPath] = connection;
1964 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath,
1965 connection);
1966 }
1967 }
1968
1969 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1970 ledConnections, std::move(callback));
1971 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1972 });
Ed Tanous62598e32023-07-17 17:06:25 -07001973 BMCWEB_LOG_DEBUG("getInventoryLeds exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001974}
1975
1976/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001977 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1978 *
1979 * Uses the specified connections (services) (currently assumes just one) to
1980 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1981 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1982 *
1983 * This data is later used to provide sensor property values in the JSON
1984 * response.
1985 *
1986 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1987 * when data has been obtained.
1988 *
1989 * The callback must have the following signature:
1990 * @code
1991 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1992 * @endcode
1993 *
1994 * @param sensorsAsyncResp Pointer to object holding response data.
1995 * @param inventoryItems D-Bus inventory items associated with sensors.
1996 * @param psAttributesConnections Connections that provide data for the Power
1997 * Supply Attributes
1998 * @param callback Callback to invoke when data has been obtained.
1999 */
2000template <typename Callback>
2001void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002002 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002003 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002004 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002005 Callback&& callback)
2006{
Ed Tanous62598e32023-07-17 17:06:25 -07002007 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002008
2009 if (psAttributesConnections.empty())
2010 {
Ed Tanous62598e32023-07-17 17:06:25 -07002011 BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002012 callback(inventoryItems);
2013 return;
2014 }
2015
2016 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002017 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002018
2019 const std::string& psAttributesPath = (*it).first;
2020 const std::string& psAttributesConnection = (*it).second;
2021
2022 // Response handler for Get DeratingFactor property
Patrick Williams5a39f772023-10-20 11:20:21 -05002023 auto respHandler = [sensorsAsyncResp, inventoryItems,
Ed Tanous8cb2c022024-03-27 16:31:46 -07002024 callback = std::forward<Callback>(callback)](
Patrick Williams5a39f772023-10-20 11:20:21 -05002025 const boost::system::error_code& ec,
2026 const uint32_t value) {
Ed Tanous62598e32023-07-17 17:06:25 -07002027 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002028 if (ec)
2029 {
Ed Tanous62598e32023-07-17 17:06:25 -07002030 BMCWEB_LOG_ERROR(
2031 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08002032 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002033 return;
2034 }
2035
Ed Tanous62598e32023-07-17 17:06:25 -07002036 BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002037 // Store value in Power Supply Inventory Items
2038 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002039 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002040 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002041 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002042 inventoryItem.powerSupplyEfficiencyPercent =
2043 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002044 }
2045 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002046
Ed Tanous62598e32023-07-17 17:06:25 -07002047 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002048 callback(inventoryItems);
2049 };
2050
2051 // Get the DeratingFactor property for the PowerSupplyAttributes
2052 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002053 sdbusplus::asio::getProperty<uint32_t>(
2054 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2055 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2056 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002057
Ed Tanous62598e32023-07-17 17:06:25 -07002058 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002059}
2060
2061/**
2062 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2063 *
2064 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2065 * data. Then gets the Power Supply Attributes data from the connection
2066 * (currently just assumes 1 connection) and stores the data in the inventory
2067 * item.
2068 *
2069 * This data is later used to provide sensor property values in the JSON
2070 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2071 *
2072 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2073 * when information has been obtained.
2074 *
2075 * The callback must have the following signature:
2076 * @code
2077 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2078 * @endcode
2079 *
2080 * @param sensorsAsyncResp Pointer to object holding response data.
2081 * @param inventoryItems D-Bus inventory items associated with sensors.
2082 * @param callback Callback to invoke when data has been obtained.
2083 */
2084template <typename Callback>
2085void getPowerSupplyAttributes(
2086 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2087 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2088 Callback&& callback)
2089{
Ed Tanous62598e32023-07-17 17:06:25 -07002090 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002091
2092 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002093 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002094 {
Ed Tanous62598e32023-07-17 17:06:25 -07002095 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002096 callback(inventoryItems);
2097 return;
2098 }
2099
George Liue99073f2022-12-09 11:06:16 +08002100 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002101 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2102
George Liue99073f2022-12-09 11:06:16 +08002103 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2104 dbus::utility::getSubTree(
2105 "/xyz/openbmc_project", 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07002106 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002107 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002108 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002109 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002110 // Response handler for parsing output from GetSubTree
2111 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
2112 if (ec)
2113 {
2114 messages::internalError(sensorsAsyncResp->asyncResp->res);
2115 BMCWEB_LOG_ERROR(
2116 "getPowerSupplyAttributes respHandler DBus error {}", ec);
2117 return;
2118 }
2119 if (subtree.empty())
2120 {
2121 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
2122 callback(inventoryItems);
2123 return;
2124 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002125
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002126 // Currently we only support 1 power supply attribute, use this for
2127 // all the power supplies. Build map of object path to connection.
2128 // Assume just 1 connection and 1 path for now.
2129 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002130
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002131 if (subtree[0].first.empty() || subtree[0].second.empty())
2132 {
2133 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
2134 callback(inventoryItems);
2135 return;
2136 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002137
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002138 const std::string& psAttributesPath = subtree[0].first;
2139 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002140
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002141 if (connection.empty())
2142 {
2143 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
2144 callback(inventoryItems);
2145 return;
2146 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002147
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002148 psAttributesConnections[psAttributesPath] = connection;
2149 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
2150 connection);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002151
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002152 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2153 psAttributesConnections,
2154 std::move(callback));
2155 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
2156 });
Ed Tanous62598e32023-07-17 17:06:25 -07002157 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002158}
2159
2160/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002161 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002162 *
2163 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002164 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002165 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002166 * This data is later used to provide sensor property values in the JSON
2167 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002168 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002169 * Finds the inventory items asynchronously. Invokes callback when the
2170 * inventory items have been obtained.
2171 *
2172 * The callback must have the following signature:
2173 * @code
2174 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2175 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002176 *
2177 * @param sensorsAsyncResp Pointer to object holding response data.
2178 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002179 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002180 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002181 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002182template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002183static void
2184 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2185 const std::shared_ptr<std::set<std::string>> sensorNames,
2186 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002187{
Ed Tanous62598e32023-07-17 17:06:25 -07002188 BMCWEB_LOG_DEBUG("getInventoryItems enter");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002189 auto getInventoryItemAssociationsCb =
Ed Tanous8cb2c022024-03-27 16:31:46 -07002190 [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002191 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002192 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
2193 auto getInventoryItemsConnectionsCb =
2194 [sensorsAsyncResp, inventoryItems,
2195 callback = std::forward<const Callback>(callback)](
2196 std::shared_ptr<std::set<std::string>> invConnections) {
2197 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
2198 auto getInventoryItemsDataCb =
2199 [sensorsAsyncResp, inventoryItems,
2200 callback{std::move(callback)}]() {
2201 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002202
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002203 auto getInventoryLedsCb =
2204 [sensorsAsyncResp, inventoryItems,
2205 callback{std::move(callback)}]() {
2206 BMCWEB_LOG_DEBUG(
2207 "getInventoryLedsCb enter");
2208 // Find Power Supply Attributes and get the
2209 // data
2210 getPowerSupplyAttributes(
2211 sensorsAsyncResp, inventoryItems,
2212 std::move(callback));
2213 BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
2214 };
2215
2216 // Find led connections and get the data
2217 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2218 std::move(getInventoryLedsCb));
2219 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
2220 };
2221
2222 // Get inventory item data from connections
2223 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2224 invConnections,
2225 std::move(getInventoryItemsDataCb));
2226 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002227 };
2228
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002229 // Get connections that provide inventory item data
2230 getInventoryItemsConnections(
2231 sensorsAsyncResp, inventoryItems,
2232 std::move(getInventoryItemsConnectionsCb));
2233 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002234 };
2235
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002236 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002237 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002238 std::move(getInventoryItemAssociationsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002239 BMCWEB_LOG_DEBUG("getInventoryItems exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002240}
2241
2242/**
2243 * @brief Returns JSON PowerSupply object for the specified inventory item.
2244 *
2245 * Searches for a JSON PowerSupply object that matches the specified inventory
2246 * item. If one is not found, a new PowerSupply object is added to the JSON
2247 * array.
2248 *
2249 * Multiple sensors are often associated with one power supply inventory item.
2250 * As a result, multiple sensor values are stored in one JSON PowerSupply
2251 * object.
2252 *
2253 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2254 * @param inventoryItem Inventory item for the power supply.
2255 * @param chassisId Chassis that contains the power supply.
2256 * @return JSON PowerSupply object for the specified inventory item.
2257 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002258inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002259 const InventoryItem& inventoryItem,
2260 const std::string& chassisId)
2261{
Ed Tanous18f8f602023-07-18 10:07:23 -07002262 std::string nameS;
Alexander Hansen6f4bd292024-03-08 17:04:54 +01002263 nameS.resize(inventoryItem.name.size());
Ed Tanous18f8f602023-07-18 10:07:23 -07002264 std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002265 // Check if matching PowerSupply object already exists in JSON array
2266 for (nlohmann::json& powerSupply : powerSupplyArray)
2267 {
Ed Tanous18f8f602023-07-18 10:07:23 -07002268 nlohmann::json::iterator nameIt = powerSupply.find("Name");
2269 if (nameIt == powerSupply.end())
2270 {
2271 continue;
2272 }
2273 const std::string* name = nameIt->get_ptr<std::string*>();
2274 if (name == nullptr)
2275 {
2276 continue;
2277 }
2278 if (nameS == *name)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002279 {
2280 return powerSupply;
2281 }
2282 }
2283
2284 // Add new PowerSupply object to JSON array
2285 powerSupplyArray.push_back({});
2286 nlohmann::json& powerSupply = powerSupplyArray.back();
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002287 boost::urls::url url =
2288 boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002289 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2290 powerSupply["@odata.id"] = std::move(url);
Ed Tanous18f8f602023-07-18 10:07:23 -07002291 std::string escaped;
Alexander Hansen6f4bd292024-03-08 17:04:54 +01002292 escaped.resize(inventoryItem.name.size());
Ed Tanous18f8f602023-07-18 10:07:23 -07002293 std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
2294 powerSupply["Name"] = std::move(escaped);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002295 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2296 powerSupply["Model"] = inventoryItem.model;
2297 powerSupply["PartNumber"] = inventoryItem.partNumber;
2298 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002299 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002300
Gunnar Mills42cbe532019-08-15 15:26:54 -05002301 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2302 {
2303 powerSupply["EfficiencyPercent"] =
2304 inventoryItem.powerSupplyEfficiencyPercent;
2305 }
2306
Matt Simmeringaaf08ac2023-10-04 08:41:01 -07002307 powerSupply["Status"]["State"] = getState(&inventoryItem, true);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002308 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2309 powerSupply["Status"]["Health"] = health;
2310
2311 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002312}
2313
2314/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002315 * @brief Gets the values of the specified sensors.
2316 *
2317 * Stores the results as JSON in the SensorsAsyncResp.
2318 *
2319 * Gets the sensor values asynchronously. Stores the results later when the
2320 * information has been obtained.
2321 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002322 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002323 *
2324 * To minimize the number of DBus calls, the DBus method
2325 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2326 * values of all sensors provided by a connection (service).
2327 *
2328 * The connections set contains all the connections that provide sensor values.
2329 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002330 * The InventoryItem vector contains D-Bus inventory items associated with the
2331 * sensors. Inventory item data is needed for some Redfish sensor properties.
2332 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002333 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002334 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002335 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002336 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002337 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002338 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002339inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002340 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002341 const std::shared_ptr<std::set<std::string>>& sensorNames,
2342 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002343 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002344{
Ed Tanous62598e32023-07-17 17:06:25 -07002345 BMCWEB_LOG_DEBUG("getSensorData enter");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002346 // Get managed objects from all services exposing sensors
2347 for (const std::string& connection : connections)
2348 {
George Liu5eb468d2023-06-20 17:03:24 +08002349 sdbusplus::message::object_path sensorPath(
2350 "/xyz/openbmc_project/sensors");
2351 dbus::utility::getManagedObjects(
2352 connection, sensorPath,
Ed Tanous002d39b2022-05-31 08:59:27 -07002353 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002354 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002355 const dbus::utility::ManagedObjectType& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002356 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
2357 if (ec)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002358 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002359 BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
2360 messages::internalError(sensorsAsyncResp->asyncResp->res);
2361 return;
2362 }
2363 // Go through all objects and update response with sensor data
2364 for (const auto& objDictEntry : resp)
2365 {
2366 const std::string& objPath =
2367 static_cast<const std::string&>(objDictEntry.first);
2368 BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002369 objPath);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002370
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002371 std::vector<std::string> split;
2372 // Reserve space for
2373 // /xyz/openbmc_project/sensors/<name>/<subname>
2374 split.reserve(6);
2375 // NOLINTNEXTLINE
2376 bmcweb::split(split, objPath, '/');
2377 if (split.size() < 6)
Nan Zhou928fefb2022-03-28 08:45:00 -07002378 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002379 BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
2380 objPath);
2381 continue;
Nan Zhou928fefb2022-03-28 08:45:00 -07002382 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002383 // These indexes aren't intuitive, as split puts an empty
2384 // string at the beginning
2385 const std::string& sensorType = split[4];
2386 const std::string& sensorName = split[5];
2387 BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
2388 sensorType);
2389 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002390 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002391 BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002392 continue;
2393 }
2394
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002395 // Find inventory item (if any) associated with sensor
2396 InventoryItem* inventoryItem =
2397 findInventoryItemForSensor(inventoryItems, objPath);
2398
2399 const std::string& sensorSchema =
2400 sensorsAsyncResp->chassisSubNode;
2401
2402 nlohmann::json* sensorJson = nullptr;
2403
2404 if (sensorSchema == sensors::node::sensors &&
2405 !sensorsAsyncResp->efficientExpand)
Nan Zhou928fefb2022-03-28 08:45:00 -07002406 {
Ed Tanous677bb752022-09-15 10:52:19 -07002407 std::string sensorTypeEscaped(sensorType);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002408 auto remove =
2409 std::ranges::remove(sensorTypeEscaped, '_');
2410
Ed Tanous3544d2a2023-08-06 18:12:20 -07002411 sensorTypeEscaped.erase(std::ranges::begin(remove),
2412 sensorTypeEscaped.end());
Ed Tanous677bb752022-09-15 10:52:19 -07002413 std::string sensorId(sensorTypeEscaped);
2414 sensorId += "_";
2415 sensorId += sensorName;
2416
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002417 sensorsAsyncResp->asyncResp->res
2418 .jsonValue["@odata.id"] = boost::urls::format(
Ed Tanousef4c65b2023-04-24 15:28:50 -07002419 "/redfish/v1/Chassis/{}/{}/{}",
Ed Tanous677bb752022-09-15 10:52:19 -07002420 sensorsAsyncResp->chassisId,
2421 sensorsAsyncResp->chassisSubNode, sensorId);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002422 sensorJson =
2423 &(sensorsAsyncResp->asyncResp->res.jsonValue);
Nan Zhou928fefb2022-03-28 08:45:00 -07002424 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002425 else
2426 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002427 std::string fieldName;
2428 if (sensorsAsyncResp->efficientExpand)
2429 {
2430 fieldName = "Members";
2431 }
2432 else if (sensorType == "temperature")
2433 {
2434 fieldName = "Temperatures";
2435 }
2436 else if (sensorType == "fan" ||
2437 sensorType == "fan_tach" ||
2438 sensorType == "fan_pwm")
2439 {
2440 fieldName = "Fans";
2441 }
2442 else if (sensorType == "voltage")
2443 {
2444 fieldName = "Voltages";
2445 }
2446 else if (sensorType == "power")
2447 {
2448 if (sensorName == "total_power")
2449 {
2450 fieldName = "PowerControl";
2451 }
2452 else if ((inventoryItem != nullptr) &&
2453 (inventoryItem->isPowerSupply))
2454 {
2455 fieldName = "PowerSupplies";
2456 }
2457 else
2458 {
2459 // Other power sensors are in SensorCollection
2460 continue;
2461 }
2462 }
2463 else
2464 {
2465 BMCWEB_LOG_ERROR(
2466 "Unsure how to handle sensorType {}",
2467 sensorType);
2468 continue;
2469 }
2470
2471 nlohmann::json& tempArray =
2472 sensorsAsyncResp->asyncResp->res
2473 .jsonValue[fieldName];
2474 if (fieldName == "PowerControl")
2475 {
2476 if (tempArray.empty())
2477 {
2478 // Put multiple "sensors" into a single
2479 // PowerControl. Follows MemberId naming and
2480 // naming in power.hpp.
2481 nlohmann::json::object_t power;
2482 boost::urls::url url = boost::urls::format(
2483 "/redfish/v1/Chassis/{}/{}",
2484 sensorsAsyncResp->chassisId,
2485 sensorsAsyncResp->chassisSubNode);
2486 url.set_fragment(
2487 (""_json_pointer / fieldName / "0")
2488 .to_string());
2489 power["@odata.id"] = std::move(url);
2490 tempArray.emplace_back(std::move(power));
2491 }
2492 sensorJson = &(tempArray.back());
2493 }
2494 else if (fieldName == "PowerSupplies")
2495 {
2496 if (inventoryItem != nullptr)
2497 {
2498 sensorJson = &(getPowerSupply(
2499 tempArray, *inventoryItem,
2500 sensorsAsyncResp->chassisId));
2501 }
2502 }
2503 else if (fieldName == "Members")
2504 {
2505 std::string sensorTypeEscaped(sensorType);
2506 auto remove =
2507 std::ranges::remove(sensorTypeEscaped, '_');
2508 sensorTypeEscaped.erase(std::ranges::begin(remove),
2509 sensorTypeEscaped.end());
2510 std::string sensorId(sensorTypeEscaped);
2511 sensorId += "_";
2512 sensorId += sensorName;
2513
2514 nlohmann::json::object_t member;
2515 member["@odata.id"] = boost::urls::format(
2516 "/redfish/v1/Chassis/{}/{}/{}",
2517 sensorsAsyncResp->chassisId,
2518 sensorsAsyncResp->chassisSubNode, sensorId);
2519 tempArray.emplace_back(std::move(member));
2520 sensorJson = &(tempArray.back());
2521 }
2522 else
2523 {
2524 nlohmann::json::object_t member;
2525 boost::urls::url url = boost::urls::format(
2526 "/redfish/v1/Chassis/{}/{}",
2527 sensorsAsyncResp->chassisId,
2528 sensorsAsyncResp->chassisSubNode);
2529 url.set_fragment(
2530 (""_json_pointer / fieldName).to_string());
2531 member["@odata.id"] = std::move(url);
2532 tempArray.emplace_back(std::move(member));
2533 sensorJson = &(tempArray.back());
2534 }
2535 }
2536
2537 if (sensorJson != nullptr)
2538 {
2539 objectInterfacesToJson(sensorName, sensorType,
2540 sensorsAsyncResp->chassisSubNode,
2541 objDictEntry.second, *sensorJson,
2542 inventoryItem);
2543
2544 std::string path = "/xyz/openbmc_project/sensors/";
2545 path += sensorType;
2546 path += "/";
2547 path += sensorName;
2548 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002549 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002550 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002551 if (sensorsAsyncResp.use_count() == 1)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002552 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002553 sortJSONResponse(sensorsAsyncResp);
2554 if (sensorsAsyncResp->chassisSubNode ==
2555 sensors::node::sensors &&
2556 sensorsAsyncResp->efficientExpand)
2557 {
2558 sensorsAsyncResp->asyncResp->res
2559 .jsonValue["Members@odata.count"] =
2560 sensorsAsyncResp->asyncResp->res
2561 .jsonValue["Members"]
2562 .size();
2563 }
2564 else if (sensorsAsyncResp->chassisSubNode ==
2565 sensors::node::thermal)
2566 {
2567 populateFanRedundancy(sensorsAsyncResp);
2568 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002569 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002570 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
2571 });
Ed Tanous23a21a12020-07-25 04:45:05 +00002572 }
Ed Tanous62598e32023-07-17 17:06:25 -07002573 BMCWEB_LOG_DEBUG("getSensorData exit");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002574}
2575
Nan Zhoufe04d492022-06-22 17:10:41 +00002576inline void
2577 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2578 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002579{
Nan Zhoufe04d492022-06-22 17:10:41 +00002580 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2581 const std::set<std::string>& connections) {
Ed Tanous62598e32023-07-17 17:06:25 -07002582 BMCWEB_LOG_DEBUG("getConnectionCb enter");
Ed Tanousd0090732022-10-04 17:22:56 -07002583 auto getInventoryItemsCb =
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002584 [sensorsAsyncResp, sensorNames, connections](
2585 const std::shared_ptr<std::vector<InventoryItem>>&
2586 inventoryItems) {
2587 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
2588 // Get sensor data and store results in JSON
2589 getSensorData(sensorsAsyncResp, sensorNames, connections,
2590 inventoryItems);
2591 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2592 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002593
Ed Tanousd0090732022-10-04 17:22:56 -07002594 // Get inventory items associated with sensors
2595 getInventoryItems(sensorsAsyncResp, sensorNames,
2596 std::move(getInventoryItemsCb));
2597
Ed Tanous62598e32023-07-17 17:06:25 -07002598 BMCWEB_LOG_DEBUG("getConnectionCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002599 };
2600
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002601 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002602 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002603}
2604
Shawn McCarneyde629b62019-03-08 10:42:51 -06002605/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002606 * @brief Entry point for retrieving sensors data related to requested
2607 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002608 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002609 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002610inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002611 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002612{
Ed Tanous62598e32023-07-17 17:06:25 -07002613 BMCWEB_LOG_DEBUG("getChassisData enter");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002614 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002615 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002616 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002617 BMCWEB_LOG_DEBUG("getChassisCb enter");
2618 processSensorList(sensorsAsyncResp, sensorNames);
2619 BMCWEB_LOG_DEBUG("getChassisCb exit");
2620 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002621 // SensorCollection doesn't contain the Redundancy property
2622 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2623 {
2624 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2625 nlohmann::json::array();
2626 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002627 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002628 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2629 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2630 std::move(getChassisCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002631 BMCWEB_LOG_DEBUG("getChassisData exit");
Ed Tanous271584a2019-07-09 16:24:22 -07002632}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002633
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302634/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002635 * @brief Find the requested sensorName in the list of all sensors supplied by
2636 * the chassis node
2637 *
2638 * @param sensorName The sensor name supplied in the PATCH request
2639 * @param sensorsList The list of sensors managed by the chassis node
2640 * @param sensorsModified The list of sensors that were found as a result of
2641 * repeated calls to this function
2642 */
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002643inline bool findSensorNameUsingSensorPath(
2644 std::string_view sensorName, const std::set<std::string>& sensorsList,
2645 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002646{
Nan Zhoufe04d492022-06-22 17:10:41 +00002647 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002648 {
George Liu28aa8de2021-02-01 15:13:30 +08002649 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002650 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002651 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002652 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002653 continue;
2654 }
2655 if (thisSensorName == sensorName)
2656 {
2657 sensorsModified.emplace(chassisSensor);
2658 return true;
2659 }
2660 }
2661 return false;
2662}
2663
Ed Tanousc71d6122022-11-29 14:10:32 -08002664inline std::pair<std::string, std::string>
2665 splitSensorNameAndType(std::string_view sensorId)
2666{
2667 size_t index = sensorId.find('_');
2668 if (index == std::string::npos)
2669 {
2670 return std::make_pair<std::string, std::string>("", "");
2671 }
2672 std::string sensorType{sensorId.substr(0, index)};
2673 std::string sensorName{sensorId.substr(index + 1)};
2674 // fan_pwm and fan_tach need special handling
2675 if (sensorType == "fantach" || sensorType == "fanpwm")
2676 {
2677 sensorType.insert(3, 1, '_');
2678 }
2679 return std::make_pair(sensorType, sensorName);
2680}
2681
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002682/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302683 * @brief Entry point for overriding sensor values of given sensor
2684 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002685 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002686 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002687 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302688 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002689inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002690 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Ed Tanous08850572024-03-06 15:09:17 -08002691 std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002692 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302693{
Ed Tanous62598e32023-07-17 17:06:25 -07002694 BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
2695 sensorAsyncResp->chassisSubNode);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302696
Ed Tanousd02aad32024-02-13 14:43:34 -08002697 std::string_view propertyValueName;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302698 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302699 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002700 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302701 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302702 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302703 if (collectionItems.first == "Temperatures")
2704 {
2705 propertyValueName = "ReadingCelsius";
2706 }
2707 else if (collectionItems.first == "Fans")
2708 {
2709 propertyValueName = "Reading";
2710 }
2711 else
2712 {
2713 propertyValueName = "ReadingVolts";
2714 }
2715 for (auto& item : collectionItems.second)
2716 {
Ed Tanous08850572024-03-06 15:09:17 -08002717 if (!json_util::readJsonObject(
2718 item, sensorAsyncResp->asyncResp->res, "MemberId", memberId,
2719 propertyValueName, value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302720 {
2721 return;
2722 }
2723 overrideMap.emplace(memberId,
2724 std::make_pair(value, collectionItems.first));
2725 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302726 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002727
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002728 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap,
2729 propertyValueNameStr =
2730 std::string(propertyValueName)](
2731 const std::shared_ptr<
2732 std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002733 // Match sensor names in the PATCH request to those managed by the
2734 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002735 const std::shared_ptr<std::set<std::string>> sensorNames =
2736 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302737 for (const auto& item : overrideMap)
2738 {
2739 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002740 std::pair<std::string, std::string> sensorNameType =
2741 splitSensorNameAndType(sensor);
2742 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2743 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302744 {
Ed Tanous62598e32023-07-17 17:06:25 -07002745 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
zhanghch058d1b46d2021-04-01 11:18:24 +08002746 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302747 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302748 return;
2749 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302750 }
2751 // Get the connection to which the memberId belongs
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002752 auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap,
2753 propertyValueNameStr](
2754 const std::set<
2755 std::string>& /*connections*/,
2756 const std::set<std::pair<
2757 std::string, std::string>>&
2758 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002759 if (objectsWithConnection.size() != overrideMap.size())
2760 {
Ed Tanous62598e32023-07-17 17:06:25 -07002761 BMCWEB_LOG_INFO(
2762 "Unable to find all objects with proper connection {} requested {}",
2763 objectsWithConnection.size(), overrideMap.size());
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002764 messages::resourceNotFound(
2765 sensorAsyncResp->asyncResp->res,
2766 sensorAsyncResp->chassisSubNode == sensors::node::thermal
2767 ? "Temperatures"
2768 : "Voltages",
2769 "Count");
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002770 return;
2771 }
2772 for (const auto& item : objectsWithConnection)
2773 {
2774 sdbusplus::message::object_path path(item.first);
2775 std::string sensorName = path.filename();
2776 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302777 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002778 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302779 return;
2780 }
Ban Feng3f5eb752023-06-29 09:19:20 +08002781 std::string id = path.parent_path().filename();
Ed Tanous3544d2a2023-08-06 18:12:20 -07002782 auto remove = std::ranges::remove(id, '_');
2783 id.erase(std::ranges::begin(remove), id.end());
Ban Feng3f5eb752023-06-29 09:19:20 +08002784 id += "_";
2785 id += sensorName;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302786
Ban Feng3f5eb752023-06-29 09:19:20 +08002787 const auto& iterator = overrideMap.find(id);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002788 if (iterator == overrideMap.end())
2789 {
Ed Tanous62598e32023-07-17 17:06:25 -07002790 BMCWEB_LOG_INFO("Unable to find sensor object{}",
2791 item.first);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002792 messages::internalError(sensorAsyncResp->asyncResp->res);
2793 return;
2794 }
Ginu Georgee93abac2024-06-14 17:35:27 +05302795 setDbusProperty(sensorAsyncResp->asyncResp,
2796 propertyValueNameStr, item.second, item.first,
2797 "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanousd02aad32024-02-13 14:43:34 -08002798 iterator->second.first);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002799 }
2800 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302801 // Get object with connection for the given sensor name
2802 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2803 std::move(getObjectsWithConnectionCb));
2804 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302805 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002806 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2807 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2808 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302809}
2810
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002811/**
2812 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2813 * path of the sensor.
2814 *
2815 * Function builds valid Redfish response for sensor query of given chassis and
2816 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2817 * it to caller in a callback.
2818 *
2819 * @param chassis Chassis for which retrieval should be performed
2820 * @param node Node (group) of sensors. See sensors::node for supported values
2821 * @param mapComplete Callback to be called with retrieval result
2822 */
Ed Tanous931edc72023-11-01 12:09:07 -07002823template <typename Callback>
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002824inline void retrieveUriToDbusMap(
2825 const std::string& chassis, const std::string& node, Callback&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002826{
Ed Tanous02da7c52022-02-27 00:09:02 -08002827 decltype(sensors::paths)::const_iterator pathIt =
2828 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2829 [&node](auto&& val) { return val.first == node; });
2830 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002831 {
Ed Tanous62598e32023-07-17 17:06:25 -07002832 BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
Ed Tanous6804b5c2023-10-31 14:50:03 -07002833 std::map<std::string, std::string> noop;
2834 mapComplete(boost::beast::http::status::bad_request, noop);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002835 return;
2836 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002837
Nan Zhou72374eb2022-01-27 17:06:51 -08002838 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002839 auto callback =
2840 [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)](
2841 const boost::beast::http::status status,
2842 const std::map<std::string, std::string>& uriToDbus) {
2843 mapCompleteCb(status, uriToDbus);
2844 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002845
2846 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002847 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002848 getChassisData(resp);
2849}
2850
Nan Zhoubacb2162022-04-06 11:28:32 -07002851namespace sensors
2852{
Nan Zhou928fefb2022-03-28 08:45:00 -07002853
Nan Zhoubacb2162022-04-06 11:28:32 -07002854inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002855 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2856 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002857 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002858{
Ed Tanous62598e32023-07-17 17:06:25 -07002859 BMCWEB_LOG_DEBUG("getChassisCallback enter ");
Nan Zhoubacb2162022-04-06 11:28:32 -07002860
Ed Tanousc1d019a2022-08-06 09:36:06 -07002861 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2862 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002863 {
Ed Tanous62598e32023-07-17 17:06:25 -07002864 BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
Nan Zhoubacb2162022-04-06 11:28:32 -07002865
2866 sdbusplus::message::object_path path(sensor);
2867 std::string sensorName = path.filename();
2868 if (sensorName.empty())
2869 {
Ed Tanous62598e32023-07-17 17:06:25 -07002870 BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002871 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002872 return;
2873 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002874 std::string type = path.parent_path().filename();
2875 // fan_tach has an underscore in it, so remove it to "normalize" the
2876 // type in the URI
Ed Tanous3544d2a2023-08-06 18:12:20 -07002877 auto remove = std::ranges::remove(type, '_');
2878 type.erase(std::ranges::begin(remove), type.end());
Ed Tanousc1d019a2022-08-06 09:36:06 -07002879
Ed Tanous14766872022-03-15 10:44:42 -07002880 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002881 std::string id = type;
2882 id += "_";
2883 id += sensorName;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002884 member["@odata.id"] = boost::urls::format(
2885 "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002886
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002887 entriesArray.emplace_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002888 }
2889
Ed Tanousc1d019a2022-08-06 09:36:06 -07002890 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous62598e32023-07-17 17:06:25 -07002891 BMCWEB_LOG_DEBUG("getChassisCallback exit");
Nan Zhoubacb2162022-04-06 11:28:32 -07002892}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002893
Ed Tanousac106bf2023-06-07 09:24:59 -07002894inline void handleSensorCollectionGet(
2895 App& app, const crow::Request& req,
2896 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2897 const std::string& chassisId)
Nan Zhoude167a62022-06-01 04:47:45 +00002898{
2899 query_param::QueryCapabilities capabilities = {
2900 .canDelegateExpandLevel = 1,
2901 };
2902 query_param::Query delegatedQuery;
Ed Tanousac106bf2023-06-07 09:24:59 -07002903 if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002904 delegatedQuery, capabilities))
2905 {
2906 return;
2907 }
2908
2909 if (delegatedQuery.expandType != query_param::ExpandType::None)
2910 {
2911 // we perform efficient expand.
Ed Tanousac106bf2023-06-07 09:24:59 -07002912 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2913 asyncResp, chassisId, sensors::dbus::sensorPaths,
Nan Zhoude167a62022-06-01 04:47:45 +00002914 sensors::node::sensors,
2915 /*efficientExpand=*/true);
Ed Tanousac106bf2023-06-07 09:24:59 -07002916 getChassisData(sensorsAsyncResp);
Nan Zhoude167a62022-06-01 04:47:45 +00002917
Ed Tanous62598e32023-07-17 17:06:25 -07002918 BMCWEB_LOG_DEBUG(
2919 "SensorCollection doGet exit via efficient expand handler");
Nan Zhoude167a62022-06-01 04:47:45 +00002920 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002921 }
Nan Zhoude167a62022-06-01 04:47:45 +00002922
Nan Zhoude167a62022-06-01 04:47:45 +00002923 // We get all sensors as hyperlinkes in the chassis (this
2924 // implies we reply on the default query parameters handler)
Ed Tanousac106bf2023-06-07 09:24:59 -07002925 getChassis(asyncResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2926 std::bind_front(sensors::getChassisCallback, asyncResp,
2927 chassisId, sensors::node::sensors));
Ed Tanousc1d019a2022-08-06 09:36:06 -07002928}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002929
Ed Tanousc1d019a2022-08-06 09:36:06 -07002930inline void
2931 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2932 const std::string& sensorPath,
2933 const ::dbus::utility::MapperGetObject& mapperResponse)
2934{
2935 if (mapperResponse.size() != 1)
2936 {
2937 messages::internalError(asyncResp->res);
2938 return;
2939 }
2940 const auto& valueIface = *mapperResponse.begin();
2941 const std::string& connectionName = valueIface.first;
Ed Tanous62598e32023-07-17 17:06:25 -07002942 BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
2943 BMCWEB_LOG_DEBUG("Path {}", sensorPath);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002944
2945 sdbusplus::asio::getAllProperties(
2946 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002947 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002948 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002949 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002950 if (ec)
2951 {
2952 messages::internalError(asyncResp->res);
2953 return;
2954 }
2955 sdbusplus::message::object_path path(sensorPath);
2956 std::string name = path.filename();
2957 path = path.parent_path();
2958 std::string type = path.filename();
2959 objectPropertiesToJson(name, type, sensors::node::sensors,
2960 valuesDict, asyncResp->res.jsonValue,
2961 nullptr);
2962 });
Nan Zhoude167a62022-06-01 04:47:45 +00002963}
2964
Nan Zhoue6bd8462022-06-01 04:35:35 +00002965inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002966 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002967 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002968 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002969{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002970 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002971 {
2972 return;
2973 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002974 std::pair<std::string, std::string> nameType =
2975 splitSensorNameAndType(sensorId);
2976 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002977 {
2978 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2979 return;
2980 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002981
Ed Tanousef4c65b2023-04-24 15:28:50 -07002982 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2983 "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002984
Ed Tanous62598e32023-07-17 17:06:25 -07002985 BMCWEB_LOG_DEBUG("Sensor doGet enter");
Nan Zhoue6bd8462022-06-01 04:35:35 +00002986
George Liu2b731192023-01-11 16:27:13 +08002987 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002988 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002989 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2990 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002991 // Get a list of all of the sensors that implement Sensor.Value
2992 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002993 ::dbus::utility::getDbusObject(
2994 sensorPath, interfaces,
Myung Baeaec0ec32023-05-31 15:10:01 -05002995 [asyncResp, sensorId,
George Liu2b731192023-01-11 16:27:13 +08002996 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002997 const ::dbus::utility::MapperGetObject& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002998 BMCWEB_LOG_DEBUG("respHandler1 enter");
2999 if (ec == boost::system::errc::io_error)
3000 {
3001 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
3002 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
3003 return;
3004 }
3005 if (ec)
3006 {
3007 messages::internalError(asyncResp->res);
3008 BMCWEB_LOG_ERROR(
3009 "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
3010 return;
3011 }
3012 getSensorFromDbus(asyncResp, sensorPath, subtree);
3013 BMCWEB_LOG_DEBUG("respHandler1 exit");
3014 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00003015}
3016
Nan Zhoubacb2162022-04-06 11:28:32 -07003017} // namespace sensors
3018
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003019inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003020{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003021 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003022 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003023 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003024 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003025}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003026
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003027inline void requestRoutesSensor(App& app)
3028{
3029 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003030 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003031 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003032 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003033}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003034
Ed Tanous1abe55e2018-09-05 08:30:59 -07003035} // namespace redfish