blob: 4969c8fe22258c577d5d53de82479f9cd7f61c6f [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 Tanous0ec8b832022-03-14 14:56:47 -070021#include "generated/enums/sensor.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include "query.hpp"
23#include "registries/privilege_registry.hpp"
Ed Tanous50ebd4a2023-01-19 19:03:17 -080024#include "str_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "utils/dbus_utils.hpp"
26#include "utils/json_utils.hpp"
27#include "utils/query_param.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070028
Ed Tanous11ba3972022-07-11 09:50:41 -070029#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070030#include <boost/algorithm/string/find.hpp>
31#include <boost/algorithm/string/predicate.hpp>
Ed Tanousc71d6122022-11-29 14:10:32 -080032#include <boost/algorithm/string/replace.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010033#include <boost/range/algorithm/replace_copy_if.hpp>
George Liue99073f2022-12-09 11:06:16 +080034#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070035#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070036#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020037#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038
George Liu7a1dbc42022-12-07 16:03:22 +080039#include <array>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000041#include <iterator>
Ed Tanous283860f2022-08-29 14:08:50 -070042#include <limits>
Nan Zhoufe04d492022-06-22 17:10:41 +000043#include <map>
44#include <set>
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 Tanouscf9e4172022-12-21 09:30:16 -080069constexpr auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080070 "/xyz/openbmc_project/sensors/power",
71 "/xyz/openbmc_project/sensors/current",
72 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070073 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080074#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080075 "/xyz/openbmc_project/sensors/voltage",
76 "/xyz/openbmc_project/sensors/fan_tach",
77 "/xyz/openbmc_project/sensors/temperature",
78 "/xyz/openbmc_project/sensors/fan_pwm",
79 "/xyz/openbmc_project/sensors/altitude",
80 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080081#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080082 "/xyz/openbmc_project/sensors/utilization"
83});
84
Ed Tanouscf9e4172022-12-21 09:30:16 -080085constexpr auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080086 "/xyz/openbmc_project/sensors/fan_tach",
87 "/xyz/openbmc_project/sensors/temperature",
88 "/xyz/openbmc_project/sensors/fan_pwm"
89});
90
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000091} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080092// clang-format on
93
Ed Tanouscf9e4172022-12-21 09:30:16 -080094using sensorPair =
95 std::pair<std::string_view, std::span<const std::string_view>>;
Ed Tanous02da7c52022-02-27 00:09:02 -080096static constexpr std::array<sensorPair, 3> paths = {
Ed Tanouscf9e4172022-12-21 09:30:16 -080097 {{node::power, dbus::powerPaths},
98 {node::sensors, dbus::sensorPaths},
99 {node::thermal, dbus::thermalPaths}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000100
Ed Tanous0ec8b832022-03-14 14:56:47 -0700101inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000102{
103 if (sensorType == "voltage")
104 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700105 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000106 }
107 if (sensorType == "power")
108 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700109 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000110 }
111 if (sensorType == "current")
112 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700113 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000114 }
115 if (sensorType == "fan_tach")
116 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700117 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000118 }
119 if (sensorType == "temperature")
120 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700121 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000122 }
123 if (sensorType == "fan_pwm" || sensorType == "utilization")
124 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700125 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000126 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600127 if (sensorType == "humidity")
128 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700129 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600130 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000131 if (sensorType == "altitude")
132 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700133 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000134 }
135 if (sensorType == "airflow")
136 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700137 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000138 }
139 if (sensorType == "energy")
140 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700141 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000142 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700143 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000144}
145
Ed Tanous1d7c0052022-08-09 12:32:26 -0700146inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000147{
148 if (sensorType == "voltage")
149 {
150 return "V";
151 }
152 if (sensorType == "power")
153 {
154 return "W";
155 }
156 if (sensorType == "current")
157 {
158 return "A";
159 }
160 if (sensorType == "fan_tach")
161 {
162 return "RPM";
163 }
164 if (sensorType == "temperature")
165 {
166 return "Cel";
167 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600168 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
169 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000170 {
171 return "%";
172 }
173 if (sensorType == "altitude")
174 {
175 return "m";
176 }
177 if (sensorType == "airflow")
178 {
179 return "cft_i/min";
180 }
181 if (sensorType == "energy")
182 {
183 return "J";
184 }
185 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200186}
187} // namespace sensors
188
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100189/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200190 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100191 * Gathers data needed for response processing after async calls are done
192 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700193class SensorsAsyncResp
194{
195 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200196 using DataCompleteCb = std::function<void(
197 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000198 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200199
200 struct SensorData
201 {
202 const std::string name;
203 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200204 const std::string dbusPath;
205 };
206
Ed Tanous8a592812022-06-04 09:06:59 -0700207 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800208 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800209 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800210 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700211 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700212 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
213 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500214 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200215
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200216 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700217 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800218 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800219 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800220 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200221 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700222 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700223 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
224 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200225 dataComplete{std::move(creationComplete)}
226 {}
227
Nan Zhou928fefb2022-03-28 08:45:00 -0700228 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700229 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700230 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800231 std::span<const std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700232 const std::string_view& subNode, bool efficientExpandIn) :
233 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700234 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700235 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700236 {}
237
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 ~SensorsAsyncResp()
239 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800240 if (asyncResp->res.result() ==
241 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242 {
243 // Reset the json object to clear out any data that made it in
244 // before the error happened todo(ed) handle error condition with
245 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800246 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200248
249 if (dataComplete && metadata)
250 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000251 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800252 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200253 {
254 for (auto& sensor : *metadata)
255 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700256 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200257 }
258 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800259 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200260 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700261 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100262
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800263 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
264 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
265 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
266 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
267
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200268 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700269 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200270 {
271 if (metadata)
272 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700273 metadata->emplace_back(SensorData{
274 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200275 }
276 }
277
278 void updateUri(const std::string& name, const std::string& uri)
279 {
280 if (metadata)
281 {
282 for (auto& sensor : *metadata)
283 {
284 if (sensor.name == name)
285 {
286 sensor.uri = uri;
287 }
288 }
289 }
290 }
291
zhanghch058d1b46d2021-04-01 11:18:24 +0800292 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200293 const std::string chassisId;
Ed Tanouscf9e4172022-12-21 09:30:16 -0800294 const std::span<const std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200295 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700296 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200297
298 private:
299 std::optional<std::vector<SensorData>> metadata;
300 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100301};
302
303/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500304 * Possible states for physical inventory leds
305 */
306enum class LedState
307{
308 OFF,
309 ON,
310 BLINK,
311 UNKNOWN
312};
313
314/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500315 * D-Bus inventory item associated with one or more sensors.
316 */
317class InventoryItem
318{
319 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700320 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500321 {
322 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800323 sdbusplus::message::object_path path(objectPath);
324 name = path.filename();
325 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500326 {
Ed Tanous62598e32023-07-17 17:06:25 -0700327 BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500328 }
329 }
330
331 std::string objectPath;
332 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800333 bool isPresent = true;
334 bool isFunctional = true;
335 bool isPowerSupply = false;
336 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500337 std::string manufacturer;
338 std::string model;
339 std::string partNumber;
340 std::string serialNumber;
341 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500342 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800343 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500344};
345
346/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530347 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200348 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100349 * @param sensorNames Sensors retrieved from chassis
350 * @param callback Callback for processing gathered connections
351 */
352template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530353void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000354 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000355 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530356 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700357{
Ed Tanous62598e32023-07-17 17:06:25 -0700358 BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700359 const std::string path = "/xyz/openbmc_project/sensors";
George Liue99073f2022-12-09 11:06:16 +0800360 constexpr std::array<std::string_view, 1> interfaces = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700361 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100362
George Liue99073f2022-12-09 11:06:16 +0800363 // Make call to ObjectMapper to find all sensors objects
364 dbus::utility::getSubTree(
365 path, 2, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700366 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
George Liue99073f2022-12-09 11:06:16 +0800367 sensorNames](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700368 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +0800369 // Response handler for parsing objects subtree
Ed Tanous62598e32023-07-17 17:06:25 -0700370 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 if (ec)
372 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800373 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -0700374 BMCWEB_LOG_ERROR(
375 "getObjectsWithConnection resp_handler: Dbus error {}", ec);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700376 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100377 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100378
Ed Tanous62598e32023-07-17 17:06:25 -0700379 BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380
381 // Make unique list of connections only for requested sensor types and
382 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000383 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530384 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700385
Ed Tanous62598e32023-07-17 17:06:25 -0700386 BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700387 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700388 {
Ed Tanous62598e32023-07-17 17:06:25 -0700389 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 }
391
392 for (const std::pair<
393 std::string,
394 std::vector<std::pair<std::string, std::vector<std::string>>>>&
395 object : subtree)
396 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700397 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700398 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700399 for (const std::pair<std::string, std::vector<std::string>>&
400 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 {
Ed Tanous62598e32023-07-17 17:06:25 -0700402 BMCWEB_LOG_DEBUG("Adding connection: {}", objData.first);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700403 connections.insert(objData.first);
404 objectsWithConnection.insert(
405 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700406 }
407 }
408 }
Ed Tanous62598e32023-07-17 17:06:25 -0700409 BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530410 callback(std::move(connections), std::move(objectsWithConnection));
Ed Tanous62598e32023-07-17 17:06:25 -0700411 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
George Liue99073f2022-12-09 11:06:16 +0800412 });
Ed Tanous62598e32023-07-17 17:06:25 -0700413 BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530414}
415
416/**
417 * @brief Create connections necessary for sensors
418 * @param SensorsAsyncResp Pointer to object holding response data
419 * @param sensorNames Sensors retrieved from chassis
420 * @param callback Callback for processing gathered connections
421 */
422template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000423void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
424 const std::shared_ptr<std::set<std::string>> sensorNames,
425 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530426{
427 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000428 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530429 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700430 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000431 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530432 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100433}
434
435/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700436 * @brief Shrinks the list of sensors for processing
437 * @param SensorsAysncResp The class holding the Redfish response
438 * @param allSensors A list of all the sensors associated to the
439 * chassis element (i.e. baseboard, front panel, etc...)
440 * @param activeSensors A list that is a reduction of the incoming
441 * allSensors list. Eliminate Thermal sensors when a Power request is
442 * made, and eliminate Power sensors when a Thermal request is made.
443 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000444inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700445 crow::Response& res, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800446 std::span<const std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000448 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700449{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700450 if ((allSensors == nullptr) || (activeSensors == nullptr))
451 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700452 messages::resourceNotFound(res, chassisSubNode,
453 chassisSubNode == sensors::node::thermal
454 ? "Temperatures"
455 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700456
457 return;
458 }
459 if (allSensors->empty())
460 {
461 // Nothing to do, the activeSensors object is also empty
462 return;
463 }
464
Ed Tanous7f1cc262022-08-09 13:33:57 -0700465 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700466 {
467 for (const std::string& sensor : *allSensors)
468 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700469 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700470 {
471 activeSensors->emplace(sensor);
472 }
473 }
474 }
475}
476
Ed Tanous7f1cc262022-08-09 13:33:57 -0700477/*
478 *Populates the top level collection for a given subnode. Populates
479 *SensorCollection, Power, or Thermal schemas.
480 *
481 * */
482inline void populateChassisNode(nlohmann::json& jsonValue,
483 std::string_view chassisSubNode)
484{
485 if (chassisSubNode == sensors::node::power)
486 {
487 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
488 }
489 else if (chassisSubNode == sensors::node::thermal)
490 {
491 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
492 jsonValue["Fans"] = nlohmann::json::array();
493 jsonValue["Temperatures"] = nlohmann::json::array();
494 }
495 else if (chassisSubNode == sensors::node::sensors)
496 {
497 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
498 jsonValue["Description"] = "Collection of Sensors for this Chassis";
499 jsonValue["Members"] = nlohmann::json::array();
500 jsonValue["Members@odata.count"] = 0;
501 }
502
503 if (chassisSubNode != sensors::node::sensors)
504 {
505 jsonValue["Id"] = chassisSubNode;
506 }
507 jsonValue["Name"] = chassisSubNode;
508}
509
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700510/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100511 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200512 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100513 * @param callback Callback for next step in gathered sensor processing
514 */
515template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700516void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
517 std::string_view chassisId, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800518 std::span<const std::string_view> sensorTypes,
519 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700520{
Ed Tanous62598e32023-07-17 17:06:25 -0700521 BMCWEB_LOG_DEBUG("getChassis enter");
George Liu7a1dbc42022-12-07 16:03:22 +0800522 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700523 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500524 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800525
526 // Get the Chassis Collection
527 dbus::utility::getSubTreePaths(
528 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700529 [callback{std::forward<Callback>(callback)}, asyncResp,
530 chassisIdStr{std::string(chassisId)},
531 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800532 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700533 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous62598e32023-07-17 17:06:25 -0700534 BMCWEB_LOG_DEBUG("getChassis respHandler enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700535 if (ec)
536 {
Ed Tanous62598e32023-07-17 17:06:25 -0700537 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
Ed Tanous7f1cc262022-08-09 13:33:57 -0700538 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700539 return;
540 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 const std::string* chassisPath = nullptr;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700542 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543 {
George Liu28aa8de2021-02-01 15:13:30 +0800544 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700545 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800546 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 {
Ed Tanous62598e32023-07-17 17:06:25 -0700548 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700549 continue;
550 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700551 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700553 chassisPath = &chassis;
554 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700555 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700556 }
557 if (chassisPath == nullptr)
558 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700559 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700560 return;
561 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700562 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700563
Ed Tanousef4c65b2023-04-24 15:28:50 -0700564 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
565 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500566
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500567 // Get the list of all sensors for this Chassis element
568 std::string sensorPath = *chassisPath + "/all_sensors";
George Liu6c3e9452023-03-03 13:55:29 +0800569 dbus::utility::getAssociationEndPoints(
570 sensorPath,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700571 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800572 callback{std::forward<const Callback>(callback)}](
Ed Tanous8b242752023-06-27 17:17:13 -0700573 const boost::system::error_code& ec2,
George Liu6c3e9452023-03-03 13:55:29 +0800574 const dbus::utility::MapperEndPoints& nodeSensorList) {
Ed Tanous8b242752023-06-27 17:17:13 -0700575 if (ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -0700576 {
Ed Tanous8b242752023-06-27 17:17:13 -0700577 if (ec2.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700578 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700579 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700581 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000583 const std::shared_ptr<std::set<std::string>> culledSensorList =
584 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700585 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
586 &nodeSensorList, culledSensorList);
Ed Tanous62598e32023-07-17 17:06:25 -0700587 BMCWEB_LOG_DEBUG("Finishing with {}", culledSensorList->size());
Ed Tanous002d39b2022-05-31 08:59:27 -0700588 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700589 });
George Liu7a1dbc42022-12-07 16:03:22 +0800590 });
Ed Tanous62598e32023-07-17 17:06:25 -0700591 BMCWEB_LOG_DEBUG("getChassis exit");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100592}
593
594/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500595 * @brief Returns the Redfish State value for the specified inventory item.
596 * @param inventoryItem D-Bus inventory item associated with a sensor.
597 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700598 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000599inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500600{
601 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
602 {
603 return "Absent";
604 }
James Feist34dd1792019-05-17 14:10:54 -0700605
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500606 return "Enabled";
607}
608
609/**
610 * @brief Returns the Redfish Health value for the specified sensor.
611 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700612 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500613 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
614 * be nullptr if no associated inventory item was found.
615 * @return Health value for sensor.
616 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700617inline std::string getHealth(nlohmann::json& sensorJson,
618 const dbus::utility::DBusPropertiesMap& valuesDict,
619 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700620{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500621 // Get current health value (if any) in the sensor JSON object. Some JSON
622 // objects contain multiple sensors (such as PowerSupplies). We want to set
623 // the overall health to be the most severe of any of the sensors.
624 std::string currentHealth;
625 auto statusIt = sensorJson.find("Status");
626 if (statusIt != sensorJson.end())
627 {
628 auto healthIt = statusIt->find("Health");
629 if (healthIt != statusIt->end())
630 {
631 std::string* health = healthIt->get_ptr<std::string*>();
632 if (health != nullptr)
633 {
634 currentHealth = *health;
635 }
636 }
637 }
638
639 // If current health in JSON object is already Critical, return that. This
640 // should override the sensor health, which might be less severe.
641 if (currentHealth == "Critical")
642 {
643 return "Critical";
644 }
645
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200646 const bool* criticalAlarmHigh = nullptr;
647 const bool* criticalAlarmLow = nullptr;
648 const bool* warningAlarmHigh = nullptr;
649 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800650
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200651 const bool success = sdbusplus::unpackPropertiesNoThrow(
652 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
653 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
654 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
655 warningAlarmLow);
656
657 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700658 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200659 // Check if sensor has critical threshold alarm
660 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
661 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700662 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200663 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700664 }
665 }
666
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500667 // Check if associated inventory item is not functional
668 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
669 {
670 return "Critical";
671 }
672
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200673 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500674 // should override the sensor status, which might be less severe.
675 if (currentHealth == "Warning")
676 {
677 return "Warning";
678 }
679
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200680 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700681 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200682 // Check if sensor has warning threshold alarm
683 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
684 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700685 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200686 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700687 }
688 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500689
James Feist34dd1792019-05-17 14:10:54 -0700690 return "OK";
691}
692
Ed Tanous23a21a12020-07-25 04:45:05 +0000693inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500694 const InventoryItem* inventoryItem)
695{
696 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
697 {
698 switch (inventoryItem->ledState)
699 {
700 case LedState::OFF:
701 sensorJson["IndicatorLED"] = "Off";
702 break;
703 case LedState::ON:
704 sensorJson["IndicatorLED"] = "Lit";
705 break;
706 case LedState::BLINK:
707 sensorJson["IndicatorLED"] = "Blinking";
708 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000709 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500710 break;
711 }
712 }
713}
714
James Feist34dd1792019-05-17 14:10:54 -0700715/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100716 * @brief Builds a json sensor representation of a sensor.
717 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500718 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100719 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700720 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
721 * @param propertiesDict A dictionary of the properties to build the sensor
722 * from.
723 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500724 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
725 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100726 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700727inline void objectPropertiesToJson(
728 std::string_view sensorName, std::string_view sensorType,
729 std::string_view chassisSubNode,
730 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000731 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700732{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700733 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500734 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800735 std::string subNodeEscaped(sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700736 subNodeEscaped.erase(
737 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
738 subNodeEscaped.end());
739
740 // For sensors in SensorCollection we set Id instead of MemberId,
741 // including power sensors.
742 subNodeEscaped += '_';
743 subNodeEscaped += sensorName;
744 sensorJson["Id"] = std::move(subNodeEscaped);
745
Ed Tanous1d7c0052022-08-09 12:32:26 -0700746 std::string sensorNameEs(sensorName);
747 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
748 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500749 }
750 else if (sensorType != "power")
751 {
752 // Set MemberId and Name for non-power sensors. For PowerSupplies and
753 // PowerControl, those properties have more general values because
754 // multiple sensors can be stored in the same JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700755 std::string sensorNameEs(sensorName);
756 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
757 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500758 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700759
Ed Tanous81ce6092020-12-17 16:54:55 +0000760 sensorJson["Status"]["State"] = getState(inventoryItem);
Patrick Williams89492a12023-05-10 07:51:34 -0500761 sensorJson["Status"]["Health"] = getHealth(sensorJson, propertiesDict,
762 inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700763
764 // Parameter to set to override the type we get from dbus, and force it to
765 // int, regardless of what is available. This is used for schemas like fan,
766 // that require integers, not floats.
767 bool forceToInt = false;
768
Anthony Wilson3929aca2019-07-19 15:42:33 -0500769 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700770 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500771 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000772 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000773
Ed Tanous0ec8b832022-03-14 14:56:47 -0700774 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
775 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500776 {
Ed Tanous62598e32023-07-17 17:06:25 -0700777 BMCWEB_LOG_ERROR("Redfish cannot map reading type for {}",
778 sensorType);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500779 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000780 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500781 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000782 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500783 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000784
Ed Tanous1d7c0052022-08-09 12:32:26 -0700785 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000786 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200787 {
Ed Tanous62598e32023-07-17 17:06:25 -0700788 BMCWEB_LOG_ERROR("Redfish cannot map reading unit for {}",
789 sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000790 }
791 else
792 {
793 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200794 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500795 }
796 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700797 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500798 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000799 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800 // TODO(ed) Documentation says that path should be type fan_tach,
801 // implementation seems to implement fan
802 }
803 else if (sensorType == "fan" || sensorType == "fan_tach")
804 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500805 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000806 sensorJson["ReadingUnits"] = "RPM";
807 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
808 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700809 forceToInt = true;
810 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700811 else if (sensorType == "fan_pwm")
812 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500813 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000814 sensorJson["ReadingUnits"] = "Percent";
815 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
816 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700817 forceToInt = true;
818 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819 else if (sensorType == "voltage")
820 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500821 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000822 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700824 else if (sensorType == "power")
825 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700826 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000827 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000828 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500829 // Put multiple "sensors" into a single PowerControl, so have
830 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000831 sensorJson["MemberId"] = "0";
832 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500833 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000834 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700835 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700836 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500837 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700838 }
839 else
840 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500841 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700842 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700843 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700844 else
845 {
Ed Tanous62598e32023-07-17 17:06:25 -0700846 BMCWEB_LOG_ERROR("Redfish cannot map object type for {}", sensorName);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700847 return;
848 }
849 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500850 std::vector<
851 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
852 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 properties.reserve(7);
854
855 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600856
Ed Tanous1d7c0052022-08-09 12:32:26 -0700857 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500858 {
859 properties.emplace_back(
860 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
861 "/Thresholds/UpperCaution/Reading"_json_pointer);
862 properties.emplace_back(
863 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
864 "/Thresholds/LowerCaution/Reading"_json_pointer);
865 properties.emplace_back(
866 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
867 "/Thresholds/UpperCritical/Reading"_json_pointer);
868 properties.emplace_back(
869 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
870 "/Thresholds/LowerCritical/Reading"_json_pointer);
871 }
872 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600873 {
874 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500875 "WarningHigh",
876 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600877 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500878 "WarningLow",
879 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600880 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500881 "CriticalHigh",
882 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600883 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500884 "CriticalLow",
885 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600886 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887
Ed Tanous2474adf2018-09-05 16:31:16 -0700888 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
889
Ed Tanous1d7c0052022-08-09 12:32:26 -0700890 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500891 {
892 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500893 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500894 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500895 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800896 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
897 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500898 }
899 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700900 {
901 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500902 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500904 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500906 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907 {
908 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500909 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500911 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700912 }
913
Anthony Wilson3929aca2019-07-19 15:42:33 -0500914 for (const std::tuple<const char*, const char*,
915 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700917 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700919 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700920 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800921 continue;
922 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700923
924 // The property we want to set may be nested json, so use
925 // a json_pointer for easy indexing into the json structure.
926 const nlohmann::json::json_pointer& key = std::get<2>(p);
927
Ed Tanous1d7c0052022-08-09 12:32:26 -0700928 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700929 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800930 {
Ed Tanous62598e32023-07-17 17:06:25 -0700931 BMCWEB_LOG_ERROR("Got value interface that wasn't double");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700932 continue;
933 }
Ed Tanous283860f2022-08-29 14:08:50 -0700934 if (!std::isfinite(*doubleValue))
935 {
936 if (valueName == "Value")
937 {
938 // Readings are allowed to be NAN for unavailable; coerce
939 // them to null in the json response.
940 sensorJson[key] = nullptr;
941 continue;
942 }
Ed Tanous62598e32023-07-17 17:06:25 -0700943 BMCWEB_LOG_WARNING("Sensor value for {} was unexpectedly {}",
944 valueName, *doubleValue);
Ed Tanous283860f2022-08-29 14:08:50 -0700945 continue;
946 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700947 if (forceToInt)
948 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700949 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700950 }
951 else
952 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700953 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700954 }
955 }
956 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100957}
958
Ed Tanous1d7c0052022-08-09 12:32:26 -0700959/**
960 * @brief Builds a json sensor representation of a sensor.
961 * @param sensorName The name of the sensor to be built
962 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
963 * build
964 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
965 * @param interfacesDict A dictionary of the interfaces and properties of said
966 * interfaces to be built from
967 * @param sensorJson The json object to fill
968 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
969 * be nullptr if no associated inventory item was found.
970 */
971inline void objectInterfacesToJson(
972 const std::string& sensorName, const std::string& sensorType,
973 const std::string& chassisSubNode,
974 const dbus::utility::DBusInteracesMap& interfacesDict,
975 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
976{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700977 for (const auto& [interface, valuesDict] : interfacesDict)
978 {
979 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
980 valuesDict, sensorJson, inventoryItem);
981 }
Ed Tanous62598e32023-07-17 17:06:25 -0700982 BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700983}
984
Ed Tanousb5a76932020-09-29 16:16:58 -0700985inline void populateFanRedundancy(
986 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700987{
George Liue99073f2022-12-09 11:06:16 +0800988 constexpr std::array<std::string_view, 1> interfaces = {
989 "xyz.openbmc_project.Control.FanRedundancy"};
990 dbus::utility::getSubTree(
991 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800992 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +0800993 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800994 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700995 if (ec)
996 {
997 return; // don't have to have this interface
998 }
George Liu6c3e9452023-03-03 13:55:29 +0800999 for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
Ed Tanous002d39b2022-05-31 08:59:27 -07001000 pathPair : resp)
1001 {
1002 const std::string& path = pathPair.first;
George Liu6c3e9452023-03-03 13:55:29 +08001003 const dbus::utility::MapperServiceMap& objDict = pathPair.second;
Ed Tanous002d39b2022-05-31 08:59:27 -07001004 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001005 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001006 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001007 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001008
1009 const std::string& owner = objDict.begin()->first;
George Liu6c3e9452023-03-03 13:55:29 +08001010 dbus::utility::getAssociationEndPoints(
1011 path + "/chassis",
1012 [path, owner, sensorsAsyncResp](
Ed Tanous8b242752023-06-27 17:17:13 -07001013 const boost::system::error_code& ec2,
George Liu6c3e9452023-03-03 13:55:29 +08001014 const dbus::utility::MapperEndPoints& endpoints) {
Ed Tanous8b242752023-06-27 17:17:13 -07001015 if (ec2)
James Feist8bd25cc2019-03-15 15:14:00 -07001016 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001017 return; // if they don't have an association we
1018 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001019 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001020 auto found =
1021 std::find_if(endpoints.begin(), endpoints.end(),
1022 [sensorsAsyncResp](const std::string& entry) {
1023 return entry.find(sensorsAsyncResp->chassisId) !=
1024 std::string::npos;
1025 });
James Feist8bd25cc2019-03-15 15:14:00 -07001026
Ed Tanous002d39b2022-05-31 08:59:27 -07001027 if (found == endpoints.end())
1028 {
1029 return;
1030 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001031 sdbusplus::asio::getAllProperties(
1032 *crow::connections::systemBus, owner, path,
1033 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001034 [path, sensorsAsyncResp](
Ed Tanous8b242752023-06-27 17:17:13 -07001035 const boost::system::error_code& ec3,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001036 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous8b242752023-06-27 17:17:13 -07001037 if (ec3)
Ed Tanous002d39b2022-05-31 08:59:27 -07001038 {
1039 return; // don't have to have this
1040 // interface
1041 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001042
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001043 const uint8_t* allowedFailures = nullptr;
1044 const std::vector<std::string>* collection = nullptr;
1045 const std::string* status = nullptr;
1046
1047 const bool success = sdbusplus::unpackPropertiesNoThrow(
1048 dbus_utils::UnpackErrorPrinter(), ret,
1049 "AllowedFailures", allowedFailures, "Collection",
1050 collection, "Status", status);
1051
1052 if (!success)
1053 {
1054 messages::internalError(
1055 sensorsAsyncResp->asyncResp->res);
1056 return;
1057 }
1058
1059 if (allowedFailures == nullptr || collection == nullptr ||
1060 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001061 {
Ed Tanous62598e32023-07-17 17:06:25 -07001062 BMCWEB_LOG_ERROR("Invalid redundancy interface");
Ed Tanous002d39b2022-05-31 08:59:27 -07001063 messages::internalError(
1064 sensorsAsyncResp->asyncResp->res);
1065 return;
1066 }
1067
Ed Tanous002d39b2022-05-31 08:59:27 -07001068 sdbusplus::message::object_path objectPath(path);
1069 std::string name = objectPath.filename();
1070 if (name.empty())
1071 {
1072 // this should be impossible
1073 messages::internalError(
1074 sensorsAsyncResp->asyncResp->res);
1075 return;
1076 }
1077 std::replace(name.begin(), name.end(), '_', ' ');
1078
1079 std::string health;
1080
Ed Tanous11ba3972022-07-11 09:50:41 -07001081 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001082 {
1083 health = "OK";
1084 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001085 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001086 {
1087 health = "Warning";
1088 }
1089 else
1090 {
1091 health = "Critical";
1092 }
1093 nlohmann::json::array_t redfishCollection;
1094 const auto& fanRedfish =
1095 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1096 for (const std::string& item : *collection)
1097 {
Ed Tanous8a592812022-06-04 09:06:59 -07001098 sdbusplus::message::object_path itemPath(item);
1099 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001100 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001101 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001102 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001103 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001104 /*
1105 todo(ed): merge patch that fixes the names
1106 std::replace(itemName.begin(),
1107 itemName.end(), '_', ' ');*/
1108 auto schemaItem =
1109 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1110 [itemName](const nlohmann::json& fan) {
George Liu3e35c762023-03-08 16:56:38 +08001111 return fan["Name"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001112 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001114 {
Ed Tanous8a592812022-06-04 09:06:59 -07001115 nlohmann::json::object_t collectionId;
1116 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001117 (*schemaItem)["@odata.id"];
1118 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001119 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 }
1121 else
1122 {
Ed Tanous62598e32023-07-17 17:06:25 -07001123 BMCWEB_LOG_ERROR("failed to find fan in schema");
Ed Tanous002d39b2022-05-31 08:59:27 -07001124 messages::internalError(
1125 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001126 return;
1127 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 }
James Feist8bd25cc2019-03-15 15:14:00 -07001129
Patrick Williams89492a12023-05-10 07:51:34 -05001130 size_t minNumNeeded = collection->empty()
1131 ? 0
1132 : collection->size() -
1133 *allowedFailures;
Ed Tanous002d39b2022-05-31 08:59:27 -07001134 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1135 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001136
Ed Tanous002d39b2022-05-31 08:59:27 -07001137 nlohmann::json::object_t redundancy;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001138 boost::urls::url url =
1139 boost::urls::format("/redfish/v1/Chassis/{}/{}",
1140 sensorsAsyncResp->chassisId,
1141 sensorsAsyncResp->chassisSubNode);
Willy Tueddfc432022-09-26 16:46:38 +00001142 url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1143 .to_string());
1144 redundancy["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -07001145 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1146 redundancy["MinNumNeeded"] = minNumNeeded;
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 redundancy["Mode"] = "N+m";
1148 redundancy["Name"] = name;
1149 redundancy["RedundancySet"] = redfishCollection;
1150 redundancy["Status"]["Health"] = health;
1151 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001152
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001153 jResp.emplace_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001154 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001155 });
1156 }
George Liue99073f2022-12-09 11:06:16 +08001157 });
James Feist8bd25cc2019-03-15 15:14:00 -07001158}
1159
Ed Tanousb5a76932020-09-29 16:16:58 -07001160inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001161 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001162{
zhanghch058d1b46d2021-04-01 11:18:24 +08001163 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001164 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001165 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001166 {
1167 sensorHeaders = {"Voltages", "PowerSupplies"};
1168 }
1169 for (const std::string& sensorGroup : sensorHeaders)
1170 {
1171 nlohmann::json::iterator entry = response.find(sensorGroup);
1172 if (entry != response.end())
1173 {
1174 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001175 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001176 return c1["Name"] < c2["Name"];
1177 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001178
1179 // add the index counts to the end of each entry
1180 size_t count = 0;
1181 for (nlohmann::json& sensorJson : *entry)
1182 {
1183 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1184 if (odata == sensorJson.end())
1185 {
1186 continue;
1187 }
1188 std::string* value = odata->get_ptr<std::string*>();
1189 if (value != nullptr)
1190 {
Willy Tueddfc432022-09-26 16:46:38 +00001191 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001192 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001193 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001194 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001195 }
1196 }
1197 }
1198 }
1199}
1200
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001201/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001202 * @brief Finds the inventory item with the specified object path.
1203 * @param inventoryItems D-Bus inventory items associated with sensors.
1204 * @param invItemObjPath D-Bus object path of inventory item.
1205 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001206 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001207inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001208 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001209 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001210{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001211 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001212 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001213 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001214 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001215 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001216 }
1217 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001218 return nullptr;
1219}
1220
1221/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001222 * @brief Finds the inventory item associated with the specified sensor.
1223 * @param inventoryItems D-Bus inventory items associated with sensors.
1224 * @param sensorObjPath D-Bus object path of sensor.
1225 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001226 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001227inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001228 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001229 const std::string& sensorObjPath)
1230{
1231 for (InventoryItem& inventoryItem : *inventoryItems)
1232 {
Ed Tanousdb0d36e2023-06-30 10:37:05 -07001233 if (inventoryItem.sensors.contains(sensorObjPath))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001234 {
1235 return &inventoryItem;
1236 }
1237 }
1238 return nullptr;
1239}
1240
1241/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001242 * @brief Finds the inventory item associated with the specified led path.
1243 * @param inventoryItems D-Bus inventory items associated with sensors.
1244 * @param ledObjPath D-Bus object path of led.
1245 * @return Inventory item within vector, or nullptr if no match found.
1246 */
1247inline InventoryItem*
1248 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1249 const std::string& ledObjPath)
1250{
1251 for (InventoryItem& inventoryItem : inventoryItems)
1252 {
1253 if (inventoryItem.ledObjectPath == ledObjPath)
1254 {
1255 return &inventoryItem;
1256 }
1257 }
1258 return nullptr;
1259}
1260
1261/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001262 * @brief Adds inventory item and associated sensor to specified vector.
1263 *
1264 * Adds a new InventoryItem to the vector if necessary. Searches for an
1265 * existing InventoryItem with the specified object path. If not found, one is
1266 * added to the vector.
1267 *
1268 * Next, the specified sensor is added to the set of sensors associated with the
1269 * InventoryItem.
1270 *
1271 * @param inventoryItems D-Bus inventory items associated with sensors.
1272 * @param invItemObjPath D-Bus object path of inventory item.
1273 * @param sensorObjPath D-Bus object path of sensor
1274 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001275inline void addInventoryItem(
1276 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1277 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001278{
1279 // Look for inventory item in vector
Patrick Williams89492a12023-05-10 07:51:34 -05001280 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1281 invItemObjPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001282
1283 // If inventory item doesn't exist in vector, add it
1284 if (inventoryItem == nullptr)
1285 {
1286 inventoryItems->emplace_back(invItemObjPath);
1287 inventoryItem = &(inventoryItems->back());
1288 }
1289
1290 // Add sensor to set of sensors associated with inventory item
1291 inventoryItem->sensors.emplace(sensorObjPath);
1292}
1293
1294/**
1295 * @brief Stores D-Bus data in the specified inventory item.
1296 *
1297 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1298 * specified InventoryItem.
1299 *
1300 * This data is later used to provide sensor property values in the JSON
1301 * response.
1302 *
1303 * @param inventoryItem Inventory item where data will be stored.
1304 * @param interfacesDict Map containing D-Bus interfaces and their properties
1305 * for the specified inventory item.
1306 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001307inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001308 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001309 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001310{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001311 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001312
Ed Tanous9eb808c2022-01-25 10:19:23 -08001313 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001314 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001315 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001316 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001317 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001318 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001319 if (name == "Present")
1320 {
1321 const bool* value = std::get_if<bool>(&dbusValue);
1322 if (value != nullptr)
1323 {
1324 inventoryItem.isPresent = *value;
1325 }
1326 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001327 }
1328 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001329 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001330
Ed Tanous711ac7a2021-12-20 09:34:41 -08001331 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001332 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001333 inventoryItem.isPowerSupply = true;
1334 }
1335
1336 // Get properties from Inventory.Decorator.Asset interface
1337 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1338 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001339 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001340 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001341 if (name == "Manufacturer")
1342 {
1343 const std::string* value =
1344 std::get_if<std::string>(&dbusValue);
1345 if (value != nullptr)
1346 {
1347 inventoryItem.manufacturer = *value;
1348 }
1349 }
1350 if (name == "Model")
1351 {
1352 const std::string* value =
1353 std::get_if<std::string>(&dbusValue);
1354 if (value != nullptr)
1355 {
1356 inventoryItem.model = *value;
1357 }
1358 }
1359 if (name == "SerialNumber")
1360 {
1361 const std::string* value =
1362 std::get_if<std::string>(&dbusValue);
1363 if (value != nullptr)
1364 {
1365 inventoryItem.serialNumber = *value;
1366 }
1367 }
1368 if (name == "PartNumber")
1369 {
1370 const std::string* value =
1371 std::get_if<std::string>(&dbusValue);
1372 if (value != nullptr)
1373 {
1374 inventoryItem.partNumber = *value;
1375 }
1376 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001377 }
1378 }
1379
Ed Tanous711ac7a2021-12-20 09:34:41 -08001380 if (interface ==
1381 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001382 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001383 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001384 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001385 if (name == "Functional")
1386 {
1387 const bool* value = std::get_if<bool>(&dbusValue);
1388 if (value != nullptr)
1389 {
1390 inventoryItem.isFunctional = *value;
1391 }
1392 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001393 }
1394 }
1395 }
1396}
1397
1398/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001399 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001400 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001401 * Uses the specified connections (services) to obtain D-Bus data for inventory
1402 * items associated with sensors. Stores the resulting data in the
1403 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001404 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001405 * This data is later used to provide sensor property values in the JSON
1406 * response.
1407 *
1408 * Finds the inventory item data asynchronously. Invokes callback when data has
1409 * been obtained.
1410 *
1411 * The callback must have the following signature:
1412 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001413 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001414 * @endcode
1415 *
1416 * This function is called recursively, obtaining data asynchronously from one
1417 * connection in each call. This ensures the callback is not invoked until the
1418 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001419 *
1420 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421 * @param inventoryItems D-Bus inventory items associated with sensors.
1422 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001423 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001424 * @param callback Callback to invoke when inventory data has been obtained.
1425 * @param invConnectionsIndex Current index in invConnections. Only specified
1426 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001427 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001428template <typename Callback>
1429static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001430 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001431 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001432 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1433 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001434{
Ed Tanous62598e32023-07-17 17:06:25 -07001435 BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001436
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001437 // If no more connections left, call callback
1438 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001439 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001440 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001441 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001442 return;
1443 }
1444
1445 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001446 auto it = invConnections->begin();
1447 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001448 if (it != invConnections->end())
1449 {
1450 const std::string& invConnection = *it;
1451
George Liu5eb468d2023-06-20 17:03:24 +08001452 // Get all object paths and their interfaces for current connection
1453 sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
1454 dbus::utility::getManagedObjects(
1455 invConnection, path,
1456 [sensorsAsyncResp, inventoryItems, invConnections,
1457 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1458 const boost::system::error_code& ec,
1459 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07001460 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001461 if (ec)
1462 {
Ed Tanous62598e32023-07-17 17:06:25 -07001463 BMCWEB_LOG_ERROR(
1464 "getInventoryItemsData respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001465 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001466 return;
1467 }
1468
1469 // Loop through returned object paths
1470 for (const auto& objDictEntry : resp)
1471 {
1472 const std::string& objPath =
1473 static_cast<const std::string&>(objDictEntry.first);
1474
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001475 // If this object path is one of the specified inventory items
Patrick Williams89492a12023-05-10 07:51:34 -05001476 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1477 objPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001478 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001479 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001480 // Store inventory data in InventoryItem
1481 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001482 }
1483 }
1484
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001485 // Recurse to get inventory item data from next connection
1486 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001487 invConnections, std::move(callback),
1488 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001489
Ed Tanous62598e32023-07-17 17:06:25 -07001490 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
George Liu5eb468d2023-06-20 17:03:24 +08001491 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001492 }
1493
Ed Tanous62598e32023-07-17 17:06:25 -07001494 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001495}
1496
1497/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001498 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001499 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001500 * Gets the D-Bus connections (services) that provide data for the inventory
1501 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001502 *
1503 * Finds the connections asynchronously. Invokes callback when information has
1504 * been obtained.
1505 *
1506 * The callback must have the following signature:
1507 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001508 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001509 * @endcode
1510 *
1511 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001512 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001513 * @param callback Callback to invoke when connections have been obtained.
1514 */
1515template <typename Callback>
1516static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001517 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1518 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001519 Callback&& callback)
1520{
Ed Tanous62598e32023-07-17 17:06:25 -07001521 BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001522
1523 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001524 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001525 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001526 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1527 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001528 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1529
George Liue99073f2022-12-09 11:06:16 +08001530 // Make call to ObjectMapper to find all inventory items
1531 dbus::utility::getSubTree(
1532 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001533 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1534 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001535 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001536 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001537 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07001538 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001539 if (ec)
1540 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001541 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07001542 BMCWEB_LOG_ERROR(
1543 "getInventoryItemsConnections respHandler DBus error {}", ec);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001544 return;
1545 }
1546
1547 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001548 std::shared_ptr<std::set<std::string>> invConnections =
1549 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001550
1551 // Loop through objects from GetSubTree
1552 for (const std::pair<
1553 std::string,
1554 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1555 object : subtree)
1556 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001557 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001558 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001559 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001561 // Store all connections to inventory item
1562 for (const std::pair<std::string, std::vector<std::string>>&
1563 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001564 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001565 const std::string& invConnection = objData.first;
1566 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001567 }
1568 }
1569 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001570
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001571 callback(invConnections);
Ed Tanous62598e32023-07-17 17:06:25 -07001572 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
George Liue99073f2022-12-09 11:06:16 +08001573 });
Ed Tanous62598e32023-07-17 17:06:25 -07001574 BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001575}
1576
1577/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001578 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001579 *
1580 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001581 * inventory items. Then finds the associations from those inventory items to
1582 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001583 *
1584 * Finds the inventory items asynchronously. Invokes callback when information
1585 * has been obtained.
1586 *
1587 * The callback must have the following signature:
1588 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001589 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 * @endcode
1591 *
1592 * @param sensorsAsyncResp Pointer to object holding response data.
1593 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001594 * implements ObjectManager.
1595 * @param callback Callback to invoke when inventory items have been obtained.
1596 */
1597template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001598static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001599 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001600 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001601 Callback&& callback)
1602{
Ed Tanous62598e32023-07-17 17:06:25 -07001603 BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001604
George Liu5eb468d2023-06-20 17:03:24 +08001605 // Call GetManagedObjects on the ObjectMapper to get all associations
1606 sdbusplus::message::object_path path("/");
1607 dbus::utility::getManagedObjects(
1608 "xyz.openbmc_project.ObjectMapper", path,
Ed Tanous02cad962022-06-30 16:50:15 -07001609 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001610 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001611 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07001612 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 if (ec)
1614 {
Ed Tanous62598e32023-07-17 17:06:25 -07001615 BMCWEB_LOG_ERROR(
1616 "getInventoryItemAssociations respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001617 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618 return;
1619 }
1620
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001621 // Create vector to hold list of inventory items
1622 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1623 std::make_shared<std::vector<InventoryItem>>();
1624
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626 std::string sensorAssocPath;
1627 sensorAssocPath.reserve(128); // avoid memory allocations
1628 for (const auto& objDictEntry : resp)
1629 {
1630 const std::string& objPath =
1631 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001632
1633 // If path is inventory association for one of the specified sensors
1634 for (const std::string& sensorName : *sensorNames)
1635 {
1636 sensorAssocPath = sensorName;
1637 sensorAssocPath += "/inventory";
1638 if (objPath == sensorAssocPath)
1639 {
1640 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001641 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001643 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001644 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001645 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001646 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001647 if (valueName == "endpoints")
1648 {
1649 const std::vector<std::string>* endpoints =
1650 std::get_if<std::vector<std::string>>(
1651 &value);
1652 if ((endpoints != nullptr) &&
1653 !endpoints->empty())
1654 {
1655 // Add inventory item to vector
1656 const std::string& invItemPath =
1657 endpoints->front();
1658 addInventoryItem(inventoryItems,
1659 invItemPath,
1660 sensorName);
1661 }
1662 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001663 }
1664 }
1665 }
1666 break;
1667 }
1668 }
1669 }
1670
Anthony Wilsond5005492019-07-31 16:34:17 -05001671 // Now loop through the returned object paths again, this time to
1672 // find the leds associated with the inventory items we just found
1673 std::string inventoryAssocPath;
1674 inventoryAssocPath.reserve(128); // avoid memory allocations
1675 for (const auto& objDictEntry : resp)
1676 {
1677 const std::string& objPath =
1678 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001679
1680 for (InventoryItem& inventoryItem : *inventoryItems)
1681 {
1682 inventoryAssocPath = inventoryItem.objectPath;
1683 inventoryAssocPath += "/leds";
1684 if (objPath == inventoryAssocPath)
1685 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001686 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001687 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001688 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001689 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001690 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001691 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001692 if (valueName == "endpoints")
1693 {
1694 const std::vector<std::string>* endpoints =
1695 std::get_if<std::vector<std::string>>(
1696 &value);
1697 if ((endpoints != nullptr) &&
1698 !endpoints->empty())
1699 {
1700 // Add inventory item to vector
1701 // Store LED path in inventory item
1702 const std::string& ledPath =
1703 endpoints->front();
1704 inventoryItem.ledObjectPath = ledPath;
1705 }
1706 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001707 }
1708 }
1709 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001710
Anthony Wilsond5005492019-07-31 16:34:17 -05001711 break;
1712 }
1713 }
1714 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001715 callback(inventoryItems);
Ed Tanous62598e32023-07-17 17:06:25 -07001716 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
George Liu5eb468d2023-06-20 17:03:24 +08001717 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001718
Ed Tanous62598e32023-07-17 17:06:25 -07001719 BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001720}
1721
1722/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001723 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1724 *
1725 * Uses the specified connections (services) to obtain D-Bus data for inventory
1726 * item leds associated with sensors. Stores the resulting data in the
1727 * inventoryItems vector.
1728 *
1729 * This data is later used to provide sensor property values in the JSON
1730 * response.
1731 *
1732 * Finds the inventory item led data asynchronously. Invokes callback when data
1733 * has been obtained.
1734 *
1735 * The callback must have the following signature:
1736 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001737 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001738 * @endcode
1739 *
1740 * This function is called recursively, obtaining data asynchronously from one
1741 * connection in each call. This ensures the callback is not invoked until the
1742 * last asynchronous function has completed.
1743 *
1744 * @param sensorsAsyncResp Pointer to object holding response data.
1745 * @param inventoryItems D-Bus inventory items associated with sensors.
1746 * @param ledConnections Connections that provide data for the inventory leds.
1747 * @param callback Callback to invoke when inventory data has been obtained.
1748 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1749 * in recursive calls to this function.
1750 */
1751template <typename Callback>
1752void getInventoryLedData(
1753 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1754 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001755 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001756 Callback&& callback, size_t ledConnectionsIndex = 0)
1757{
Ed Tanous62598e32023-07-17 17:06:25 -07001758 BMCWEB_LOG_DEBUG("getInventoryLedData enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001759
1760 // If no more connections left, call callback
1761 if (ledConnectionsIndex >= ledConnections->size())
1762 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001763 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001764 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001765 return;
1766 }
1767
1768 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001769 auto it = ledConnections->begin();
1770 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001771 if (it != ledConnections->end())
1772 {
1773 const std::string& ledPath = (*it).first;
1774 const std::string& ledConnection = (*it).second;
1775 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001776 auto respHandler =
1777 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001778 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001779 const boost::system::error_code& ec, const std::string& state) {
Ed Tanous62598e32023-07-17 17:06:25 -07001780 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07001781 if (ec)
1782 {
Ed Tanous62598e32023-07-17 17:06:25 -07001783 BMCWEB_LOG_ERROR(
1784 "getInventoryLedData respHandler DBus error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 messages::internalError(sensorsAsyncResp->asyncResp->res);
1786 return;
1787 }
1788
Ed Tanous62598e32023-07-17 17:06:25 -07001789 BMCWEB_LOG_DEBUG("Led state: {}", state);
Ed Tanous002d39b2022-05-31 08:59:27 -07001790 // Find inventory item with this LED object path
1791 InventoryItem* inventoryItem =
1792 findInventoryItemForLed(*inventoryItems, ledPath);
1793 if (inventoryItem != nullptr)
1794 {
1795 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001796 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001797 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001798 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001799 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001800 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001801 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001803 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001804 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 {
1806 inventoryItem->ledState = LedState::OFF;
1807 }
1808 else
1809 {
1810 inventoryItem->ledState = LedState::UNKNOWN;
1811 }
1812 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001813
Ed Tanous002d39b2022-05-31 08:59:27 -07001814 // Recurse to get LED data from next connection
1815 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1816 ledConnections, std::move(callback),
1817 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001818
Ed Tanous62598e32023-07-17 17:06:25 -07001819 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07001820 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001821
1822 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001823 sdbusplus::asio::getProperty<std::string>(
1824 *crow::connections::systemBus, ledConnection, ledPath,
1825 "xyz.openbmc_project.Led.Physical", "State",
1826 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001827 }
1828
Ed Tanous62598e32023-07-17 17:06:25 -07001829 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001830}
1831
1832/**
1833 * @brief Gets LED data for LEDs associated with given inventory items.
1834 *
1835 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1836 * associated with the specified inventory items. Then gets the LED data from
1837 * each connection and stores it in the inventory item.
1838 *
1839 * This data is later used to provide sensor property values in the JSON
1840 * response.
1841 *
1842 * Finds the LED data asynchronously. Invokes callback when information has
1843 * been obtained.
1844 *
1845 * The callback must have the following signature:
1846 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001847 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001848 * @endcode
1849 *
1850 * @param sensorsAsyncResp Pointer to object holding response data.
1851 * @param inventoryItems D-Bus inventory items associated with sensors.
1852 * @param callback Callback to invoke when inventory items have been obtained.
1853 */
1854template <typename Callback>
1855void getInventoryLeds(
1856 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1857 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1858 Callback&& callback)
1859{
Ed Tanous62598e32023-07-17 17:06:25 -07001860 BMCWEB_LOG_DEBUG("getInventoryLeds enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001861
1862 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001863 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001864 "xyz.openbmc_project.Led.Physical"};
1865
George Liue99073f2022-12-09 11:06:16 +08001866 // Make call to ObjectMapper to find all inventory items
1867 dbus::utility::getSubTree(
1868 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001869 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1870 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001871 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001872 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001873 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07001874 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001875 if (ec)
1876 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001877 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07001878 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}", ec);
Anthony Wilsond5005492019-07-31 16:34:17 -05001879 return;
1880 }
1881
1882 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001883 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1884 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001885
1886 // Loop through objects from GetSubTree
1887 for (const std::pair<
1888 std::string,
1889 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1890 object : subtree)
1891 {
1892 // Check if object path is LED for one of the specified inventory
1893 // items
1894 const std::string& ledPath = object.first;
1895 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1896 {
1897 // Add mapping from ledPath to connection
1898 const std::string& connection = object.second.begin()->first;
1899 (*ledConnections)[ledPath] = connection;
Ed Tanous62598e32023-07-17 17:06:25 -07001900 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath, connection);
Anthony Wilsond5005492019-07-31 16:34:17 -05001901 }
1902 }
1903
1904 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1905 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07001906 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
George Liue99073f2022-12-09 11:06:16 +08001907 });
Ed Tanous62598e32023-07-17 17:06:25 -07001908 BMCWEB_LOG_DEBUG("getInventoryLeds exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001909}
1910
1911/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001912 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1913 *
1914 * Uses the specified connections (services) (currently assumes just one) to
1915 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1916 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1917 *
1918 * This data is later used to provide sensor property values in the JSON
1919 * response.
1920 *
1921 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1922 * when data has been obtained.
1923 *
1924 * The callback must have the following signature:
1925 * @code
1926 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1927 * @endcode
1928 *
1929 * @param sensorsAsyncResp Pointer to object holding response data.
1930 * @param inventoryItems D-Bus inventory items associated with sensors.
1931 * @param psAttributesConnections Connections that provide data for the Power
1932 * Supply Attributes
1933 * @param callback Callback to invoke when data has been obtained.
1934 */
1935template <typename Callback>
1936void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001937 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001938 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001939 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001940 Callback&& callback)
1941{
Ed Tanous62598e32023-07-17 17:06:25 -07001942 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001943
1944 if (psAttributesConnections.empty())
1945 {
Ed Tanous62598e32023-07-17 17:06:25 -07001946 BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001947 callback(inventoryItems);
1948 return;
1949 }
1950
1951 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001952 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001953
1954 const std::string& psAttributesPath = (*it).first;
1955 const std::string& psAttributesConnection = (*it).second;
1956
1957 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001958 auto respHandler =
1959 [sensorsAsyncResp, inventoryItems,
1960 callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001961 const boost::system::error_code& ec, const uint32_t value) {
Ed Tanous62598e32023-07-17 17:06:25 -07001962 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001963 if (ec)
1964 {
Ed Tanous62598e32023-07-17 17:06:25 -07001965 BMCWEB_LOG_ERROR(
1966 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001967 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001968 return;
1969 }
1970
Ed Tanous62598e32023-07-17 17:06:25 -07001971 BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001972 // Store value in Power Supply Inventory Items
1973 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001974 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001975 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001976 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001977 inventoryItem.powerSupplyEfficiencyPercent =
1978 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001979 }
1980 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001981
Ed Tanous62598e32023-07-17 17:06:25 -07001982 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001983 callback(inventoryItems);
1984 };
1985
1986 // Get the DeratingFactor property for the PowerSupplyAttributes
1987 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001988 sdbusplus::asio::getProperty<uint32_t>(
1989 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1990 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
1991 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05001992
Ed Tanous62598e32023-07-17 17:06:25 -07001993 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001994}
1995
1996/**
1997 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
1998 *
1999 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2000 * data. Then gets the Power Supply Attributes data from the connection
2001 * (currently just assumes 1 connection) and stores the data in the inventory
2002 * item.
2003 *
2004 * This data is later used to provide sensor property values in the JSON
2005 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2006 *
2007 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2008 * when information has been obtained.
2009 *
2010 * The callback must have the following signature:
2011 * @code
2012 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2013 * @endcode
2014 *
2015 * @param sensorsAsyncResp Pointer to object holding response data.
2016 * @param inventoryItems D-Bus inventory items associated with sensors.
2017 * @param callback Callback to invoke when data has been obtained.
2018 */
2019template <typename Callback>
2020void getPowerSupplyAttributes(
2021 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2022 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2023 Callback&& callback)
2024{
Ed Tanous62598e32023-07-17 17:06:25 -07002025 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002026
2027 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002028 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002029 {
Ed Tanous62598e32023-07-17 17:06:25 -07002030 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002031 callback(inventoryItems);
2032 return;
2033 }
2034
George Liue99073f2022-12-09 11:06:16 +08002035 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002036 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2037
George Liue99073f2022-12-09 11:06:16 +08002038 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2039 dbus::utility::getSubTree(
2040 "/xyz/openbmc_project", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002041 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2042 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002043 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002044 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002045 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07002046 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002047 if (ec)
2048 {
2049 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07002050 BMCWEB_LOG_ERROR(
2051 "getPowerSupplyAttributes respHandler DBus error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002052 return;
2053 }
2054 if (subtree.empty())
2055 {
Ed Tanous62598e32023-07-17 17:06:25 -07002056 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002057 callback(inventoryItems);
2058 return;
2059 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002060
Ed Tanous002d39b2022-05-31 08:59:27 -07002061 // Currently we only support 1 power supply attribute, use this for
2062 // all the power supplies. Build map of object path to connection.
2063 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002064 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002065
Ed Tanous002d39b2022-05-31 08:59:27 -07002066 if (subtree[0].first.empty() || subtree[0].second.empty())
2067 {
Ed Tanous62598e32023-07-17 17:06:25 -07002068 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002069 callback(inventoryItems);
2070 return;
2071 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002072
Ed Tanous002d39b2022-05-31 08:59:27 -07002073 const std::string& psAttributesPath = subtree[0].first;
2074 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002075
Ed Tanous002d39b2022-05-31 08:59:27 -07002076 if (connection.empty())
2077 {
Ed Tanous62598e32023-07-17 17:06:25 -07002078 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002079 callback(inventoryItems);
2080 return;
2081 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002082
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 psAttributesConnections[psAttributesPath] = connection;
Ed Tanous62598e32023-07-17 17:06:25 -07002084 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
2085 connection);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002086
Ed Tanous002d39b2022-05-31 08:59:27 -07002087 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2088 psAttributesConnections,
2089 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07002090 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
George Liue99073f2022-12-09 11:06:16 +08002091 });
Ed Tanous62598e32023-07-17 17:06:25 -07002092 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002093}
2094
2095/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002096 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002097 *
2098 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002099 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002100 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002101 * This data is later used to provide sensor property values in the JSON
2102 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002103 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002104 * Finds the inventory items asynchronously. Invokes callback when the
2105 * inventory items have been obtained.
2106 *
2107 * The callback must have the following signature:
2108 * @code
2109 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2110 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002111 *
2112 * @param sensorsAsyncResp Pointer to object holding response data.
2113 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002114 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002115 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002116 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002117template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002118static void
2119 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2120 const std::shared_ptr<std::set<std::string>> sensorNames,
2121 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002122{
Ed Tanous62598e32023-07-17 17:06:25 -07002123 BMCWEB_LOG_DEBUG("getInventoryItems enter");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002124 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002125 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002126 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous62598e32023-07-17 17:06:25 -07002127 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002128 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002129 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002130 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002131 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous62598e32023-07-17 17:06:25 -07002132 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002133 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2134 callback{std::move(callback)}]() {
Ed Tanous62598e32023-07-17 17:06:25 -07002135 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002136
Ed Tanous002d39b2022-05-31 08:59:27 -07002137 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2138 callback{std::move(callback)}]() {
Ed Tanous62598e32023-07-17 17:06:25 -07002139 BMCWEB_LOG_DEBUG("getInventoryLedsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002140 // Find Power Supply Attributes and get the data
2141 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2142 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07002143 BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002144 };
2145
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 // Find led connections and get the data
2147 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2148 std::move(getInventoryLedsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002149 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002150 };
2151
2152 // Get inventory item data from connections
2153 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002154 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002155 std::move(getInventoryItemsDataCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002156 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002157 };
2158
Ed Tanous002d39b2022-05-31 08:59:27 -07002159 // Get connections that provide inventory item data
2160 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2161 std::move(getInventoryItemsConnectionsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002162 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002163 };
2164
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002165 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002166 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002167 std::move(getInventoryItemAssociationsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002168 BMCWEB_LOG_DEBUG("getInventoryItems exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002169}
2170
2171/**
2172 * @brief Returns JSON PowerSupply object for the specified inventory item.
2173 *
2174 * Searches for a JSON PowerSupply object that matches the specified inventory
2175 * item. If one is not found, a new PowerSupply object is added to the JSON
2176 * array.
2177 *
2178 * Multiple sensors are often associated with one power supply inventory item.
2179 * As a result, multiple sensor values are stored in one JSON PowerSupply
2180 * object.
2181 *
2182 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2183 * @param inventoryItem Inventory item for the power supply.
2184 * @param chassisId Chassis that contains the power supply.
2185 * @return JSON PowerSupply object for the specified inventory item.
2186 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002187inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002188 const InventoryItem& inventoryItem,
2189 const std::string& chassisId)
2190{
2191 // Check if matching PowerSupply object already exists in JSON array
2192 for (nlohmann::json& powerSupply : powerSupplyArray)
2193 {
Alexander Hansenf7afb172023-04-27 14:34:08 +02002194 if (powerSupply["Name"] ==
2195 boost::replace_all_copy(inventoryItem.name, "_", " "))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002196 {
2197 return powerSupply;
2198 }
2199 }
2200
2201 // Add new PowerSupply object to JSON array
2202 powerSupplyArray.push_back({});
2203 nlohmann::json& powerSupply = powerSupplyArray.back();
Ed Tanousef4c65b2023-04-24 15:28:50 -07002204 boost::urls::url url = boost::urls::format("/redfish/v1/Chassis/{}/Power",
2205 chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002206 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2207 powerSupply["@odata.id"] = std::move(url);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002208 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2209 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2210 powerSupply["Model"] = inventoryItem.model;
2211 powerSupply["PartNumber"] = inventoryItem.partNumber;
2212 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002213 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002214
Gunnar Mills42cbe532019-08-15 15:26:54 -05002215 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2216 {
2217 powerSupply["EfficiencyPercent"] =
2218 inventoryItem.powerSupplyEfficiencyPercent;
2219 }
2220
2221 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002222 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2223 powerSupply["Status"]["Health"] = health;
2224
2225 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002226}
2227
2228/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002229 * @brief Gets the values of the specified sensors.
2230 *
2231 * Stores the results as JSON in the SensorsAsyncResp.
2232 *
2233 * Gets the sensor values asynchronously. Stores the results later when the
2234 * information has been obtained.
2235 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002236 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002237 *
2238 * To minimize the number of DBus calls, the DBus method
2239 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2240 * values of all sensors provided by a connection (service).
2241 *
2242 * The connections set contains all the connections that provide sensor values.
2243 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002244 * The InventoryItem vector contains D-Bus inventory items associated with the
2245 * sensors. Inventory item data is needed for some Redfish sensor properties.
2246 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002247 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002248 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002249 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002250 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002251 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002252 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002253inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002254 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002255 const std::shared_ptr<std::set<std::string>>& sensorNames,
2256 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002257 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002258{
Ed Tanous62598e32023-07-17 17:06:25 -07002259 BMCWEB_LOG_DEBUG("getSensorData enter");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002260 // Get managed objects from all services exposing sensors
2261 for (const std::string& connection : connections)
2262 {
George Liu5eb468d2023-06-20 17:03:24 +08002263 sdbusplus::message::object_path sensorPath(
2264 "/xyz/openbmc_project/sensors");
2265 dbus::utility::getManagedObjects(
2266 connection, sensorPath,
Ed Tanous002d39b2022-05-31 08:59:27 -07002267 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002268 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002269 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07002270 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002271 if (ec)
2272 {
Ed Tanous62598e32023-07-17 17:06:25 -07002273 BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08002274 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002275 return;
2276 }
2277 // Go through all objects and update response with sensor data
2278 for (const auto& objDictEntry : resp)
2279 {
2280 const std::string& objPath =
2281 static_cast<const std::string&>(objDictEntry.first);
Ed Tanous62598e32023-07-17 17:06:25 -07002282 BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
2283 objPath);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002284
Shawn McCarneyde629b62019-03-08 10:42:51 -06002285 std::vector<std::string> split;
2286 // Reserve space for
2287 // /xyz/openbmc_project/sensors/<name>/<subname>
2288 split.reserve(6);
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002289 // NOLINTNEXTLINE
2290 bmcweb::split(split, objPath, '/');
Shawn McCarneyde629b62019-03-08 10:42:51 -06002291 if (split.size() < 6)
2292 {
Ed Tanous62598e32023-07-17 17:06:25 -07002293 BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
2294 objPath);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002295 continue;
2296 }
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002297 // These indexes aren't intuitive, as split puts an empty
Shawn McCarneyde629b62019-03-08 10:42:51 -06002298 // string at the beginning
2299 const std::string& sensorType = split[4];
2300 const std::string& sensorName = split[5];
Ed Tanous62598e32023-07-17 17:06:25 -07002301 BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
2302 sensorType);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002303 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002304 {
Ed Tanous62598e32023-07-17 17:06:25 -07002305 BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002306 continue;
2307 }
2308
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002309 // Find inventory item (if any) associated with sensor
2310 InventoryItem* inventoryItem =
2311 findInventoryItemForSensor(inventoryItems, objPath);
2312
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002313 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002314 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002315
2316 nlohmann::json* sensorJson = nullptr;
2317
Nan Zhou928fefb2022-03-28 08:45:00 -07002318 if (sensorSchema == sensors::node::sensors &&
2319 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002320 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002321 std::string sensorTypeEscaped(sensorType);
2322 sensorTypeEscaped.erase(
2323 std::remove(sensorTypeEscaped.begin(),
2324 sensorTypeEscaped.end(), '_'),
2325 sensorTypeEscaped.end());
2326 std::string sensorId(sensorTypeEscaped);
2327 sensorId += "_";
2328 sensorId += sensorName;
2329
zhanghch058d1b46d2021-04-01 11:18:24 +08002330 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07002331 boost::urls::format("/redfish/v1/Chassis/{}/{}/{}",
2332 sensorsAsyncResp->chassisId,
2333 sensorsAsyncResp->chassisSubNode,
2334 sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002335 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002336 }
2337 else
2338 {
Ed Tanous271584a2019-07-09 16:24:22 -07002339 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002340 if (sensorsAsyncResp->efficientExpand)
2341 {
2342 fieldName = "Members";
2343 }
2344 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002345 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002346 fieldName = "Temperatures";
2347 }
2348 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2349 sensorType == "fan_pwm")
2350 {
2351 fieldName = "Fans";
2352 }
2353 else if (sensorType == "voltage")
2354 {
2355 fieldName = "Voltages";
2356 }
2357 else if (sensorType == "power")
2358 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002359 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002360 {
2361 fieldName = "PowerControl";
2362 }
2363 else if ((inventoryItem != nullptr) &&
2364 (inventoryItem->isPowerSupply))
2365 {
2366 fieldName = "PowerSupplies";
2367 }
2368 else
2369 {
2370 // Other power sensors are in SensorCollection
2371 continue;
2372 }
2373 }
2374 else
2375 {
Ed Tanous62598e32023-07-17 17:06:25 -07002376 BMCWEB_LOG_ERROR("Unsure how to handle sensorType {}",
2377 sensorType);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002378 continue;
2379 }
2380
2381 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002382 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002383 if (fieldName == "PowerControl")
2384 {
2385 if (tempArray.empty())
2386 {
2387 // Put multiple "sensors" into a single
2388 // PowerControl. Follows MemberId naming and
2389 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002390 nlohmann::json::object_t power;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002391 boost::urls::url url = boost::urls::format(
2392 "/redfish/v1/Chassis/{}/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002393 sensorsAsyncResp->chassisId,
2394 sensorsAsyncResp->chassisSubNode);
2395 url.set_fragment((""_json_pointer / fieldName / "0")
2396 .to_string());
2397 power["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002398 tempArray.emplace_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002399 }
2400 sensorJson = &(tempArray.back());
2401 }
2402 else if (fieldName == "PowerSupplies")
2403 {
2404 if (inventoryItem != nullptr)
2405 {
2406 sensorJson =
2407 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002408 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002409 }
2410 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002411 else if (fieldName == "Members")
2412 {
Ed Tanous677bb752022-09-15 10:52:19 -07002413 std::string sensorTypeEscaped(sensorType);
2414 sensorTypeEscaped.erase(
2415 std::remove(sensorTypeEscaped.begin(),
2416 sensorTypeEscaped.end(), '_'),
2417 sensorTypeEscaped.end());
2418 std::string sensorId(sensorTypeEscaped);
2419 sensorId += "_";
2420 sensorId += sensorName;
2421
Ed Tanous14766872022-03-15 10:44:42 -07002422 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002423 member["@odata.id"] = boost::urls::format(
2424 "/redfish/v1/Chassis/{}/{}/{}",
Ed Tanous677bb752022-09-15 10:52:19 -07002425 sensorsAsyncResp->chassisId,
2426 sensorsAsyncResp->chassisSubNode, sensorId);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002427 tempArray.emplace_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002428 sensorJson = &(tempArray.back());
2429 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002430 else
2431 {
Ed Tanous14766872022-03-15 10:44:42 -07002432 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002433 boost::urls::url url = boost::urls::format(
2434 "/redfish/v1/Chassis/{}/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002435 sensorsAsyncResp->chassisId,
2436 sensorsAsyncResp->chassisSubNode);
2437 url.set_fragment(
2438 (""_json_pointer / fieldName).to_string());
2439 member["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002440 tempArray.emplace_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002441 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002442 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002443 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002444
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002445 if (sensorJson != nullptr)
2446 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002447 objectInterfacesToJson(sensorName, sensorType,
2448 sensorsAsyncResp->chassisSubNode,
2449 objDictEntry.second, *sensorJson,
2450 inventoryItem);
2451
2452 std::string path = "/xyz/openbmc_project/sensors/";
2453 path += sensorType;
2454 path += "/";
2455 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002456 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002457 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002458 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002459 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002460 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002461 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002462 if (sensorsAsyncResp->chassisSubNode ==
2463 sensors::node::sensors &&
2464 sensorsAsyncResp->efficientExpand)
2465 {
2466 sensorsAsyncResp->asyncResp->res
2467 .jsonValue["Members@odata.count"] =
2468 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2469 .size();
2470 }
2471 else if (sensorsAsyncResp->chassisSubNode ==
2472 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002473 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002474 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002475 }
James Feist8bd25cc2019-03-15 15:14:00 -07002476 }
Ed Tanous62598e32023-07-17 17:06:25 -07002477 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
George Liu5eb468d2023-06-20 17:03:24 +08002478 });
Ed Tanous23a21a12020-07-25 04:45:05 +00002479 }
Ed Tanous62598e32023-07-17 17:06:25 -07002480 BMCWEB_LOG_DEBUG("getSensorData exit");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002481}
2482
Nan Zhoufe04d492022-06-22 17:10:41 +00002483inline void
2484 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2485 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002486{
Nan Zhoufe04d492022-06-22 17:10:41 +00002487 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2488 const std::set<std::string>& connections) {
Ed Tanous62598e32023-07-17 17:06:25 -07002489 BMCWEB_LOG_DEBUG("getConnectionCb enter");
Ed Tanousd0090732022-10-04 17:22:56 -07002490 auto getInventoryItemsCb =
2491 [sensorsAsyncResp, sensorNames,
2492 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2493 inventoryItems) {
Ed Tanous62598e32023-07-17 17:06:25 -07002494 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
Ed Tanousd0090732022-10-04 17:22:56 -07002495 // Get sensor data and store results in JSON
2496 getSensorData(sensorsAsyncResp, sensorNames, connections,
2497 inventoryItems);
Ed Tanous62598e32023-07-17 17:06:25 -07002498 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002499 };
2500
Ed Tanousd0090732022-10-04 17:22:56 -07002501 // Get inventory items associated with sensors
2502 getInventoryItems(sensorsAsyncResp, sensorNames,
2503 std::move(getInventoryItemsCb));
2504
Ed Tanous62598e32023-07-17 17:06:25 -07002505 BMCWEB_LOG_DEBUG("getConnectionCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002506 };
2507
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002508 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002509 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002510}
2511
Shawn McCarneyde629b62019-03-08 10:42:51 -06002512/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002513 * @brief Entry point for retrieving sensors data related to requested
2514 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002515 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002516 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002517inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002518 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002519{
Ed Tanous62598e32023-07-17 17:06:25 -07002520 BMCWEB_LOG_DEBUG("getChassisData enter");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002521 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002522 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002523 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous62598e32023-07-17 17:06:25 -07002524 BMCWEB_LOG_DEBUG("getChassisCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002525 processSensorList(sensorsAsyncResp, sensorNames);
Ed Tanous62598e32023-07-17 17:06:25 -07002526 BMCWEB_LOG_DEBUG("getChassisCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002527 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002528 // SensorCollection doesn't contain the Redundancy property
2529 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2530 {
2531 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2532 nlohmann::json::array();
2533 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002534 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002535 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2536 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2537 std::move(getChassisCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002538 BMCWEB_LOG_DEBUG("getChassisData exit");
Ed Tanous271584a2019-07-09 16:24:22 -07002539}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002540
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302541/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002542 * @brief Find the requested sensorName in the list of all sensors supplied by
2543 * the chassis node
2544 *
2545 * @param sensorName The sensor name supplied in the PATCH request
2546 * @param sensorsList The list of sensors managed by the chassis node
2547 * @param sensorsModified The list of sensors that were found as a result of
2548 * repeated calls to this function
2549 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002550inline bool
2551 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002552 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002553 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002554{
Nan Zhoufe04d492022-06-22 17:10:41 +00002555 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002556 {
George Liu28aa8de2021-02-01 15:13:30 +08002557 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002558 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002559 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002560 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002561 continue;
2562 }
2563 if (thisSensorName == sensorName)
2564 {
2565 sensorsModified.emplace(chassisSensor);
2566 return true;
2567 }
2568 }
2569 return false;
2570}
2571
Ed Tanousc71d6122022-11-29 14:10:32 -08002572inline std::pair<std::string, std::string>
2573 splitSensorNameAndType(std::string_view sensorId)
2574{
2575 size_t index = sensorId.find('_');
2576 if (index == std::string::npos)
2577 {
2578 return std::make_pair<std::string, std::string>("", "");
2579 }
2580 std::string sensorType{sensorId.substr(0, index)};
2581 std::string sensorName{sensorId.substr(index + 1)};
2582 // fan_pwm and fan_tach need special handling
2583 if (sensorType == "fantach" || sensorType == "fanpwm")
2584 {
2585 sensorType.insert(3, 1, '_');
2586 }
2587 return std::make_pair(sensorType, sensorName);
2588}
2589
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002590/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302591 * @brief Entry point for overriding sensor values of given sensor
2592 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002593 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002594 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002595 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302596 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002597inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002598 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002599 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002600 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302601{
Ed Tanous62598e32023-07-17 17:06:25 -07002602 BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
2603 sensorAsyncResp->chassisSubNode);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302604
Ed Tanous543f4402022-01-06 13:12:53 -08002605 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302606 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302607 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002608 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302609 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302610 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302611 if (collectionItems.first == "Temperatures")
2612 {
2613 propertyValueName = "ReadingCelsius";
2614 }
2615 else if (collectionItems.first == "Fans")
2616 {
2617 propertyValueName = "Reading";
2618 }
2619 else
2620 {
2621 propertyValueName = "ReadingVolts";
2622 }
2623 for (auto& item : collectionItems.second)
2624 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002625 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2626 "MemberId", memberId, propertyValueName,
2627 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302628 {
2629 return;
2630 }
2631 overrideMap.emplace(memberId,
2632 std::make_pair(value, collectionItems.first));
2633 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302634 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002635
Ed Tanous002d39b2022-05-31 08:59:27 -07002636 auto getChassisSensorListCb =
2637 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002638 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002639 // Match sensor names in the PATCH request to those managed by the
2640 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002641 const std::shared_ptr<std::set<std::string>> sensorNames =
2642 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302643 for (const auto& item : overrideMap)
2644 {
2645 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002646 std::pair<std::string, std::string> sensorNameType =
2647 splitSensorNameAndType(sensor);
2648 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2649 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302650 {
Ed Tanous62598e32023-07-17 17:06:25 -07002651 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
zhanghch058d1b46d2021-04-01 11:18:24 +08002652 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302653 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302654 return;
2655 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302656 }
2657 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002658 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002659 [sensorAsyncResp,
2660 overrideMap](const std::set<std::string>& /*connections*/,
2661 const std::set<std::pair<std::string, std::string>>&
2662 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002663 if (objectsWithConnection.size() != overrideMap.size())
2664 {
Ed Tanous62598e32023-07-17 17:06:25 -07002665 BMCWEB_LOG_INFO(
2666 "Unable to find all objects with proper connection {} requested {}",
2667 objectsWithConnection.size(), overrideMap.size());
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002668 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2669 sensorAsyncResp->chassisSubNode ==
2670 sensors::node::thermal
2671 ? "Temperatures"
2672 : "Voltages",
2673 "Count");
2674 return;
2675 }
2676 for (const auto& item : objectsWithConnection)
2677 {
2678 sdbusplus::message::object_path path(item.first);
2679 std::string sensorName = path.filename();
2680 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302681 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002682 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302683 return;
2684 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302685
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002686 const auto& iterator = overrideMap.find(sensorName);
2687 if (iterator == overrideMap.end())
2688 {
Ed Tanous62598e32023-07-17 17:06:25 -07002689 BMCWEB_LOG_INFO("Unable to find sensor object{}",
2690 item.first);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002691 messages::internalError(sensorAsyncResp->asyncResp->res);
2692 return;
2693 }
George Liu9ae226f2023-06-21 17:56:46 +08002694 sdbusplus::asio::setProperty(
2695 *crow::connections::systemBus, item.second, item.first,
2696 "xyz.openbmc_project.Sensor.Value", "Value",
2697 iterator->second.first,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002698 [sensorAsyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002699 if (ec)
2700 {
2701 if (ec.value() ==
2702 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002703 {
Ed Tanous62598e32023-07-17 17:06:25 -07002704 BMCWEB_LOG_WARNING(
2705 "Manufacturing mode is not Enabled...can't "
2706 "Override the sensor value. ");
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002707
Ed Tanous002d39b2022-05-31 08:59:27 -07002708 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002709 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002710 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002711 }
Ed Tanous62598e32023-07-17 17:06:25 -07002712 BMCWEB_LOG_DEBUG(
2713 "setOverrideValueStatus DBUS error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002714 messages::internalError(
2715 sensorAsyncResp->asyncResp->res);
2716 }
George Liu9ae226f2023-06-21 17:56:46 +08002717 });
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002718 }
2719 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302720 // Get object with connection for the given sensor name
2721 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2722 std::move(getObjectsWithConnectionCb));
2723 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302724 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002725 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2726 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2727 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302728}
2729
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002730/**
2731 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2732 * path of the sensor.
2733 *
2734 * Function builds valid Redfish response for sensor query of given chassis and
2735 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2736 * it to caller in a callback.
2737 *
2738 * @param chassis Chassis for which retrieval should be performed
2739 * @param node Node (group) of sensors. See sensors::node for supported values
2740 * @param mapComplete Callback to be called with retrieval result
2741 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002742inline void retrieveUriToDbusMap(const std::string& chassis,
2743 const std::string& node,
2744 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002745{
Ed Tanous02da7c52022-02-27 00:09:02 -08002746 decltype(sensors::paths)::const_iterator pathIt =
2747 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2748 [&node](auto&& val) { return val.first == node; });
2749 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002750 {
Ed Tanous62598e32023-07-17 17:06:25 -07002751 BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002752 mapComplete(boost::beast::http::status::bad_request, {});
2753 return;
2754 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002755
Nan Zhou72374eb2022-01-27 17:06:51 -08002756 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002757 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2758 const boost::beast::http::status status,
2759 const std::map<std::string, std::string>& uriToDbus) {
2760 mapCompleteCb(status, uriToDbus);
2761 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002762
2763 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002764 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002765 getChassisData(resp);
2766}
2767
Nan Zhoubacb2162022-04-06 11:28:32 -07002768namespace sensors
2769{
Nan Zhou928fefb2022-03-28 08:45:00 -07002770
Nan Zhoubacb2162022-04-06 11:28:32 -07002771inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002772 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2773 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002774 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002775{
Ed Tanous62598e32023-07-17 17:06:25 -07002776 BMCWEB_LOG_DEBUG("getChassisCallback enter ");
Nan Zhoubacb2162022-04-06 11:28:32 -07002777
Ed Tanousc1d019a2022-08-06 09:36:06 -07002778 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2779 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002780 {
Ed Tanous62598e32023-07-17 17:06:25 -07002781 BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
Nan Zhoubacb2162022-04-06 11:28:32 -07002782
2783 sdbusplus::message::object_path path(sensor);
2784 std::string sensorName = path.filename();
2785 if (sensorName.empty())
2786 {
Ed Tanous62598e32023-07-17 17:06:25 -07002787 BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002788 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002789 return;
2790 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002791 std::string type = path.parent_path().filename();
2792 // fan_tach has an underscore in it, so remove it to "normalize" the
2793 // type in the URI
2794 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2795
Ed Tanous14766872022-03-15 10:44:42 -07002796 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002797 std::string id = type;
2798 id += "_";
2799 id += sensorName;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002800 member["@odata.id"] = boost::urls::format(
2801 "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002802
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002803 entriesArray.emplace_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002804 }
2805
Ed Tanousc1d019a2022-08-06 09:36:06 -07002806 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous62598e32023-07-17 17:06:25 -07002807 BMCWEB_LOG_DEBUG("getChassisCallback exit");
Nan Zhoubacb2162022-04-06 11:28:32 -07002808}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002809
Ed Tanousac106bf2023-06-07 09:24:59 -07002810inline void handleSensorCollectionGet(
2811 App& app, const crow::Request& req,
2812 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2813 const std::string& chassisId)
Nan Zhoude167a62022-06-01 04:47:45 +00002814{
2815 query_param::QueryCapabilities capabilities = {
2816 .canDelegateExpandLevel = 1,
2817 };
2818 query_param::Query delegatedQuery;
Ed Tanousac106bf2023-06-07 09:24:59 -07002819 if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002820 delegatedQuery, capabilities))
2821 {
2822 return;
2823 }
2824
2825 if (delegatedQuery.expandType != query_param::ExpandType::None)
2826 {
2827 // we perform efficient expand.
Ed Tanousac106bf2023-06-07 09:24:59 -07002828 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2829 asyncResp, chassisId, sensors::dbus::sensorPaths,
Nan Zhoude167a62022-06-01 04:47:45 +00002830 sensors::node::sensors,
2831 /*efficientExpand=*/true);
Ed Tanousac106bf2023-06-07 09:24:59 -07002832 getChassisData(sensorsAsyncResp);
Nan Zhoude167a62022-06-01 04:47:45 +00002833
Ed Tanous62598e32023-07-17 17:06:25 -07002834 BMCWEB_LOG_DEBUG(
2835 "SensorCollection doGet exit via efficient expand handler");
Nan Zhoude167a62022-06-01 04:47:45 +00002836 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002837 }
Nan Zhoude167a62022-06-01 04:47:45 +00002838
Nan Zhoude167a62022-06-01 04:47:45 +00002839 // We get all sensors as hyperlinkes in the chassis (this
2840 // implies we reply on the default query parameters handler)
Ed Tanousac106bf2023-06-07 09:24:59 -07002841 getChassis(asyncResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2842 std::bind_front(sensors::getChassisCallback, asyncResp,
2843 chassisId, sensors::node::sensors));
Ed Tanousc1d019a2022-08-06 09:36:06 -07002844}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002845
Ed Tanousc1d019a2022-08-06 09:36:06 -07002846inline void
2847 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2848 const std::string& sensorPath,
2849 const ::dbus::utility::MapperGetObject& mapperResponse)
2850{
2851 if (mapperResponse.size() != 1)
2852 {
2853 messages::internalError(asyncResp->res);
2854 return;
2855 }
2856 const auto& valueIface = *mapperResponse.begin();
2857 const std::string& connectionName = valueIface.first;
Ed Tanous62598e32023-07-17 17:06:25 -07002858 BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
2859 BMCWEB_LOG_DEBUG("Path {}", sensorPath);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002860
2861 sdbusplus::asio::getAllProperties(
2862 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002863 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002864 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002865 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2866 if (ec)
2867 {
2868 messages::internalError(asyncResp->res);
2869 return;
2870 }
2871 sdbusplus::message::object_path path(sensorPath);
2872 std::string name = path.filename();
2873 path = path.parent_path();
2874 std::string type = path.filename();
2875 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2876 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002877 });
Nan Zhoude167a62022-06-01 04:47:45 +00002878}
2879
Nan Zhoue6bd8462022-06-01 04:35:35 +00002880inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002881 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002882 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002883 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002884{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002885 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002886 {
2887 return;
2888 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002889 std::pair<std::string, std::string> nameType =
2890 splitSensorNameAndType(sensorId);
2891 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002892 {
2893 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2894 return;
2895 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002896
Ed Tanousef4c65b2023-04-24 15:28:50 -07002897 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2898 "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002899
Ed Tanous62598e32023-07-17 17:06:25 -07002900 BMCWEB_LOG_DEBUG("Sensor doGet enter");
Nan Zhoue6bd8462022-06-01 04:35:35 +00002901
George Liu2b731192023-01-11 16:27:13 +08002902 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002903 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002904 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2905 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002906 // Get a list of all of the sensors that implement Sensor.Value
2907 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002908 ::dbus::utility::getDbusObject(
2909 sensorPath, interfaces,
Myung Baeaec0ec32023-05-31 15:10:01 -05002910 [asyncResp, sensorId,
George Liu2b731192023-01-11 16:27:13 +08002911 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002912 const ::dbus::utility::MapperGetObject& subtree) {
Ed Tanous62598e32023-07-17 17:06:25 -07002913 BMCWEB_LOG_DEBUG("respHandler1 enter");
Myung Baeaec0ec32023-05-31 15:10:01 -05002914 if (ec == boost::system::errc::io_error)
2915 {
Ed Tanous62598e32023-07-17 17:06:25 -07002916 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
Myung Baeaec0ec32023-05-31 15:10:01 -05002917 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2918 return;
2919 }
Nan Zhoue6bd8462022-06-01 04:35:35 +00002920 if (ec)
2921 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002922 messages::internalError(asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07002923 BMCWEB_LOG_ERROR(
2924 "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002925 return;
2926 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002927 getSensorFromDbus(asyncResp, sensorPath, subtree);
Ed Tanous62598e32023-07-17 17:06:25 -07002928 BMCWEB_LOG_DEBUG("respHandler1 exit");
George Liu2b731192023-01-11 16:27:13 +08002929 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002930}
2931
Nan Zhoubacb2162022-04-06 11:28:32 -07002932} // namespace sensors
2933
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002934inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002935{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002936 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002937 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002938 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002939 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002940}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002941
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002942inline void requestRoutesSensor(App& app)
2943{
2944 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002945 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002946 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002947 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002948}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002949
Ed Tanous1abe55e2018-09-05 08:30:59 -07002950} // namespace redfish