blob: 7258d1d85009f78e8860b73bc7c75240f5e0b15c [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>
Ed Tanous3544d2a2023-08-06 18:12:20 -070044#include <ranges>
Nan Zhoufe04d492022-06-22 17:10:41 +000045#include <set>
George Liu7a1dbc42022-12-07 16:03:22 +080046#include <string_view>
Ed Tanousb5a76932020-09-29 16:16:58 -070047#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080048#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010049
Ed Tanous1abe55e2018-09-05 08:30:59 -070050namespace redfish
51{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010052
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020053namespace sensors
54{
55namespace node
56{
57static constexpr std::string_view power = "Power";
58static constexpr std::string_view sensors = "Sensors";
59static constexpr std::string_view thermal = "Thermal";
60} // namespace node
61
Ed Tanous02da7c52022-02-27 00:09:02 -080062// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020063namespace dbus
64{
Ed Tanouscf9e4172022-12-21 09:30:16 -080065constexpr auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080066 "/xyz/openbmc_project/sensors/voltage",
67 "/xyz/openbmc_project/sensors/power"
68});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000069
Ed Tanouscf9e4172022-12-21 09:30:16 -080070constexpr auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080071 "/xyz/openbmc_project/sensors/power",
72 "/xyz/openbmc_project/sensors/current",
73 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070074 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080075#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080076 "/xyz/openbmc_project/sensors/voltage",
77 "/xyz/openbmc_project/sensors/fan_tach",
78 "/xyz/openbmc_project/sensors/temperature",
79 "/xyz/openbmc_project/sensors/fan_pwm",
80 "/xyz/openbmc_project/sensors/altitude",
81 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080082#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080083 "/xyz/openbmc_project/sensors/utilization"
84});
85
Ed Tanouscf9e4172022-12-21 09:30:16 -080086constexpr auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080087 "/xyz/openbmc_project/sensors/fan_tach",
88 "/xyz/openbmc_project/sensors/temperature",
89 "/xyz/openbmc_project/sensors/fan_pwm"
90});
91
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000092} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080093// clang-format on
94
Ed Tanouscf9e4172022-12-21 09:30:16 -080095using sensorPair =
96 std::pair<std::string_view, std::span<const std::string_view>>;
Ed Tanous02da7c52022-02-27 00:09:02 -080097static constexpr std::array<sensorPair, 3> paths = {
Ed Tanouscf9e4172022-12-21 09:30:16 -080098 {{node::power, dbus::powerPaths},
99 {node::sensors, dbus::sensorPaths},
100 {node::thermal, dbus::thermalPaths}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000101
Ed Tanous0ec8b832022-03-14 14:56:47 -0700102inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000103{
104 if (sensorType == "voltage")
105 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700106 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000107 }
108 if (sensorType == "power")
109 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700110 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000111 }
112 if (sensorType == "current")
113 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700114 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000115 }
116 if (sensorType == "fan_tach")
117 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700118 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000119 }
120 if (sensorType == "temperature")
121 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700122 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000123 }
124 if (sensorType == "fan_pwm" || sensorType == "utilization")
125 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700126 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000127 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600128 if (sensorType == "humidity")
129 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700130 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600131 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000132 if (sensorType == "altitude")
133 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700134 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000135 }
136 if (sensorType == "airflow")
137 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700138 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000139 }
140 if (sensorType == "energy")
141 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700142 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000143 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700144 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000145}
146
Ed Tanous1d7c0052022-08-09 12:32:26 -0700147inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000148{
149 if (sensorType == "voltage")
150 {
151 return "V";
152 }
153 if (sensorType == "power")
154 {
155 return "W";
156 }
157 if (sensorType == "current")
158 {
159 return "A";
160 }
161 if (sensorType == "fan_tach")
162 {
163 return "RPM";
164 }
165 if (sensorType == "temperature")
166 {
167 return "Cel";
168 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600169 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
170 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000171 {
172 return "%";
173 }
174 if (sensorType == "altitude")
175 {
176 return "m";
177 }
178 if (sensorType == "airflow")
179 {
180 return "cft_i/min";
181 }
182 if (sensorType == "energy")
183 {
184 return "J";
185 }
186 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200187}
188} // namespace sensors
189
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100190/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200191 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100192 * Gathers data needed for response processing after async calls are done
193 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700194class SensorsAsyncResp
195{
196 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200197 using DataCompleteCb = std::function<void(
198 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000199 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200200
201 struct SensorData
202 {
203 const std::string name;
204 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200205 const std::string dbusPath;
206 };
207
Ed Tanous8a592812022-06-04 09:06:59 -0700208 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800209 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800210 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800211 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700212 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700213 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
214 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500215 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200216
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200217 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700218 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800219 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800220 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800221 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200222 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700223 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700224 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
225 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200226 dataComplete{std::move(creationComplete)}
227 {}
228
Nan Zhou928fefb2022-03-28 08:45:00 -0700229 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700230 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700231 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800232 std::span<const std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700233 const std::string_view& subNode, bool efficientExpandIn) :
234 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700235 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700236 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700237 {}
238
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 ~SensorsAsyncResp()
240 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800241 if (asyncResp->res.result() ==
242 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700243 {
244 // Reset the json object to clear out any data that made it in
245 // before the error happened todo(ed) handle error condition with
246 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800247 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200249
250 if (dataComplete && metadata)
251 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000252 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800253 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200254 {
255 for (auto& sensor : *metadata)
256 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700257 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200258 }
259 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800260 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200261 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100263
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800264 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
265 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
266 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
267 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
268
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200269 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700270 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200271 {
272 if (metadata)
273 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700274 metadata->emplace_back(SensorData{
275 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200276 }
277 }
278
279 void updateUri(const std::string& name, const std::string& uri)
280 {
281 if (metadata)
282 {
283 for (auto& sensor : *metadata)
284 {
285 if (sensor.name == name)
286 {
287 sensor.uri = uri;
288 }
289 }
290 }
291 }
292
zhanghch058d1b46d2021-04-01 11:18:24 +0800293 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200294 const std::string chassisId;
Ed Tanouscf9e4172022-12-21 09:30:16 -0800295 const std::span<const std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200296 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700297 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200298
299 private:
300 std::optional<std::vector<SensorData>> metadata;
301 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100302};
303
304/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500305 * Possible states for physical inventory leds
306 */
307enum class LedState
308{
309 OFF,
310 ON,
311 BLINK,
312 UNKNOWN
313};
314
315/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500316 * D-Bus inventory item associated with one or more sensors.
317 */
318class InventoryItem
319{
320 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700321 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500322 {
323 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800324 sdbusplus::message::object_path path(objectPath);
325 name = path.filename();
326 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500327 {
Ed Tanous62598e32023-07-17 17:06:25 -0700328 BMCWEB_LOG_ERROR("Failed to find '/' in {}", objectPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500329 }
330 }
331
332 std::string objectPath;
333 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800334 bool isPresent = true;
335 bool isFunctional = true;
336 bool isPowerSupply = false;
337 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500338 std::string manufacturer;
339 std::string model;
340 std::string partNumber;
341 std::string serialNumber;
342 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500343 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800344 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500345};
346
347/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530348 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200349 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100350 * @param sensorNames Sensors retrieved from chassis
351 * @param callback Callback for processing gathered connections
352 */
353template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530354void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000355 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000356 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530357 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700358{
Ed Tanous62598e32023-07-17 17:06:25 -0700359 BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700360 const std::string path = "/xyz/openbmc_project/sensors";
George Liue99073f2022-12-09 11:06:16 +0800361 constexpr std::array<std::string_view, 1> interfaces = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700362 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100363
George Liue99073f2022-12-09 11:06:16 +0800364 // Make call to ObjectMapper to find all sensors objects
365 dbus::utility::getSubTree(
366 path, 2, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700367 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
George Liue99073f2022-12-09 11:06:16 +0800368 sensorNames](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700369 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +0800370 // Response handler for parsing objects subtree
Ed Tanous62598e32023-07-17 17:06:25 -0700371 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700372 if (ec)
373 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800374 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -0700375 BMCWEB_LOG_ERROR(
376 "getObjectsWithConnection resp_handler: Dbus error {}", ec);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100378 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100379
Ed Tanous62598e32023-07-17 17:06:25 -0700380 BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700381
382 // Make unique list of connections only for requested sensor types and
383 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000384 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530385 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386
Ed Tanous62598e32023-07-17 17:06:25 -0700387 BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700388 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389 {
Ed Tanous62598e32023-07-17 17:06:25 -0700390 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700391 }
392
393 for (const std::pair<
394 std::string,
395 std::vector<std::pair<std::string, std::vector<std::string>>>>&
396 object : subtree)
397 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700398 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700400 for (const std::pair<std::string, std::vector<std::string>>&
401 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 {
Ed Tanous62598e32023-07-17 17:06:25 -0700403 BMCWEB_LOG_DEBUG("Adding connection: {}", objData.first);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700404 connections.insert(objData.first);
405 objectsWithConnection.insert(
406 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407 }
408 }
409 }
Ed Tanous62598e32023-07-17 17:06:25 -0700410 BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530411 callback(std::move(connections), std::move(objectsWithConnection));
Ed Tanous62598e32023-07-17 17:06:25 -0700412 BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -0500413 });
Ed Tanous62598e32023-07-17 17:06:25 -0700414 BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530415}
416
417/**
418 * @brief Create connections necessary for sensors
419 * @param SensorsAsyncResp Pointer to object holding response data
420 * @param sensorNames Sensors retrieved from chassis
421 * @param callback Callback for processing gathered connections
422 */
423template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000424void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
425 const std::shared_ptr<std::set<std::string>> sensorNames,
426 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530427{
428 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000429 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530430 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700431 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000432 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530433 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100434}
435
436/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700437 * @brief Shrinks the list of sensors for processing
438 * @param SensorsAysncResp The class holding the Redfish response
439 * @param allSensors A list of all the sensors associated to the
440 * chassis element (i.e. baseboard, front panel, etc...)
441 * @param activeSensors A list that is a reduction of the incoming
442 * allSensors list. Eliminate Thermal sensors when a Power request is
443 * made, and eliminate Power sensors when a Thermal request is made.
444 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000445inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700446 crow::Response& res, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800447 std::span<const std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700448 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000449 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700450{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700451 if ((allSensors == nullptr) || (activeSensors == nullptr))
452 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700453 messages::resourceNotFound(res, chassisSubNode,
454 chassisSubNode == sensors::node::thermal
455 ? "Temperatures"
456 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700457
458 return;
459 }
460 if (allSensors->empty())
461 {
462 // Nothing to do, the activeSensors object is also empty
463 return;
464 }
465
Ed Tanous7f1cc262022-08-09 13:33:57 -0700466 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700467 {
468 for (const std::string& sensor : *allSensors)
469 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700470 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700471 {
472 activeSensors->emplace(sensor);
473 }
474 }
475 }
476}
477
Ed Tanous7f1cc262022-08-09 13:33:57 -0700478/*
479 *Populates the top level collection for a given subnode. Populates
480 *SensorCollection, Power, or Thermal schemas.
481 *
482 * */
483inline void populateChassisNode(nlohmann::json& jsonValue,
484 std::string_view chassisSubNode)
485{
486 if (chassisSubNode == sensors::node::power)
487 {
488 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
489 }
490 else if (chassisSubNode == sensors::node::thermal)
491 {
492 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
493 jsonValue["Fans"] = nlohmann::json::array();
494 jsonValue["Temperatures"] = nlohmann::json::array();
495 }
496 else if (chassisSubNode == sensors::node::sensors)
497 {
498 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
499 jsonValue["Description"] = "Collection of Sensors for this Chassis";
500 jsonValue["Members"] = nlohmann::json::array();
501 jsonValue["Members@odata.count"] = 0;
502 }
503
504 if (chassisSubNode != sensors::node::sensors)
505 {
506 jsonValue["Id"] = chassisSubNode;
507 }
508 jsonValue["Name"] = chassisSubNode;
509}
510
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700511/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100512 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200513 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100514 * @param callback Callback for next step in gathered sensor processing
515 */
516template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700517void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
518 std::string_view chassisId, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800519 std::span<const std::string_view> sensorTypes,
520 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700521{
Ed Tanous62598e32023-07-17 17:06:25 -0700522 BMCWEB_LOG_DEBUG("getChassis enter");
George Liu7a1dbc42022-12-07 16:03:22 +0800523 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700524 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500525 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800526
527 // Get the Chassis Collection
528 dbus::utility::getSubTreePaths(
529 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700530 [callback{std::forward<Callback>(callback)}, asyncResp,
531 chassisIdStr{std::string(chassisId)},
532 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800533 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700534 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous62598e32023-07-17 17:06:25 -0700535 BMCWEB_LOG_DEBUG("getChassis respHandler enter");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700536 if (ec)
537 {
Ed Tanous62598e32023-07-17 17:06:25 -0700538 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
Ed Tanous7f1cc262022-08-09 13:33:57 -0700539 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 return;
541 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700542 const std::string* chassisPath = nullptr;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700543 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700544 {
George Liu28aa8de2021-02-01 15:13:30 +0800545 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700546 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800547 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 {
Ed Tanous62598e32023-07-17 17:06:25 -0700549 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 continue;
551 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700552 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700554 chassisPath = &chassis;
555 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700557 }
558 if (chassisPath == nullptr)
559 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700560 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700561 return;
562 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700563 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700564
Ed Tanousef4c65b2023-04-24 15:28:50 -0700565 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
566 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500567
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500568 // Get the list of all sensors for this Chassis element
569 std::string sensorPath = *chassisPath + "/all_sensors";
George Liu6c3e9452023-03-03 13:55:29 +0800570 dbus::utility::getAssociationEndPoints(
571 sensorPath,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700572 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800573 callback{std::forward<const Callback>(callback)}](
Ed Tanous8b242752023-06-27 17:17:13 -0700574 const boost::system::error_code& ec2,
George Liu6c3e9452023-03-03 13:55:29 +0800575 const dbus::utility::MapperEndPoints& nodeSensorList) {
Ed Tanous8b242752023-06-27 17:17:13 -0700576 if (ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -0700577 {
Ed Tanous8b242752023-06-27 17:17:13 -0700578 if (ec2.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700579 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700580 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700582 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700583 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000584 const std::shared_ptr<std::set<std::string>> culledSensorList =
585 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700586 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
587 &nodeSensorList, culledSensorList);
Ed Tanous62598e32023-07-17 17:06:25 -0700588 BMCWEB_LOG_DEBUG("Finishing with {}", culledSensorList->size());
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 callback(culledSensorList);
George Liu7a1dbc42022-12-07 16:03:22 +0800590 });
Patrick Williams5a39f772023-10-20 11:20:21 -0500591 });
Ed Tanous62598e32023-07-17 17:06:25 -0700592 BMCWEB_LOG_DEBUG("getChassis exit");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100593}
594
595/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500596 * @brief Returns the Redfish State value for the specified inventory item.
597 * @param inventoryItem D-Bus inventory item associated with a sensor.
598 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700599 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000600inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500601{
602 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
603 {
604 return "Absent";
605 }
James Feist34dd1792019-05-17 14:10:54 -0700606
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500607 return "Enabled";
608}
609
610/**
611 * @brief Returns the Redfish Health value for the specified sensor.
612 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700613 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500614 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
615 * be nullptr if no associated inventory item was found.
616 * @return Health value for sensor.
617 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700618inline std::string getHealth(nlohmann::json& sensorJson,
619 const dbus::utility::DBusPropertiesMap& valuesDict,
620 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700621{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500622 // Get current health value (if any) in the sensor JSON object. Some JSON
623 // objects contain multiple sensors (such as PowerSupplies). We want to set
624 // the overall health to be the most severe of any of the sensors.
625 std::string currentHealth;
626 auto statusIt = sensorJson.find("Status");
627 if (statusIt != sensorJson.end())
628 {
629 auto healthIt = statusIt->find("Health");
630 if (healthIt != statusIt->end())
631 {
632 std::string* health = healthIt->get_ptr<std::string*>();
633 if (health != nullptr)
634 {
635 currentHealth = *health;
636 }
637 }
638 }
639
640 // If current health in JSON object is already Critical, return that. This
641 // should override the sensor health, which might be less severe.
642 if (currentHealth == "Critical")
643 {
644 return "Critical";
645 }
646
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200647 const bool* criticalAlarmHigh = nullptr;
648 const bool* criticalAlarmLow = nullptr;
649 const bool* warningAlarmHigh = nullptr;
650 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800651
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200652 const bool success = sdbusplus::unpackPropertiesNoThrow(
653 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
654 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
655 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
656 warningAlarmLow);
657
658 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700659 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200660 // Check if sensor has critical threshold alarm
661 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
662 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700663 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200664 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700665 }
666 }
667
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500668 // Check if associated inventory item is not functional
669 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
670 {
671 return "Critical";
672 }
673
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200674 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500675 // should override the sensor status, which might be less severe.
676 if (currentHealth == "Warning")
677 {
678 return "Warning";
679 }
680
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200681 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700682 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200683 // Check if sensor has warning threshold alarm
684 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
685 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700686 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200687 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700688 }
689 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500690
James Feist34dd1792019-05-17 14:10:54 -0700691 return "OK";
692}
693
Ed Tanous23a21a12020-07-25 04:45:05 +0000694inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500695 const InventoryItem* inventoryItem)
696{
697 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
698 {
699 switch (inventoryItem->ledState)
700 {
701 case LedState::OFF:
702 sensorJson["IndicatorLED"] = "Off";
703 break;
704 case LedState::ON:
705 sensorJson["IndicatorLED"] = "Lit";
706 break;
707 case LedState::BLINK:
708 sensorJson["IndicatorLED"] = "Blinking";
709 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000710 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500711 break;
712 }
713 }
714}
715
James Feist34dd1792019-05-17 14:10:54 -0700716/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100717 * @brief Builds a json sensor representation of a sensor.
718 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500719 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100720 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700721 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
722 * @param propertiesDict A dictionary of the properties to build the sensor
723 * from.
724 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500725 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
726 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100727 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700728inline void objectPropertiesToJson(
729 std::string_view sensorName, std::string_view sensorType,
730 std::string_view chassisSubNode,
731 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000732 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700734 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500735 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800736 std::string subNodeEscaped(sensorType);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700737 auto remove = std::ranges::remove(subNodeEscaped, '_');
738 subNodeEscaped.erase(std::ranges::begin(remove), subNodeEscaped.end());
Ed Tanousc1d019a2022-08-06 09:36:06 -0700739
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 }
Anjaliintel-210f3fbe52023-08-11 05:56:12 +0000835 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,
Michael Shen80f79a42023-08-24 13:41:53 +0000974 const dbus::utility::DBusInterfacesMap& interfacesDict,
Ed Tanous1d7c0052022-08-09 12:32:26 -0700975 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 Tanous3544d2a2023-08-06 18:12:20 -07001020 auto found = std::ranges::find_if(
1021 endpoints, [sensorsAsyncResp](const std::string& entry) {
Patrick Williams5a39f772023-10-20 11:20:21 -05001022 return entry.find(sensorsAsyncResp->chassisId) !=
1023 std::string::npos;
1024 });
James Feist8bd25cc2019-03-15 15:14:00 -07001025
Ed Tanous002d39b2022-05-31 08:59:27 -07001026 if (found == endpoints.end())
1027 {
1028 return;
1029 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001030 sdbusplus::asio::getAllProperties(
1031 *crow::connections::systemBus, owner, path,
1032 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001033 [path, sensorsAsyncResp](
Ed Tanous8b242752023-06-27 17:17:13 -07001034 const boost::system::error_code& ec3,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001035 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous8b242752023-06-27 17:17:13 -07001036 if (ec3)
Ed Tanous002d39b2022-05-31 08:59:27 -07001037 {
1038 return; // don't have to have this
1039 // interface
1040 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001041
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001042 const uint8_t* allowedFailures = nullptr;
1043 const std::vector<std::string>* collection = nullptr;
1044 const std::string* status = nullptr;
1045
1046 const bool success = sdbusplus::unpackPropertiesNoThrow(
1047 dbus_utils::UnpackErrorPrinter(), ret,
1048 "AllowedFailures", allowedFailures, "Collection",
1049 collection, "Status", status);
1050
1051 if (!success)
1052 {
1053 messages::internalError(
1054 sensorsAsyncResp->asyncResp->res);
1055 return;
1056 }
1057
1058 if (allowedFailures == nullptr || collection == nullptr ||
1059 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001060 {
Ed Tanous62598e32023-07-17 17:06:25 -07001061 BMCWEB_LOG_ERROR("Invalid redundancy interface");
Ed Tanous002d39b2022-05-31 08:59:27 -07001062 messages::internalError(
1063 sensorsAsyncResp->asyncResp->res);
1064 return;
1065 }
1066
Ed Tanous002d39b2022-05-31 08:59:27 -07001067 sdbusplus::message::object_path objectPath(path);
1068 std::string name = objectPath.filename();
1069 if (name.empty())
1070 {
1071 // this should be impossible
1072 messages::internalError(
1073 sensorsAsyncResp->asyncResp->res);
1074 return;
1075 }
1076 std::replace(name.begin(), name.end(), '_', ' ');
1077
1078 std::string health;
1079
Ed Tanous11ba3972022-07-11 09:50:41 -07001080 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001081 {
1082 health = "OK";
1083 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001084 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001085 {
1086 health = "Warning";
1087 }
1088 else
1089 {
1090 health = "Critical";
1091 }
1092 nlohmann::json::array_t redfishCollection;
1093 const auto& fanRedfish =
1094 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1095 for (const std::string& item : *collection)
1096 {
Ed Tanous8a592812022-06-04 09:06:59 -07001097 sdbusplus::message::object_path itemPath(item);
1098 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001099 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001100 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001101 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001102 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001103 /*
1104 todo(ed): merge patch that fixes the names
1105 std::replace(itemName.begin(),
1106 itemName.end(), '_', ' ');*/
Ed Tanous3544d2a2023-08-06 18:12:20 -07001107 auto schemaItem = std::ranges::find_if(
1108 fanRedfish, [itemName](const nlohmann::json& fan) {
Patrick Williams5a39f772023-10-20 11:20:21 -05001109 return fan["Name"] == itemName;
1110 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001111 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001112 {
Ed Tanous8a592812022-06-04 09:06:59 -07001113 nlohmann::json::object_t collectionId;
1114 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001115 (*schemaItem)["@odata.id"];
1116 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001117 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001118 }
1119 else
1120 {
Ed Tanous62598e32023-07-17 17:06:25 -07001121 BMCWEB_LOG_ERROR("failed to find fan in schema");
Ed Tanous002d39b2022-05-31 08:59:27 -07001122 messages::internalError(
1123 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001124 return;
1125 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001126 }
James Feist8bd25cc2019-03-15 15:14:00 -07001127
Patrick Williams89492a12023-05-10 07:51:34 -05001128 size_t minNumNeeded = collection->empty()
1129 ? 0
1130 : collection->size() -
1131 *allowedFailures;
Ed Tanous002d39b2022-05-31 08:59:27 -07001132 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1133 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001134
Ed Tanous002d39b2022-05-31 08:59:27 -07001135 nlohmann::json::object_t redundancy;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001136 boost::urls::url url =
1137 boost::urls::format("/redfish/v1/Chassis/{}/{}",
1138 sensorsAsyncResp->chassisId,
1139 sensorsAsyncResp->chassisSubNode);
Willy Tueddfc432022-09-26 16:46:38 +00001140 url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1141 .to_string());
1142 redundancy["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -07001143 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1144 redundancy["MinNumNeeded"] = minNumNeeded;
Ed Tanous002d39b2022-05-31 08:59:27 -07001145 redundancy["Mode"] = "N+m";
1146 redundancy["Name"] = name;
1147 redundancy["RedundancySet"] = redfishCollection;
1148 redundancy["Status"]["Health"] = health;
1149 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001150
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001151 jResp.emplace_back(std::move(redundancy));
Ed Tanous002d39b2022-05-31 08:59:27 -07001152 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001153 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001154 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001155 });
James Feist8bd25cc2019-03-15 15:14:00 -07001156}
1157
Ed Tanousb5a76932020-09-29 16:16:58 -07001158inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001159 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001160{
zhanghch058d1b46d2021-04-01 11:18:24 +08001161 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001162 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001163 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001164 {
1165 sensorHeaders = {"Voltages", "PowerSupplies"};
1166 }
1167 for (const std::string& sensorGroup : sensorHeaders)
1168 {
1169 nlohmann::json::iterator entry = response.find(sensorGroup);
1170 if (entry != response.end())
1171 {
1172 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001173 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001174 return c1["Name"] < c2["Name"];
1175 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001176
1177 // add the index counts to the end of each entry
1178 size_t count = 0;
1179 for (nlohmann::json& sensorJson : *entry)
1180 {
1181 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1182 if (odata == sensorJson.end())
1183 {
1184 continue;
1185 }
1186 std::string* value = odata->get_ptr<std::string*>();
1187 if (value != nullptr)
1188 {
Willy Tueddfc432022-09-26 16:46:38 +00001189 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001190 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001191 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001192 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001193 }
1194 }
1195 }
1196 }
1197}
1198
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001199/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001200 * @brief Finds the inventory item with the specified object path.
1201 * @param inventoryItems D-Bus inventory items associated with sensors.
1202 * @param invItemObjPath D-Bus object path of inventory item.
1203 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001204 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001205inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001206 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001207 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001208{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001209 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001210 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001211 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001212 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001213 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001214 }
1215 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001216 return nullptr;
1217}
1218
1219/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001220 * @brief Finds the inventory item associated with the specified sensor.
1221 * @param inventoryItems D-Bus inventory items associated with sensors.
1222 * @param sensorObjPath D-Bus object path of sensor.
1223 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001224 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001225inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001226 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001227 const std::string& sensorObjPath)
1228{
1229 for (InventoryItem& inventoryItem : *inventoryItems)
1230 {
Ed Tanousdb0d36e2023-06-30 10:37:05 -07001231 if (inventoryItem.sensors.contains(sensorObjPath))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001232 {
1233 return &inventoryItem;
1234 }
1235 }
1236 return nullptr;
1237}
1238
1239/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001240 * @brief Finds the inventory item associated with the specified led path.
1241 * @param inventoryItems D-Bus inventory items associated with sensors.
1242 * @param ledObjPath D-Bus object path of led.
1243 * @return Inventory item within vector, or nullptr if no match found.
1244 */
1245inline InventoryItem*
1246 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1247 const std::string& ledObjPath)
1248{
1249 for (InventoryItem& inventoryItem : inventoryItems)
1250 {
1251 if (inventoryItem.ledObjectPath == ledObjPath)
1252 {
1253 return &inventoryItem;
1254 }
1255 }
1256 return nullptr;
1257}
1258
1259/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001260 * @brief Adds inventory item and associated sensor to specified vector.
1261 *
1262 * Adds a new InventoryItem to the vector if necessary. Searches for an
1263 * existing InventoryItem with the specified object path. If not found, one is
1264 * added to the vector.
1265 *
1266 * Next, the specified sensor is added to the set of sensors associated with the
1267 * InventoryItem.
1268 *
1269 * @param inventoryItems D-Bus inventory items associated with sensors.
1270 * @param invItemObjPath D-Bus object path of inventory item.
1271 * @param sensorObjPath D-Bus object path of sensor
1272 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001273inline void addInventoryItem(
1274 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1275 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001276{
1277 // Look for inventory item in vector
Patrick Williams89492a12023-05-10 07:51:34 -05001278 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1279 invItemObjPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001280
1281 // If inventory item doesn't exist in vector, add it
1282 if (inventoryItem == nullptr)
1283 {
1284 inventoryItems->emplace_back(invItemObjPath);
1285 inventoryItem = &(inventoryItems->back());
1286 }
1287
1288 // Add sensor to set of sensors associated with inventory item
1289 inventoryItem->sensors.emplace(sensorObjPath);
1290}
1291
1292/**
1293 * @brief Stores D-Bus data in the specified inventory item.
1294 *
1295 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1296 * specified InventoryItem.
1297 *
1298 * This data is later used to provide sensor property values in the JSON
1299 * response.
1300 *
1301 * @param inventoryItem Inventory item where data will be stored.
1302 * @param interfacesDict Map containing D-Bus interfaces and their properties
1303 * for the specified inventory item.
1304 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001305inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001306 InventoryItem& inventoryItem,
Michael Shen80f79a42023-08-24 13:41:53 +00001307 const dbus::utility::DBusInterfacesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001308{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001309 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001310
Ed Tanous9eb808c2022-01-25 10:19:23 -08001311 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001312 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001313 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001314 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001315 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001316 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001317 if (name == "Present")
1318 {
1319 const bool* value = std::get_if<bool>(&dbusValue);
1320 if (value != nullptr)
1321 {
1322 inventoryItem.isPresent = *value;
1323 }
1324 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001325 }
1326 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001327 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001328
Ed Tanous711ac7a2021-12-20 09:34:41 -08001329 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001330 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001331 inventoryItem.isPowerSupply = true;
1332 }
1333
1334 // Get properties from Inventory.Decorator.Asset interface
1335 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1336 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001337 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001338 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001339 if (name == "Manufacturer")
1340 {
1341 const std::string* value =
1342 std::get_if<std::string>(&dbusValue);
1343 if (value != nullptr)
1344 {
1345 inventoryItem.manufacturer = *value;
1346 }
1347 }
1348 if (name == "Model")
1349 {
1350 const std::string* value =
1351 std::get_if<std::string>(&dbusValue);
1352 if (value != nullptr)
1353 {
1354 inventoryItem.model = *value;
1355 }
1356 }
1357 if (name == "SerialNumber")
1358 {
1359 const std::string* value =
1360 std::get_if<std::string>(&dbusValue);
1361 if (value != nullptr)
1362 {
1363 inventoryItem.serialNumber = *value;
1364 }
1365 }
1366 if (name == "PartNumber")
1367 {
1368 const std::string* value =
1369 std::get_if<std::string>(&dbusValue);
1370 if (value != nullptr)
1371 {
1372 inventoryItem.partNumber = *value;
1373 }
1374 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001375 }
1376 }
1377
Ed Tanous711ac7a2021-12-20 09:34:41 -08001378 if (interface ==
1379 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001380 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001381 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001382 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001383 if (name == "Functional")
1384 {
1385 const bool* value = std::get_if<bool>(&dbusValue);
1386 if (value != nullptr)
1387 {
1388 inventoryItem.isFunctional = *value;
1389 }
1390 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001391 }
1392 }
1393 }
1394}
1395
1396/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001397 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001398 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001399 * Uses the specified connections (services) to obtain D-Bus data for inventory
1400 * items associated with sensors. Stores the resulting data in the
1401 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001402 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001403 * This data is later used to provide sensor property values in the JSON
1404 * response.
1405 *
1406 * Finds the inventory item data asynchronously. Invokes callback when data has
1407 * been obtained.
1408 *
1409 * The callback must have the following signature:
1410 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001411 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001412 * @endcode
1413 *
1414 * This function is called recursively, obtaining data asynchronously from one
1415 * connection in each call. This ensures the callback is not invoked until the
1416 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001417 *
1418 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001419 * @param inventoryItems D-Bus inventory items associated with sensors.
1420 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001421 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001422 * @param callback Callback to invoke when inventory data has been obtained.
1423 * @param invConnectionsIndex Current index in invConnections. Only specified
1424 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001425 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426template <typename Callback>
1427static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001428 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001429 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001430 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1431 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001432{
Ed Tanous62598e32023-07-17 17:06:25 -07001433 BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001434
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001435 // If no more connections left, call callback
1436 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001437 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001438 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001439 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001440 return;
1441 }
1442
1443 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001444 auto it = invConnections->begin();
1445 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001446 if (it != invConnections->end())
1447 {
1448 const std::string& invConnection = *it;
1449
George Liu5eb468d2023-06-20 17:03:24 +08001450 // Get all object paths and their interfaces for current connection
1451 sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
1452 dbus::utility::getManagedObjects(
1453 invConnection, path,
1454 [sensorsAsyncResp, inventoryItems, invConnections,
1455 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1456 const boost::system::error_code& ec,
1457 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07001458 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001459 if (ec)
1460 {
Ed Tanous62598e32023-07-17 17:06:25 -07001461 BMCWEB_LOG_ERROR(
1462 "getInventoryItemsData respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001463 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001464 return;
1465 }
1466
1467 // Loop through returned object paths
1468 for (const auto& objDictEntry : resp)
1469 {
1470 const std::string& objPath =
1471 static_cast<const std::string&>(objDictEntry.first);
1472
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001473 // If this object path is one of the specified inventory items
Patrick Williams89492a12023-05-10 07:51:34 -05001474 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1475 objPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001476 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001477 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001478 // Store inventory data in InventoryItem
1479 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001480 }
1481 }
1482
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001483 // Recurse to get inventory item data from next connection
1484 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001485 invConnections, std::move(callback),
1486 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001487
Ed Tanous62598e32023-07-17 17:06:25 -07001488 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05001489 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001490 }
1491
Ed Tanous62598e32023-07-17 17:06:25 -07001492 BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001493}
1494
1495/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001497 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001498 * Gets the D-Bus connections (services) that provide data for the inventory
1499 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001500 *
1501 * Finds the connections asynchronously. Invokes callback when information has
1502 * been obtained.
1503 *
1504 * The callback must have the following signature:
1505 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001506 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001507 * @endcode
1508 *
1509 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001510 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001511 * @param callback Callback to invoke when connections have been obtained.
1512 */
1513template <typename Callback>
1514static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001515 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1516 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001517 Callback&& callback)
1518{
Ed Tanous62598e32023-07-17 17:06:25 -07001519 BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001520
1521 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001522 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001523 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001524 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1525 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001526 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1527
George Liue99073f2022-12-09 11:06:16 +08001528 // Make call to ObjectMapper to find all inventory items
1529 dbus::utility::getSubTree(
1530 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001531 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1532 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001533 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001534 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001535 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07001536 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001537 if (ec)
1538 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001539 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07001540 BMCWEB_LOG_ERROR(
1541 "getInventoryItemsConnections respHandler DBus error {}", ec);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001542 return;
1543 }
1544
1545 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001546 std::shared_ptr<std::set<std::string>> invConnections =
1547 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001548
1549 // Loop through objects from GetSubTree
1550 for (const std::pair<
1551 std::string,
1552 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1553 object : subtree)
1554 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001555 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001556 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001557 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001558 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001559 // Store all connections to inventory item
1560 for (const std::pair<std::string, std::vector<std::string>>&
1561 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001562 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001563 const std::string& invConnection = objData.first;
1564 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001565 }
1566 }
1567 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001568
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001569 callback(invConnections);
Ed Tanous62598e32023-07-17 17:06:25 -07001570 BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05001571 });
Ed Tanous62598e32023-07-17 17:06:25 -07001572 BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001573}
1574
1575/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001576 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001577 *
1578 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001579 * inventory items. Then finds the associations from those inventory items to
1580 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001581 *
1582 * Finds the inventory items asynchronously. Invokes callback when information
1583 * has been obtained.
1584 *
1585 * The callback must have the following signature:
1586 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001587 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001588 * @endcode
1589 *
1590 * @param sensorsAsyncResp Pointer to object holding response data.
1591 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001592 * implements ObjectManager.
1593 * @param callback Callback to invoke when inventory items have been obtained.
1594 */
1595template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001596static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001597 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001598 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001599 Callback&& callback)
1600{
Ed Tanous62598e32023-07-17 17:06:25 -07001601 BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001602
George Liu5eb468d2023-06-20 17:03:24 +08001603 // Call GetManagedObjects on the ObjectMapper to get all associations
1604 sdbusplus::message::object_path path("/");
1605 dbus::utility::getManagedObjects(
1606 "xyz.openbmc_project.ObjectMapper", path,
Ed Tanous02cad962022-06-30 16:50:15 -07001607 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001608 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001609 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07001610 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611 if (ec)
1612 {
Ed Tanous62598e32023-07-17 17:06:25 -07001613 BMCWEB_LOG_ERROR(
1614 "getInventoryItemAssociations respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001615 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001616 return;
1617 }
1618
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001619 // Create vector to hold list of inventory items
1620 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1621 std::make_shared<std::vector<InventoryItem>>();
1622
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001623 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001624 std::string sensorAssocPath;
1625 sensorAssocPath.reserve(128); // avoid memory allocations
1626 for (const auto& objDictEntry : resp)
1627 {
1628 const std::string& objPath =
1629 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001630
1631 // If path is inventory association for one of the specified sensors
1632 for (const std::string& sensorName : *sensorNames)
1633 {
1634 sensorAssocPath = sensorName;
1635 sensorAssocPath += "/inventory";
1636 if (objPath == sensorAssocPath)
1637 {
1638 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001639 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001640 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001641 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001643 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001644 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001645 if (valueName == "endpoints")
1646 {
1647 const std::vector<std::string>* endpoints =
1648 std::get_if<std::vector<std::string>>(
1649 &value);
1650 if ((endpoints != nullptr) &&
1651 !endpoints->empty())
1652 {
1653 // Add inventory item to vector
1654 const std::string& invItemPath =
1655 endpoints->front();
1656 addInventoryItem(inventoryItems,
1657 invItemPath,
1658 sensorName);
1659 }
1660 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001661 }
1662 }
1663 }
1664 break;
1665 }
1666 }
1667 }
1668
Anthony Wilsond5005492019-07-31 16:34:17 -05001669 // Now loop through the returned object paths again, this time to
1670 // find the leds associated with the inventory items we just found
1671 std::string inventoryAssocPath;
1672 inventoryAssocPath.reserve(128); // avoid memory allocations
1673 for (const auto& objDictEntry : resp)
1674 {
1675 const std::string& objPath =
1676 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001677
1678 for (InventoryItem& inventoryItem : *inventoryItems)
1679 {
1680 inventoryAssocPath = inventoryItem.objectPath;
1681 inventoryAssocPath += "/leds";
1682 if (objPath == inventoryAssocPath)
1683 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001684 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001685 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001686 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001687 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001688 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001689 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001690 if (valueName == "endpoints")
1691 {
1692 const std::vector<std::string>* endpoints =
1693 std::get_if<std::vector<std::string>>(
1694 &value);
1695 if ((endpoints != nullptr) &&
1696 !endpoints->empty())
1697 {
1698 // Add inventory item to vector
1699 // Store LED path in inventory item
1700 const std::string& ledPath =
1701 endpoints->front();
1702 inventoryItem.ledObjectPath = ledPath;
1703 }
1704 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001705 }
1706 }
1707 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001708
Anthony Wilsond5005492019-07-31 16:34:17 -05001709 break;
1710 }
1711 }
1712 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001713 callback(inventoryItems);
Ed Tanous62598e32023-07-17 17:06:25 -07001714 BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05001715 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001716
Ed Tanous62598e32023-07-17 17:06:25 -07001717 BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001718}
1719
1720/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001721 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1722 *
1723 * Uses the specified connections (services) to obtain D-Bus data for inventory
1724 * item leds associated with sensors. Stores the resulting data in the
1725 * inventoryItems vector.
1726 *
1727 * This data is later used to provide sensor property values in the JSON
1728 * response.
1729 *
1730 * Finds the inventory item led data asynchronously. Invokes callback when data
1731 * has been obtained.
1732 *
1733 * The callback must have the following signature:
1734 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001735 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001736 * @endcode
1737 *
1738 * This function is called recursively, obtaining data asynchronously from one
1739 * connection in each call. This ensures the callback is not invoked until the
1740 * last asynchronous function has completed.
1741 *
1742 * @param sensorsAsyncResp Pointer to object holding response data.
1743 * @param inventoryItems D-Bus inventory items associated with sensors.
1744 * @param ledConnections Connections that provide data for the inventory leds.
1745 * @param callback Callback to invoke when inventory data has been obtained.
1746 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1747 * in recursive calls to this function.
1748 */
1749template <typename Callback>
1750void getInventoryLedData(
1751 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1752 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001753 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001754 Callback&& callback, size_t ledConnectionsIndex = 0)
1755{
Ed Tanous62598e32023-07-17 17:06:25 -07001756 BMCWEB_LOG_DEBUG("getInventoryLedData enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001757
1758 // If no more connections left, call callback
1759 if (ledConnectionsIndex >= ledConnections->size())
1760 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001761 callback();
Ed Tanous62598e32023-07-17 17:06:25 -07001762 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001763 return;
1764 }
1765
1766 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001767 auto it = ledConnections->begin();
1768 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001769 if (it != ledConnections->end())
1770 {
1771 const std::string& ledPath = (*it).first;
1772 const std::string& ledConnection = (*it).second;
1773 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001774 auto respHandler =
1775 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001776 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001777 const boost::system::error_code& ec, const std::string& state) {
Ed Tanous62598e32023-07-17 17:06:25 -07001778 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07001779 if (ec)
1780 {
Ed Tanous62598e32023-07-17 17:06:25 -07001781 BMCWEB_LOG_ERROR(
1782 "getInventoryLedData respHandler DBus error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001783 messages::internalError(sensorsAsyncResp->asyncResp->res);
1784 return;
1785 }
1786
Ed Tanous62598e32023-07-17 17:06:25 -07001787 BMCWEB_LOG_DEBUG("Led state: {}", state);
Ed Tanous002d39b2022-05-31 08:59:27 -07001788 // Find inventory item with this LED object path
1789 InventoryItem* inventoryItem =
1790 findInventoryItemForLed(*inventoryItems, ledPath);
1791 if (inventoryItem != nullptr)
1792 {
1793 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001794 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001795 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001797 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001798 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001799 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001800 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001801 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001802 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 {
1804 inventoryItem->ledState = LedState::OFF;
1805 }
1806 else
1807 {
1808 inventoryItem->ledState = LedState::UNKNOWN;
1809 }
1810 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001811
Ed Tanous002d39b2022-05-31 08:59:27 -07001812 // Recurse to get LED data from next connection
1813 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1814 ledConnections, std::move(callback),
1815 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001816
Ed Tanous62598e32023-07-17 17:06:25 -07001817 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07001818 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001819
1820 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001821 sdbusplus::asio::getProperty<std::string>(
1822 *crow::connections::systemBus, ledConnection, ledPath,
1823 "xyz.openbmc_project.Led.Physical", "State",
1824 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001825 }
1826
Ed Tanous62598e32023-07-17 17:06:25 -07001827 BMCWEB_LOG_DEBUG("getInventoryLedData exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001828}
1829
1830/**
1831 * @brief Gets LED data for LEDs associated with given inventory items.
1832 *
1833 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1834 * associated with the specified inventory items. Then gets the LED data from
1835 * each connection and stores it in the inventory item.
1836 *
1837 * This data is later used to provide sensor property values in the JSON
1838 * response.
1839 *
1840 * Finds the LED data asynchronously. Invokes callback when information has
1841 * been obtained.
1842 *
1843 * The callback must have the following signature:
1844 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001845 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001846 * @endcode
1847 *
1848 * @param sensorsAsyncResp Pointer to object holding response data.
1849 * @param inventoryItems D-Bus inventory items associated with sensors.
1850 * @param callback Callback to invoke when inventory items have been obtained.
1851 */
1852template <typename Callback>
1853void getInventoryLeds(
1854 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1855 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1856 Callback&& callback)
1857{
Ed Tanous62598e32023-07-17 17:06:25 -07001858 BMCWEB_LOG_DEBUG("getInventoryLeds enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001859
1860 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001861 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001862 "xyz.openbmc_project.Led.Physical"};
1863
George Liue99073f2022-12-09 11:06:16 +08001864 // Make call to ObjectMapper to find all inventory items
1865 dbus::utility::getSubTree(
1866 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001867 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1868 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001869 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001870 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001871 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07001872 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
Anthony Wilsond5005492019-07-31 16:34:17 -05001873 if (ec)
1874 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001875 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07001876 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}", ec);
Anthony Wilsond5005492019-07-31 16:34:17 -05001877 return;
1878 }
1879
1880 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001881 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1882 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001883
1884 // Loop through objects from GetSubTree
1885 for (const std::pair<
1886 std::string,
1887 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1888 object : subtree)
1889 {
1890 // Check if object path is LED for one of the specified inventory
1891 // items
1892 const std::string& ledPath = object.first;
1893 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1894 {
1895 // Add mapping from ledPath to connection
1896 const std::string& connection = object.second.begin()->first;
1897 (*ledConnections)[ledPath] = connection;
Ed Tanous62598e32023-07-17 17:06:25 -07001898 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath, connection);
Anthony Wilsond5005492019-07-31 16:34:17 -05001899 }
1900 }
1901
1902 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1903 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07001904 BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05001905 });
Ed Tanous62598e32023-07-17 17:06:25 -07001906 BMCWEB_LOG_DEBUG("getInventoryLeds exit");
Anthony Wilsond5005492019-07-31 16:34:17 -05001907}
1908
1909/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001910 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1911 *
1912 * Uses the specified connections (services) (currently assumes just one) to
1913 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1914 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1915 *
1916 * This data is later used to provide sensor property values in the JSON
1917 * response.
1918 *
1919 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1920 * when data has been obtained.
1921 *
1922 * The callback must have the following signature:
1923 * @code
1924 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1925 * @endcode
1926 *
1927 * @param sensorsAsyncResp Pointer to object holding response data.
1928 * @param inventoryItems D-Bus inventory items associated with sensors.
1929 * @param psAttributesConnections Connections that provide data for the Power
1930 * Supply Attributes
1931 * @param callback Callback to invoke when data has been obtained.
1932 */
1933template <typename Callback>
1934void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001935 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001936 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001937 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001938 Callback&& callback)
1939{
Ed Tanous62598e32023-07-17 17:06:25 -07001940 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001941
1942 if (psAttributesConnections.empty())
1943 {
Ed Tanous62598e32023-07-17 17:06:25 -07001944 BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001945 callback(inventoryItems);
1946 return;
1947 }
1948
1949 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001950 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001951
1952 const std::string& psAttributesPath = (*it).first;
1953 const std::string& psAttributesConnection = (*it).second;
1954
1955 // Response handler for Get DeratingFactor property
Patrick Williams5a39f772023-10-20 11:20:21 -05001956 auto respHandler = [sensorsAsyncResp, inventoryItems,
1957 callback{std::forward<Callback>(callback)}](
1958 const boost::system::error_code& ec,
1959 const uint32_t value) {
Ed Tanous62598e32023-07-17 17:06:25 -07001960 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001961 if (ec)
1962 {
Ed Tanous62598e32023-07-17 17:06:25 -07001963 BMCWEB_LOG_ERROR(
1964 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08001965 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001966 return;
1967 }
1968
Ed Tanous62598e32023-07-17 17:06:25 -07001969 BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001970 // Store value in Power Supply Inventory Items
1971 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001972 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001973 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001974 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001975 inventoryItem.powerSupplyEfficiencyPercent =
1976 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001977 }
1978 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001979
Ed Tanous62598e32023-07-17 17:06:25 -07001980 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001981 callback(inventoryItems);
1982 };
1983
1984 // Get the DeratingFactor property for the PowerSupplyAttributes
1985 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001986 sdbusplus::asio::getProperty<uint32_t>(
1987 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1988 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
1989 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05001990
Ed Tanous62598e32023-07-17 17:06:25 -07001991 BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05001992}
1993
1994/**
1995 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
1996 *
1997 * Gets the D-Bus connection (service) that provides Power Supply Attributes
1998 * data. Then gets the Power Supply Attributes data from the connection
1999 * (currently just assumes 1 connection) and stores the data in the inventory
2000 * item.
2001 *
2002 * This data is later used to provide sensor property values in the JSON
2003 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2004 *
2005 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2006 * when information has been obtained.
2007 *
2008 * The callback must have the following signature:
2009 * @code
2010 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2011 * @endcode
2012 *
2013 * @param sensorsAsyncResp Pointer to object holding response data.
2014 * @param inventoryItems D-Bus inventory items associated with sensors.
2015 * @param callback Callback to invoke when data has been obtained.
2016 */
2017template <typename Callback>
2018void getPowerSupplyAttributes(
2019 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2020 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2021 Callback&& callback)
2022{
Ed Tanous62598e32023-07-17 17:06:25 -07002023 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002024
2025 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002026 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002027 {
Ed Tanous62598e32023-07-17 17:06:25 -07002028 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002029 callback(inventoryItems);
2030 return;
2031 }
2032
George Liue99073f2022-12-09 11:06:16 +08002033 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002034 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2035
George Liue99073f2022-12-09 11:06:16 +08002036 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2037 dbus::utility::getSubTree(
2038 "/xyz/openbmc_project", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002039 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2040 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002041 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002042 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002043 // Response handler for parsing output from GetSubTree
Ed Tanous62598e32023-07-17 17:06:25 -07002044 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002045 if (ec)
2046 {
2047 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07002048 BMCWEB_LOG_ERROR(
2049 "getPowerSupplyAttributes respHandler DBus error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002050 return;
2051 }
2052 if (subtree.empty())
2053 {
Ed Tanous62598e32023-07-17 17:06:25 -07002054 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002055 callback(inventoryItems);
2056 return;
2057 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002058
Ed Tanous002d39b2022-05-31 08:59:27 -07002059 // Currently we only support 1 power supply attribute, use this for
2060 // all the power supplies. Build map of object path to connection.
2061 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002062 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002063
Ed Tanous002d39b2022-05-31 08:59:27 -07002064 if (subtree[0].first.empty() || subtree[0].second.empty())
2065 {
Ed Tanous62598e32023-07-17 17:06:25 -07002066 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002067 callback(inventoryItems);
2068 return;
2069 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002070
Ed Tanous002d39b2022-05-31 08:59:27 -07002071 const std::string& psAttributesPath = subtree[0].first;
2072 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002073
Ed Tanous002d39b2022-05-31 08:59:27 -07002074 if (connection.empty())
2075 {
Ed Tanous62598e32023-07-17 17:06:25 -07002076 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002077 callback(inventoryItems);
2078 return;
2079 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002080
Ed Tanous002d39b2022-05-31 08:59:27 -07002081 psAttributesConnections[psAttributesPath] = connection;
Ed Tanous62598e32023-07-17 17:06:25 -07002082 BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
2083 connection);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002084
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2086 psAttributesConnections,
2087 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07002088 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05002089 });
Ed Tanous62598e32023-07-17 17:06:25 -07002090 BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002091}
2092
2093/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002094 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002095 *
2096 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002097 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002098 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002099 * This data is later used to provide sensor property values in the JSON
2100 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002101 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002102 * Finds the inventory items asynchronously. Invokes callback when the
2103 * inventory items have been obtained.
2104 *
2105 * The callback must have the following signature:
2106 * @code
2107 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2108 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002109 *
2110 * @param sensorsAsyncResp Pointer to object holding response data.
2111 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002112 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002113 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002114 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002115template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002116static void
2117 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2118 const std::shared_ptr<std::set<std::string>> sensorNames,
2119 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002120{
Ed Tanous62598e32023-07-17 17:06:25 -07002121 BMCWEB_LOG_DEBUG("getInventoryItems enter");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002122 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002123 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002124 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous62598e32023-07-17 17:06:25 -07002125 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002126 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002127 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002128 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002129 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous62598e32023-07-17 17:06:25 -07002130 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2132 callback{std::move(callback)}]() {
Ed Tanous62598e32023-07-17 17:06:25 -07002133 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
Gunnar Mills42cbe532019-08-15 15:26:54 -05002134
Ed Tanous002d39b2022-05-31 08:59:27 -07002135 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2136 callback{std::move(callback)}]() {
Ed Tanous62598e32023-07-17 17:06:25 -07002137 BMCWEB_LOG_DEBUG("getInventoryLedsCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002138 // Find Power Supply Attributes and get the data
2139 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2140 std::move(callback));
Ed Tanous62598e32023-07-17 17:06:25 -07002141 BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002142 };
2143
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 // Find led connections and get the data
2145 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2146 std::move(getInventoryLedsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002147 BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002148 };
2149
2150 // Get inventory item data from connections
2151 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002152 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 std::move(getInventoryItemsDataCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002154 BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002155 };
2156
Ed Tanous002d39b2022-05-31 08:59:27 -07002157 // Get connections that provide inventory item data
2158 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2159 std::move(getInventoryItemsConnectionsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002160 BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002161 };
2162
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002163 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002164 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002165 std::move(getInventoryItemAssociationsCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002166 BMCWEB_LOG_DEBUG("getInventoryItems exit");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002167}
2168
2169/**
2170 * @brief Returns JSON PowerSupply object for the specified inventory item.
2171 *
2172 * Searches for a JSON PowerSupply object that matches the specified inventory
2173 * item. If one is not found, a new PowerSupply object is added to the JSON
2174 * array.
2175 *
2176 * Multiple sensors are often associated with one power supply inventory item.
2177 * As a result, multiple sensor values are stored in one JSON PowerSupply
2178 * object.
2179 *
2180 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2181 * @param inventoryItem Inventory item for the power supply.
2182 * @param chassisId Chassis that contains the power supply.
2183 * @return JSON PowerSupply object for the specified inventory item.
2184 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002185inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002186 const InventoryItem& inventoryItem,
2187 const std::string& chassisId)
2188{
2189 // Check if matching PowerSupply object already exists in JSON array
2190 for (nlohmann::json& powerSupply : powerSupplyArray)
2191 {
Alexander Hansenf7afb172023-04-27 14:34:08 +02002192 if (powerSupply["Name"] ==
2193 boost::replace_all_copy(inventoryItem.name, "_", " "))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002194 {
2195 return powerSupply;
2196 }
2197 }
2198
2199 // Add new PowerSupply object to JSON array
2200 powerSupplyArray.push_back({});
2201 nlohmann::json& powerSupply = powerSupplyArray.back();
Ed Tanousef4c65b2023-04-24 15:28:50 -07002202 boost::urls::url url = boost::urls::format("/redfish/v1/Chassis/{}/Power",
2203 chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002204 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2205 powerSupply["@odata.id"] = std::move(url);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002206 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2207 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2208 powerSupply["Model"] = inventoryItem.model;
2209 powerSupply["PartNumber"] = inventoryItem.partNumber;
2210 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002211 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002212
Gunnar Mills42cbe532019-08-15 15:26:54 -05002213 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2214 {
2215 powerSupply["EfficiencyPercent"] =
2216 inventoryItem.powerSupplyEfficiencyPercent;
2217 }
2218
2219 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002220 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2221 powerSupply["Status"]["Health"] = health;
2222
2223 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002224}
2225
2226/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002227 * @brief Gets the values of the specified sensors.
2228 *
2229 * Stores the results as JSON in the SensorsAsyncResp.
2230 *
2231 * Gets the sensor values asynchronously. Stores the results later when the
2232 * information has been obtained.
2233 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002234 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002235 *
2236 * To minimize the number of DBus calls, the DBus method
2237 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2238 * values of all sensors provided by a connection (service).
2239 *
2240 * The connections set contains all the connections that provide sensor values.
2241 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002242 * The InventoryItem vector contains D-Bus inventory items associated with the
2243 * sensors. Inventory item data is needed for some Redfish sensor properties.
2244 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002245 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002246 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002247 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002248 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002249 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002250 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002251inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002252 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002253 const std::shared_ptr<std::set<std::string>>& sensorNames,
2254 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002255 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002256{
Ed Tanous62598e32023-07-17 17:06:25 -07002257 BMCWEB_LOG_DEBUG("getSensorData enter");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002258 // Get managed objects from all services exposing sensors
2259 for (const std::string& connection : connections)
2260 {
George Liu5eb468d2023-06-20 17:03:24 +08002261 sdbusplus::message::object_path sensorPath(
2262 "/xyz/openbmc_project/sensors");
2263 dbus::utility::getManagedObjects(
2264 connection, sensorPath,
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002266 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002267 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous62598e32023-07-17 17:06:25 -07002268 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002269 if (ec)
2270 {
Ed Tanous62598e32023-07-17 17:06:25 -07002271 BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
zhanghch058d1b46d2021-04-01 11:18:24 +08002272 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002273 return;
2274 }
2275 // Go through all objects and update response with sensor data
2276 for (const auto& objDictEntry : resp)
2277 {
2278 const std::string& objPath =
2279 static_cast<const std::string&>(objDictEntry.first);
Ed Tanous62598e32023-07-17 17:06:25 -07002280 BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
2281 objPath);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002282
Shawn McCarneyde629b62019-03-08 10:42:51 -06002283 std::vector<std::string> split;
2284 // Reserve space for
2285 // /xyz/openbmc_project/sensors/<name>/<subname>
2286 split.reserve(6);
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002287 // NOLINTNEXTLINE
2288 bmcweb::split(split, objPath, '/');
Shawn McCarneyde629b62019-03-08 10:42:51 -06002289 if (split.size() < 6)
2290 {
Ed Tanous62598e32023-07-17 17:06:25 -07002291 BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
2292 objPath);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002293 continue;
2294 }
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002295 // These indexes aren't intuitive, as split puts an empty
Shawn McCarneyde629b62019-03-08 10:42:51 -06002296 // string at the beginning
2297 const std::string& sensorType = split[4];
2298 const std::string& sensorName = split[5];
Ed Tanous62598e32023-07-17 17:06:25 -07002299 BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
2300 sensorType);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002301 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002302 {
Ed Tanous62598e32023-07-17 17:06:25 -07002303 BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002304 continue;
2305 }
2306
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002307 // Find inventory item (if any) associated with sensor
2308 InventoryItem* inventoryItem =
2309 findInventoryItemForSensor(inventoryItems, objPath);
2310
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002311 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002312 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002313
2314 nlohmann::json* sensorJson = nullptr;
2315
Nan Zhou928fefb2022-03-28 08:45:00 -07002316 if (sensorSchema == sensors::node::sensors &&
2317 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002318 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002319 std::string sensorTypeEscaped(sensorType);
Ed Tanous3544d2a2023-08-06 18:12:20 -07002320 auto remove = std::ranges::remove(sensorTypeEscaped, '_');
2321
2322 sensorTypeEscaped.erase(std::ranges::begin(remove),
2323 sensorTypeEscaped.end());
Ed Tanousc1d019a2022-08-06 09:36:06 -07002324 std::string sensorId(sensorTypeEscaped);
2325 sensorId += "_";
2326 sensorId += sensorName;
2327
zhanghch058d1b46d2021-04-01 11:18:24 +08002328 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07002329 boost::urls::format("/redfish/v1/Chassis/{}/{}/{}",
2330 sensorsAsyncResp->chassisId,
2331 sensorsAsyncResp->chassisSubNode,
2332 sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002333 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002334 }
2335 else
2336 {
Ed Tanous271584a2019-07-09 16:24:22 -07002337 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002338 if (sensorsAsyncResp->efficientExpand)
2339 {
2340 fieldName = "Members";
2341 }
2342 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002343 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002344 fieldName = "Temperatures";
2345 }
2346 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2347 sensorType == "fan_pwm")
2348 {
2349 fieldName = "Fans";
2350 }
2351 else if (sensorType == "voltage")
2352 {
2353 fieldName = "Voltages";
2354 }
2355 else if (sensorType == "power")
2356 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002357 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002358 {
2359 fieldName = "PowerControl";
2360 }
2361 else if ((inventoryItem != nullptr) &&
2362 (inventoryItem->isPowerSupply))
2363 {
2364 fieldName = "PowerSupplies";
2365 }
2366 else
2367 {
2368 // Other power sensors are in SensorCollection
2369 continue;
2370 }
2371 }
2372 else
2373 {
Ed Tanous62598e32023-07-17 17:06:25 -07002374 BMCWEB_LOG_ERROR("Unsure how to handle sensorType {}",
2375 sensorType);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002376 continue;
2377 }
2378
2379 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002380 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002381 if (fieldName == "PowerControl")
2382 {
2383 if (tempArray.empty())
2384 {
2385 // Put multiple "sensors" into a single
2386 // PowerControl. Follows MemberId naming and
2387 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002388 nlohmann::json::object_t power;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002389 boost::urls::url url = boost::urls::format(
2390 "/redfish/v1/Chassis/{}/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002391 sensorsAsyncResp->chassisId,
2392 sensorsAsyncResp->chassisSubNode);
2393 url.set_fragment((""_json_pointer / fieldName / "0")
2394 .to_string());
2395 power["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002396 tempArray.emplace_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002397 }
2398 sensorJson = &(tempArray.back());
2399 }
2400 else if (fieldName == "PowerSupplies")
2401 {
2402 if (inventoryItem != nullptr)
2403 {
2404 sensorJson =
2405 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002406 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002407 }
2408 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002409 else if (fieldName == "Members")
2410 {
Ed Tanous677bb752022-09-15 10:52:19 -07002411 std::string sensorTypeEscaped(sensorType);
Ed Tanous3544d2a2023-08-06 18:12:20 -07002412 auto remove = std::ranges::remove(sensorTypeEscaped,
2413 '_');
2414 sensorTypeEscaped.erase(std::ranges::begin(remove),
2415 sensorTypeEscaped.end());
Ed Tanous677bb752022-09-15 10:52:19 -07002416 std::string sensorId(sensorTypeEscaped);
2417 sensorId += "_";
2418 sensorId += sensorName;
2419
Ed Tanous14766872022-03-15 10:44:42 -07002420 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002421 member["@odata.id"] = boost::urls::format(
2422 "/redfish/v1/Chassis/{}/{}/{}",
Ed Tanous677bb752022-09-15 10:52:19 -07002423 sensorsAsyncResp->chassisId,
2424 sensorsAsyncResp->chassisSubNode, sensorId);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002425 tempArray.emplace_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002426 sensorJson = &(tempArray.back());
2427 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002428 else
2429 {
Ed Tanous14766872022-03-15 10:44:42 -07002430 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002431 boost::urls::url url = boost::urls::format(
2432 "/redfish/v1/Chassis/{}/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002433 sensorsAsyncResp->chassisId,
2434 sensorsAsyncResp->chassisSubNode);
2435 url.set_fragment(
2436 (""_json_pointer / fieldName).to_string());
2437 member["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002438 tempArray.emplace_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002439 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002440 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002441 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002442
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002443 if (sensorJson != nullptr)
2444 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002445 objectInterfacesToJson(sensorName, sensorType,
2446 sensorsAsyncResp->chassisSubNode,
2447 objDictEntry.second, *sensorJson,
2448 inventoryItem);
2449
2450 std::string path = "/xyz/openbmc_project/sensors/";
2451 path += sensorType;
2452 path += "/";
2453 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002454 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002455 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002456 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002457 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002458 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002459 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002460 if (sensorsAsyncResp->chassisSubNode ==
2461 sensors::node::sensors &&
2462 sensorsAsyncResp->efficientExpand)
2463 {
2464 sensorsAsyncResp->asyncResp->res
2465 .jsonValue["Members@odata.count"] =
2466 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2467 .size();
2468 }
2469 else if (sensorsAsyncResp->chassisSubNode ==
2470 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002471 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002472 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002473 }
James Feist8bd25cc2019-03-15 15:14:00 -07002474 }
Ed Tanous62598e32023-07-17 17:06:25 -07002475 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05002476 });
Ed Tanous23a21a12020-07-25 04:45:05 +00002477 }
Ed Tanous62598e32023-07-17 17:06:25 -07002478 BMCWEB_LOG_DEBUG("getSensorData exit");
Shawn McCarneyde629b62019-03-08 10:42:51 -06002479}
2480
Nan Zhoufe04d492022-06-22 17:10:41 +00002481inline void
2482 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2483 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002484{
Nan Zhoufe04d492022-06-22 17:10:41 +00002485 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2486 const std::set<std::string>& connections) {
Ed Tanous62598e32023-07-17 17:06:25 -07002487 BMCWEB_LOG_DEBUG("getConnectionCb enter");
Ed Tanousd0090732022-10-04 17:22:56 -07002488 auto getInventoryItemsCb =
2489 [sensorsAsyncResp, sensorNames,
2490 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2491 inventoryItems) {
Ed Tanous62598e32023-07-17 17:06:25 -07002492 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
Ed Tanousd0090732022-10-04 17:22:56 -07002493 // Get sensor data and store results in JSON
2494 getSensorData(sensorsAsyncResp, sensorNames, connections,
2495 inventoryItems);
Ed Tanous62598e32023-07-17 17:06:25 -07002496 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002497 };
2498
Ed Tanousd0090732022-10-04 17:22:56 -07002499 // Get inventory items associated with sensors
2500 getInventoryItems(sensorsAsyncResp, sensorNames,
2501 std::move(getInventoryItemsCb));
2502
Ed Tanous62598e32023-07-17 17:06:25 -07002503 BMCWEB_LOG_DEBUG("getConnectionCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002504 };
2505
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002506 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002507 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002508}
2509
Shawn McCarneyde629b62019-03-08 10:42:51 -06002510/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002511 * @brief Entry point for retrieving sensors data related to requested
2512 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002513 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002514 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002515inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002516 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002517{
Ed Tanous62598e32023-07-17 17:06:25 -07002518 BMCWEB_LOG_DEBUG("getChassisData enter");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002519 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002520 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002521 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous62598e32023-07-17 17:06:25 -07002522 BMCWEB_LOG_DEBUG("getChassisCb enter");
Ed Tanous002d39b2022-05-31 08:59:27 -07002523 processSensorList(sensorsAsyncResp, sensorNames);
Ed Tanous62598e32023-07-17 17:06:25 -07002524 BMCWEB_LOG_DEBUG("getChassisCb exit");
Ed Tanous002d39b2022-05-31 08:59:27 -07002525 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002526 // SensorCollection doesn't contain the Redundancy property
2527 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2528 {
2529 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2530 nlohmann::json::array();
2531 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002532 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002533 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2534 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2535 std::move(getChassisCb));
Ed Tanous62598e32023-07-17 17:06:25 -07002536 BMCWEB_LOG_DEBUG("getChassisData exit");
Ed Tanous271584a2019-07-09 16:24:22 -07002537}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002538
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302539/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002540 * @brief Find the requested sensorName in the list of all sensors supplied by
2541 * the chassis node
2542 *
2543 * @param sensorName The sensor name supplied in the PATCH request
2544 * @param sensorsList The list of sensors managed by the chassis node
2545 * @param sensorsModified The list of sensors that were found as a result of
2546 * repeated calls to this function
2547 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002548inline bool
2549 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002550 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002551 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002552{
Nan Zhoufe04d492022-06-22 17:10:41 +00002553 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002554 {
George Liu28aa8de2021-02-01 15:13:30 +08002555 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002556 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002557 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002558 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002559 continue;
2560 }
2561 if (thisSensorName == sensorName)
2562 {
2563 sensorsModified.emplace(chassisSensor);
2564 return true;
2565 }
2566 }
2567 return false;
2568}
2569
Ed Tanousc71d6122022-11-29 14:10:32 -08002570inline std::pair<std::string, std::string>
2571 splitSensorNameAndType(std::string_view sensorId)
2572{
2573 size_t index = sensorId.find('_');
2574 if (index == std::string::npos)
2575 {
2576 return std::make_pair<std::string, std::string>("", "");
2577 }
2578 std::string sensorType{sensorId.substr(0, index)};
2579 std::string sensorName{sensorId.substr(index + 1)};
2580 // fan_pwm and fan_tach need special handling
2581 if (sensorType == "fantach" || sensorType == "fanpwm")
2582 {
2583 sensorType.insert(3, 1, '_');
2584 }
2585 return std::make_pair(sensorType, sensorName);
2586}
2587
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002588/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302589 * @brief Entry point for overriding sensor values of given sensor
2590 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002591 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002592 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002593 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302594 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002595inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002596 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002597 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002598 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302599{
Ed Tanous62598e32023-07-17 17:06:25 -07002600 BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
2601 sensorAsyncResp->chassisSubNode);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302602
Ed Tanous543f4402022-01-06 13:12:53 -08002603 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302604 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302605 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002606 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302607 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302608 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302609 if (collectionItems.first == "Temperatures")
2610 {
2611 propertyValueName = "ReadingCelsius";
2612 }
2613 else if (collectionItems.first == "Fans")
2614 {
2615 propertyValueName = "Reading";
2616 }
2617 else
2618 {
2619 propertyValueName = "ReadingVolts";
2620 }
2621 for (auto& item : collectionItems.second)
2622 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002623 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2624 "MemberId", memberId, propertyValueName,
2625 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302626 {
2627 return;
2628 }
2629 overrideMap.emplace(memberId,
2630 std::make_pair(value, collectionItems.first));
2631 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302632 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002633
Ed Tanous002d39b2022-05-31 08:59:27 -07002634 auto getChassisSensorListCb =
2635 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002636 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002637 // Match sensor names in the PATCH request to those managed by the
2638 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002639 const std::shared_ptr<std::set<std::string>> sensorNames =
2640 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302641 for (const auto& item : overrideMap)
2642 {
2643 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002644 std::pair<std::string, std::string> sensorNameType =
2645 splitSensorNameAndType(sensor);
2646 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2647 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302648 {
Ed Tanous62598e32023-07-17 17:06:25 -07002649 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
zhanghch058d1b46d2021-04-01 11:18:24 +08002650 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302651 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302652 return;
2653 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302654 }
2655 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002656 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002657 [sensorAsyncResp,
2658 overrideMap](const std::set<std::string>& /*connections*/,
2659 const std::set<std::pair<std::string, std::string>>&
2660 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002661 if (objectsWithConnection.size() != overrideMap.size())
2662 {
Ed Tanous62598e32023-07-17 17:06:25 -07002663 BMCWEB_LOG_INFO(
2664 "Unable to find all objects with proper connection {} requested {}",
2665 objectsWithConnection.size(), overrideMap.size());
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002666 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2667 sensorAsyncResp->chassisSubNode ==
2668 sensors::node::thermal
2669 ? "Temperatures"
2670 : "Voltages",
2671 "Count");
2672 return;
2673 }
2674 for (const auto& item : objectsWithConnection)
2675 {
2676 sdbusplus::message::object_path path(item.first);
2677 std::string sensorName = path.filename();
2678 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302679 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002680 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302681 return;
2682 }
Ban Feng3f5eb752023-06-29 09:19:20 +08002683 std::string id = path.parent_path().filename();
Ed Tanous3544d2a2023-08-06 18:12:20 -07002684 auto remove = std::ranges::remove(id, '_');
2685 id.erase(std::ranges::begin(remove), id.end());
Ban Feng3f5eb752023-06-29 09:19:20 +08002686 id += "_";
2687 id += sensorName;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302688
Ban Feng3f5eb752023-06-29 09:19:20 +08002689 const auto& iterator = overrideMap.find(id);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002690 if (iterator == overrideMap.end())
2691 {
Ed Tanous62598e32023-07-17 17:06:25 -07002692 BMCWEB_LOG_INFO("Unable to find sensor object{}",
2693 item.first);
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002694 messages::internalError(sensorAsyncResp->asyncResp->res);
2695 return;
2696 }
George Liu9ae226f2023-06-21 17:56:46 +08002697 sdbusplus::asio::setProperty(
2698 *crow::connections::systemBus, item.second, item.first,
2699 "xyz.openbmc_project.Sensor.Value", "Value",
2700 iterator->second.first,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002701 [sensorAsyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002702 if (ec)
2703 {
2704 if (ec.value() ==
2705 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002706 {
Ed Tanous62598e32023-07-17 17:06:25 -07002707 BMCWEB_LOG_WARNING(
2708 "Manufacturing mode is not Enabled...can't "
2709 "Override the sensor value. ");
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002710
Ed Tanous002d39b2022-05-31 08:59:27 -07002711 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002712 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002713 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002714 }
Ed Tanous62598e32023-07-17 17:06:25 -07002715 BMCWEB_LOG_DEBUG(
2716 "setOverrideValueStatus DBUS error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002717 messages::internalError(
2718 sensorAsyncResp->asyncResp->res);
2719 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002720 });
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002721 }
2722 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302723 // Get object with connection for the given sensor name
2724 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2725 std::move(getObjectsWithConnectionCb));
2726 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302727 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002728 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2729 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2730 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302731}
2732
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002733/**
2734 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2735 * path of the sensor.
2736 *
2737 * Function builds valid Redfish response for sensor query of given chassis and
2738 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2739 * it to caller in a callback.
2740 *
2741 * @param chassis Chassis for which retrieval should be performed
2742 * @param node Node (group) of sensors. See sensors::node for supported values
2743 * @param mapComplete Callback to be called with retrieval result
2744 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002745inline void retrieveUriToDbusMap(const std::string& chassis,
2746 const std::string& node,
2747 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002748{
Ed Tanous02da7c52022-02-27 00:09:02 -08002749 decltype(sensors::paths)::const_iterator pathIt =
2750 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2751 [&node](auto&& val) { return val.first == node; });
2752 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002753 {
Ed Tanous62598e32023-07-17 17:06:25 -07002754 BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
Ed Tanous6804b5c2023-10-31 14:50:03 -07002755 std::map<std::string, std::string> noop;
2756 mapComplete(boost::beast::http::status::bad_request, noop);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002757 return;
2758 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002759
Nan Zhou72374eb2022-01-27 17:06:51 -08002760 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002761 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2762 const boost::beast::http::status status,
2763 const std::map<std::string, std::string>& uriToDbus) {
2764 mapCompleteCb(status, uriToDbus);
2765 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002766
2767 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002768 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002769 getChassisData(resp);
2770}
2771
Nan Zhoubacb2162022-04-06 11:28:32 -07002772namespace sensors
2773{
Nan Zhou928fefb2022-03-28 08:45:00 -07002774
Nan Zhoubacb2162022-04-06 11:28:32 -07002775inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002776 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2777 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002778 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002779{
Ed Tanous62598e32023-07-17 17:06:25 -07002780 BMCWEB_LOG_DEBUG("getChassisCallback enter ");
Nan Zhoubacb2162022-04-06 11:28:32 -07002781
Ed Tanousc1d019a2022-08-06 09:36:06 -07002782 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2783 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002784 {
Ed Tanous62598e32023-07-17 17:06:25 -07002785 BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
Nan Zhoubacb2162022-04-06 11:28:32 -07002786
2787 sdbusplus::message::object_path path(sensor);
2788 std::string sensorName = path.filename();
2789 if (sensorName.empty())
2790 {
Ed Tanous62598e32023-07-17 17:06:25 -07002791 BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002792 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002793 return;
2794 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002795 std::string type = path.parent_path().filename();
2796 // fan_tach has an underscore in it, so remove it to "normalize" the
2797 // type in the URI
Ed Tanous3544d2a2023-08-06 18:12:20 -07002798 auto remove = std::ranges::remove(type, '_');
2799 type.erase(std::ranges::begin(remove), type.end());
Ed Tanousc1d019a2022-08-06 09:36:06 -07002800
Ed Tanous14766872022-03-15 10:44:42 -07002801 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002802 std::string id = type;
2803 id += "_";
2804 id += sensorName;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002805 member["@odata.id"] = boost::urls::format(
2806 "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002807
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002808 entriesArray.emplace_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002809 }
2810
Ed Tanousc1d019a2022-08-06 09:36:06 -07002811 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous62598e32023-07-17 17:06:25 -07002812 BMCWEB_LOG_DEBUG("getChassisCallback exit");
Nan Zhoubacb2162022-04-06 11:28:32 -07002813}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002814
Ed Tanousac106bf2023-06-07 09:24:59 -07002815inline void handleSensorCollectionGet(
2816 App& app, const crow::Request& req,
2817 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2818 const std::string& chassisId)
Nan Zhoude167a62022-06-01 04:47:45 +00002819{
2820 query_param::QueryCapabilities capabilities = {
2821 .canDelegateExpandLevel = 1,
2822 };
2823 query_param::Query delegatedQuery;
Ed Tanousac106bf2023-06-07 09:24:59 -07002824 if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002825 delegatedQuery, capabilities))
2826 {
2827 return;
2828 }
2829
2830 if (delegatedQuery.expandType != query_param::ExpandType::None)
2831 {
2832 // we perform efficient expand.
Ed Tanousac106bf2023-06-07 09:24:59 -07002833 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2834 asyncResp, chassisId, sensors::dbus::sensorPaths,
Nan Zhoude167a62022-06-01 04:47:45 +00002835 sensors::node::sensors,
2836 /*efficientExpand=*/true);
Ed Tanousac106bf2023-06-07 09:24:59 -07002837 getChassisData(sensorsAsyncResp);
Nan Zhoude167a62022-06-01 04:47:45 +00002838
Ed Tanous62598e32023-07-17 17:06:25 -07002839 BMCWEB_LOG_DEBUG(
2840 "SensorCollection doGet exit via efficient expand handler");
Nan Zhoude167a62022-06-01 04:47:45 +00002841 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002842 }
Nan Zhoude167a62022-06-01 04:47:45 +00002843
Nan Zhoude167a62022-06-01 04:47:45 +00002844 // We get all sensors as hyperlinkes in the chassis (this
2845 // implies we reply on the default query parameters handler)
Ed Tanousac106bf2023-06-07 09:24:59 -07002846 getChassis(asyncResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2847 std::bind_front(sensors::getChassisCallback, asyncResp,
2848 chassisId, sensors::node::sensors));
Ed Tanousc1d019a2022-08-06 09:36:06 -07002849}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002850
Ed Tanousc1d019a2022-08-06 09:36:06 -07002851inline void
2852 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2853 const std::string& sensorPath,
2854 const ::dbus::utility::MapperGetObject& mapperResponse)
2855{
2856 if (mapperResponse.size() != 1)
2857 {
2858 messages::internalError(asyncResp->res);
2859 return;
2860 }
2861 const auto& valueIface = *mapperResponse.begin();
2862 const std::string& connectionName = valueIface.first;
Ed Tanous62598e32023-07-17 17:06:25 -07002863 BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
2864 BMCWEB_LOG_DEBUG("Path {}", sensorPath);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002865
2866 sdbusplus::asio::getAllProperties(
2867 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002868 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002869 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002870 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2871 if (ec)
2872 {
2873 messages::internalError(asyncResp->res);
2874 return;
2875 }
2876 sdbusplus::message::object_path path(sensorPath);
2877 std::string name = path.filename();
2878 path = path.parent_path();
2879 std::string type = path.filename();
2880 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2881 asyncResp->res.jsonValue, nullptr);
Patrick Williams5a39f772023-10-20 11:20:21 -05002882 });
Nan Zhoude167a62022-06-01 04:47:45 +00002883}
2884
Nan Zhoue6bd8462022-06-01 04:35:35 +00002885inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002886 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002887 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002888 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002889{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002890 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002891 {
2892 return;
2893 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002894 std::pair<std::string, std::string> nameType =
2895 splitSensorNameAndType(sensorId);
2896 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002897 {
2898 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2899 return;
2900 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002901
Ed Tanousef4c65b2023-04-24 15:28:50 -07002902 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2903 "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002904
Ed Tanous62598e32023-07-17 17:06:25 -07002905 BMCWEB_LOG_DEBUG("Sensor doGet enter");
Nan Zhoue6bd8462022-06-01 04:35:35 +00002906
George Liu2b731192023-01-11 16:27:13 +08002907 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002908 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002909 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2910 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002911 // Get a list of all of the sensors that implement Sensor.Value
2912 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002913 ::dbus::utility::getDbusObject(
2914 sensorPath, interfaces,
Myung Baeaec0ec32023-05-31 15:10:01 -05002915 [asyncResp, sensorId,
George Liu2b731192023-01-11 16:27:13 +08002916 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002917 const ::dbus::utility::MapperGetObject& subtree) {
Ed Tanous62598e32023-07-17 17:06:25 -07002918 BMCWEB_LOG_DEBUG("respHandler1 enter");
Myung Baeaec0ec32023-05-31 15:10:01 -05002919 if (ec == boost::system::errc::io_error)
2920 {
Ed Tanous62598e32023-07-17 17:06:25 -07002921 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
Myung Baeaec0ec32023-05-31 15:10:01 -05002922 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2923 return;
2924 }
Nan Zhoue6bd8462022-06-01 04:35:35 +00002925 if (ec)
2926 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002927 messages::internalError(asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07002928 BMCWEB_LOG_ERROR(
2929 "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002930 return;
2931 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002932 getSensorFromDbus(asyncResp, sensorPath, subtree);
Ed Tanous62598e32023-07-17 17:06:25 -07002933 BMCWEB_LOG_DEBUG("respHandler1 exit");
Patrick Williams5a39f772023-10-20 11:20:21 -05002934 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002935}
2936
Nan Zhoubacb2162022-04-06 11:28:32 -07002937} // namespace sensors
2938
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002939inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002940{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002941 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002942 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002943 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002944 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002945}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002946
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002947inline void requestRoutesSensor(App& app)
2948{
2949 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002950 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002951 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002952 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002953}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002954
Ed Tanous1abe55e2018-09-05 08:30:59 -07002955} // namespace redfish