blob: 59c954b588ed037d872b61dc5af4db09e9c68ae6 [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>
42#include <map>
43#include <set>
George Liu7a1dbc42022-12-07 16:03:22 +080044#include <string_view>
Ed Tanousb5a76932020-09-29 16:16:58 -070045#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080046#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010047
Ed Tanous1abe55e2018-09-05 08:30:59 -070048namespace redfish
49{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010050
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020051namespace sensors
52{
53namespace node
54{
55static constexpr std::string_view power = "Power";
56static constexpr std::string_view sensors = "Sensors";
57static constexpr std::string_view thermal = "Thermal";
58} // namespace node
59
Ed Tanous02da7c52022-02-27 00:09:02 -080060// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020061namespace dbus
62{
Ed Tanouscf9e4172022-12-21 09:30:16 -080063constexpr auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080064 "/xyz/openbmc_project/sensors/voltage",
65 "/xyz/openbmc_project/sensors/power"
66});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000067
Ed Tanouscf9e4172022-12-21 09:30:16 -080068constexpr auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080069 "/xyz/openbmc_project/sensors/power",
70 "/xyz/openbmc_project/sensors/current",
71 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070072 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080073#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080074 "/xyz/openbmc_project/sensors/voltage",
75 "/xyz/openbmc_project/sensors/fan_tach",
76 "/xyz/openbmc_project/sensors/temperature",
77 "/xyz/openbmc_project/sensors/fan_pwm",
78 "/xyz/openbmc_project/sensors/altitude",
79 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080080#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080081 "/xyz/openbmc_project/sensors/utilization"
82});
83
Ed Tanouscf9e4172022-12-21 09:30:16 -080084constexpr auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080085 "/xyz/openbmc_project/sensors/fan_tach",
86 "/xyz/openbmc_project/sensors/temperature",
87 "/xyz/openbmc_project/sensors/fan_pwm"
88});
89
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000090} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080091// clang-format on
92
Ed Tanouscf9e4172022-12-21 09:30:16 -080093using sensorPair =
94 std::pair<std::string_view, std::span<const std::string_view>>;
Ed Tanous02da7c52022-02-27 00:09:02 -080095static constexpr std::array<sensorPair, 3> paths = {
Ed Tanouscf9e4172022-12-21 09:30:16 -080096 {{node::power, dbus::powerPaths},
97 {node::sensors, dbus::sensorPaths},
98 {node::thermal, dbus::thermalPaths}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000099
Ed Tanous0ec8b832022-03-14 14:56:47 -0700100inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000101{
102 if (sensorType == "voltage")
103 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700104 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000105 }
106 if (sensorType == "power")
107 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700108 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000109 }
110 if (sensorType == "current")
111 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700112 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000113 }
114 if (sensorType == "fan_tach")
115 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700116 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000117 }
118 if (sensorType == "temperature")
119 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700120 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000121 }
122 if (sensorType == "fan_pwm" || sensorType == "utilization")
123 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700124 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000125 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600126 if (sensorType == "humidity")
127 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700128 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600129 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000130 if (sensorType == "altitude")
131 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700132 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000133 }
134 if (sensorType == "airflow")
135 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700136 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000137 }
138 if (sensorType == "energy")
139 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700140 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000141 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700142 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000143}
144
Ed Tanous1d7c0052022-08-09 12:32:26 -0700145inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000146{
147 if (sensorType == "voltage")
148 {
149 return "V";
150 }
151 if (sensorType == "power")
152 {
153 return "W";
154 }
155 if (sensorType == "current")
156 {
157 return "A";
158 }
159 if (sensorType == "fan_tach")
160 {
161 return "RPM";
162 }
163 if (sensorType == "temperature")
164 {
165 return "Cel";
166 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600167 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
168 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000169 {
170 return "%";
171 }
172 if (sensorType == "altitude")
173 {
174 return "m";
175 }
176 if (sensorType == "airflow")
177 {
178 return "cft_i/min";
179 }
180 if (sensorType == "energy")
181 {
182 return "J";
183 }
184 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200185}
186} // namespace sensors
187
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100188/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200189 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100190 * Gathers data needed for response processing after async calls are done
191 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700192class SensorsAsyncResp
193{
194 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200195 using DataCompleteCb = std::function<void(
196 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000197 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200198
199 struct SensorData
200 {
201 const std::string name;
202 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200203 const std::string dbusPath;
204 };
205
Ed Tanous8a592812022-06-04 09:06:59 -0700206 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800207 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800208 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800209 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700210 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700211 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
212 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500213 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200214
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200215 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700216 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800217 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800218 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800219 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200220 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700221 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700222 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
223 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200224 dataComplete{std::move(creationComplete)}
225 {}
226
Nan Zhou928fefb2022-03-28 08:45:00 -0700227 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700228 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700229 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800230 std::span<const std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700231 const std::string_view& subNode, bool efficientExpandIn) :
232 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700233 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700234 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700235 {}
236
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237 ~SensorsAsyncResp()
238 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800239 if (asyncResp->res.result() ==
240 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241 {
242 // Reset the json object to clear out any data that made it in
243 // before the error happened todo(ed) handle error condition with
244 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800245 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200247
248 if (dataComplete && metadata)
249 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000250 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800251 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200252 {
253 for (auto& sensor : *metadata)
254 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700255 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200256 }
257 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800258 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200259 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700260 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100261
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800262 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
263 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
264 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
265 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
266
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200267 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700268 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200269 {
270 if (metadata)
271 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700272 metadata->emplace_back(SensorData{
273 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200274 }
275 }
276
277 void updateUri(const std::string& name, const std::string& uri)
278 {
279 if (metadata)
280 {
281 for (auto& sensor : *metadata)
282 {
283 if (sensor.name == name)
284 {
285 sensor.uri = uri;
286 }
287 }
288 }
289 }
290
zhanghch058d1b46d2021-04-01 11:18:24 +0800291 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200292 const std::string chassisId;
Ed Tanouscf9e4172022-12-21 09:30:16 -0800293 const std::span<const std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200294 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700295 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200296
297 private:
298 std::optional<std::vector<SensorData>> metadata;
299 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100300};
301
302/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500303 * Possible states for physical inventory leds
304 */
305enum class LedState
306{
307 OFF,
308 ON,
309 BLINK,
310 UNKNOWN
311};
312
313/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500314 * D-Bus inventory item associated with one or more sensors.
315 */
316class InventoryItem
317{
318 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700319 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500320 {
321 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800322 sdbusplus::message::object_path path(objectPath);
323 name = path.filename();
324 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500325 {
George Liu28aa8de2021-02-01 15:13:30 +0800326 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500327 }
328 }
329
330 std::string objectPath;
331 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800332 bool isPresent = true;
333 bool isFunctional = true;
334 bool isPowerSupply = false;
335 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500336 std::string manufacturer;
337 std::string model;
338 std::string partNumber;
339 std::string serialNumber;
340 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500341 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800342 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500343};
344
345/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530346 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200347 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100348 * @param sensorNames Sensors retrieved from chassis
349 * @param callback Callback for processing gathered connections
350 */
351template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530352void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000353 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000354 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530355 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700356{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530357 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700358 const std::string path = "/xyz/openbmc_project/sensors";
George Liue99073f2022-12-09 11:06:16 +0800359 constexpr std::array<std::string_view, 1> interfaces = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700360 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100361
George Liue99073f2022-12-09 11:06:16 +0800362 // Make call to ObjectMapper to find all sensors objects
363 dbus::utility::getSubTree(
364 path, 2, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700365 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
George Liue99073f2022-12-09 11:06:16 +0800366 sensorNames](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700367 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +0800368 // Response handler for parsing objects subtree
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530369 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700370 if (ec)
371 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800372 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530373 BMCWEB_LOG_ERROR
374 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700375 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100376 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100377
Ed Tanous1abe55e2018-09-05 08:30:59 -0700378 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
379
380 // Make unique list of connections only for requested sensor types and
381 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000382 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530383 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700384
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700385 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
386 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700387 {
388 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
389 }
390
391 for (const std::pair<
392 std::string,
393 std::vector<std::pair<std::string, std::vector<std::string>>>>&
394 object : subtree)
395 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700396 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700398 for (const std::pair<std::string, std::vector<std::string>>&
399 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700400 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700401 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
402 connections.insert(objData.first);
403 objectsWithConnection.insert(
404 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700405 }
406 }
407 }
408 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530409 callback(std::move(connections), std::move(objectsWithConnection));
410 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
George Liue99073f2022-12-09 11:06:16 +0800411 });
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530412 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
413}
414
415/**
416 * @brief Create connections necessary for sensors
417 * @param SensorsAsyncResp Pointer to object holding response data
418 * @param sensorNames Sensors retrieved from chassis
419 * @param callback Callback for processing gathered connections
420 */
421template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000422void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
423 const std::shared_ptr<std::set<std::string>> sensorNames,
424 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530425{
426 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000427 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530428 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700429 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000430 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530431 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100432}
433
434/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700435 * @brief Shrinks the list of sensors for processing
436 * @param SensorsAysncResp The class holding the Redfish response
437 * @param allSensors A list of all the sensors associated to the
438 * chassis element (i.e. baseboard, front panel, etc...)
439 * @param activeSensors A list that is a reduction of the incoming
440 * allSensors list. Eliminate Thermal sensors when a Power request is
441 * made, and eliminate Power sensors when a Thermal request is made.
442 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000443inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700444 crow::Response& res, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800445 std::span<const std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700446 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000447 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700448{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700449 if ((allSensors == nullptr) || (activeSensors == nullptr))
450 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700451 messages::resourceNotFound(res, chassisSubNode,
452 chassisSubNode == sensors::node::thermal
453 ? "Temperatures"
454 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700455
456 return;
457 }
458 if (allSensors->empty())
459 {
460 // Nothing to do, the activeSensors object is also empty
461 return;
462 }
463
Ed Tanous7f1cc262022-08-09 13:33:57 -0700464 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700465 {
466 for (const std::string& sensor : *allSensors)
467 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700468 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700469 {
470 activeSensors->emplace(sensor);
471 }
472 }
473 }
474}
475
Ed Tanous7f1cc262022-08-09 13:33:57 -0700476/*
477 *Populates the top level collection for a given subnode. Populates
478 *SensorCollection, Power, or Thermal schemas.
479 *
480 * */
481inline void populateChassisNode(nlohmann::json& jsonValue,
482 std::string_view chassisSubNode)
483{
484 if (chassisSubNode == sensors::node::power)
485 {
486 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
487 }
488 else if (chassisSubNode == sensors::node::thermal)
489 {
490 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
491 jsonValue["Fans"] = nlohmann::json::array();
492 jsonValue["Temperatures"] = nlohmann::json::array();
493 }
494 else if (chassisSubNode == sensors::node::sensors)
495 {
496 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
497 jsonValue["Description"] = "Collection of Sensors for this Chassis";
498 jsonValue["Members"] = nlohmann::json::array();
499 jsonValue["Members@odata.count"] = 0;
500 }
501
502 if (chassisSubNode != sensors::node::sensors)
503 {
504 jsonValue["Id"] = chassisSubNode;
505 }
506 jsonValue["Name"] = chassisSubNode;
507}
508
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700509/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100510 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200511 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100512 * @param callback Callback for next step in gathered sensor processing
513 */
514template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700515void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
516 std::string_view chassisId, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800517 std::span<const std::string_view> sensorTypes,
518 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700519{
520 BMCWEB_LOG_DEBUG << "getChassis enter";
George Liu7a1dbc42022-12-07 16:03:22 +0800521 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700522 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500523 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800524
525 // Get the Chassis Collection
526 dbus::utility::getSubTreePaths(
527 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700528 [callback{std::forward<Callback>(callback)}, asyncResp,
529 chassisIdStr{std::string(chassisId)},
530 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800531 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700532 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700533 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
534 if (ec)
535 {
536 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700537 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700538 return;
539 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700540 const std::string* chassisPath = nullptr;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542 {
George Liu28aa8de2021-02-01 15:13:30 +0800543 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700544 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800545 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700547 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 continue;
549 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700550 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700551 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700552 chassisPath = &chassis;
553 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700554 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700555 }
556 if (chassisPath == nullptr)
557 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700558 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700559 return;
560 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700561 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700562
Ed Tanousef4c65b2023-04-24 15:28:50 -0700563 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
564 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500565
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500566 // Get the list of all sensors for this Chassis element
567 std::string sensorPath = *chassisPath + "/all_sensors";
George Liu6c3e9452023-03-03 13:55:29 +0800568 dbus::utility::getAssociationEndPoints(
569 sensorPath,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700570 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800571 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700572 const boost::system::error_code& e,
George Liu6c3e9452023-03-03 13:55:29 +0800573 const dbus::utility::MapperEndPoints& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 if (e)
575 {
576 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700577 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700578 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700580 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000582 const std::shared_ptr<std::set<std::string>> culledSensorList =
583 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700584 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
585 &nodeSensorList, culledSensorList);
586 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700587 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700588 });
George Liu7a1dbc42022-12-07 16:03:22 +0800589 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700590 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100591}
592
593/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500594 * @brief Returns the Redfish State value for the specified inventory item.
595 * @param inventoryItem D-Bus inventory item associated with a sensor.
596 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700597 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000598inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500599{
600 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
601 {
602 return "Absent";
603 }
James Feist34dd1792019-05-17 14:10:54 -0700604
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500605 return "Enabled";
606}
607
608/**
609 * @brief Returns the Redfish Health value for the specified sensor.
610 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700611 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500612 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
613 * be nullptr if no associated inventory item was found.
614 * @return Health value for sensor.
615 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700616inline std::string getHealth(nlohmann::json& sensorJson,
617 const dbus::utility::DBusPropertiesMap& valuesDict,
618 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700619{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500620 // Get current health value (if any) in the sensor JSON object. Some JSON
621 // objects contain multiple sensors (such as PowerSupplies). We want to set
622 // the overall health to be the most severe of any of the sensors.
623 std::string currentHealth;
624 auto statusIt = sensorJson.find("Status");
625 if (statusIt != sensorJson.end())
626 {
627 auto healthIt = statusIt->find("Health");
628 if (healthIt != statusIt->end())
629 {
630 std::string* health = healthIt->get_ptr<std::string*>();
631 if (health != nullptr)
632 {
633 currentHealth = *health;
634 }
635 }
636 }
637
638 // If current health in JSON object is already Critical, return that. This
639 // should override the sensor health, which might be less severe.
640 if (currentHealth == "Critical")
641 {
642 return "Critical";
643 }
644
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200645 const bool* criticalAlarmHigh = nullptr;
646 const bool* criticalAlarmLow = nullptr;
647 const bool* warningAlarmHigh = nullptr;
648 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800649
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200650 const bool success = sdbusplus::unpackPropertiesNoThrow(
651 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
652 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
653 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
654 warningAlarmLow);
655
656 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700657 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200658 // Check if sensor has critical threshold alarm
659 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
660 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700661 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200662 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700663 }
664 }
665
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500666 // Check if associated inventory item is not functional
667 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
668 {
669 return "Critical";
670 }
671
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200672 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500673 // should override the sensor status, which might be less severe.
674 if (currentHealth == "Warning")
675 {
676 return "Warning";
677 }
678
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200679 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700680 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200681 // Check if sensor has warning threshold alarm
682 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
683 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700684 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200685 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700686 }
687 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500688
James Feist34dd1792019-05-17 14:10:54 -0700689 return "OK";
690}
691
Ed Tanous23a21a12020-07-25 04:45:05 +0000692inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500693 const InventoryItem* inventoryItem)
694{
695 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
696 {
697 switch (inventoryItem->ledState)
698 {
699 case LedState::OFF:
700 sensorJson["IndicatorLED"] = "Off";
701 break;
702 case LedState::ON:
703 sensorJson["IndicatorLED"] = "Lit";
704 break;
705 case LedState::BLINK:
706 sensorJson["IndicatorLED"] = "Blinking";
707 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000708 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500709 break;
710 }
711 }
712}
713
James Feist34dd1792019-05-17 14:10:54 -0700714/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100715 * @brief Builds a json sensor representation of a sensor.
716 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500717 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100718 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700719 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
720 * @param propertiesDict A dictionary of the properties to build the sensor
721 * from.
722 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500723 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
724 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100725 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700726inline void objectPropertiesToJson(
727 std::string_view sensorName, std::string_view sensorType,
728 std::string_view chassisSubNode,
729 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000730 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700731{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700732 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500733 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800734 std::string subNodeEscaped(sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700735 subNodeEscaped.erase(
736 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
737 subNodeEscaped.end());
738
739 // For sensors in SensorCollection we set Id instead of MemberId,
740 // including power sensors.
741 subNodeEscaped += '_';
742 subNodeEscaped += sensorName;
743 sensorJson["Id"] = std::move(subNodeEscaped);
744
Ed Tanous1d7c0052022-08-09 12:32:26 -0700745 std::string sensorNameEs(sensorName);
746 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
747 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500748 }
749 else if (sensorType != "power")
750 {
751 // Set MemberId and Name for non-power sensors. For PowerSupplies and
752 // PowerControl, those properties have more general values because
753 // multiple sensors can be stored in the same JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700754 std::string sensorNameEs(sensorName);
755 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
756 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500757 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700758
Ed Tanous81ce6092020-12-17 16:54:55 +0000759 sensorJson["Status"]["State"] = getState(inventoryItem);
Patrick Williams89492a12023-05-10 07:51:34 -0500760 sensorJson["Status"]["Health"] = getHealth(sensorJson, propertiesDict,
761 inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700762
763 // Parameter to set to override the type we get from dbus, and force it to
764 // int, regardless of what is available. This is used for schemas like fan,
765 // that require integers, not floats.
766 bool forceToInt = false;
767
Anthony Wilson3929aca2019-07-19 15:42:33 -0500768 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700769 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500770 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000771 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000772
Ed Tanous0ec8b832022-03-14 14:56:47 -0700773 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
774 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500775 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000776 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
777 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500778 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000779 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500780 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000781 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500782 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000783
Ed Tanous1d7c0052022-08-09 12:32:26 -0700784 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000785 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200786 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000787 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
788 << sensorType;
789 }
790 else
791 {
792 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200793 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500794 }
795 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700796 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500797 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000798 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700799 // TODO(ed) Documentation says that path should be type fan_tach,
800 // implementation seems to implement fan
801 }
802 else if (sensorType == "fan" || sensorType == "fan_tach")
803 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500804 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000805 sensorJson["ReadingUnits"] = "RPM";
806 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
807 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700808 forceToInt = true;
809 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700810 else if (sensorType == "fan_pwm")
811 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500812 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000813 sensorJson["ReadingUnits"] = "Percent";
814 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
815 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700816 forceToInt = true;
817 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700818 else if (sensorType == "voltage")
819 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500820 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000821 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700822 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700823 else if (sensorType == "power")
824 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700825 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000826 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000827 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500828 // Put multiple "sensors" into a single PowerControl, so have
829 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000830 sensorJson["MemberId"] = "0";
831 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500832 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000833 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700834 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700835 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500836 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700837 }
838 else
839 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500840 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700841 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700842 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700843 else
844 {
845 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
846 return;
847 }
848 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500849 std::vector<
850 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
851 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852 properties.reserve(7);
853
854 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600855
Ed Tanous1d7c0052022-08-09 12:32:26 -0700856 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500857 {
858 properties.emplace_back(
859 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
860 "/Thresholds/UpperCaution/Reading"_json_pointer);
861 properties.emplace_back(
862 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
863 "/Thresholds/LowerCaution/Reading"_json_pointer);
864 properties.emplace_back(
865 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
866 "/Thresholds/UpperCritical/Reading"_json_pointer);
867 properties.emplace_back(
868 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
869 "/Thresholds/LowerCritical/Reading"_json_pointer);
870 }
871 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600872 {
873 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500874 "WarningHigh",
875 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600876 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500877 "WarningLow",
878 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600879 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500880 "CriticalHigh",
881 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600882 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500883 "CriticalLow",
884 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600885 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700886
Ed Tanous2474adf2018-09-05 16:31:16 -0700887 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
888
Ed Tanous1d7c0052022-08-09 12:32:26 -0700889 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500890 {
891 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500892 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500893 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500894 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800895 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
896 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500897 }
898 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700899 {
900 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500901 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700902 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500903 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500905 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700906 {
907 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500908 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500910 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700911 }
912
Anthony Wilson3929aca2019-07-19 15:42:33 -0500913 for (const std::tuple<const char*, const char*,
914 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700915 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700916 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700918 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800920 continue;
921 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700922
923 // The property we want to set may be nested json, so use
924 // a json_pointer for easy indexing into the json structure.
925 const nlohmann::json::json_pointer& key = std::get<2>(p);
926
Ed Tanous1d7c0052022-08-09 12:32:26 -0700927 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700928 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800929 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700930 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700931 continue;
932 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700933 if (forceToInt)
934 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700935 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700936 }
937 else
938 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700939 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940 }
941 }
942 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100943}
944
Ed Tanous1d7c0052022-08-09 12:32:26 -0700945/**
946 * @brief Builds a json sensor representation of a sensor.
947 * @param sensorName The name of the sensor to be built
948 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
949 * build
950 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
951 * @param interfacesDict A dictionary of the interfaces and properties of said
952 * interfaces to be built from
953 * @param sensorJson The json object to fill
954 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
955 * be nullptr if no associated inventory item was found.
956 */
957inline void objectInterfacesToJson(
958 const std::string& sensorName, const std::string& sensorType,
959 const std::string& chassisSubNode,
960 const dbus::utility::DBusInteracesMap& interfacesDict,
961 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
962{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700963 for (const auto& [interface, valuesDict] : interfacesDict)
964 {
965 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
966 valuesDict, sensorJson, inventoryItem);
967 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700968 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700969}
970
Ed Tanousb5a76932020-09-29 16:16:58 -0700971inline void populateFanRedundancy(
972 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700973{
George Liue99073f2022-12-09 11:06:16 +0800974 constexpr std::array<std::string_view, 1> interfaces = {
975 "xyz.openbmc_project.Control.FanRedundancy"};
976 dbus::utility::getSubTree(
977 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800978 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +0800979 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800980 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700981 if (ec)
982 {
983 return; // don't have to have this interface
984 }
George Liu6c3e9452023-03-03 13:55:29 +0800985 for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
Ed Tanous002d39b2022-05-31 08:59:27 -0700986 pathPair : resp)
987 {
988 const std::string& path = pathPair.first;
George Liu6c3e9452023-03-03 13:55:29 +0800989 const dbus::utility::MapperServiceMap& objDict = pathPair.second;
Ed Tanous002d39b2022-05-31 08:59:27 -0700990 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700991 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700992 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700993 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700994
995 const std::string& owner = objDict.begin()->first;
George Liu6c3e9452023-03-03 13:55:29 +0800996 dbus::utility::getAssociationEndPoints(
997 path + "/chassis",
998 [path, owner, sensorsAsyncResp](
999 const boost::system::error_code& e,
1000 const dbus::utility::MapperEndPoints& endpoints) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001001 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001002 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001003 return; // if they don't have an association we
1004 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001005 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001006 auto found =
1007 std::find_if(endpoints.begin(), endpoints.end(),
1008 [sensorsAsyncResp](const std::string& entry) {
1009 return entry.find(sensorsAsyncResp->chassisId) !=
1010 std::string::npos;
1011 });
James Feist8bd25cc2019-03-15 15:14:00 -07001012
Ed Tanous002d39b2022-05-31 08:59:27 -07001013 if (found == endpoints.end())
1014 {
1015 return;
1016 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001017 sdbusplus::asio::getAllProperties(
1018 *crow::connections::systemBus, owner, path,
1019 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001020 [path, sensorsAsyncResp](
1021 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001022 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001023 if (err)
1024 {
1025 return; // don't have to have this
1026 // interface
1027 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001028
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001029 const uint8_t* allowedFailures = nullptr;
1030 const std::vector<std::string>* collection = nullptr;
1031 const std::string* status = nullptr;
1032
1033 const bool success = sdbusplus::unpackPropertiesNoThrow(
1034 dbus_utils::UnpackErrorPrinter(), ret,
1035 "AllowedFailures", allowedFailures, "Collection",
1036 collection, "Status", status);
1037
1038 if (!success)
1039 {
1040 messages::internalError(
1041 sensorsAsyncResp->asyncResp->res);
1042 return;
1043 }
1044
1045 if (allowedFailures == nullptr || collection == nullptr ||
1046 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001047 {
1048 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1049 messages::internalError(
1050 sensorsAsyncResp->asyncResp->res);
1051 return;
1052 }
1053
Ed Tanous002d39b2022-05-31 08:59:27 -07001054 sdbusplus::message::object_path objectPath(path);
1055 std::string name = objectPath.filename();
1056 if (name.empty())
1057 {
1058 // this should be impossible
1059 messages::internalError(
1060 sensorsAsyncResp->asyncResp->res);
1061 return;
1062 }
1063 std::replace(name.begin(), name.end(), '_', ' ');
1064
1065 std::string health;
1066
Ed Tanous11ba3972022-07-11 09:50:41 -07001067 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001068 {
1069 health = "OK";
1070 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001071 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001072 {
1073 health = "Warning";
1074 }
1075 else
1076 {
1077 health = "Critical";
1078 }
1079 nlohmann::json::array_t redfishCollection;
1080 const auto& fanRedfish =
1081 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1082 for (const std::string& item : *collection)
1083 {
Ed Tanous8a592812022-06-04 09:06:59 -07001084 sdbusplus::message::object_path itemPath(item);
1085 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001086 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001087 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001088 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001089 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001090 /*
1091 todo(ed): merge patch that fixes the names
1092 std::replace(itemName.begin(),
1093 itemName.end(), '_', ' ');*/
1094 auto schemaItem =
1095 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1096 [itemName](const nlohmann::json& fan) {
George Liu3e35c762023-03-08 16:56:38 +08001097 return fan["Name"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001098 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001099 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001100 {
Ed Tanous8a592812022-06-04 09:06:59 -07001101 nlohmann::json::object_t collectionId;
1102 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001103 (*schemaItem)["@odata.id"];
1104 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001105 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001106 }
1107 else
1108 {
1109 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1110 messages::internalError(
1111 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001112 return;
1113 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001114 }
James Feist8bd25cc2019-03-15 15:14:00 -07001115
Patrick Williams89492a12023-05-10 07:51:34 -05001116 size_t minNumNeeded = collection->empty()
1117 ? 0
1118 : collection->size() -
1119 *allowedFailures;
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1121 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001122
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 nlohmann::json::object_t redundancy;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001124 boost::urls::url url =
1125 boost::urls::format("/redfish/v1/Chassis/{}/{}",
1126 sensorsAsyncResp->chassisId,
1127 sensorsAsyncResp->chassisSubNode);
Willy Tueddfc432022-09-26 16:46:38 +00001128 url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1129 .to_string());
1130 redundancy["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -07001131 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1132 redundancy["MinNumNeeded"] = minNumNeeded;
Ed Tanous002d39b2022-05-31 08:59:27 -07001133 redundancy["Mode"] = "N+m";
1134 redundancy["Name"] = name;
1135 redundancy["RedundancySet"] = redfishCollection;
1136 redundancy["Status"]["Health"] = health;
1137 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001138
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001139 jResp.emplace_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001140 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001141 });
1142 }
George Liue99073f2022-12-09 11:06:16 +08001143 });
James Feist8bd25cc2019-03-15 15:14:00 -07001144}
1145
Ed Tanousb5a76932020-09-29 16:16:58 -07001146inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001147 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001148{
zhanghch058d1b46d2021-04-01 11:18:24 +08001149 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001150 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001151 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001152 {
1153 sensorHeaders = {"Voltages", "PowerSupplies"};
1154 }
1155 for (const std::string& sensorGroup : sensorHeaders)
1156 {
1157 nlohmann::json::iterator entry = response.find(sensorGroup);
1158 if (entry != response.end())
1159 {
1160 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001161 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001162 return c1["Name"] < c2["Name"];
1163 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001164
1165 // add the index counts to the end of each entry
1166 size_t count = 0;
1167 for (nlohmann::json& sensorJson : *entry)
1168 {
1169 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1170 if (odata == sensorJson.end())
1171 {
1172 continue;
1173 }
1174 std::string* value = odata->get_ptr<std::string*>();
1175 if (value != nullptr)
1176 {
Willy Tueddfc432022-09-26 16:46:38 +00001177 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001178 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001179 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001180 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001181 }
1182 }
1183 }
1184 }
1185}
1186
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001187/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001188 * @brief Finds the inventory item with the specified object path.
1189 * @param inventoryItems D-Bus inventory items associated with sensors.
1190 * @param invItemObjPath D-Bus object path of inventory item.
1191 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001192 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001193inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001194 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001195 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001196{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001197 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001198 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001199 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001200 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001201 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001202 }
1203 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001204 return nullptr;
1205}
1206
1207/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001208 * @brief Finds the inventory item associated with the specified sensor.
1209 * @param inventoryItems D-Bus inventory items associated with sensors.
1210 * @param sensorObjPath D-Bus object path of sensor.
1211 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001212 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001213inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001214 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001215 const std::string& sensorObjPath)
1216{
1217 for (InventoryItem& inventoryItem : *inventoryItems)
1218 {
1219 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1220 {
1221 return &inventoryItem;
1222 }
1223 }
1224 return nullptr;
1225}
1226
1227/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001228 * @brief Finds the inventory item associated with the specified led path.
1229 * @param inventoryItems D-Bus inventory items associated with sensors.
1230 * @param ledObjPath D-Bus object path of led.
1231 * @return Inventory item within vector, or nullptr if no match found.
1232 */
1233inline InventoryItem*
1234 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1235 const std::string& ledObjPath)
1236{
1237 for (InventoryItem& inventoryItem : inventoryItems)
1238 {
1239 if (inventoryItem.ledObjectPath == ledObjPath)
1240 {
1241 return &inventoryItem;
1242 }
1243 }
1244 return nullptr;
1245}
1246
1247/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001248 * @brief Adds inventory item and associated sensor to specified vector.
1249 *
1250 * Adds a new InventoryItem to the vector if necessary. Searches for an
1251 * existing InventoryItem with the specified object path. If not found, one is
1252 * added to the vector.
1253 *
1254 * Next, the specified sensor is added to the set of sensors associated with the
1255 * InventoryItem.
1256 *
1257 * @param inventoryItems D-Bus inventory items associated with sensors.
1258 * @param invItemObjPath D-Bus object path of inventory item.
1259 * @param sensorObjPath D-Bus object path of sensor
1260 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001261inline void addInventoryItem(
1262 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1263 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001264{
1265 // Look for inventory item in vector
Patrick Williams89492a12023-05-10 07:51:34 -05001266 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1267 invItemObjPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001268
1269 // If inventory item doesn't exist in vector, add it
1270 if (inventoryItem == nullptr)
1271 {
1272 inventoryItems->emplace_back(invItemObjPath);
1273 inventoryItem = &(inventoryItems->back());
1274 }
1275
1276 // Add sensor to set of sensors associated with inventory item
1277 inventoryItem->sensors.emplace(sensorObjPath);
1278}
1279
1280/**
1281 * @brief Stores D-Bus data in the specified inventory item.
1282 *
1283 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1284 * specified InventoryItem.
1285 *
1286 * This data is later used to provide sensor property values in the JSON
1287 * response.
1288 *
1289 * @param inventoryItem Inventory item where data will be stored.
1290 * @param interfacesDict Map containing D-Bus interfaces and their properties
1291 * for the specified inventory item.
1292 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001293inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001294 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001295 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001296{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001297 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001298
Ed Tanous9eb808c2022-01-25 10:19:23 -08001299 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001300 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001301 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001302 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001303 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001304 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001305 if (name == "Present")
1306 {
1307 const bool* value = std::get_if<bool>(&dbusValue);
1308 if (value != nullptr)
1309 {
1310 inventoryItem.isPresent = *value;
1311 }
1312 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001313 }
1314 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001315 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001316
Ed Tanous711ac7a2021-12-20 09:34:41 -08001317 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001318 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001319 inventoryItem.isPowerSupply = true;
1320 }
1321
1322 // Get properties from Inventory.Decorator.Asset interface
1323 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1324 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001325 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001326 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001327 if (name == "Manufacturer")
1328 {
1329 const std::string* value =
1330 std::get_if<std::string>(&dbusValue);
1331 if (value != nullptr)
1332 {
1333 inventoryItem.manufacturer = *value;
1334 }
1335 }
1336 if (name == "Model")
1337 {
1338 const std::string* value =
1339 std::get_if<std::string>(&dbusValue);
1340 if (value != nullptr)
1341 {
1342 inventoryItem.model = *value;
1343 }
1344 }
1345 if (name == "SerialNumber")
1346 {
1347 const std::string* value =
1348 std::get_if<std::string>(&dbusValue);
1349 if (value != nullptr)
1350 {
1351 inventoryItem.serialNumber = *value;
1352 }
1353 }
1354 if (name == "PartNumber")
1355 {
1356 const std::string* value =
1357 std::get_if<std::string>(&dbusValue);
1358 if (value != nullptr)
1359 {
1360 inventoryItem.partNumber = *value;
1361 }
1362 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001363 }
1364 }
1365
Ed Tanous711ac7a2021-12-20 09:34:41 -08001366 if (interface ==
1367 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001368 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001369 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001370 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001371 if (name == "Functional")
1372 {
1373 const bool* value = std::get_if<bool>(&dbusValue);
1374 if (value != nullptr)
1375 {
1376 inventoryItem.isFunctional = *value;
1377 }
1378 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001379 }
1380 }
1381 }
1382}
1383
1384/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001385 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001386 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001387 * Uses the specified connections (services) to obtain D-Bus data for inventory
1388 * items associated with sensors. Stores the resulting data in the
1389 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001390 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001391 * This data is later used to provide sensor property values in the JSON
1392 * response.
1393 *
1394 * Finds the inventory item data asynchronously. Invokes callback when data has
1395 * been obtained.
1396 *
1397 * The callback must have the following signature:
1398 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001399 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001400 * @endcode
1401 *
1402 * This function is called recursively, obtaining data asynchronously from one
1403 * connection in each call. This ensures the callback is not invoked until the
1404 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001405 *
1406 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001407 * @param inventoryItems D-Bus inventory items associated with sensors.
1408 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001409 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001410 * @param callback Callback to invoke when inventory data has been obtained.
1411 * @param invConnectionsIndex Current index in invConnections. Only specified
1412 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001413 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001414template <typename Callback>
1415static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001416 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001417 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001418 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1419 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001420{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001422
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001423 // If no more connections left, call callback
1424 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001425 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001426 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001427 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1428 return;
1429 }
1430
1431 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001432 auto it = invConnections->begin();
1433 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001434 if (it != invConnections->end())
1435 {
1436 const std::string& invConnection = *it;
1437
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001438 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001439 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1440 callback{std::forward<Callback>(callback)},
1441 invConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001442 const boost::system::error_code& ec,
Ed Tanousd0090732022-10-04 17:22:56 -07001443 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001444 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001445 if (ec)
1446 {
1447 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001448 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001449 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001450 return;
1451 }
1452
1453 // Loop through returned object paths
1454 for (const auto& objDictEntry : resp)
1455 {
1456 const std::string& objPath =
1457 static_cast<const std::string&>(objDictEntry.first);
1458
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001459 // If this object path is one of the specified inventory items
Patrick Williams89492a12023-05-10 07:51:34 -05001460 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1461 objPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001462 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001463 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001464 // Store inventory data in InventoryItem
1465 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001466 }
1467 }
1468
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001469 // Recurse to get inventory item data from next connection
1470 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001471 invConnections, std::move(callback),
1472 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001473
1474 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001475 };
1476
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001477 // Get all object paths and their interfaces for current connection
1478 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001479 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001480 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001481 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1482 }
1483
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001484 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001485}
1486
1487/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001488 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001489 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001490 * Gets the D-Bus connections (services) that provide data for the inventory
1491 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001492 *
1493 * Finds the connections asynchronously. Invokes callback when information has
1494 * been obtained.
1495 *
1496 * The callback must have the following signature:
1497 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001498 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001499 * @endcode
1500 *
1501 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001502 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001503 * @param callback Callback to invoke when connections have been obtained.
1504 */
1505template <typename Callback>
1506static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001507 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1508 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001509 Callback&& callback)
1510{
1511 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1512
1513 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001514 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001515 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001516 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1517 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001518 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1519
George Liue99073f2022-12-09 11:06:16 +08001520 // Make call to ObjectMapper to find all inventory items
1521 dbus::utility::getSubTree(
1522 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001523 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1524 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001525 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001526 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001527 // Response handler for parsing output from GetSubTree
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001528 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1529 if (ec)
1530 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001531 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001532 BMCWEB_LOG_ERROR
1533 << "getInventoryItemsConnections respHandler DBus error " << ec;
1534 return;
1535 }
1536
1537 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001538 std::shared_ptr<std::set<std::string>> invConnections =
1539 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001540
1541 // Loop through objects from GetSubTree
1542 for (const std::pair<
1543 std::string,
1544 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1545 object : subtree)
1546 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001547 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001548 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001549 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001550 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001551 // Store all connections to inventory item
1552 for (const std::pair<std::string, std::vector<std::string>>&
1553 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001555 const std::string& invConnection = objData.first;
1556 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001557 }
1558 }
1559 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001560
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001561 callback(invConnections);
1562 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001563 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001564 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1565}
1566
1567/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001568 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001569 *
1570 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001571 * inventory items. Then finds the associations from those inventory items to
1572 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001573 *
1574 * Finds the inventory items asynchronously. Invokes callback when information
1575 * has been obtained.
1576 *
1577 * The callback must have the following signature:
1578 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001580 * @endcode
1581 *
1582 * @param sensorsAsyncResp Pointer to object holding response data.
1583 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001584 * implements ObjectManager.
1585 * @param callback Callback to invoke when inventory items have been obtained.
1586 */
1587template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001588static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001589 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001590 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001591 Callback&& callback)
1592{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001593 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001594
1595 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001596 auto respHandler =
1597 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001598 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001599 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001600 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001601 if (ec)
1602 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001603 BMCWEB_LOG_ERROR
1604 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001605 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001606 return;
1607 }
1608
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001609 // Create vector to hold list of inventory items
1610 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1611 std::make_shared<std::vector<InventoryItem>>();
1612
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001614 std::string sensorAssocPath;
1615 sensorAssocPath.reserve(128); // avoid memory allocations
1616 for (const auto& objDictEntry : resp)
1617 {
1618 const std::string& objPath =
1619 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001620
1621 // If path is inventory association for one of the specified sensors
1622 for (const std::string& sensorName : *sensorNames)
1623 {
1624 sensorAssocPath = sensorName;
1625 sensorAssocPath += "/inventory";
1626 if (objPath == sensorAssocPath)
1627 {
1628 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001629 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001630 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001631 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001632 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001633 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001634 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001635 if (valueName == "endpoints")
1636 {
1637 const std::vector<std::string>* endpoints =
1638 std::get_if<std::vector<std::string>>(
1639 &value);
1640 if ((endpoints != nullptr) &&
1641 !endpoints->empty())
1642 {
1643 // Add inventory item to vector
1644 const std::string& invItemPath =
1645 endpoints->front();
1646 addInventoryItem(inventoryItems,
1647 invItemPath,
1648 sensorName);
1649 }
1650 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001651 }
1652 }
1653 }
1654 break;
1655 }
1656 }
1657 }
1658
Anthony Wilsond5005492019-07-31 16:34:17 -05001659 // Now loop through the returned object paths again, this time to
1660 // find the leds associated with the inventory items we just found
1661 std::string inventoryAssocPath;
1662 inventoryAssocPath.reserve(128); // avoid memory allocations
1663 for (const auto& objDictEntry : resp)
1664 {
1665 const std::string& objPath =
1666 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001667
1668 for (InventoryItem& inventoryItem : *inventoryItems)
1669 {
1670 inventoryAssocPath = inventoryItem.objectPath;
1671 inventoryAssocPath += "/leds";
1672 if (objPath == inventoryAssocPath)
1673 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001674 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001675 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001676 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001677 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001678 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001679 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001680 if (valueName == "endpoints")
1681 {
1682 const std::vector<std::string>* endpoints =
1683 std::get_if<std::vector<std::string>>(
1684 &value);
1685 if ((endpoints != nullptr) &&
1686 !endpoints->empty())
1687 {
1688 // Add inventory item to vector
1689 // Store LED path in inventory item
1690 const std::string& ledPath =
1691 endpoints->front();
1692 inventoryItem.ledObjectPath = ledPath;
1693 }
1694 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001695 }
1696 }
1697 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001698
Anthony Wilsond5005492019-07-31 16:34:17 -05001699 break;
1700 }
1701 }
1702 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001703 callback(inventoryItems);
1704 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001705 };
1706
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001707 // Call GetManagedObjects on the ObjectMapper to get all associations
1708 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001709 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001710 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1711
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001712 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001713}
1714
1715/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001716 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1717 *
1718 * Uses the specified connections (services) to obtain D-Bus data for inventory
1719 * item leds associated with sensors. Stores the resulting data in the
1720 * inventoryItems vector.
1721 *
1722 * This data is later used to provide sensor property values in the JSON
1723 * response.
1724 *
1725 * Finds the inventory item led data asynchronously. Invokes callback when data
1726 * has been obtained.
1727 *
1728 * The callback must have the following signature:
1729 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001730 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001731 * @endcode
1732 *
1733 * This function is called recursively, obtaining data asynchronously from one
1734 * connection in each call. This ensures the callback is not invoked until the
1735 * last asynchronous function has completed.
1736 *
1737 * @param sensorsAsyncResp Pointer to object holding response data.
1738 * @param inventoryItems D-Bus inventory items associated with sensors.
1739 * @param ledConnections Connections that provide data for the inventory leds.
1740 * @param callback Callback to invoke when inventory data has been obtained.
1741 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1742 * in recursive calls to this function.
1743 */
1744template <typename Callback>
1745void getInventoryLedData(
1746 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1747 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001748 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001749 Callback&& callback, size_t ledConnectionsIndex = 0)
1750{
1751 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1752
1753 // If no more connections left, call callback
1754 if (ledConnectionsIndex >= ledConnections->size())
1755 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001756 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001757 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1758 return;
1759 }
1760
1761 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001762 auto it = ledConnections->begin();
1763 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001764 if (it != ledConnections->end())
1765 {
1766 const std::string& ledPath = (*it).first;
1767 const std::string& ledConnection = (*it).second;
1768 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001769 auto respHandler =
1770 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001771 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001772 const boost::system::error_code& ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001773 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1774 if (ec)
1775 {
1776 BMCWEB_LOG_ERROR
1777 << "getInventoryLedData respHandler DBus error " << ec;
1778 messages::internalError(sensorsAsyncResp->asyncResp->res);
1779 return;
1780 }
1781
1782 BMCWEB_LOG_DEBUG << "Led state: " << state;
1783 // Find inventory item with this LED object path
1784 InventoryItem* inventoryItem =
1785 findInventoryItemForLed(*inventoryItems, ledPath);
1786 if (inventoryItem != nullptr)
1787 {
1788 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001789 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001790 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001791 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001792 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001793 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001794 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001796 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001797 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001798 {
1799 inventoryItem->ledState = LedState::OFF;
1800 }
1801 else
1802 {
1803 inventoryItem->ledState = LedState::UNKNOWN;
1804 }
1805 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001806
Ed Tanous002d39b2022-05-31 08:59:27 -07001807 // Recurse to get LED data from next connection
1808 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1809 ledConnections, std::move(callback),
1810 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001811
Ed Tanous002d39b2022-05-31 08:59:27 -07001812 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1813 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001814
1815 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001816 sdbusplus::asio::getProperty<std::string>(
1817 *crow::connections::systemBus, ledConnection, ledPath,
1818 "xyz.openbmc_project.Led.Physical", "State",
1819 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001820 }
1821
1822 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1823}
1824
1825/**
1826 * @brief Gets LED data for LEDs associated with given inventory items.
1827 *
1828 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1829 * associated with the specified inventory items. Then gets the LED data from
1830 * each connection and stores it in the inventory item.
1831 *
1832 * This data is later used to provide sensor property values in the JSON
1833 * response.
1834 *
1835 * Finds the LED data asynchronously. Invokes callback when information has
1836 * been obtained.
1837 *
1838 * The callback must have the following signature:
1839 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001840 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001841 * @endcode
1842 *
1843 * @param sensorsAsyncResp Pointer to object holding response data.
1844 * @param inventoryItems D-Bus inventory items associated with sensors.
1845 * @param callback Callback to invoke when inventory items have been obtained.
1846 */
1847template <typename Callback>
1848void getInventoryLeds(
1849 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1850 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1851 Callback&& callback)
1852{
1853 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1854
1855 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001856 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001857 "xyz.openbmc_project.Led.Physical"};
1858
George Liue99073f2022-12-09 11:06:16 +08001859 // Make call to ObjectMapper to find all inventory items
1860 dbus::utility::getSubTree(
1861 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001862 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1863 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001864 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001865 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001866 // Response handler for parsing output from GetSubTree
Anthony Wilsond5005492019-07-31 16:34:17 -05001867 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1868 if (ec)
1869 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001870 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001871 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1872 << ec;
1873 return;
1874 }
1875
1876 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001877 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1878 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001879
1880 // Loop through objects from GetSubTree
1881 for (const std::pair<
1882 std::string,
1883 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1884 object : subtree)
1885 {
1886 // Check if object path is LED for one of the specified inventory
1887 // items
1888 const std::string& ledPath = object.first;
1889 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1890 {
1891 // Add mapping from ledPath to connection
1892 const std::string& connection = object.second.begin()->first;
1893 (*ledConnections)[ledPath] = connection;
1894 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1895 << connection;
1896 }
1897 }
1898
1899 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1900 std::move(callback));
1901 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001902 });
Anthony Wilsond5005492019-07-31 16:34:17 -05001903 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1904}
1905
1906/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001907 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1908 *
1909 * Uses the specified connections (services) (currently assumes just one) to
1910 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1911 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1912 *
1913 * This data is later used to provide sensor property values in the JSON
1914 * response.
1915 *
1916 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1917 * when data has been obtained.
1918 *
1919 * The callback must have the following signature:
1920 * @code
1921 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1922 * @endcode
1923 *
1924 * @param sensorsAsyncResp Pointer to object holding response data.
1925 * @param inventoryItems D-Bus inventory items associated with sensors.
1926 * @param psAttributesConnections Connections that provide data for the Power
1927 * Supply Attributes
1928 * @param callback Callback to invoke when data has been obtained.
1929 */
1930template <typename Callback>
1931void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001932 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001933 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001934 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001935 Callback&& callback)
1936{
1937 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1938
1939 if (psAttributesConnections.empty())
1940 {
1941 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1942 callback(inventoryItems);
1943 return;
1944 }
1945
1946 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001947 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001948
1949 const std::string& psAttributesPath = (*it).first;
1950 const std::string& psAttributesConnection = (*it).second;
1951
1952 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001953 auto respHandler =
1954 [sensorsAsyncResp, inventoryItems,
1955 callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001956 const boost::system::error_code& ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001957 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1958 if (ec)
1959 {
1960 BMCWEB_LOG_ERROR
1961 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001962 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001963 return;
1964 }
1965
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001966 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1967 // Store value in Power Supply Inventory Items
1968 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001969 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001970 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001971 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001972 inventoryItem.powerSupplyEfficiencyPercent =
1973 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001974 }
1975 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001976
1977 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1978 callback(inventoryItems);
1979 };
1980
1981 // Get the DeratingFactor property for the PowerSupplyAttributes
1982 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001983 sdbusplus::asio::getProperty<uint32_t>(
1984 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1985 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
1986 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05001987
1988 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
1989}
1990
1991/**
1992 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
1993 *
1994 * Gets the D-Bus connection (service) that provides Power Supply Attributes
1995 * data. Then gets the Power Supply Attributes data from the connection
1996 * (currently just assumes 1 connection) and stores the data in the inventory
1997 * item.
1998 *
1999 * This data is later used to provide sensor property values in the JSON
2000 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2001 *
2002 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2003 * when information has been obtained.
2004 *
2005 * The callback must have the following signature:
2006 * @code
2007 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2008 * @endcode
2009 *
2010 * @param sensorsAsyncResp Pointer to object holding response data.
2011 * @param inventoryItems D-Bus inventory items associated with sensors.
2012 * @param callback Callback to invoke when data has been obtained.
2013 */
2014template <typename Callback>
2015void getPowerSupplyAttributes(
2016 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2017 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2018 Callback&& callback)
2019{
2020 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2021
2022 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002023 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002024 {
2025 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2026 callback(inventoryItems);
2027 return;
2028 }
2029
George Liue99073f2022-12-09 11:06:16 +08002030 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002031 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2032
George Liue99073f2022-12-09 11:06:16 +08002033 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2034 dbus::utility::getSubTree(
2035 "/xyz/openbmc_project", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002036 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2037 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002038 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002039 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002040 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07002041 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2042 if (ec)
2043 {
2044 messages::internalError(sensorsAsyncResp->asyncResp->res);
2045 BMCWEB_LOG_ERROR
2046 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2047 return;
2048 }
2049 if (subtree.empty())
2050 {
2051 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2052 callback(inventoryItems);
2053 return;
2054 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002055
Ed Tanous002d39b2022-05-31 08:59:27 -07002056 // Currently we only support 1 power supply attribute, use this for
2057 // all the power supplies. Build map of object path to connection.
2058 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002059 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002060
Ed Tanous002d39b2022-05-31 08:59:27 -07002061 if (subtree[0].first.empty() || subtree[0].second.empty())
2062 {
2063 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2064 callback(inventoryItems);
2065 return;
2066 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002067
Ed Tanous002d39b2022-05-31 08:59:27 -07002068 const std::string& psAttributesPath = subtree[0].first;
2069 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002070
Ed Tanous002d39b2022-05-31 08:59:27 -07002071 if (connection.empty())
2072 {
2073 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2074 callback(inventoryItems);
2075 return;
2076 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002077
Ed Tanous002d39b2022-05-31 08:59:27 -07002078 psAttributesConnections[psAttributesPath] = connection;
2079 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2080 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002081
Ed Tanous002d39b2022-05-31 08:59:27 -07002082 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2083 psAttributesConnections,
2084 std::move(callback));
2085 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08002086 });
Gunnar Mills42cbe532019-08-15 15:26:54 -05002087 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2088}
2089
2090/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002091 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002092 *
2093 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002094 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002095 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002096 * This data is later used to provide sensor property values in the JSON
2097 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002098 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002099 * Finds the inventory items asynchronously. Invokes callback when the
2100 * inventory items have been obtained.
2101 *
2102 * The callback must have the following signature:
2103 * @code
2104 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2105 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002106 *
2107 * @param sensorsAsyncResp Pointer to object holding response data.
2108 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002109 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002110 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002111 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002112template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002113static void
2114 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2115 const std::shared_ptr<std::set<std::string>> sensorNames,
2116 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002117{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002118 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2119 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002120 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002121 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002122 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2123 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002124 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002125 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002126 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002127 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2128 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2129 callback{std::move(callback)}]() {
2130 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002131
Ed Tanous002d39b2022-05-31 08:59:27 -07002132 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2133 callback{std::move(callback)}]() {
2134 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2135 // Find Power Supply Attributes and get the data
2136 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2137 std::move(callback));
2138 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002139 };
2140
Ed Tanous002d39b2022-05-31 08:59:27 -07002141 // Find led connections and get the data
2142 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2143 std::move(getInventoryLedsCb));
2144 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2145 };
2146
2147 // Get inventory item data from connections
2148 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002149 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002150 std::move(getInventoryItemsDataCb));
2151 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002152 };
2153
Ed Tanous002d39b2022-05-31 08:59:27 -07002154 // Get connections that provide inventory item data
2155 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2156 std::move(getInventoryItemsConnectionsCb));
2157 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2158 };
2159
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002160 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002161 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002162 std::move(getInventoryItemAssociationsCb));
2163 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2164}
2165
2166/**
2167 * @brief Returns JSON PowerSupply object for the specified inventory item.
2168 *
2169 * Searches for a JSON PowerSupply object that matches the specified inventory
2170 * item. If one is not found, a new PowerSupply object is added to the JSON
2171 * array.
2172 *
2173 * Multiple sensors are often associated with one power supply inventory item.
2174 * As a result, multiple sensor values are stored in one JSON PowerSupply
2175 * object.
2176 *
2177 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2178 * @param inventoryItem Inventory item for the power supply.
2179 * @param chassisId Chassis that contains the power supply.
2180 * @return JSON PowerSupply object for the specified inventory item.
2181 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002182inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002183 const InventoryItem& inventoryItem,
2184 const std::string& chassisId)
2185{
2186 // Check if matching PowerSupply object already exists in JSON array
2187 for (nlohmann::json& powerSupply : powerSupplyArray)
2188 {
Alexander Hansenf7afb172023-04-27 14:34:08 +02002189 if (powerSupply["Name"] ==
2190 boost::replace_all_copy(inventoryItem.name, "_", " "))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002191 {
2192 return powerSupply;
2193 }
2194 }
2195
2196 // Add new PowerSupply object to JSON array
2197 powerSupplyArray.push_back({});
2198 nlohmann::json& powerSupply = powerSupplyArray.back();
Ed Tanousef4c65b2023-04-24 15:28:50 -07002199 boost::urls::url url = boost::urls::format("/redfish/v1/Chassis/{}/Power",
2200 chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002201 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2202 powerSupply["@odata.id"] = std::move(url);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002203 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2204 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2205 powerSupply["Model"] = inventoryItem.model;
2206 powerSupply["PartNumber"] = inventoryItem.partNumber;
2207 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002208 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002209
Gunnar Mills42cbe532019-08-15 15:26:54 -05002210 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2211 {
2212 powerSupply["EfficiencyPercent"] =
2213 inventoryItem.powerSupplyEfficiencyPercent;
2214 }
2215
2216 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002217 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2218 powerSupply["Status"]["Health"] = health;
2219
2220 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002221}
2222
2223/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002224 * @brief Gets the values of the specified sensors.
2225 *
2226 * Stores the results as JSON in the SensorsAsyncResp.
2227 *
2228 * Gets the sensor values asynchronously. Stores the results later when the
2229 * information has been obtained.
2230 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002231 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002232 *
2233 * To minimize the number of DBus calls, the DBus method
2234 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2235 * values of all sensors provided by a connection (service).
2236 *
2237 * The connections set contains all the connections that provide sensor values.
2238 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002239 * The InventoryItem vector contains D-Bus inventory items associated with the
2240 * sensors. Inventory item data is needed for some Redfish sensor properties.
2241 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002242 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002243 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002244 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002245 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002246 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002247 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002248inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002249 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002250 const std::shared_ptr<std::set<std::string>>& sensorNames,
2251 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002252 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002253{
2254 BMCWEB_LOG_DEBUG << "getSensorData enter";
2255 // Get managed objects from all services exposing sensors
2256 for (const std::string& connection : connections)
2257 {
2258 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002259 auto getManagedObjectsCb =
2260 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002261 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002262 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002263 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2264 if (ec)
2265 {
2266 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002267 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002268 return;
2269 }
2270 // Go through all objects and update response with sensor data
2271 for (const auto& objDictEntry : resp)
2272 {
2273 const std::string& objPath =
2274 static_cast<const std::string&>(objDictEntry.first);
2275 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2276 << objPath;
2277
Shawn McCarneyde629b62019-03-08 10:42:51 -06002278 std::vector<std::string> split;
2279 // Reserve space for
2280 // /xyz/openbmc_project/sensors/<name>/<subname>
2281 split.reserve(6);
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002282 // NOLINTNEXTLINE
2283 bmcweb::split(split, objPath, '/');
Shawn McCarneyde629b62019-03-08 10:42:51 -06002284 if (split.size() < 6)
2285 {
2286 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2287 << objPath;
2288 continue;
2289 }
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002290 // These indexes aren't intuitive, as split puts an empty
Shawn McCarneyde629b62019-03-08 10:42:51 -06002291 // string at the beginning
2292 const std::string& sensorType = split[4];
2293 const std::string& sensorName = split[5];
2294 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2295 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002296 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002297 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002298 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002299 continue;
2300 }
2301
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002302 // Find inventory item (if any) associated with sensor
2303 InventoryItem* inventoryItem =
2304 findInventoryItemForSensor(inventoryItems, objPath);
2305
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002306 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002307 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002308
2309 nlohmann::json* sensorJson = nullptr;
2310
Nan Zhou928fefb2022-03-28 08:45:00 -07002311 if (sensorSchema == sensors::node::sensors &&
2312 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002313 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002314 std::string sensorTypeEscaped(sensorType);
2315 sensorTypeEscaped.erase(
2316 std::remove(sensorTypeEscaped.begin(),
2317 sensorTypeEscaped.end(), '_'),
2318 sensorTypeEscaped.end());
2319 std::string sensorId(sensorTypeEscaped);
2320 sensorId += "_";
2321 sensorId += sensorName;
2322
zhanghch058d1b46d2021-04-01 11:18:24 +08002323 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07002324 boost::urls::format("/redfish/v1/Chassis/{}/{}/{}",
2325 sensorsAsyncResp->chassisId,
2326 sensorsAsyncResp->chassisSubNode,
2327 sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002328 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002329 }
2330 else
2331 {
Ed Tanous271584a2019-07-09 16:24:22 -07002332 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002333 if (sensorsAsyncResp->efficientExpand)
2334 {
2335 fieldName = "Members";
2336 }
2337 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002338 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002339 fieldName = "Temperatures";
2340 }
2341 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2342 sensorType == "fan_pwm")
2343 {
2344 fieldName = "Fans";
2345 }
2346 else if (sensorType == "voltage")
2347 {
2348 fieldName = "Voltages";
2349 }
2350 else if (sensorType == "power")
2351 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002352 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002353 {
2354 fieldName = "PowerControl";
2355 }
2356 else if ((inventoryItem != nullptr) &&
2357 (inventoryItem->isPowerSupply))
2358 {
2359 fieldName = "PowerSupplies";
2360 }
2361 else
2362 {
2363 // Other power sensors are in SensorCollection
2364 continue;
2365 }
2366 }
2367 else
2368 {
2369 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2370 << sensorType;
2371 continue;
2372 }
2373
2374 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002375 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002376 if (fieldName == "PowerControl")
2377 {
2378 if (tempArray.empty())
2379 {
2380 // Put multiple "sensors" into a single
2381 // PowerControl. Follows MemberId naming and
2382 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002383 nlohmann::json::object_t power;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002384 boost::urls::url url = boost::urls::format(
2385 "/redfish/v1/Chassis/{}/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002386 sensorsAsyncResp->chassisId,
2387 sensorsAsyncResp->chassisSubNode);
2388 url.set_fragment((""_json_pointer / fieldName / "0")
2389 .to_string());
2390 power["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002391 tempArray.emplace_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002392 }
2393 sensorJson = &(tempArray.back());
2394 }
2395 else if (fieldName == "PowerSupplies")
2396 {
2397 if (inventoryItem != nullptr)
2398 {
2399 sensorJson =
2400 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002401 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002402 }
2403 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002404 else if (fieldName == "Members")
2405 {
Ed Tanous677bb752022-09-15 10:52:19 -07002406 std::string sensorTypeEscaped(sensorType);
2407 sensorTypeEscaped.erase(
2408 std::remove(sensorTypeEscaped.begin(),
2409 sensorTypeEscaped.end(), '_'),
2410 sensorTypeEscaped.end());
2411 std::string sensorId(sensorTypeEscaped);
2412 sensorId += "_";
2413 sensorId += sensorName;
2414
Ed Tanous14766872022-03-15 10:44:42 -07002415 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002416 member["@odata.id"] = boost::urls::format(
2417 "/redfish/v1/Chassis/{}/{}/{}",
Ed Tanous677bb752022-09-15 10:52:19 -07002418 sensorsAsyncResp->chassisId,
2419 sensorsAsyncResp->chassisSubNode, sensorId);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002420 tempArray.emplace_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002421 sensorJson = &(tempArray.back());
2422 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002423 else
2424 {
Ed Tanous14766872022-03-15 10:44:42 -07002425 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002426 boost::urls::url url = boost::urls::format(
2427 "/redfish/v1/Chassis/{}/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002428 sensorsAsyncResp->chassisId,
2429 sensorsAsyncResp->chassisSubNode);
2430 url.set_fragment(
2431 (""_json_pointer / fieldName).to_string());
2432 member["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002433 tempArray.emplace_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002434 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002435 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002436 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002437
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002438 if (sensorJson != nullptr)
2439 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002440 objectInterfacesToJson(sensorName, sensorType,
2441 sensorsAsyncResp->chassisSubNode,
2442 objDictEntry.second, *sensorJson,
2443 inventoryItem);
2444
2445 std::string path = "/xyz/openbmc_project/sensors/";
2446 path += sensorType;
2447 path += "/";
2448 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002449 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002450 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002451 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002452 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002453 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002454 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002455 if (sensorsAsyncResp->chassisSubNode ==
2456 sensors::node::sensors &&
2457 sensorsAsyncResp->efficientExpand)
2458 {
2459 sensorsAsyncResp->asyncResp->res
2460 .jsonValue["Members@odata.count"] =
2461 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2462 .size();
2463 }
2464 else if (sensorsAsyncResp->chassisSubNode ==
2465 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002466 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002467 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002468 }
James Feist8bd25cc2019-03-15 15:14:00 -07002469 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002470 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2471 };
2472
Shawn McCarneyde629b62019-03-08 10:42:51 -06002473 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002474 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002475 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002476 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002477 BMCWEB_LOG_DEBUG << "getSensorData exit";
2478}
2479
Nan Zhoufe04d492022-06-22 17:10:41 +00002480inline void
2481 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2482 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002483{
Nan Zhoufe04d492022-06-22 17:10:41 +00002484 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2485 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002486 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002487 auto getInventoryItemsCb =
2488 [sensorsAsyncResp, sensorNames,
2489 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2490 inventoryItems) {
2491 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2492 // Get sensor data and store results in JSON
2493 getSensorData(sensorsAsyncResp, sensorNames, connections,
2494 inventoryItems);
2495 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002496 };
2497
Ed Tanousd0090732022-10-04 17:22:56 -07002498 // Get inventory items associated with sensors
2499 getInventoryItems(sensorsAsyncResp, sensorNames,
2500 std::move(getInventoryItemsCb));
2501
Ed Tanous002d39b2022-05-31 08:59:27 -07002502 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2503 };
2504
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002505 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002506 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002507}
2508
Shawn McCarneyde629b62019-03-08 10:42:51 -06002509/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002510 * @brief Entry point for retrieving sensors data related to requested
2511 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002512 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002513 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002514inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002515 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002516{
2517 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002518 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002519 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002520 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002521 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2522 processSensorList(sensorsAsyncResp, sensorNames);
2523 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2524 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002525 // SensorCollection doesn't contain the Redundancy property
2526 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2527 {
2528 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2529 nlohmann::json::array();
2530 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002531 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002532 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2533 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2534 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002535 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002536}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002537
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302538/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002539 * @brief Find the requested sensorName in the list of all sensors supplied by
2540 * the chassis node
2541 *
2542 * @param sensorName The sensor name supplied in the PATCH request
2543 * @param sensorsList The list of sensors managed by the chassis node
2544 * @param sensorsModified The list of sensors that were found as a result of
2545 * repeated calls to this function
2546 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002547inline bool
2548 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002549 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002550 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002551{
Nan Zhoufe04d492022-06-22 17:10:41 +00002552 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002553 {
George Liu28aa8de2021-02-01 15:13:30 +08002554 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002555 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002556 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002557 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002558 continue;
2559 }
2560 if (thisSensorName == sensorName)
2561 {
2562 sensorsModified.emplace(chassisSensor);
2563 return true;
2564 }
2565 }
2566 return false;
2567}
2568
Ed Tanousc71d6122022-11-29 14:10:32 -08002569inline std::pair<std::string, std::string>
2570 splitSensorNameAndType(std::string_view sensorId)
2571{
2572 size_t index = sensorId.find('_');
2573 if (index == std::string::npos)
2574 {
2575 return std::make_pair<std::string, std::string>("", "");
2576 }
2577 std::string sensorType{sensorId.substr(0, index)};
2578 std::string sensorName{sensorId.substr(index + 1)};
2579 // fan_pwm and fan_tach need special handling
2580 if (sensorType == "fantach" || sensorType == "fanpwm")
2581 {
2582 sensorType.insert(3, 1, '_');
2583 }
2584 return std::make_pair(sensorType, sensorName);
2585}
2586
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002587/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302588 * @brief Entry point for overriding sensor values of given sensor
2589 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002590 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002591 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002592 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302593 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002594inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002595 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002596 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002597 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302598{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002599 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002600 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302601
Ed Tanous543f4402022-01-06 13:12:53 -08002602 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302603 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302604 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002605 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302606 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302607 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302608 if (collectionItems.first == "Temperatures")
2609 {
2610 propertyValueName = "ReadingCelsius";
2611 }
2612 else if (collectionItems.first == "Fans")
2613 {
2614 propertyValueName = "Reading";
2615 }
2616 else
2617 {
2618 propertyValueName = "ReadingVolts";
2619 }
2620 for (auto& item : collectionItems.second)
2621 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002622 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2623 "MemberId", memberId, propertyValueName,
2624 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302625 {
2626 return;
2627 }
2628 overrideMap.emplace(memberId,
2629 std::make_pair(value, collectionItems.first));
2630 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302631 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002632
Ed Tanous002d39b2022-05-31 08:59:27 -07002633 auto getChassisSensorListCb =
2634 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002635 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002636 // Match sensor names in the PATCH request to those managed by the
2637 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002638 const std::shared_ptr<std::set<std::string>> sensorNames =
2639 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302640 for (const auto& item : overrideMap)
2641 {
2642 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002643 std::pair<std::string, std::string> sensorNameType =
2644 splitSensorNameAndType(sensor);
2645 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2646 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302647 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302648 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002649 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302650 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302651 return;
2652 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302653 }
2654 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002655 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002656 [sensorAsyncResp,
2657 overrideMap](const std::set<std::string>& /*connections*/,
2658 const std::set<std::pair<std::string, std::string>>&
2659 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002660 if (objectsWithConnection.size() != overrideMap.size())
2661 {
2662 BMCWEB_LOG_INFO
2663 << "Unable to find all objects with proper connection "
2664 << objectsWithConnection.size() << " requested "
2665 << overrideMap.size() << "\n";
2666 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 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302683
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002684 const auto& iterator = overrideMap.find(sensorName);
2685 if (iterator == overrideMap.end())
2686 {
2687 BMCWEB_LOG_INFO << "Unable to find sensor object"
2688 << item.first << "\n";
2689 messages::internalError(sensorAsyncResp->asyncResp->res);
2690 return;
2691 }
2692 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002693 [sensorAsyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002694 if (ec)
2695 {
2696 if (ec.value() ==
2697 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002698 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002699 BMCWEB_LOG_WARNING
2700 << "Manufacturing mode is not Enabled...can't "
2701 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002702
Ed Tanous002d39b2022-05-31 08:59:27 -07002703 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002704 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002705 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002706 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002707 BMCWEB_LOG_DEBUG
2708 << "setOverrideValueStatus DBUS error: " << ec;
2709 messages::internalError(
2710 sensorAsyncResp->asyncResp->res);
2711 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002712 },
2713 item.second, item.first, "org.freedesktop.DBus.Properties",
2714 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002715 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002716 }
2717 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302718 // Get object with connection for the given sensor name
2719 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2720 std::move(getObjectsWithConnectionCb));
2721 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302722 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002723 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2724 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2725 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302726}
2727
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002728/**
2729 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2730 * path of the sensor.
2731 *
2732 * Function builds valid Redfish response for sensor query of given chassis and
2733 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2734 * it to caller in a callback.
2735 *
2736 * @param chassis Chassis for which retrieval should be performed
2737 * @param node Node (group) of sensors. See sensors::node for supported values
2738 * @param mapComplete Callback to be called with retrieval result
2739 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002740inline void retrieveUriToDbusMap(const std::string& chassis,
2741 const std::string& node,
2742 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002743{
Ed Tanous02da7c52022-02-27 00:09:02 -08002744 decltype(sensors::paths)::const_iterator pathIt =
2745 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2746 [&node](auto&& val) { return val.first == node; });
2747 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002748 {
2749 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2750 mapComplete(boost::beast::http::status::bad_request, {});
2751 return;
2752 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002753
Nan Zhou72374eb2022-01-27 17:06:51 -08002754 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002755 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2756 const boost::beast::http::status status,
2757 const std::map<std::string, std::string>& uriToDbus) {
2758 mapCompleteCb(status, uriToDbus);
2759 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002760
2761 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002762 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002763 getChassisData(resp);
2764}
2765
Nan Zhoubacb2162022-04-06 11:28:32 -07002766namespace sensors
2767{
Nan Zhou928fefb2022-03-28 08:45:00 -07002768
Nan Zhoubacb2162022-04-06 11:28:32 -07002769inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002770 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2771 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002772 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002773{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002774 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002775
Ed Tanousc1d019a2022-08-06 09:36:06 -07002776 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2777 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002778 {
2779 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2780
2781 sdbusplus::message::object_path path(sensor);
2782 std::string sensorName = path.filename();
2783 if (sensorName.empty())
2784 {
2785 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002786 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002787 return;
2788 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002789 std::string type = path.parent_path().filename();
2790 // fan_tach has an underscore in it, so remove it to "normalize" the
2791 // type in the URI
2792 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2793
Ed Tanous14766872022-03-15 10:44:42 -07002794 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002795 std::string id = type;
2796 id += "_";
2797 id += sensorName;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002798 member["@odata.id"] = boost::urls::format(
2799 "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002800
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002801 entriesArray.emplace_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002802 }
2803
Ed Tanousc1d019a2022-08-06 09:36:06 -07002804 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002805 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2806}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002807
Nan Zhoude167a62022-06-01 04:47:45 +00002808inline void
2809 handleSensorCollectionGet(App& app, const crow::Request& req,
2810 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2811 const std::string& chassisId)
2812{
2813 query_param::QueryCapabilities capabilities = {
2814 .canDelegateExpandLevel = 1,
2815 };
2816 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002817 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002818 delegatedQuery, capabilities))
2819 {
2820 return;
2821 }
2822
2823 if (delegatedQuery.expandType != query_param::ExpandType::None)
2824 {
2825 // we perform efficient expand.
2826 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2827 aResp, chassisId, sensors::dbus::sensorPaths,
2828 sensors::node::sensors,
2829 /*efficientExpand=*/true);
2830 getChassisData(asyncResp);
2831
2832 BMCWEB_LOG_DEBUG
2833 << "SensorCollection doGet exit via efficient expand handler";
2834 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002835 }
Nan Zhoude167a62022-06-01 04:47:45 +00002836
Nan Zhoude167a62022-06-01 04:47:45 +00002837 // We get all sensors as hyperlinkes in the chassis (this
2838 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002839 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2840 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2841 sensors::node::sensors));
2842}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002843
Ed Tanousc1d019a2022-08-06 09:36:06 -07002844inline void
2845 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2846 const std::string& sensorPath,
2847 const ::dbus::utility::MapperGetObject& mapperResponse)
2848{
2849 if (mapperResponse.size() != 1)
2850 {
2851 messages::internalError(asyncResp->res);
2852 return;
2853 }
2854 const auto& valueIface = *mapperResponse.begin();
2855 const std::string& connectionName = valueIface.first;
2856 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2857 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002858
2859 sdbusplus::asio::getAllProperties(
2860 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002861 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002862 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002863 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2864 if (ec)
2865 {
2866 messages::internalError(asyncResp->res);
2867 return;
2868 }
2869 sdbusplus::message::object_path path(sensorPath);
2870 std::string name = path.filename();
2871 path = path.parent_path();
2872 std::string type = path.filename();
2873 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2874 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002875 });
Nan Zhoude167a62022-06-01 04:47:45 +00002876}
2877
Nan Zhoue6bd8462022-06-01 04:35:35 +00002878inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002879 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002880 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002881 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002882{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002883 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002884 {
2885 return;
2886 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002887 std::pair<std::string, std::string> nameType =
2888 splitSensorNameAndType(sensorId);
2889 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002890 {
2891 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2892 return;
2893 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002894
Ed Tanousef4c65b2023-04-24 15:28:50 -07002895 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2896 "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002897
Nan Zhoue6bd8462022-06-01 04:35:35 +00002898 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002899
George Liu2b731192023-01-11 16:27:13 +08002900 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002901 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002902 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2903 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002904 // Get a list of all of the sensors that implement Sensor.Value
2905 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002906 ::dbus::utility::getDbusObject(
2907 sensorPath, interfaces,
Ed Tanousc71d6122022-11-29 14:10:32 -08002908 [asyncResp,
George Liu2b731192023-01-11 16:27:13 +08002909 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002910 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002911 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2912 if (ec)
2913 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002914 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002915 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2916 << "Dbus error " << ec;
2917 return;
2918 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002919 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002920 BMCWEB_LOG_DEBUG << "respHandler1 exit";
George Liu2b731192023-01-11 16:27:13 +08002921 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002922}
2923
Nan Zhoubacb2162022-04-06 11:28:32 -07002924} // namespace sensors
2925
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002926inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002927{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002928 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002929 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002930 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002931 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002932}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002933
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002934inline void requestRoutesSensor(App& app)
2935{
2936 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002937 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002938 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002939 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002940}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002941
Ed Tanous1abe55e2018-09-05 08:30:59 -07002942} // namespace redfish