blob: 228fae046efe53126d342688c8b59c7f15a16d6d [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) :
Ed Tanous8a592812022-06-04 09:06:59 -0700220 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700221 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
222 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500223 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200224
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200225 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700226 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800227 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800228 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800229 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200230 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700231 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700232 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
233 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200234 dataComplete{std::move(creationComplete)}
235 {}
236
Nan Zhou928fefb2022-03-28 08:45:00 -0700237 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700238 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700239 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800240 std::span<const std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700241 const std::string_view& subNode, bool efficientExpandIn) :
242 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700243 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700244 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700245 {}
246
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247 ~SensorsAsyncResp()
248 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800249 if (asyncResp->res.result() ==
250 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251 {
252 // Reset the json object to clear out any data that made it in
253 // before the error happened todo(ed) handle error condition with
254 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800255 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700256 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200257
258 if (dataComplete && metadata)
259 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000260 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800261 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200262 {
263 for (auto& sensor : *metadata)
264 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700265 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200266 }
267 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800268 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200269 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700270 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100271
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800272 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
273 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
274 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
275 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
276
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200277 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700278 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200279 {
280 if (metadata)
281 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700282 metadata->emplace_back(SensorData{
283 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200284 }
285 }
286
287 void updateUri(const std::string& name, const std::string& uri)
288 {
289 if (metadata)
290 {
291 for (auto& sensor : *metadata)
292 {
293 if (sensor.name == name)
294 {
295 sensor.uri = uri;
296 }
297 }
298 }
299 }
300
zhanghch058d1b46d2021-04-01 11:18:24 +0800301 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200302 const std::string chassisId;
Ed Tanouscf9e4172022-12-21 09:30:16 -0800303 const std::span<const std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200304 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700305 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200306
307 private:
308 std::optional<std::vector<SensorData>> metadata;
309 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100310};
311
312/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500313 * Possible states for physical inventory leds
314 */
315enum class LedState
316{
317 OFF,
318 ON,
319 BLINK,
320 UNKNOWN
321};
322
323/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500324 * D-Bus inventory item associated with one or more sensors.
325 */
326class InventoryItem
327{
328 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700329 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500330 {
331 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800332 sdbusplus::message::object_path path(objectPath);
333 name = path.filename();
334 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500335 {
Ed Tanous62598e32023-07-17 17:06:25 -0700336 BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500337 }
338 }
339
340 std::string objectPath;
341 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800342 bool isPresent = true;
343 bool isFunctional = true;
344 bool isPowerSupply = false;
345 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500346 std::string manufacturer;
347 std::string model;
348 std::string partNumber;
349 std::string serialNumber;
350 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500351 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800352 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500353};
354
355/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530356 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200357 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100358 * @param sensorNames Sensors retrieved from chassis
359 * @param callback Callback for processing gathered connections
360 */
361template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530362void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000363 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000364 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530365 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700366{
Ed Tanous62598e32023-07-17 17:06:25 -0700367 BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700368 const std::string path = "/xyz/openbmc_project/sensors";
George Liue99073f2022-12-09 11:06:16 +0800369 constexpr std::array<std::string_view, 1> interfaces = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700370 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100371
George Liue99073f2022-12-09 11:06:16 +0800372 // Make call to ObjectMapper to find all sensors objects
373 dbus::utility::getSubTree(
374 path, 2, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700375 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
George Liue99073f2022-12-09 11:06:16 +0800376 sensorNames](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700377 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +0800378 // Response handler for parsing objects subtree
Ed Tanous62598e32023-07-17 17:06:25 -0700379 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380 if (ec)
381 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800382 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -0700383 BMCWEB_LOG_ERROR(
384 "getObjectsWithConnection resp_handler: Dbus error {}", ec);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700385 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100386 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100387
Ed Tanous62598e32023-07-17 17:06:25 -0700388 BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389
390 // Make unique list of connections only for requested sensor types and
391 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000392 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530393 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394
Ed Tanous62598e32023-07-17 17:06:25 -0700395 BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700396 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 {
Ed Tanous62598e32023-07-17 17:06:25 -0700398 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399 }
400
401 for (const std::pair<
402 std::string,
403 std::vector<std::pair<std::string, std::vector<std::string>>>>&
404 object : subtree)
405 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700406 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700408 for (const std::pair<std::string, std::vector<std::string>>&
409 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410 {
Ed Tanous62598e32023-07-17 17:06:25 -0700411 BMCWEB_LOG_DEBUG("Adding connection: {}", objData.first);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700412 connections.insert(objData.first);
413 objectsWithConnection.insert(
414 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700415 }
416 }
417 }
Ed Tanous62598e32023-07-17 17:06:25 -0700418 BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530419 callback(std::move(connections), std::move(objectsWithConnection));
Ed Tanous62598e32023-07-17 17:06:25 -0700420 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -0500421 });
Ed Tanous62598e32023-07-17 17:06:25 -0700422 BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530423}
424
425/**
426 * @brief Create connections necessary for sensors
427 * @param SensorsAsyncResp Pointer to object holding response data
428 * @param sensorNames Sensors retrieved from chassis
429 * @param callback Callback for processing gathered connections
430 */
431template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000432void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
433 const std::shared_ptr<std::set<std::string>> sensorNames,
434 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530435{
436 auto objectsWithConnectionCb =
Ed Tanous8cb2c022024-03-27 16:31:46 -0700437 [callback = std::forward<Callback>(callback)](
438 const std::set<std::string>& connections,
439 const std::set<std::pair<std::string, std::string>>&
440 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000441 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530442 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100443}
444
445/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700446 * @brief Shrinks the list of sensors for processing
447 * @param SensorsAysncResp The class holding the Redfish response
448 * @param allSensors A list of all the sensors associated to the
449 * chassis element (i.e. baseboard, front panel, etc...)
450 * @param activeSensors A list that is a reduction of the incoming
451 * allSensors list. Eliminate Thermal sensors when a Power request is
452 * made, and eliminate Power sensors when a Thermal request is made.
453 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000454inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700455 crow::Response& res, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800456 std::span<const std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700457 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000458 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700459{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700460 if ((allSensors == nullptr) || (activeSensors == nullptr))
461 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700462 messages::resourceNotFound(res, chassisSubNode,
463 chassisSubNode == sensors::node::thermal
464 ? "Temperatures"
465 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700466
467 return;
468 }
469 if (allSensors->empty())
470 {
471 // Nothing to do, the activeSensors object is also empty
472 return;
473 }
474
Ed Tanous7f1cc262022-08-09 13:33:57 -0700475 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700476 {
477 for (const std::string& sensor : *allSensors)
478 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700479 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700480 {
481 activeSensors->emplace(sensor);
482 }
483 }
484 }
485}
486
Ed Tanous7f1cc262022-08-09 13:33:57 -0700487/*
488 *Populates the top level collection for a given subnode. Populates
489 *SensorCollection, Power, or Thermal schemas.
490 *
491 * */
492inline void populateChassisNode(nlohmann::json& jsonValue,
493 std::string_view chassisSubNode)
494{
495 if (chassisSubNode == sensors::node::power)
496 {
497 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
498 }
499 else if (chassisSubNode == sensors::node::thermal)
500 {
501 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
502 jsonValue["Fans"] = nlohmann::json::array();
503 jsonValue["Temperatures"] = nlohmann::json::array();
504 }
505 else if (chassisSubNode == sensors::node::sensors)
506 {
507 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
508 jsonValue["Description"] = "Collection of Sensors for this Chassis";
509 jsonValue["Members"] = nlohmann::json::array();
510 jsonValue["Members@odata.count"] = 0;
511 }
512
513 if (chassisSubNode != sensors::node::sensors)
514 {
515 jsonValue["Id"] = chassisSubNode;
516 }
517 jsonValue["Name"] = chassisSubNode;
518}
519
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700520/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100521 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200522 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100523 * @param callback Callback for next step in gathered sensor processing
524 */
525template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700526void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
527 std::string_view chassisId, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800528 std::span<const std::string_view> sensorTypes,
529 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530{
Ed Tanous62598e32023-07-17 17:06:25 -0700531 BMCWEB_LOG_DEBUG("getChassis enter");
George Liu7a1dbc42022-12-07 16:03:22 +0800532 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700533 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500534 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800535
536 // Get the Chassis Collection
537 dbus::utility::getSubTreePaths(
538 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700539 [callback = std::forward<Callback>(callback), asyncResp,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700540 chassisIdStr{std::string(chassisId)},
541 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800542 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous62598e32023-07-17 17:06:25 -0700544 BMCWEB_LOG_DEBUG("getChassis respHandler enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545 if (ec)
546 {
Ed Tanous62598e32023-07-17 17:06:25 -0700547 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
Ed Tanous7f1cc262022-08-09 13:33:57 -0700548 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700549 return;
550 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700551 const std::string* chassisPath = nullptr;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700552 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 {
George Liu28aa8de2021-02-01 15:13:30 +0800554 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700555 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800556 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700557 {
Ed Tanous62598e32023-07-17 17:06:25 -0700558 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700559 continue;
560 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700561 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700562 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700563 chassisPath = &chassis;
564 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700565 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700566 }
567 if (chassisPath == nullptr)
568 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700569 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700570 return;
571 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700572 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700573
Ed Tanousef4c65b2023-04-24 15:28:50 -0700574 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
575 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500576
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500577 // Get the list of all sensors for this Chassis element
578 std::string sensorPath = *chassisPath + "/all_sensors";
George Liu6c3e9452023-03-03 13:55:29 +0800579 dbus::utility::getAssociationEndPoints(
580 sensorPath,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700581 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700582 callback = std::forward<const Callback>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -0700583 const boost::system::error_code& ec2,
George Liu6c3e9452023-03-03 13:55:29 +0800584 const dbus::utility::MapperEndPoints& nodeSensorList) {
Ed Tanous8b242752023-06-27 17:17:13 -0700585 if (ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 {
Ed Tanous8b242752023-06-27 17:17:13 -0700587 if (ec2.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700588 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700589 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700591 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000593 const std::shared_ptr<std::set<std::string>> culledSensorList =
594 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700595 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
596 &nodeSensorList, culledSensorList);
Ed Tanous62598e32023-07-17 17:06:25 -0700597 BMCWEB_LOG_DEBUG("Finishing with {}", culledSensorList->size());
Ed Tanous002d39b2022-05-31 08:59:27 -0700598 callback(culledSensorList);
George Liu7a1dbc42022-12-07 16:03:22 +0800599 });
Patrick Williams5a39f772023-10-20 11:20:21 -0500600 });
Ed Tanous62598e32023-07-17 17:06:25 -0700601 BMCWEB_LOG_DEBUG("getChassis exit");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100602}
603
604/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500605 * @brief Returns the Redfish State value for the specified inventory item.
606 * @param inventoryItem D-Bus inventory item associated with a sensor.
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700607 * @param sensorAvailable Boolean representing if D-Bus sensor is marked as
608 * available.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500609 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700610 */
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700611inline resource::State getState(const InventoryItem* inventoryItem,
612 const bool sensorAvailable)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500613{
614 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
615 {
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700616 return resource::State::Absent;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500617 }
James Feist34dd1792019-05-17 14:10:54 -0700618
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700619 if (!sensorAvailable)
620 {
621 return resource::State::UnavailableOffline;
622 }
623
624 return resource::State::Enabled;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500625}
626
627/**
628 * @brief Returns the Redfish Health value for the specified sensor.
629 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700630 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500631 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
632 * be nullptr if no associated inventory item was found.
633 * @return Health value for sensor.
634 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700635inline std::string getHealth(nlohmann::json& sensorJson,
636 const dbus::utility::DBusPropertiesMap& valuesDict,
637 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700638{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500639 // Get current health value (if any) in the sensor JSON object. Some JSON
640 // objects contain multiple sensors (such as PowerSupplies). We want to set
641 // the overall health to be the most severe of any of the sensors.
642 std::string currentHealth;
643 auto statusIt = sensorJson.find("Status");
644 if (statusIt != sensorJson.end())
645 {
646 auto healthIt = statusIt->find("Health");
647 if (healthIt != statusIt->end())
648 {
649 std::string* health = healthIt->get_ptr<std::string*>();
650 if (health != nullptr)
651 {
652 currentHealth = *health;
653 }
654 }
655 }
656
657 // If current health in JSON object is already Critical, return that. This
658 // should override the sensor health, which might be less severe.
659 if (currentHealth == "Critical")
660 {
661 return "Critical";
662 }
663
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200664 const bool* criticalAlarmHigh = nullptr;
665 const bool* criticalAlarmLow = nullptr;
666 const bool* warningAlarmHigh = nullptr;
667 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800668
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200669 const bool success = sdbusplus::unpackPropertiesNoThrow(
670 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
671 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
672 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
673 warningAlarmLow);
674
675 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700676 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200677 // Check if sensor has critical threshold alarm
678 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
679 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700680 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200681 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700682 }
683 }
684
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500685 // Check if associated inventory item is not functional
686 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
687 {
688 return "Critical";
689 }
690
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200691 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500692 // should override the sensor status, which might be less severe.
693 if (currentHealth == "Warning")
694 {
695 return "Warning";
696 }
697
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200698 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700699 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200700 // Check if sensor has warning threshold alarm
701 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
702 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700703 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200704 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700705 }
706 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500707
James Feist34dd1792019-05-17 14:10:54 -0700708 return "OK";
709}
710
Ed Tanous23a21a12020-07-25 04:45:05 +0000711inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500712 const InventoryItem* inventoryItem)
713{
714 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
715 {
716 switch (inventoryItem->ledState)
717 {
718 case LedState::OFF:
Ed Tanous539d8c62024-06-19 14:38:27 -0700719 sensorJson["IndicatorLED"] = resource::IndicatorLED::Off;
Anthony Wilsond5005492019-07-31 16:34:17 -0500720 break;
721 case LedState::ON:
Ed Tanous539d8c62024-06-19 14:38:27 -0700722 sensorJson["IndicatorLED"] = resource::IndicatorLED::Lit;
Anthony Wilsond5005492019-07-31 16:34:17 -0500723 break;
724 case LedState::BLINK:
Ed Tanous539d8c62024-06-19 14:38:27 -0700725 sensorJson["IndicatorLED"] = resource::IndicatorLED::Blinking;
Anthony Wilsond5005492019-07-31 16:34:17 -0500726 break;
Ed Tanous4da04902024-03-19 11:32:44 -0700727 default:
Anthony Wilsond5005492019-07-31 16:34:17 -0500728 break;
729 }
730 }
731}
732
James Feist34dd1792019-05-17 14:10:54 -0700733/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100734 * @brief Builds a json sensor representation of a sensor.
735 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500736 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100737 * build
Ed Tanous8ece0e42024-01-02 13:16:50 -0800738 * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
Ed Tanous1d7c0052022-08-09 12:32:26 -0700739 * @param propertiesDict A dictionary of the properties to build the sensor
740 * from.
741 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500742 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
743 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100744 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700745inline void objectPropertiesToJson(
746 std::string_view sensorName, std::string_view sensorType,
747 std::string_view chassisSubNode,
748 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000749 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700750{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700751 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500752 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800753 std::string subNodeEscaped(sensorType);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700754 auto remove = std::ranges::remove(subNodeEscaped, '_');
755 subNodeEscaped.erase(std::ranges::begin(remove), subNodeEscaped.end());
Ed Tanousc1d019a2022-08-06 09:36:06 -0700756
757 // For sensors in SensorCollection we set Id instead of MemberId,
758 // including power sensors.
759 subNodeEscaped += '_';
760 subNodeEscaped += sensorName;
761 sensorJson["Id"] = std::move(subNodeEscaped);
762
Ed Tanous1d7c0052022-08-09 12:32:26 -0700763 std::string sensorNameEs(sensorName);
764 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
765 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500766 }
767 else if (sensorType != "power")
768 {
769 // Set MemberId and Name for non-power sensors. For PowerSupplies and
770 // PowerControl, those properties have more general values because
771 // multiple sensors can be stored in the same JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700772 std::string sensorNameEs(sensorName);
773 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
774 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500775 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700776
Matt Simmeringaaf08ac2023-10-04 08:41:01 -0700777 const bool* checkAvailable = nullptr;
778 bool available = true;
779 const bool success = sdbusplus::unpackPropertiesNoThrow(
780 dbus_utils::UnpackErrorPrinter(), propertiesDict, "Available",
781 checkAvailable);
782 if (!success)
783 {
784 messages::internalError();
785 }
786 if (checkAvailable != nullptr)
787 {
788 available = *checkAvailable;
789 }
790
791 sensorJson["Status"]["State"] = getState(inventoryItem, available);
Patrick Williams89492a12023-05-10 07:51:34 -0500792 sensorJson["Status"]["Health"] = getHealth(sensorJson, propertiesDict,
793 inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700794
795 // Parameter to set to override the type we get from dbus, and force it to
796 // int, regardless of what is available. This is used for schemas like fan,
797 // that require integers, not floats.
798 bool forceToInt = false;
799
Anthony Wilson3929aca2019-07-19 15:42:33 -0500800 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700801 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500802 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000803 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000804
Ed Tanous0ec8b832022-03-14 14:56:47 -0700805 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
806 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500807 {
Ed Tanous62598e32023-07-17 17:06:25 -0700808 BMCWEB_LOG_ERROR("Redfish cannot map reading type for {}",
809 sensorType);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500810 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000811 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500812 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000813 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500814 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000815
Ed Tanous1d7c0052022-08-09 12:32:26 -0700816 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000817 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200818 {
Ed Tanous62598e32023-07-17 17:06:25 -0700819 BMCWEB_LOG_ERROR("Redfish cannot map reading unit for {}",
820 sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000821 }
822 else
823 {
824 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200825 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500826 }
827 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700828 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500829 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000830 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700831 // TODO(ed) Documentation says that path should be type fan_tach,
832 // implementation seems to implement fan
833 }
834 else if (sensorType == "fan" || sensorType == "fan_tach")
835 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500836 unit = "/Reading"_json_pointer;
Ed Tanous539d8c62024-06-19 14:38:27 -0700837 sensorJson["ReadingUnits"] = thermal::ReadingUnits::RPM;
Ed Tanous81ce6092020-12-17 16:54:55 +0000838 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
839 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700840 forceToInt = true;
841 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700842 else if (sensorType == "fan_pwm")
843 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500844 unit = "/Reading"_json_pointer;
Ed Tanous539d8c62024-06-19 14:38:27 -0700845 sensorJson["ReadingUnits"] = thermal::ReadingUnits::Percent;
Ed Tanous81ce6092020-12-17 16:54:55 +0000846 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
847 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700848 forceToInt = true;
849 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700850 else if (sensorType == "voltage")
851 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500852 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000853 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700854 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700855 else if (sensorType == "power")
856 {
Ed Tanous18f8f602023-07-18 10:07:23 -0700857 std::string lower;
858 std::ranges::transform(sensorName, std::back_inserter(lower),
859 bmcweb::asciiToLower);
860 if (lower == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000861 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000862 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500863 // Put multiple "sensors" into a single PowerControl, so have
864 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000865 sensorJson["MemberId"] = "0";
866 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500867 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000868 }
Ed Tanous18f8f602023-07-18 10:07:23 -0700869 else if (lower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700870 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500871 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700872 }
873 else
874 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500875 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700876 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700877 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700878 else
879 {
Ed Tanous62598e32023-07-17 17:06:25 -0700880 BMCWEB_LOG_ERROR("Redfish cannot map object type for {}", sensorName);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 return;
882 }
883 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500884 std::vector<
885 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
886 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887 properties.reserve(7);
888
889 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600890
Ed Tanous1d7c0052022-08-09 12:32:26 -0700891 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500892 {
893 properties.emplace_back(
894 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
895 "/Thresholds/UpperCaution/Reading"_json_pointer);
896 properties.emplace_back(
897 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
898 "/Thresholds/LowerCaution/Reading"_json_pointer);
899 properties.emplace_back(
900 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
901 "/Thresholds/UpperCritical/Reading"_json_pointer);
902 properties.emplace_back(
903 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
904 "/Thresholds/LowerCritical/Reading"_json_pointer);
905 }
906 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600907 {
908 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500909 "WarningHigh",
910 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600911 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 "WarningLow",
913 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600914 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500915 "CriticalHigh",
916 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600917 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500918 "CriticalLow",
919 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600920 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700921
Ed Tanous2474adf2018-09-05 16:31:16 -0700922 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
923
Ed Tanous1d7c0052022-08-09 12:32:26 -0700924 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500925 {
926 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500927 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500928 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500929 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800930 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
931 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500932 }
933 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934 {
935 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500936 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700937 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500938 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500940 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 {
942 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500943 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700944 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500945 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700946 }
947
Anthony Wilson3929aca2019-07-19 15:42:33 -0500948 for (const std::tuple<const char*, const char*,
949 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700950 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700951 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700952 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700953 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700954 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800955 continue;
956 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700957
958 // The property we want to set may be nested json, so use
959 // a json_pointer for easy indexing into the json structure.
960 const nlohmann::json::json_pointer& key = std::get<2>(p);
961
Ed Tanous1d7c0052022-08-09 12:32:26 -0700962 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700963 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800964 {
Ed Tanous62598e32023-07-17 17:06:25 -0700965 BMCWEB_LOG_ERROR("Got value interface that wasn't double");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700966 continue;
967 }
Ed Tanous283860f2022-08-29 14:08:50 -0700968 if (!std::isfinite(*doubleValue))
969 {
970 if (valueName == "Value")
971 {
972 // Readings are allowed to be NAN for unavailable; coerce
973 // them to null in the json response.
974 sensorJson[key] = nullptr;
975 continue;
976 }
Ed Tanous62598e32023-07-17 17:06:25 -0700977 BMCWEB_LOG_WARNING("Sensor value for {} was unexpectedly {}",
978 valueName, *doubleValue);
Ed Tanous283860f2022-08-29 14:08:50 -0700979 continue;
980 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700981 if (forceToInt)
982 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700983 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700984 }
985 else
986 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700987 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700988 }
989 }
990 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100991}
992
Ed Tanous1d7c0052022-08-09 12:32:26 -0700993/**
994 * @brief Builds a json sensor representation of a sensor.
995 * @param sensorName The name of the sensor to be built
996 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
997 * build
Ed Tanous8ece0e42024-01-02 13:16:50 -0800998 * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
Ed Tanous1d7c0052022-08-09 12:32:26 -0700999 * @param interfacesDict A dictionary of the interfaces and properties of said
1000 * interfaces to be built from
1001 * @param sensorJson The json object to fill
1002 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1003 * be nullptr if no associated inventory item was found.
1004 */
1005inline void objectInterfacesToJson(
1006 const std::string& sensorName, const std::string& sensorType,
1007 const std::string& chassisSubNode,
Michael Shen80f79a42023-08-24 13:41:53 +00001008 const dbus::utility::DBusInterfacesMap& interfacesDict,
Ed Tanous1d7c0052022-08-09 12:32:26 -07001009 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1010{
Ed Tanous1d7c0052022-08-09 12:32:26 -07001011 for (const auto& [interface, valuesDict] : interfacesDict)
1012 {
1013 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1014 valuesDict, sensorJson, inventoryItem);
1015 }
Ed Tanous62598e32023-07-17 17:06:25 -07001016 BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
Ed Tanous1d7c0052022-08-09 12:32:26 -07001017}
1018
Ed Tanousb5a76932020-09-29 16:16:58 -07001019inline void populateFanRedundancy(
1020 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001021{
George Liue99073f2022-12-09 11:06:16 +08001022 constexpr std::array<std::string_view, 1> interfaces = {
1023 "xyz.openbmc_project.Control.FanRedundancy"};
1024 dbus::utility::getSubTree(
1025 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001026 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +08001027 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001028 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001029 if (ec)
1030 {
1031 return; // don't have to have this interface
1032 }
George Liu6c3e9452023-03-03 13:55:29 +08001033 for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
Ed Tanous002d39b2022-05-31 08:59:27 -07001034 pathPair : resp)
1035 {
1036 const std::string& path = pathPair.first;
George Liu6c3e9452023-03-03 13:55:29 +08001037 const dbus::utility::MapperServiceMap& objDict = pathPair.second;
Ed Tanous002d39b2022-05-31 08:59:27 -07001038 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001039 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001040 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001041 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001042
1043 const std::string& owner = objDict.begin()->first;
George Liu6c3e9452023-03-03 13:55:29 +08001044 dbus::utility::getAssociationEndPoints(
1045 path + "/chassis",
1046 [path, owner, sensorsAsyncResp](
Ed Tanous8b242752023-06-27 17:17:13 -07001047 const boost::system::error_code& ec2,
George Liu6c3e9452023-03-03 13:55:29 +08001048 const dbus::utility::MapperEndPoints& endpoints) {
Ed Tanous8b242752023-06-27 17:17:13 -07001049 if (ec2)
James Feist8bd25cc2019-03-15 15:14:00 -07001050 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001051 return; // if they don't have an association we
1052 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001053 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001054 auto found = std::ranges::find_if(
1055 endpoints, [sensorsAsyncResp](const std::string& entry) {
Patrick Williams5a39f772023-10-20 11:20:21 -05001056 return entry.find(sensorsAsyncResp->chassisId) !=
1057 std::string::npos;
1058 });
James Feist8bd25cc2019-03-15 15:14:00 -07001059
Ed Tanous002d39b2022-05-31 08:59:27 -07001060 if (found == endpoints.end())
1061 {
1062 return;
1063 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001064 sdbusplus::asio::getAllProperties(
1065 *crow::connections::systemBus, owner, path,
1066 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001067 [path, sensorsAsyncResp](
Ed Tanous8b242752023-06-27 17:17:13 -07001068 const boost::system::error_code& ec3,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001069 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous8b242752023-06-27 17:17:13 -07001070 if (ec3)
Ed Tanous002d39b2022-05-31 08:59:27 -07001071 {
1072 return; // don't have to have this
1073 // interface
1074 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001075
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001076 const uint8_t* allowedFailures = nullptr;
1077 const std::vector<std::string>* collection = nullptr;
1078 const std::string* status = nullptr;
1079
1080 const bool success = sdbusplus::unpackPropertiesNoThrow(
1081 dbus_utils::UnpackErrorPrinter(), ret,
1082 "AllowedFailures", allowedFailures, "Collection",
1083 collection, "Status", status);
1084
1085 if (!success)
1086 {
1087 messages::internalError(
1088 sensorsAsyncResp->asyncResp->res);
1089 return;
1090 }
1091
1092 if (allowedFailures == nullptr || collection == nullptr ||
1093 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001094 {
Ed Tanous62598e32023-07-17 17:06:25 -07001095 BMCWEB_LOG_ERROR("Invalid redundancy interface");
Ed Tanous002d39b2022-05-31 08:59:27 -07001096 messages::internalError(
1097 sensorsAsyncResp->asyncResp->res);
1098 return;
1099 }
1100
Ed Tanous002d39b2022-05-31 08:59:27 -07001101 sdbusplus::message::object_path objectPath(path);
1102 std::string name = objectPath.filename();
1103 if (name.empty())
1104 {
1105 // this should be impossible
1106 messages::internalError(
1107 sensorsAsyncResp->asyncResp->res);
1108 return;
1109 }
Ed Tanous18f8f602023-07-18 10:07:23 -07001110 std::ranges::replace(name, '_', ' ');
Ed Tanous002d39b2022-05-31 08:59:27 -07001111
1112 std::string health;
1113
Ed Tanous11ba3972022-07-11 09:50:41 -07001114 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001115 {
1116 health = "OK";
1117 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001118 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001119 {
1120 health = "Warning";
1121 }
1122 else
1123 {
1124 health = "Critical";
1125 }
1126 nlohmann::json::array_t redfishCollection;
1127 const auto& fanRedfish =
1128 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1129 for (const std::string& item : *collection)
1130 {
Ed Tanous8a592812022-06-04 09:06:59 -07001131 sdbusplus::message::object_path itemPath(item);
1132 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001133 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001134 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001135 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001136 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001137 /*
1138 todo(ed): merge patch that fixes the names
1139 std::replace(itemName.begin(),
1140 itemName.end(), '_', ' ');*/
Ed Tanous3544d2a2023-08-06 18:12:20 -07001141 auto schemaItem = std::ranges::find_if(
1142 fanRedfish, [itemName](const nlohmann::json& fan) {
Patrick Williams5a39f772023-10-20 11:20:21 -05001143 return fan["Name"] == itemName;
1144 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001145 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001146 {
Ed Tanous8a592812022-06-04 09:06:59 -07001147 nlohmann::json::object_t collectionId;
1148 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001149 (*schemaItem)["@odata.id"];
1150 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001151 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001152 }
1153 else
1154 {
Ed Tanous62598e32023-07-17 17:06:25 -07001155 BMCWEB_LOG_ERROR("failed to find fan in schema");
Ed Tanous002d39b2022-05-31 08:59:27 -07001156 messages::internalError(
1157 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001158 return;
1159 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001160 }
James Feist8bd25cc2019-03-15 15:14:00 -07001161
Patrick Williams89492a12023-05-10 07:51:34 -05001162 size_t minNumNeeded = collection->empty()
1163 ? 0
1164 : collection->size() -
1165 *allowedFailures;
Ed Tanous002d39b2022-05-31 08:59:27 -07001166 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1167 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001168
Ed Tanous002d39b2022-05-31 08:59:27 -07001169 nlohmann::json::object_t redundancy;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001170 boost::urls::url url =
1171 boost::urls::format("/redfish/v1/Chassis/{}/{}",
1172 sensorsAsyncResp->chassisId,
1173 sensorsAsyncResp->chassisSubNode);
Willy Tueddfc432022-09-26 16:46:38 +00001174 url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1175 .to_string());
1176 redundancy["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -07001177 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1178 redundancy["MinNumNeeded"] = minNumNeeded;
Ed Tanous539d8c62024-06-19 14:38:27 -07001179 redundancy["Mode"] = redundancy::RedundancyType::NPlusM;
Ed Tanous002d39b2022-05-31 08:59:27 -07001180 redundancy["Name"] = name;
1181 redundancy["RedundancySet"] = redfishCollection;
1182 redundancy["Status"]["Health"] = health;
Ed Tanous539d8c62024-06-19 14:38:27 -07001183 redundancy["Status"]["State"] = resource::State::Enabled;
James Feist8bd25cc2019-03-15 15:14:00 -07001184
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001185 jResp.emplace_back(std::move(redundancy));
Ed Tanous002d39b2022-05-31 08:59:27 -07001186 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001187 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001188 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001189 });
James Feist8bd25cc2019-03-15 15:14:00 -07001190}
1191
Ed Tanousb5a76932020-09-29 16:16:58 -07001192inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001193 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001194{
zhanghch058d1b46d2021-04-01 11:18:24 +08001195 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001196 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001197 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001198 {
1199 sensorHeaders = {"Voltages", "PowerSupplies"};
1200 }
1201 for (const std::string& sensorGroup : sensorHeaders)
1202 {
1203 nlohmann::json::iterator entry = response.find(sensorGroup);
1204 if (entry != response.end())
1205 {
1206 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001207 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001208 return c1["Name"] < c2["Name"];
1209 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001210
1211 // add the index counts to the end of each entry
1212 size_t count = 0;
1213 for (nlohmann::json& sensorJson : *entry)
1214 {
1215 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1216 if (odata == sensorJson.end())
1217 {
1218 continue;
1219 }
1220 std::string* value = odata->get_ptr<std::string*>();
1221 if (value != nullptr)
1222 {
Willy Tueddfc432022-09-26 16:46:38 +00001223 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001224 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001225 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001226 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001227 }
1228 }
1229 }
1230 }
1231}
1232
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001233/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001234 * @brief Finds the inventory item with the specified object path.
1235 * @param inventoryItems D-Bus inventory items associated with sensors.
1236 * @param invItemObjPath D-Bus object path of inventory item.
1237 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001238 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001239inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001240 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001241 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001242{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001243 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001244 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001245 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001246 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001247 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001248 }
1249 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001250 return nullptr;
1251}
1252
1253/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001254 * @brief Finds the inventory item associated with the specified sensor.
1255 * @param inventoryItems D-Bus inventory items associated with sensors.
1256 * @param sensorObjPath D-Bus object path of sensor.
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* findInventoryItemForSensor(
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& sensorObjPath)
1262{
1263 for (InventoryItem& inventoryItem : *inventoryItems)
1264 {
Ed Tanousdb0d36e2023-06-30 10:37:05 -07001265 if (inventoryItem.sensors.contains(sensorObjPath))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001266 {
1267 return &inventoryItem;
1268 }
1269 }
1270 return nullptr;
1271}
1272
1273/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001274 * @brief Finds the inventory item associated with the specified led path.
1275 * @param inventoryItems D-Bus inventory items associated with sensors.
1276 * @param ledObjPath D-Bus object path of led.
1277 * @return Inventory item within vector, or nullptr if no match found.
1278 */
1279inline InventoryItem*
1280 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1281 const std::string& ledObjPath)
1282{
1283 for (InventoryItem& inventoryItem : inventoryItems)
1284 {
1285 if (inventoryItem.ledObjectPath == ledObjPath)
1286 {
1287 return &inventoryItem;
1288 }
1289 }
1290 return nullptr;
1291}
1292
1293/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001294 * @brief Adds inventory item and associated sensor to specified vector.
1295 *
1296 * Adds a new InventoryItem to the vector if necessary. Searches for an
1297 * existing InventoryItem with the specified object path. If not found, one is
1298 * added to the vector.
1299 *
1300 * Next, the specified sensor is added to the set of sensors associated with the
1301 * InventoryItem.
1302 *
1303 * @param inventoryItems D-Bus inventory items associated with sensors.
1304 * @param invItemObjPath D-Bus object path of inventory item.
1305 * @param sensorObjPath D-Bus object path of sensor
1306 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001307inline void addInventoryItem(
1308 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1309 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001310{
1311 // Look for inventory item in vector
Patrick Williams89492a12023-05-10 07:51:34 -05001312 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1313 invItemObjPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001314
1315 // If inventory item doesn't exist in vector, add it
1316 if (inventoryItem == nullptr)
1317 {
1318 inventoryItems->emplace_back(invItemObjPath);
1319 inventoryItem = &(inventoryItems->back());
1320 }
1321
1322 // Add sensor to set of sensors associated with inventory item
1323 inventoryItem->sensors.emplace(sensorObjPath);
1324}
1325
1326/**
1327 * @brief Stores D-Bus data in the specified inventory item.
1328 *
1329 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1330 * specified InventoryItem.
1331 *
1332 * This data is later used to provide sensor property values in the JSON
1333 * response.
1334 *
1335 * @param inventoryItem Inventory item where data will be stored.
1336 * @param interfacesDict Map containing D-Bus interfaces and their properties
1337 * for the specified inventory item.
1338 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001339inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001340 InventoryItem& inventoryItem,
Michael Shen80f79a42023-08-24 13:41:53 +00001341 const dbus::utility::DBusInterfacesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001342{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001343 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001344
Ed Tanous9eb808c2022-01-25 10:19:23 -08001345 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001346 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001347 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001348 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001349 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001350 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001351 if (name == "Present")
1352 {
1353 const bool* value = std::get_if<bool>(&dbusValue);
1354 if (value != nullptr)
1355 {
1356 inventoryItem.isPresent = *value;
1357 }
1358 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001359 }
1360 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001361 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001362
Ed Tanous711ac7a2021-12-20 09:34:41 -08001363 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001364 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001365 inventoryItem.isPowerSupply = true;
1366 }
1367
1368 // Get properties from Inventory.Decorator.Asset interface
1369 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1370 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001371 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001372 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001373 if (name == "Manufacturer")
1374 {
1375 const std::string* value =
1376 std::get_if<std::string>(&dbusValue);
1377 if (value != nullptr)
1378 {
1379 inventoryItem.manufacturer = *value;
1380 }
1381 }
1382 if (name == "Model")
1383 {
1384 const std::string* value =
1385 std::get_if<std::string>(&dbusValue);
1386 if (value != nullptr)
1387 {
1388 inventoryItem.model = *value;
1389 }
1390 }
1391 if (name == "SerialNumber")
1392 {
1393 const std::string* value =
1394 std::get_if<std::string>(&dbusValue);
1395 if (value != nullptr)
1396 {
1397 inventoryItem.serialNumber = *value;
1398 }
1399 }
1400 if (name == "PartNumber")
1401 {
1402 const std::string* value =
1403 std::get_if<std::string>(&dbusValue);
1404 if (value != nullptr)
1405 {
1406 inventoryItem.partNumber = *value;
1407 }
1408 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001409 }
1410 }
1411
Ed Tanous711ac7a2021-12-20 09:34:41 -08001412 if (interface ==
1413 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001414 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001415 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001416 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001417 if (name == "Functional")
1418 {
1419 const bool* value = std::get_if<bool>(&dbusValue);
1420 if (value != nullptr)
1421 {
1422 inventoryItem.isFunctional = *value;
1423 }
1424 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001425 }
1426 }
1427 }
1428}
1429
1430/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001431 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001432 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001433 * Uses the specified connections (services) to obtain D-Bus data for inventory
1434 * items associated with sensors. Stores the resulting data in the
1435 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001436 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001437 * This data is later used to provide sensor property values in the JSON
1438 * response.
1439 *
1440 * Finds the inventory item data asynchronously. Invokes callback when data has
1441 * been obtained.
1442 *
1443 * The callback must have the following signature:
1444 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001445 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001446 * @endcode
1447 *
1448 * This function is called recursively, obtaining data asynchronously from one
1449 * connection in each call. This ensures the callback is not invoked until the
1450 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001451 *
1452 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001453 * @param inventoryItems D-Bus inventory items associated with sensors.
1454 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001455 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001456 * @param callback Callback to invoke when inventory data has been obtained.
1457 * @param invConnectionsIndex Current index in invConnections. Only specified
1458 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001459 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001460template <typename Callback>
1461static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001462 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001463 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001464 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1465 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001466{
Ed Tanous62598e32023-07-17 17:06:25 -07001467 BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001468
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001469 // If no more connections left, call callback
1470 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001471 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001472 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001473 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001474 return;
1475 }
1476
1477 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001478 auto it = invConnections->begin();
1479 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001480 if (it != invConnections->end())
1481 {
1482 const std::string& invConnection = *it;
1483
George Liu5eb468d2023-06-20 17:03:24 +08001484 // Get all object paths and their interfaces for current connection
1485 sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
1486 dbus::utility::getManagedObjects(
1487 invConnection, path,
1488 [sensorsAsyncResp, inventoryItems, invConnections,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001489 callback = std::forward<Callback>(callback), invConnectionsIndex](
George Liu5eb468d2023-06-20 17:03:24 +08001490 const boost::system::error_code& ec,
1491 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07001492 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001493 if (ec)
1494 {
Ed Tanous62598e32023-07-17 17:06:25 -07001495 BMCWEB_LOG_ERROR(
1496 "getInventoryItemsData respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001497 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498 return;
1499 }
1500
1501 // Loop through returned object paths
1502 for (const auto& objDictEntry : resp)
1503 {
1504 const std::string& objPath =
1505 static_cast<const std::string&>(objDictEntry.first);
1506
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001507 // If this object path is one of the specified inventory items
Patrick Williams89492a12023-05-10 07:51:34 -05001508 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1509 objPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001510 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001511 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001512 // Store inventory data in InventoryItem
1513 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001514 }
1515 }
1516
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001517 // Recurse to get inventory item data from next connection
1518 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001519 invConnections, std::move(callback),
1520 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001521
Ed Tanous62598e32023-07-17 17:06:25 -07001522 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05001523 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001524 }
1525
Ed Tanous62598e32023-07-17 17:06:25 -07001526 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001527}
1528
1529/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001530 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001531 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001532 * Gets the D-Bus connections (services) that provide data for the inventory
1533 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534 *
1535 * Finds the connections asynchronously. Invokes callback when information has
1536 * been obtained.
1537 *
1538 * The callback must have the following signature:
1539 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001540 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001541 * @endcode
1542 *
1543 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001544 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001545 * @param callback Callback to invoke when connections have been obtained.
1546 */
1547template <typename Callback>
1548static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001549 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1550 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001551 Callback&& callback)
1552{
Ed Tanous62598e32023-07-17 17:06:25 -07001553 BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554
1555 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001556 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001557 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001558 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1559 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1561
George Liue99073f2022-12-09 11:06:16 +08001562 // Make call to ObjectMapper to find all inventory items
1563 dbus::utility::getSubTree(
1564 path, 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001565 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous002d39b2022-05-31 08:59:27 -07001566 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001567 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001568 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001569 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07001570 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001571 if (ec)
1572 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001573 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07001574 BMCWEB_LOG_ERROR(
1575 "getInventoryItemsConnections respHandler DBus error {}", ec);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001576 return;
1577 }
1578
1579 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001580 std::shared_ptr<std::set<std::string>> invConnections =
1581 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001582
1583 // Loop through objects from GetSubTree
1584 for (const std::pair<
1585 std::string,
1586 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1587 object : subtree)
1588 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001589 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001591 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001592 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001593 // Store all connections to inventory item
1594 for (const std::pair<std::string, std::vector<std::string>>&
1595 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001596 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001597 const std::string& invConnection = objData.first;
1598 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001599 }
1600 }
1601 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001602
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001603 callback(invConnections);
Ed Tanous62598e32023-07-17 17:06:25 -07001604 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05001605 });
Ed Tanous62598e32023-07-17 17:06:25 -07001606 BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001607}
1608
1609/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001610 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611 *
1612 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001613 * inventory items. Then finds the associations from those inventory items to
1614 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001615 *
1616 * Finds the inventory items asynchronously. Invokes callback when information
1617 * has been obtained.
1618 *
1619 * The callback must have the following signature:
1620 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001621 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001622 * @endcode
1623 *
1624 * @param sensorsAsyncResp Pointer to object holding response data.
1625 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626 * implements ObjectManager.
1627 * @param callback Callback to invoke when inventory items have been obtained.
1628 */
1629template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001630static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001631 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001632 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633 Callback&& callback)
1634{
Ed Tanous62598e32023-07-17 17:06:25 -07001635 BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001636
George Liu5eb468d2023-06-20 17:03:24 +08001637 // Call GetManagedObjects on the ObjectMapper to get all associations
1638 sdbusplus::message::object_path path("/");
1639 dbus::utility::getManagedObjects(
1640 "xyz.openbmc_project.ObjectMapper", path,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001641 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001642 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001643 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07001644 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001645 if (ec)
1646 {
Ed Tanous62598e32023-07-17 17:06:25 -07001647 BMCWEB_LOG_ERROR(
1648 "getInventoryItemAssociations respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001649 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001650 return;
1651 }
1652
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001653 // Create vector to hold list of inventory items
1654 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1655 std::make_shared<std::vector<InventoryItem>>();
1656
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001657 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001658 std::string sensorAssocPath;
1659 sensorAssocPath.reserve(128); // avoid memory allocations
1660 for (const auto& objDictEntry : resp)
1661 {
1662 const std::string& objPath =
1663 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001664
1665 // If path is inventory association for one of the specified sensors
1666 for (const std::string& sensorName : *sensorNames)
1667 {
1668 sensorAssocPath = sensorName;
1669 sensorAssocPath += "/inventory";
1670 if (objPath == sensorAssocPath)
1671 {
1672 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001673 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001674 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001675 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001676 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001677 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001678 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001679 if (valueName == "endpoints")
1680 {
1681 const std::vector<std::string>* endpoints =
1682 std::get_if<std::vector<std::string>>(
1683 &value);
1684 if ((endpoints != nullptr) &&
1685 !endpoints->empty())
1686 {
1687 // Add inventory item to vector
1688 const std::string& invItemPath =
1689 endpoints->front();
1690 addInventoryItem(inventoryItems,
1691 invItemPath,
1692 sensorName);
1693 }
1694 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001695 }
1696 }
1697 }
1698 break;
1699 }
1700 }
1701 }
1702
Anthony Wilsond5005492019-07-31 16:34:17 -05001703 // Now loop through the returned object paths again, this time to
1704 // find the leds associated with the inventory items we just found
1705 std::string inventoryAssocPath;
1706 inventoryAssocPath.reserve(128); // avoid memory allocations
1707 for (const auto& objDictEntry : resp)
1708 {
1709 const std::string& objPath =
1710 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001711
1712 for (InventoryItem& inventoryItem : *inventoryItems)
1713 {
1714 inventoryAssocPath = inventoryItem.objectPath;
1715 inventoryAssocPath += "/leds";
1716 if (objPath == inventoryAssocPath)
1717 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001718 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001719 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001720 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001721 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001722 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001723 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001724 if (valueName == "endpoints")
1725 {
1726 const std::vector<std::string>* endpoints =
1727 std::get_if<std::vector<std::string>>(
1728 &value);
1729 if ((endpoints != nullptr) &&
1730 !endpoints->empty())
1731 {
1732 // Add inventory item to vector
1733 // Store LED path in inventory item
1734 const std::string& ledPath =
1735 endpoints->front();
1736 inventoryItem.ledObjectPath = ledPath;
1737 }
1738 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001739 }
1740 }
1741 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001742
Anthony Wilsond5005492019-07-31 16:34:17 -05001743 break;
1744 }
1745 }
1746 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001747 callback(inventoryItems);
Ed Tanous62598e32023-07-17 17:06:25 -07001748 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05001749 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001750
Ed Tanous62598e32023-07-17 17:06:25 -07001751 BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001752}
1753
1754/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001755 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1756 *
1757 * Uses the specified connections (services) to obtain D-Bus data for inventory
1758 * item leds associated with sensors. Stores the resulting data in the
1759 * inventoryItems vector.
1760 *
1761 * This data is later used to provide sensor property values in the JSON
1762 * response.
1763 *
1764 * Finds the inventory item led data asynchronously. Invokes callback when data
1765 * has been obtained.
1766 *
1767 * The callback must have the following signature:
1768 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001769 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001770 * @endcode
1771 *
1772 * This function is called recursively, obtaining data asynchronously from one
1773 * connection in each call. This ensures the callback is not invoked until the
1774 * last asynchronous function has completed.
1775 *
1776 * @param sensorsAsyncResp Pointer to object holding response data.
1777 * @param inventoryItems D-Bus inventory items associated with sensors.
1778 * @param ledConnections Connections that provide data for the inventory leds.
1779 * @param callback Callback to invoke when inventory data has been obtained.
1780 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1781 * in recursive calls to this function.
1782 */
1783template <typename Callback>
1784void getInventoryLedData(
1785 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1786 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001787 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001788 Callback&& callback, size_t ledConnectionsIndex = 0)
1789{
Ed Tanous62598e32023-07-17 17:06:25 -07001790 BMCWEB_LOG_DEBUG("getInventoryLedData enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001791
1792 // If no more connections left, call callback
1793 if (ledConnectionsIndex >= ledConnections->size())
1794 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001795 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001796 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001797 return;
1798 }
1799
1800 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001801 auto it = ledConnections->begin();
1802 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001803 if (it != ledConnections->end())
1804 {
1805 const std::string& ledPath = (*it).first;
1806 const std::string& ledConnection = (*it).second;
1807 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001808 auto respHandler =
1809 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001810 callback = std::forward<Callback>(callback), ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001811 const boost::system::error_code& ec, const std::string& state) {
Ed Tanous62598e32023-07-17 17:06:25 -07001812 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 if (ec)
1814 {
Ed Tanous62598e32023-07-17 17:06:25 -07001815 BMCWEB_LOG_ERROR(
1816 "getInventoryLedData respHandler DBus error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001817 messages::internalError(sensorsAsyncResp->asyncResp->res);
1818 return;
1819 }
1820
Ed Tanous62598e32023-07-17 17:06:25 -07001821 BMCWEB_LOG_DEBUG("Led state: {}", state);
Ed Tanous002d39b2022-05-31 08:59:27 -07001822 // Find inventory item with this LED object path
1823 InventoryItem* inventoryItem =
1824 findInventoryItemForLed(*inventoryItems, ledPath);
1825 if (inventoryItem != nullptr)
1826 {
1827 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001828 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001829 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001830 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001831 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001832 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001833 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001834 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001835 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001836 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001837 {
1838 inventoryItem->ledState = LedState::OFF;
1839 }
1840 else
1841 {
1842 inventoryItem->ledState = LedState::UNKNOWN;
1843 }
1844 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001845
Ed Tanous002d39b2022-05-31 08:59:27 -07001846 // Recurse to get LED data from next connection
1847 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1848 ledConnections, std::move(callback),
1849 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001850
Ed Tanous62598e32023-07-17 17:06:25 -07001851 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07001852 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001853
1854 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001855 sdbusplus::asio::getProperty<std::string>(
1856 *crow::connections::systemBus, ledConnection, ledPath,
1857 "xyz.openbmc_project.Led.Physical", "State",
1858 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001859 }
1860
Ed Tanous62598e32023-07-17 17:06:25 -07001861 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001862}
1863
1864/**
1865 * @brief Gets LED data for LEDs associated with given inventory items.
1866 *
1867 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1868 * associated with the specified inventory items. Then gets the LED data from
1869 * each connection and stores it in the inventory item.
1870 *
1871 * This data is later used to provide sensor property values in the JSON
1872 * response.
1873 *
1874 * Finds the LED data asynchronously. Invokes callback when information has
1875 * been obtained.
1876 *
1877 * The callback must have the following signature:
1878 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001879 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001880 * @endcode
1881 *
1882 * @param sensorsAsyncResp Pointer to object holding response data.
1883 * @param inventoryItems D-Bus inventory items associated with sensors.
1884 * @param callback Callback to invoke when inventory items have been obtained.
1885 */
1886template <typename Callback>
1887void getInventoryLeds(
1888 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1889 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1890 Callback&& callback)
1891{
Ed Tanous62598e32023-07-17 17:06:25 -07001892 BMCWEB_LOG_DEBUG("getInventoryLeds enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001893
1894 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001895 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001896 "xyz.openbmc_project.Led.Physical"};
1897
George Liue99073f2022-12-09 11:06:16 +08001898 // Make call to ObjectMapper to find all inventory items
1899 dbus::utility::getSubTree(
1900 path, 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001901 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanous002d39b2022-05-31 08:59:27 -07001902 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001903 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001904 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001905 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07001906 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001907 if (ec)
1908 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001909 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07001910 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}", ec);
Anthony Wilsond5005492019-07-31 16:34:17 -05001911 return;
1912 }
1913
1914 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001915 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1916 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001917
1918 // Loop through objects from GetSubTree
1919 for (const std::pair<
1920 std::string,
1921 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1922 object : subtree)
1923 {
1924 // Check if object path is LED for one of the specified inventory
1925 // items
1926 const std::string& ledPath = object.first;
1927 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1928 {
1929 // Add mapping from ledPath to connection
1930 const std::string& connection = object.second.begin()->first;
1931 (*ledConnections)[ledPath] = connection;
Ed Tanous62598e32023-07-17 17:06:25 -07001932 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath, connection);
Anthony Wilsond5005492019-07-31 16:34:17 -05001933 }
1934 }
1935
1936 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1937 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07001938 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05001939 });
Ed Tanous62598e32023-07-17 17:06:25 -07001940 BMCWEB_LOG_DEBUG("getInventoryLeds exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001941}
1942
1943/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001944 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1945 *
1946 * Uses the specified connections (services) (currently assumes just one) to
1947 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1948 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1949 *
1950 * This data is later used to provide sensor property values in the JSON
1951 * response.
1952 *
1953 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1954 * when data has been obtained.
1955 *
1956 * The callback must have the following signature:
1957 * @code
1958 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1959 * @endcode
1960 *
1961 * @param sensorsAsyncResp Pointer to object holding response data.
1962 * @param inventoryItems D-Bus inventory items associated with sensors.
1963 * @param psAttributesConnections Connections that provide data for the Power
1964 * Supply Attributes
1965 * @param callback Callback to invoke when data has been obtained.
1966 */
1967template <typename Callback>
1968void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001969 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001970 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001971 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001972 Callback&& callback)
1973{
Ed Tanous62598e32023-07-17 17:06:25 -07001974 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001975
1976 if (psAttributesConnections.empty())
1977 {
Ed Tanous62598e32023-07-17 17:06:25 -07001978 BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001979 callback(inventoryItems);
1980 return;
1981 }
1982
1983 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001984 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001985
1986 const std::string& psAttributesPath = (*it).first;
1987 const std::string& psAttributesConnection = (*it).second;
1988
1989 // Response handler for Get DeratingFactor property
Patrick Williams5a39f772023-10-20 11:20:21 -05001990 auto respHandler = [sensorsAsyncResp, inventoryItems,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001991 callback = std::forward<Callback>(callback)](
Patrick Williams5a39f772023-10-20 11:20:21 -05001992 const boost::system::error_code& ec,
1993 const uint32_t value) {
Ed Tanous62598e32023-07-17 17:06:25 -07001994 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001995 if (ec)
1996 {
Ed Tanous62598e32023-07-17 17:06:25 -07001997 BMCWEB_LOG_ERROR(
1998 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001999 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002000 return;
2001 }
2002
Ed Tanous62598e32023-07-17 17:06:25 -07002003 BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002004 // Store value in Power Supply Inventory Items
2005 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002006 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002007 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002008 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002009 inventoryItem.powerSupplyEfficiencyPercent =
2010 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002011 }
2012 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002013
Ed Tanous62598e32023-07-17 17:06:25 -07002014 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002015 callback(inventoryItems);
2016 };
2017
2018 // Get the DeratingFactor property for the PowerSupplyAttributes
2019 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002020 sdbusplus::asio::getProperty<uint32_t>(
2021 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2022 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2023 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002024
Ed Tanous62598e32023-07-17 17:06:25 -07002025 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002026}
2027
2028/**
2029 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2030 *
2031 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2032 * data. Then gets the Power Supply Attributes data from the connection
2033 * (currently just assumes 1 connection) and stores the data in the inventory
2034 * item.
2035 *
2036 * This data is later used to provide sensor property values in the JSON
2037 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2038 *
2039 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2040 * when information has been obtained.
2041 *
2042 * The callback must have the following signature:
2043 * @code
2044 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2045 * @endcode
2046 *
2047 * @param sensorsAsyncResp Pointer to object holding response data.
2048 * @param inventoryItems D-Bus inventory items associated with sensors.
2049 * @param callback Callback to invoke when data has been obtained.
2050 */
2051template <typename Callback>
2052void getPowerSupplyAttributes(
2053 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2054 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2055 Callback&& callback)
2056{
Ed Tanous62598e32023-07-17 17:06:25 -07002057 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002058
2059 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002060 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002061 {
Ed Tanous62598e32023-07-17 17:06:25 -07002062 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002063 callback(inventoryItems);
2064 return;
2065 }
2066
George Liue99073f2022-12-09 11:06:16 +08002067 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002068 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2069
George Liue99073f2022-12-09 11:06:16 +08002070 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2071 dbus::utility::getSubTree(
2072 "/xyz/openbmc_project", 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -07002073 [callback = std::forward<Callback>(callback), sensorsAsyncResp,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002074 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002075 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002076 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002077 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07002078 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002079 if (ec)
2080 {
2081 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07002082 BMCWEB_LOG_ERROR(
2083 "getPowerSupplyAttributes respHandler DBus error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 return;
2085 }
2086 if (subtree.empty())
2087 {
Ed Tanous62598e32023-07-17 17:06:25 -07002088 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002089 callback(inventoryItems);
2090 return;
2091 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002092
Ed Tanous002d39b2022-05-31 08:59:27 -07002093 // Currently we only support 1 power supply attribute, use this for
2094 // all the power supplies. Build map of object path to connection.
2095 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002096 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002097
Ed Tanous002d39b2022-05-31 08:59:27 -07002098 if (subtree[0].first.empty() || subtree[0].second.empty())
2099 {
Ed Tanous62598e32023-07-17 17:06:25 -07002100 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002101 callback(inventoryItems);
2102 return;
2103 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002104
Ed Tanous002d39b2022-05-31 08:59:27 -07002105 const std::string& psAttributesPath = subtree[0].first;
2106 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002107
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 if (connection.empty())
2109 {
Ed Tanous62598e32023-07-17 17:06:25 -07002110 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002111 callback(inventoryItems);
2112 return;
2113 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002114
Ed Tanous002d39b2022-05-31 08:59:27 -07002115 psAttributesConnections[psAttributesPath] = connection;
Ed Tanous62598e32023-07-17 17:06:25 -07002116 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
2117 connection);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002118
Ed Tanous002d39b2022-05-31 08:59:27 -07002119 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2120 psAttributesConnections,
2121 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07002122 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05002123 });
Ed Tanous62598e32023-07-17 17:06:25 -07002124 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002125}
2126
2127/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002128 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002129 *
2130 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002131 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002132 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002133 * This data is later used to provide sensor property values in the JSON
2134 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002135 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002136 * Finds the inventory items asynchronously. Invokes callback when the
2137 * inventory items have been obtained.
2138 *
2139 * The callback must have the following signature:
2140 * @code
2141 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2142 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002143 *
2144 * @param sensorsAsyncResp Pointer to object holding response data.
2145 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002146 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002147 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002148 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002149template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002150static void
2151 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2152 const std::shared_ptr<std::set<std::string>> sensorNames,
2153 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002154{
Ed Tanous62598e32023-07-17 17:06:25 -07002155 BMCWEB_LOG_DEBUG("getInventoryItems enter");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002156 auto getInventoryItemAssociationsCb =
Ed Tanous8cb2c022024-03-27 16:31:46 -07002157 [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002158 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous62598e32023-07-17 17:06:25 -07002159 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002160 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002161 [sensorsAsyncResp, inventoryItems,
Ed Tanous8cb2c022024-03-27 16:31:46 -07002162 callback = std::forward<const Callback>(callback)](
Nan Zhoufe04d492022-06-22 17:10:41 +00002163 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous62598e32023-07-17 17:06:25 -07002164 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002165 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2166 callback{std::move(callback)}]() {
Ed Tanous62598e32023-07-17 17:06:25 -07002167 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002168
Ed Tanous002d39b2022-05-31 08:59:27 -07002169 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2170 callback{std::move(callback)}]() {
Ed Tanous62598e32023-07-17 17:06:25 -07002171 BMCWEB_LOG_DEBUG("getInventoryLedsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002172 // Find Power Supply Attributes and get the data
2173 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2174 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07002175 BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002176 };
2177
Ed Tanous002d39b2022-05-31 08:59:27 -07002178 // Find led connections and get the data
2179 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2180 std::move(getInventoryLedsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002181 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002182 };
2183
2184 // Get inventory item data from connections
2185 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002186 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002187 std::move(getInventoryItemsDataCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002188 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002189 };
2190
Ed Tanous002d39b2022-05-31 08:59:27 -07002191 // Get connections that provide inventory item data
2192 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2193 std::move(getInventoryItemsConnectionsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002194 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002195 };
2196
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002197 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002198 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002199 std::move(getInventoryItemAssociationsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002200 BMCWEB_LOG_DEBUG("getInventoryItems exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002201}
2202
2203/**
2204 * @brief Returns JSON PowerSupply object for the specified inventory item.
2205 *
2206 * Searches for a JSON PowerSupply object that matches the specified inventory
2207 * item. If one is not found, a new PowerSupply object is added to the JSON
2208 * array.
2209 *
2210 * Multiple sensors are often associated with one power supply inventory item.
2211 * As a result, multiple sensor values are stored in one JSON PowerSupply
2212 * object.
2213 *
2214 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2215 * @param inventoryItem Inventory item for the power supply.
2216 * @param chassisId Chassis that contains the power supply.
2217 * @return JSON PowerSupply object for the specified inventory item.
2218 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002219inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002220 const InventoryItem& inventoryItem,
2221 const std::string& chassisId)
2222{
Ed Tanous18f8f602023-07-18 10:07:23 -07002223 std::string nameS;
Alexander Hansen6f4bd292024-03-08 17:04:54 +01002224 nameS.resize(inventoryItem.name.size());
Ed Tanous18f8f602023-07-18 10:07:23 -07002225 std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002226 // Check if matching PowerSupply object already exists in JSON array
2227 for (nlohmann::json& powerSupply : powerSupplyArray)
2228 {
Ed Tanous18f8f602023-07-18 10:07:23 -07002229 nlohmann::json::iterator nameIt = powerSupply.find("Name");
2230 if (nameIt == powerSupply.end())
2231 {
2232 continue;
2233 }
2234 const std::string* name = nameIt->get_ptr<std::string*>();
2235 if (name == nullptr)
2236 {
2237 continue;
2238 }
2239 if (nameS == *name)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002240 {
2241 return powerSupply;
2242 }
2243 }
2244
2245 // Add new PowerSupply object to JSON array
2246 powerSupplyArray.push_back({});
2247 nlohmann::json& powerSupply = powerSupplyArray.back();
Ed Tanousef4c65b2023-04-24 15:28:50 -07002248 boost::urls::url url = boost::urls::format("/redfish/v1/Chassis/{}/Power",
2249 chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002250 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2251 powerSupply["@odata.id"] = std::move(url);
Ed Tanous18f8f602023-07-18 10:07:23 -07002252 std::string escaped;
Alexander Hansen6f4bd292024-03-08 17:04:54 +01002253 escaped.resize(inventoryItem.name.size());
Ed Tanous18f8f602023-07-18 10:07:23 -07002254 std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
2255 powerSupply["Name"] = std::move(escaped);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2257 powerSupply["Model"] = inventoryItem.model;
2258 powerSupply["PartNumber"] = inventoryItem.partNumber;
2259 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002260 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002261
Gunnar Mills42cbe532019-08-15 15:26:54 -05002262 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2263 {
2264 powerSupply["EfficiencyPercent"] =
2265 inventoryItem.powerSupplyEfficiencyPercent;
2266 }
2267
Matt Simmeringaaf08ac2023-10-04 08:41:01 -07002268 powerSupply["Status"]["State"] = getState(&inventoryItem, true);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002269 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2270 powerSupply["Status"]["Health"] = health;
2271
2272 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002273}
2274
2275/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002276 * @brief Gets the values of the specified sensors.
2277 *
2278 * Stores the results as JSON in the SensorsAsyncResp.
2279 *
2280 * Gets the sensor values asynchronously. Stores the results later when the
2281 * information has been obtained.
2282 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002283 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002284 *
2285 * To minimize the number of DBus calls, the DBus method
2286 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2287 * values of all sensors provided by a connection (service).
2288 *
2289 * The connections set contains all the connections that provide sensor values.
2290 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002291 * The InventoryItem vector contains D-Bus inventory items associated with the
2292 * sensors. Inventory item data is needed for some Redfish sensor properties.
2293 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002294 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002295 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002296 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002297 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002298 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002299 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002300inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002301 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002302 const std::shared_ptr<std::set<std::string>>& sensorNames,
2303 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002304 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002305{
Ed Tanous62598e32023-07-17 17:06:25 -07002306 BMCWEB_LOG_DEBUG("getSensorData enter");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002307 // Get managed objects from all services exposing sensors
2308 for (const std::string& connection : connections)
2309 {
George Liu5eb468d2023-06-20 17:03:24 +08002310 sdbusplus::message::object_path sensorPath(
2311 "/xyz/openbmc_project/sensors");
2312 dbus::utility::getManagedObjects(
2313 connection, sensorPath,
Ed Tanous002d39b2022-05-31 08:59:27 -07002314 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002315 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002316 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07002317 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002318 if (ec)
2319 {
Ed Tanous62598e32023-07-17 17:06:25 -07002320 BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08002321 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002322 return;
2323 }
2324 // Go through all objects and update response with sensor data
2325 for (const auto& objDictEntry : resp)
2326 {
2327 const std::string& objPath =
2328 static_cast<const std::string&>(objDictEntry.first);
Ed Tanous62598e32023-07-17 17:06:25 -07002329 BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
2330 objPath);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002331
Shawn McCarneyde629b62019-03-08 10:42:51 -06002332 std::vector<std::string> split;
2333 // Reserve space for
2334 // /xyz/openbmc_project/sensors/<name>/<subname>
2335 split.reserve(6);
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002336 // NOLINTNEXTLINE
2337 bmcweb::split(split, objPath, '/');
Shawn McCarneyde629b62019-03-08 10:42:51 -06002338 if (split.size() < 6)
2339 {
Ed Tanous62598e32023-07-17 17:06:25 -07002340 BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
2341 objPath);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002342 continue;
2343 }
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002344 // These indexes aren't intuitive, as split puts an empty
Shawn McCarneyde629b62019-03-08 10:42:51 -06002345 // string at the beginning
2346 const std::string& sensorType = split[4];
2347 const std::string& sensorName = split[5];
Ed Tanous62598e32023-07-17 17:06:25 -07002348 BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
2349 sensorType);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002350 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002351 {
Ed Tanous62598e32023-07-17 17:06:25 -07002352 BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002353 continue;
2354 }
2355
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002356 // Find inventory item (if any) associated with sensor
2357 InventoryItem* inventoryItem =
2358 findInventoryItemForSensor(inventoryItems, objPath);
2359
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002360 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002361 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002362
2363 nlohmann::json* sensorJson = nullptr;
2364
Nan Zhou928fefb2022-03-28 08:45:00 -07002365 if (sensorSchema == sensors::node::sensors &&
2366 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002367 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002368 std::string sensorTypeEscaped(sensorType);
Ed Tanous3544d2a2023-08-06 18:12:20 -07002369 auto remove = std::ranges::remove(sensorTypeEscaped, '_');
2370
2371 sensorTypeEscaped.erase(std::ranges::begin(remove),
2372 sensorTypeEscaped.end());
Ed Tanousc1d019a2022-08-06 09:36:06 -07002373 std::string sensorId(sensorTypeEscaped);
2374 sensorId += "_";
2375 sensorId += sensorName;
2376
zhanghch058d1b46d2021-04-01 11:18:24 +08002377 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07002378 boost::urls::format("/redfish/v1/Chassis/{}/{}/{}",
2379 sensorsAsyncResp->chassisId,
2380 sensorsAsyncResp->chassisSubNode,
2381 sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002382 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002383 }
2384 else
2385 {
Ed Tanous271584a2019-07-09 16:24:22 -07002386 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002387 if (sensorsAsyncResp->efficientExpand)
2388 {
2389 fieldName = "Members";
2390 }
2391 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002392 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002393 fieldName = "Temperatures";
2394 }
2395 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2396 sensorType == "fan_pwm")
2397 {
2398 fieldName = "Fans";
2399 }
2400 else if (sensorType == "voltage")
2401 {
2402 fieldName = "Voltages";
2403 }
2404 else if (sensorType == "power")
2405 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002406 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002407 {
2408 fieldName = "PowerControl";
2409 }
2410 else if ((inventoryItem != nullptr) &&
2411 (inventoryItem->isPowerSupply))
2412 {
2413 fieldName = "PowerSupplies";
2414 }
2415 else
2416 {
2417 // Other power sensors are in SensorCollection
2418 continue;
2419 }
2420 }
2421 else
2422 {
Ed Tanous62598e32023-07-17 17:06:25 -07002423 BMCWEB_LOG_ERROR("Unsure how to handle sensorType {}",
2424 sensorType);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002425 continue;
2426 }
2427
2428 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002429 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002430 if (fieldName == "PowerControl")
2431 {
2432 if (tempArray.empty())
2433 {
2434 // Put multiple "sensors" into a single
2435 // PowerControl. Follows MemberId naming and
2436 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002437 nlohmann::json::object_t power;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002438 boost::urls::url url = boost::urls::format(
2439 "/redfish/v1/Chassis/{}/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002440 sensorsAsyncResp->chassisId,
2441 sensorsAsyncResp->chassisSubNode);
2442 url.set_fragment((""_json_pointer / fieldName / "0")
2443 .to_string());
2444 power["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002445 tempArray.emplace_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002446 }
2447 sensorJson = &(tempArray.back());
2448 }
2449 else if (fieldName == "PowerSupplies")
2450 {
2451 if (inventoryItem != nullptr)
2452 {
2453 sensorJson =
2454 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002455 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002456 }
2457 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002458 else if (fieldName == "Members")
2459 {
Ed Tanous677bb752022-09-15 10:52:19 -07002460 std::string sensorTypeEscaped(sensorType);
Ed Tanous3544d2a2023-08-06 18:12:20 -07002461 auto remove = std::ranges::remove(sensorTypeEscaped,
2462 '_');
2463 sensorTypeEscaped.erase(std::ranges::begin(remove),
2464 sensorTypeEscaped.end());
Ed Tanous677bb752022-09-15 10:52:19 -07002465 std::string sensorId(sensorTypeEscaped);
2466 sensorId += "_";
2467 sensorId += sensorName;
2468
Ed Tanous14766872022-03-15 10:44:42 -07002469 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002470 member["@odata.id"] = boost::urls::format(
2471 "/redfish/v1/Chassis/{}/{}/{}",
Ed Tanous677bb752022-09-15 10:52:19 -07002472 sensorsAsyncResp->chassisId,
2473 sensorsAsyncResp->chassisSubNode, sensorId);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002474 tempArray.emplace_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002475 sensorJson = &(tempArray.back());
2476 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002477 else
2478 {
Ed Tanous14766872022-03-15 10:44:42 -07002479 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002480 boost::urls::url url = boost::urls::format(
2481 "/redfish/v1/Chassis/{}/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002482 sensorsAsyncResp->chassisId,
2483 sensorsAsyncResp->chassisSubNode);
2484 url.set_fragment(
2485 (""_json_pointer / fieldName).to_string());
2486 member["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002487 tempArray.emplace_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002488 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002489 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002490 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002491
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002492 if (sensorJson != nullptr)
2493 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002494 objectInterfacesToJson(sensorName, sensorType,
2495 sensorsAsyncResp->chassisSubNode,
2496 objDictEntry.second, *sensorJson,
2497 inventoryItem);
2498
2499 std::string path = "/xyz/openbmc_project/sensors/";
2500 path += sensorType;
2501 path += "/";
2502 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002503 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002504 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002505 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002506 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002507 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002508 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002509 if (sensorsAsyncResp->chassisSubNode ==
2510 sensors::node::sensors &&
2511 sensorsAsyncResp->efficientExpand)
2512 {
2513 sensorsAsyncResp->asyncResp->res
2514 .jsonValue["Members@odata.count"] =
2515 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2516 .size();
2517 }
2518 else if (sensorsAsyncResp->chassisSubNode ==
2519 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002520 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002521 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002522 }
James Feist8bd25cc2019-03-15 15:14:00 -07002523 }
Ed Tanous62598e32023-07-17 17:06:25 -07002524 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05002525 });
Ed Tanous23a21a12020-07-25 04:45:05 +00002526 }
Ed Tanous62598e32023-07-17 17:06:25 -07002527 BMCWEB_LOG_DEBUG("getSensorData exit");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002528}
2529
Nan Zhoufe04d492022-06-22 17:10:41 +00002530inline void
2531 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2532 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002533{
Nan Zhoufe04d492022-06-22 17:10:41 +00002534 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2535 const std::set<std::string>& connections) {
Ed Tanous62598e32023-07-17 17:06:25 -07002536 BMCWEB_LOG_DEBUG("getConnectionCb enter");
Ed Tanousd0090732022-10-04 17:22:56 -07002537 auto getInventoryItemsCb =
2538 [sensorsAsyncResp, sensorNames,
2539 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2540 inventoryItems) {
Ed Tanous62598e32023-07-17 17:06:25 -07002541 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
Ed Tanousd0090732022-10-04 17:22:56 -07002542 // Get sensor data and store results in JSON
2543 getSensorData(sensorsAsyncResp, sensorNames, connections,
2544 inventoryItems);
Ed Tanous62598e32023-07-17 17:06:25 -07002545 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002546 };
2547
Ed Tanousd0090732022-10-04 17:22:56 -07002548 // Get inventory items associated with sensors
2549 getInventoryItems(sensorsAsyncResp, sensorNames,
2550 std::move(getInventoryItemsCb));
2551
Ed Tanous62598e32023-07-17 17:06:25 -07002552 BMCWEB_LOG_DEBUG("getConnectionCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002553 };
2554
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002555 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002556 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002557}
2558
Shawn McCarneyde629b62019-03-08 10:42:51 -06002559/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002560 * @brief Entry point for retrieving sensors data related to requested
2561 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002562 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002563 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002564inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002565 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002566{
Ed Tanous62598e32023-07-17 17:06:25 -07002567 BMCWEB_LOG_DEBUG("getChassisData enter");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002568 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002569 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002570 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous62598e32023-07-17 17:06:25 -07002571 BMCWEB_LOG_DEBUG("getChassisCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002572 processSensorList(sensorsAsyncResp, sensorNames);
Ed Tanous62598e32023-07-17 17:06:25 -07002573 BMCWEB_LOG_DEBUG("getChassisCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002574 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002575 // SensorCollection doesn't contain the Redundancy property
2576 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2577 {
2578 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2579 nlohmann::json::array();
2580 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002581 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002582 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2583 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2584 std::move(getChassisCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002585 BMCWEB_LOG_DEBUG("getChassisData exit");
Ed Tanous271584a2019-07-09 16:24:22 -07002586}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002587
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302588/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002589 * @brief Find the requested sensorName in the list of all sensors supplied by
2590 * the chassis node
2591 *
2592 * @param sensorName The sensor name supplied in the PATCH request
2593 * @param sensorsList The list of sensors managed by the chassis node
2594 * @param sensorsModified The list of sensors that were found as a result of
2595 * repeated calls to this function
2596 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002597inline bool
2598 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002599 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002600 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002601{
Nan Zhoufe04d492022-06-22 17:10:41 +00002602 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002603 {
George Liu28aa8de2021-02-01 15:13:30 +08002604 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002605 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002606 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002607 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002608 continue;
2609 }
2610 if (thisSensorName == sensorName)
2611 {
2612 sensorsModified.emplace(chassisSensor);
2613 return true;
2614 }
2615 }
2616 return false;
2617}
2618
Ed Tanousc71d6122022-11-29 14:10:32 -08002619inline std::pair<std::string, std::string>
2620 splitSensorNameAndType(std::string_view sensorId)
2621{
2622 size_t index = sensorId.find('_');
2623 if (index == std::string::npos)
2624 {
2625 return std::make_pair<std::string, std::string>("", "");
2626 }
2627 std::string sensorType{sensorId.substr(0, index)};
2628 std::string sensorName{sensorId.substr(index + 1)};
2629 // fan_pwm and fan_tach need special handling
2630 if (sensorType == "fantach" || sensorType == "fanpwm")
2631 {
2632 sensorType.insert(3, 1, '_');
2633 }
2634 return std::make_pair(sensorType, sensorName);
2635}
2636
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002637/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302638 * @brief Entry point for overriding sensor values of given sensor
2639 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002640 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002641 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002642 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302643 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002644inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002645 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Ed Tanous08850572024-03-06 15:09:17 -08002646 std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002647 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302648{
Ed Tanous62598e32023-07-17 17:06:25 -07002649 BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
2650 sensorAsyncResp->chassisSubNode);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302651
Ed Tanousd02aad32024-02-13 14:43:34 -08002652 std::string_view propertyValueName;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302653 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302654 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002655 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302656 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302657 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302658 if (collectionItems.first == "Temperatures")
2659 {
2660 propertyValueName = "ReadingCelsius";
2661 }
2662 else if (collectionItems.first == "Fans")
2663 {
2664 propertyValueName = "Reading";
2665 }
2666 else
2667 {
2668 propertyValueName = "ReadingVolts";
2669 }
2670 for (auto& item : collectionItems.second)
2671 {
Ed Tanous08850572024-03-06 15:09:17 -08002672 if (!json_util::readJsonObject(
2673 item, sensorAsyncResp->asyncResp->res, "MemberId", memberId,
2674 propertyValueName, value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302675 {
2676 return;
2677 }
2678 overrideMap.emplace(memberId,
2679 std::make_pair(value, collectionItems.first));
2680 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302681 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002682
Ed Tanous002d39b2022-05-31 08:59:27 -07002683 auto getChassisSensorListCb =
Ed Tanousd02aad32024-02-13 14:43:34 -08002684 [sensorAsyncResp, overrideMap,
2685 propertyValueNameStr = std::string(propertyValueName)](
Nan Zhoufe04d492022-06-22 17:10:41 +00002686 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002687 // Match sensor names in the PATCH request to those managed by the
2688 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002689 const std::shared_ptr<std::set<std::string>> sensorNames =
2690 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302691 for (const auto& item : overrideMap)
2692 {
2693 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002694 std::pair<std::string, std::string> sensorNameType =
2695 splitSensorNameAndType(sensor);
2696 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2697 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302698 {
Ed Tanous62598e32023-07-17 17:06:25 -07002699 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
zhanghch058d1b46d2021-04-01 11:18:24 +08002700 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302701 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302702 return;
2703 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302704 }
2705 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002706 auto getObjectsWithConnectionCb =
Ed Tanousd02aad32024-02-13 14:43:34 -08002707 [sensorAsyncResp, overrideMap, propertyValueNameStr](
2708 const std::set<std::string>& /*connections*/,
2709 const std::set<std::pair<std::string, std::string>>&
2710 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002711 if (objectsWithConnection.size() != overrideMap.size())
2712 {
Ed Tanous62598e32023-07-17 17:06:25 -07002713 BMCWEB_LOG_INFO(
2714 "Unable to find all objects with proper connection {} requested {}",
2715 objectsWithConnection.size(), overrideMap.size());
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002716 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2717 sensorAsyncResp->chassisSubNode ==
2718 sensors::node::thermal
2719 ? "Temperatures"
2720 : "Voltages",
2721 "Count");
2722 return;
2723 }
2724 for (const auto& item : objectsWithConnection)
2725 {
2726 sdbusplus::message::object_path path(item.first);
2727 std::string sensorName = path.filename();
2728 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302729 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002730 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302731 return;
2732 }
Ban Feng3f5eb752023-06-29 09:19:20 +08002733 std::string id = path.parent_path().filename();
Ed Tanous3544d2a2023-08-06 18:12:20 -07002734 auto remove = std::ranges::remove(id, '_');
2735 id.erase(std::ranges::begin(remove), id.end());
Ban Feng3f5eb752023-06-29 09:19:20 +08002736 id += "_";
2737 id += sensorName;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302738
Ban Feng3f5eb752023-06-29 09:19:20 +08002739 const auto& iterator = overrideMap.find(id);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002740 if (iterator == overrideMap.end())
2741 {
Ed Tanous62598e32023-07-17 17:06:25 -07002742 BMCWEB_LOG_INFO("Unable to find sensor object{}",
2743 item.first);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002744 messages::internalError(sensorAsyncResp->asyncResp->res);
2745 return;
2746 }
Ginu Georgee93abac2024-06-14 17:35:27 +05302747 setDbusProperty(sensorAsyncResp->asyncResp,
2748 propertyValueNameStr, item.second, item.first,
2749 "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanousd02aad32024-02-13 14:43:34 -08002750 iterator->second.first);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002751 }
2752 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302753 // Get object with connection for the given sensor name
2754 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2755 std::move(getObjectsWithConnectionCb));
2756 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302757 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002758 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2759 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2760 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302761}
2762
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002763/**
2764 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2765 * path of the sensor.
2766 *
2767 * Function builds valid Redfish response for sensor query of given chassis and
2768 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2769 * it to caller in a callback.
2770 *
2771 * @param chassis Chassis for which retrieval should be performed
2772 * @param node Node (group) of sensors. See sensors::node for supported values
2773 * @param mapComplete Callback to be called with retrieval result
2774 */
Ed Tanous931edc72023-11-01 12:09:07 -07002775template <typename Callback>
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002776inline void retrieveUriToDbusMap(const std::string& chassis,
2777 const std::string& node,
Ed Tanous931edc72023-11-01 12:09:07 -07002778 Callback&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002779{
Ed Tanous02da7c52022-02-27 00:09:02 -08002780 decltype(sensors::paths)::const_iterator pathIt =
2781 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2782 [&node](auto&& val) { return val.first == node; });
2783 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002784 {
Ed Tanous62598e32023-07-17 17:06:25 -07002785 BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
Ed Tanous6804b5c2023-10-31 14:50:03 -07002786 std::map<std::string, std::string> noop;
2787 mapComplete(boost::beast::http::status::bad_request, noop);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002788 return;
2789 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002790
Nan Zhou72374eb2022-01-27 17:06:51 -08002791 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Ed Tanous931edc72023-11-01 12:09:07 -07002792 auto callback = [asyncResp,
Ed Tanous8cb2c022024-03-27 16:31:46 -07002793 mapCompleteCb = std::forward<Callback>(mapComplete)](
Nan Zhoufe04d492022-06-22 17:10:41 +00002794 const boost::beast::http::status status,
2795 const std::map<std::string, std::string>& uriToDbus) {
2796 mapCompleteCb(status, uriToDbus);
2797 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002798
2799 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002800 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002801 getChassisData(resp);
2802}
2803
Nan Zhoubacb2162022-04-06 11:28:32 -07002804namespace sensors
2805{
Nan Zhou928fefb2022-03-28 08:45:00 -07002806
Nan Zhoubacb2162022-04-06 11:28:32 -07002807inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002808 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2809 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002810 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002811{
Ed Tanous62598e32023-07-17 17:06:25 -07002812 BMCWEB_LOG_DEBUG("getChassisCallback enter ");
Nan Zhoubacb2162022-04-06 11:28:32 -07002813
Ed Tanousc1d019a2022-08-06 09:36:06 -07002814 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2815 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002816 {
Ed Tanous62598e32023-07-17 17:06:25 -07002817 BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
Nan Zhoubacb2162022-04-06 11:28:32 -07002818
2819 sdbusplus::message::object_path path(sensor);
2820 std::string sensorName = path.filename();
2821 if (sensorName.empty())
2822 {
Ed Tanous62598e32023-07-17 17:06:25 -07002823 BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002824 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002825 return;
2826 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002827 std::string type = path.parent_path().filename();
2828 // fan_tach has an underscore in it, so remove it to "normalize" the
2829 // type in the URI
Ed Tanous3544d2a2023-08-06 18:12:20 -07002830 auto remove = std::ranges::remove(type, '_');
2831 type.erase(std::ranges::begin(remove), type.end());
Ed Tanousc1d019a2022-08-06 09:36:06 -07002832
Ed Tanous14766872022-03-15 10:44:42 -07002833 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002834 std::string id = type;
2835 id += "_";
2836 id += sensorName;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002837 member["@odata.id"] = boost::urls::format(
2838 "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002839
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002840 entriesArray.emplace_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002841 }
2842
Ed Tanousc1d019a2022-08-06 09:36:06 -07002843 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous62598e32023-07-17 17:06:25 -07002844 BMCWEB_LOG_DEBUG("getChassisCallback exit");
Nan Zhoubacb2162022-04-06 11:28:32 -07002845}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002846
Ed Tanousac106bf2023-06-07 09:24:59 -07002847inline void handleSensorCollectionGet(
2848 App& app, const crow::Request& req,
2849 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2850 const std::string& chassisId)
Nan Zhoude167a62022-06-01 04:47:45 +00002851{
2852 query_param::QueryCapabilities capabilities = {
2853 .canDelegateExpandLevel = 1,
2854 };
2855 query_param::Query delegatedQuery;
Ed Tanousac106bf2023-06-07 09:24:59 -07002856 if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002857 delegatedQuery, capabilities))
2858 {
2859 return;
2860 }
2861
2862 if (delegatedQuery.expandType != query_param::ExpandType::None)
2863 {
2864 // we perform efficient expand.
Ed Tanousac106bf2023-06-07 09:24:59 -07002865 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2866 asyncResp, chassisId, sensors::dbus::sensorPaths,
Nan Zhoude167a62022-06-01 04:47:45 +00002867 sensors::node::sensors,
2868 /*efficientExpand=*/true);
Ed Tanousac106bf2023-06-07 09:24:59 -07002869 getChassisData(sensorsAsyncResp);
Nan Zhoude167a62022-06-01 04:47:45 +00002870
Ed Tanous62598e32023-07-17 17:06:25 -07002871 BMCWEB_LOG_DEBUG(
2872 "SensorCollection doGet exit via efficient expand handler");
Nan Zhoude167a62022-06-01 04:47:45 +00002873 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002874 }
Nan Zhoude167a62022-06-01 04:47:45 +00002875
Nan Zhoude167a62022-06-01 04:47:45 +00002876 // We get all sensors as hyperlinkes in the chassis (this
2877 // implies we reply on the default query parameters handler)
Ed Tanousac106bf2023-06-07 09:24:59 -07002878 getChassis(asyncResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2879 std::bind_front(sensors::getChassisCallback, asyncResp,
2880 chassisId, sensors::node::sensors));
Ed Tanousc1d019a2022-08-06 09:36:06 -07002881}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002882
Ed Tanousc1d019a2022-08-06 09:36:06 -07002883inline void
2884 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2885 const std::string& sensorPath,
2886 const ::dbus::utility::MapperGetObject& mapperResponse)
2887{
2888 if (mapperResponse.size() != 1)
2889 {
2890 messages::internalError(asyncResp->res);
2891 return;
2892 }
2893 const auto& valueIface = *mapperResponse.begin();
2894 const std::string& connectionName = valueIface.first;
Ed Tanous62598e32023-07-17 17:06:25 -07002895 BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
2896 BMCWEB_LOG_DEBUG("Path {}", sensorPath);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002897
2898 sdbusplus::asio::getAllProperties(
2899 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002900 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002901 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002902 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2903 if (ec)
2904 {
2905 messages::internalError(asyncResp->res);
2906 return;
2907 }
2908 sdbusplus::message::object_path path(sensorPath);
2909 std::string name = path.filename();
2910 path = path.parent_path();
2911 std::string type = path.filename();
2912 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2913 asyncResp->res.jsonValue, nullptr);
Patrick Williams5a39f772023-10-20 11:20:21 -05002914 });
Nan Zhoude167a62022-06-01 04:47:45 +00002915}
2916
Nan Zhoue6bd8462022-06-01 04:35:35 +00002917inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002918 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002919 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002920 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002921{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002922 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002923 {
2924 return;
2925 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002926 std::pair<std::string, std::string> nameType =
2927 splitSensorNameAndType(sensorId);
2928 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002929 {
2930 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2931 return;
2932 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002933
Ed Tanousef4c65b2023-04-24 15:28:50 -07002934 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2935 "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002936
Ed Tanous62598e32023-07-17 17:06:25 -07002937 BMCWEB_LOG_DEBUG("Sensor doGet enter");
Nan Zhoue6bd8462022-06-01 04:35:35 +00002938
George Liu2b731192023-01-11 16:27:13 +08002939 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002940 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002941 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2942 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002943 // Get a list of all of the sensors that implement Sensor.Value
2944 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002945 ::dbus::utility::getDbusObject(
2946 sensorPath, interfaces,
Myung Baeaec0ec32023-05-31 15:10:01 -05002947 [asyncResp, sensorId,
George Liu2b731192023-01-11 16:27:13 +08002948 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002949 const ::dbus::utility::MapperGetObject& subtree) {
Ed Tanous62598e32023-07-17 17:06:25 -07002950 BMCWEB_LOG_DEBUG("respHandler1 enter");
Myung Baeaec0ec32023-05-31 15:10:01 -05002951 if (ec == boost::system::errc::io_error)
2952 {
Ed Tanous62598e32023-07-17 17:06:25 -07002953 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
Myung Baeaec0ec32023-05-31 15:10:01 -05002954 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2955 return;
2956 }
Nan Zhoue6bd8462022-06-01 04:35:35 +00002957 if (ec)
2958 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002959 messages::internalError(asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07002960 BMCWEB_LOG_ERROR(
2961 "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002962 return;
2963 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002964 getSensorFromDbus(asyncResp, sensorPath, subtree);
Ed Tanous62598e32023-07-17 17:06:25 -07002965 BMCWEB_LOG_DEBUG("respHandler1 exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05002966 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002967}
2968
Nan Zhoubacb2162022-04-06 11:28:32 -07002969} // namespace sensors
2970
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002971inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002972{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002973 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002974 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002975 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002976 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002977}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002978
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002979inline void requestRoutesSensor(App& app)
2980{
2981 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002982 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002983 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002984 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002985}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002986
Ed Tanous1abe55e2018-09-05 08:30:59 -07002987} // namespace redfish