blob: ef3354d980e5491eb2fb8288d87b7ccdc7b440d5 [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>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070035#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020036#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050037
George Liu7a1dbc42022-12-07 16:03:22 +080038#include <array>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000040#include <iterator>
41#include <map>
42#include <set>
George Liu7a1dbc42022-12-07 16:03:22 +080043#include <string_view>
Ed Tanousb5a76932020-09-29 16:16:58 -070044#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080045#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047namespace redfish
48{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010049
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020050namespace sensors
51{
52namespace node
53{
54static constexpr std::string_view power = "Power";
55static constexpr std::string_view sensors = "Sensors";
56static constexpr std::string_view thermal = "Thermal";
57} // namespace node
58
Ed Tanous02da7c52022-02-27 00:09:02 -080059// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020060namespace dbus
61{
Ed Tanouscf9e4172022-12-21 09:30:16 -080062constexpr auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080063 "/xyz/openbmc_project/sensors/voltage",
64 "/xyz/openbmc_project/sensors/power"
65});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000066
Ed Tanouscf9e4172022-12-21 09:30:16 -080067constexpr auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080068 "/xyz/openbmc_project/sensors/power",
69 "/xyz/openbmc_project/sensors/current",
70 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070071 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080072#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080073 "/xyz/openbmc_project/sensors/voltage",
74 "/xyz/openbmc_project/sensors/fan_tach",
75 "/xyz/openbmc_project/sensors/temperature",
76 "/xyz/openbmc_project/sensors/fan_pwm",
77 "/xyz/openbmc_project/sensors/altitude",
78 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080079#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080080 "/xyz/openbmc_project/sensors/utilization"
81});
82
Ed Tanouscf9e4172022-12-21 09:30:16 -080083constexpr auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080084 "/xyz/openbmc_project/sensors/fan_tach",
85 "/xyz/openbmc_project/sensors/temperature",
86 "/xyz/openbmc_project/sensors/fan_pwm"
87});
88
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000089} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080090// clang-format on
91
Ed Tanouscf9e4172022-12-21 09:30:16 -080092using sensorPair =
93 std::pair<std::string_view, std::span<const std::string_view>>;
Ed Tanous02da7c52022-02-27 00:09:02 -080094static constexpr std::array<sensorPair, 3> paths = {
Ed Tanouscf9e4172022-12-21 09:30:16 -080095 {{node::power, dbus::powerPaths},
96 {node::sensors, dbus::sensorPaths},
97 {node::thermal, dbus::thermalPaths}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000098
Ed Tanous0ec8b832022-03-14 14:56:47 -070099inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000100{
101 if (sensorType == "voltage")
102 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700103 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000104 }
105 if (sensorType == "power")
106 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700107 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000108 }
109 if (sensorType == "current")
110 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700111 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000112 }
113 if (sensorType == "fan_tach")
114 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700115 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000116 }
117 if (sensorType == "temperature")
118 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700119 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000120 }
121 if (sensorType == "fan_pwm" || sensorType == "utilization")
122 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700123 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000124 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600125 if (sensorType == "humidity")
126 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700127 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600128 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000129 if (sensorType == "altitude")
130 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700131 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000132 }
133 if (sensorType == "airflow")
134 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700135 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000136 }
137 if (sensorType == "energy")
138 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700139 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000140 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700141 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000142}
143
Ed Tanous1d7c0052022-08-09 12:32:26 -0700144inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000145{
146 if (sensorType == "voltage")
147 {
148 return "V";
149 }
150 if (sensorType == "power")
151 {
152 return "W";
153 }
154 if (sensorType == "current")
155 {
156 return "A";
157 }
158 if (sensorType == "fan_tach")
159 {
160 return "RPM";
161 }
162 if (sensorType == "temperature")
163 {
164 return "Cel";
165 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600166 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
167 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000168 {
169 return "%";
170 }
171 if (sensorType == "altitude")
172 {
173 return "m";
174 }
175 if (sensorType == "airflow")
176 {
177 return "cft_i/min";
178 }
179 if (sensorType == "energy")
180 {
181 return "J";
182 }
183 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200184}
185} // namespace sensors
186
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100187/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200188 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100189 * Gathers data needed for response processing after async calls are done
190 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191class SensorsAsyncResp
192{
193 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200194 using DataCompleteCb = std::function<void(
195 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000196 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200197
198 struct SensorData
199 {
200 const std::string name;
201 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200202 const std::string dbusPath;
203 };
204
Ed Tanous8a592812022-06-04 09:06:59 -0700205 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800206 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800207 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800208 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700209 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700210 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
211 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500212 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200213
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200214 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700215 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800216 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800217 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800218 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200219 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700220 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700221 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
222 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200223 dataComplete{std::move(creationComplete)}
224 {}
225
Nan Zhou928fefb2022-03-28 08:45:00 -0700226 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700227 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700228 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800229 std::span<const std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700230 const std::string_view& subNode, bool efficientExpandIn) :
231 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700232 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700233 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700234 {}
235
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 ~SensorsAsyncResp()
237 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800238 if (asyncResp->res.result() ==
239 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 {
241 // Reset the json object to clear out any data that made it in
242 // before the error happened todo(ed) handle error condition with
243 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800244 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200246
247 if (dataComplete && metadata)
248 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000249 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200251 {
252 for (auto& sensor : *metadata)
253 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700254 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200255 }
256 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800257 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200258 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100260
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800261 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
262 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
263 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
264 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
265
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200266 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700267 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200268 {
269 if (metadata)
270 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700271 metadata->emplace_back(SensorData{
272 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200273 }
274 }
275
276 void updateUri(const std::string& name, const std::string& uri)
277 {
278 if (metadata)
279 {
280 for (auto& sensor : *metadata)
281 {
282 if (sensor.name == name)
283 {
284 sensor.uri = uri;
285 }
286 }
287 }
288 }
289
zhanghch058d1b46d2021-04-01 11:18:24 +0800290 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200291 const std::string chassisId;
Ed Tanouscf9e4172022-12-21 09:30:16 -0800292 const std::span<const std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200293 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700294 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200295
296 private:
297 std::optional<std::vector<SensorData>> metadata;
298 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100299};
300
301/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500302 * Possible states for physical inventory leds
303 */
304enum class LedState
305{
306 OFF,
307 ON,
308 BLINK,
309 UNKNOWN
310};
311
312/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500313 * D-Bus inventory item associated with one or more sensors.
314 */
315class InventoryItem
316{
317 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700318 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500319 {
320 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800321 sdbusplus::message::object_path path(objectPath);
322 name = path.filename();
323 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500324 {
George Liu28aa8de2021-02-01 15:13:30 +0800325 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500326 }
327 }
328
329 std::string objectPath;
330 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800331 bool isPresent = true;
332 bool isFunctional = true;
333 bool isPowerSupply = false;
334 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500335 std::string manufacturer;
336 std::string model;
337 std::string partNumber;
338 std::string serialNumber;
339 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500340 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800341 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500342};
343
344/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530345 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200346 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100347 * @param sensorNames Sensors retrieved from chassis
348 * @param callback Callback for processing gathered connections
349 */
350template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530351void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000352 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000353 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530354 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700355{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530356 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700357 const std::string path = "/xyz/openbmc_project/sensors";
George Liue99073f2022-12-09 11:06:16 +0800358 constexpr std::array<std::string_view, 1> interfaces = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700359 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100360
George Liue99073f2022-12-09 11:06:16 +0800361 // Make call to ObjectMapper to find all sensors objects
362 dbus::utility::getSubTree(
363 path, 2, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700364 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
George Liue99073f2022-12-09 11:06:16 +0800365 sensorNames](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700366 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +0800367 // Response handler for parsing objects subtree
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530368 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700369 if (ec)
370 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800371 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530372 BMCWEB_LOG_ERROR
373 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700374 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100375 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100376
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
378
379 // Make unique list of connections only for requested sensor types and
380 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000381 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530382 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700383
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700384 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
385 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386 {
387 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
388 }
389
390 for (const std::pair<
391 std::string,
392 std::vector<std::pair<std::string, std::vector<std::string>>>>&
393 object : subtree)
394 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700395 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700396 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700397 for (const std::pair<std::string, std::vector<std::string>>&
398 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700400 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
401 connections.insert(objData.first);
402 objectsWithConnection.insert(
403 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404 }
405 }
406 }
407 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530408 callback(std::move(connections), std::move(objectsWithConnection));
409 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
George Liue99073f2022-12-09 11:06:16 +0800410 });
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530411 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
412}
413
414/**
415 * @brief Create connections necessary for sensors
416 * @param SensorsAsyncResp Pointer to object holding response data
417 * @param sensorNames Sensors retrieved from chassis
418 * @param callback Callback for processing gathered connections
419 */
420template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000421void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
422 const std::shared_ptr<std::set<std::string>> sensorNames,
423 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530424{
425 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000426 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530427 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700428 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000429 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530430 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100431}
432
433/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700434 * @brief Shrinks the list of sensors for processing
435 * @param SensorsAysncResp The class holding the Redfish response
436 * @param allSensors A list of all the sensors associated to the
437 * chassis element (i.e. baseboard, front panel, etc...)
438 * @param activeSensors A list that is a reduction of the incoming
439 * allSensors list. Eliminate Thermal sensors when a Power request is
440 * made, and eliminate Power sensors when a Thermal request is made.
441 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000442inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700443 crow::Response& res, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800444 std::span<const std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000446 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700448 if ((allSensors == nullptr) || (activeSensors == nullptr))
449 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700450 messages::resourceNotFound(res, chassisSubNode,
451 chassisSubNode == sensors::node::thermal
452 ? "Temperatures"
453 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700454
455 return;
456 }
457 if (allSensors->empty())
458 {
459 // Nothing to do, the activeSensors object is also empty
460 return;
461 }
462
Ed Tanous7f1cc262022-08-09 13:33:57 -0700463 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700464 {
465 for (const std::string& sensor : *allSensors)
466 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700467 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700468 {
469 activeSensors->emplace(sensor);
470 }
471 }
472 }
473}
474
Ed Tanous7f1cc262022-08-09 13:33:57 -0700475/*
476 *Populates the top level collection for a given subnode. Populates
477 *SensorCollection, Power, or Thermal schemas.
478 *
479 * */
480inline void populateChassisNode(nlohmann::json& jsonValue,
481 std::string_view chassisSubNode)
482{
483 if (chassisSubNode == sensors::node::power)
484 {
485 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
486 }
487 else if (chassisSubNode == sensors::node::thermal)
488 {
489 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
490 jsonValue["Fans"] = nlohmann::json::array();
491 jsonValue["Temperatures"] = nlohmann::json::array();
492 }
493 else if (chassisSubNode == sensors::node::sensors)
494 {
495 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
496 jsonValue["Description"] = "Collection of Sensors for this Chassis";
497 jsonValue["Members"] = nlohmann::json::array();
498 jsonValue["Members@odata.count"] = 0;
499 }
500
501 if (chassisSubNode != sensors::node::sensors)
502 {
503 jsonValue["Id"] = chassisSubNode;
504 }
505 jsonValue["Name"] = chassisSubNode;
506}
507
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700508/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100509 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200510 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100511 * @param callback Callback for next step in gathered sensor processing
512 */
513template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700514void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
515 std::string_view chassisId, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800516 std::span<const std::string_view> sensorTypes,
517 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518{
519 BMCWEB_LOG_DEBUG << "getChassis enter";
George Liu7a1dbc42022-12-07 16:03:22 +0800520 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700521 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500522 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800523
524 // Get the Chassis Collection
525 dbus::utility::getSubTreePaths(
526 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700527 [callback{std::forward<Callback>(callback)}, asyncResp,
528 chassisIdStr{std::string(chassisId)},
529 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800530 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700531 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
533 if (ec)
534 {
535 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700536 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700537 return;
538 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700539 const std::string* chassisPath = nullptr;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700540 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 {
George Liu28aa8de2021-02-01 15:13:30 +0800542 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700543 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800544 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700546 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 continue;
548 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700549 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700551 chassisPath = &chassis;
552 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700554 }
555 if (chassisPath == nullptr)
556 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700557 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700558 return;
559 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700560 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561
Willy Tueddfc432022-09-26 16:46:38 +0000562 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
563 "redfish", "v1", "Chassis", chassisIdStr, chassisSubNode);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500564
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500565 // Get the list of all sensors for this Chassis element
566 std::string sensorPath = *chassisPath + "/all_sensors";
George Liu6c3e9452023-03-03 13:55:29 +0800567 dbus::utility::getAssociationEndPoints(
568 sensorPath,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700569 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800570 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700571 const boost::system::error_code& e,
George Liu6c3e9452023-03-03 13:55:29 +0800572 const dbus::utility::MapperEndPoints& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700573 if (e)
574 {
575 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700576 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700577 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700578 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700579 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000581 const std::shared_ptr<std::set<std::string>> culledSensorList =
582 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700583 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
584 &nodeSensorList, culledSensorList);
585 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700587 });
George Liu7a1dbc42022-12-07 16:03:22 +0800588 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100590}
591
592/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500593 * @brief Returns the Redfish State value for the specified inventory item.
594 * @param inventoryItem D-Bus inventory item associated with a sensor.
595 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700596 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000597inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500598{
599 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
600 {
601 return "Absent";
602 }
James Feist34dd1792019-05-17 14:10:54 -0700603
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500604 return "Enabled";
605}
606
607/**
608 * @brief Returns the Redfish Health value for the specified sensor.
609 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700610 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500611 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
612 * be nullptr if no associated inventory item was found.
613 * @return Health value for sensor.
614 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700615inline std::string getHealth(nlohmann::json& sensorJson,
616 const dbus::utility::DBusPropertiesMap& valuesDict,
617 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700618{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500619 // Get current health value (if any) in the sensor JSON object. Some JSON
620 // objects contain multiple sensors (such as PowerSupplies). We want to set
621 // the overall health to be the most severe of any of the sensors.
622 std::string currentHealth;
623 auto statusIt = sensorJson.find("Status");
624 if (statusIt != sensorJson.end())
625 {
626 auto healthIt = statusIt->find("Health");
627 if (healthIt != statusIt->end())
628 {
629 std::string* health = healthIt->get_ptr<std::string*>();
630 if (health != nullptr)
631 {
632 currentHealth = *health;
633 }
634 }
635 }
636
637 // If current health in JSON object is already Critical, return that. This
638 // should override the sensor health, which might be less severe.
639 if (currentHealth == "Critical")
640 {
641 return "Critical";
642 }
643
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200644 const bool* criticalAlarmHigh = nullptr;
645 const bool* criticalAlarmLow = nullptr;
646 const bool* warningAlarmHigh = nullptr;
647 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800648
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200649 const bool success = sdbusplus::unpackPropertiesNoThrow(
650 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
651 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
652 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
653 warningAlarmLow);
654
655 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700656 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200657 // Check if sensor has critical threshold alarm
658 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
659 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700660 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200661 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700662 }
663 }
664
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500665 // Check if associated inventory item is not functional
666 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
667 {
668 return "Critical";
669 }
670
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200671 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500672 // should override the sensor status, which might be less severe.
673 if (currentHealth == "Warning")
674 {
675 return "Warning";
676 }
677
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200678 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700679 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200680 // Check if sensor has warning threshold alarm
681 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
682 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700683 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200684 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700685 }
686 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500687
James Feist34dd1792019-05-17 14:10:54 -0700688 return "OK";
689}
690
Ed Tanous23a21a12020-07-25 04:45:05 +0000691inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500692 const InventoryItem* inventoryItem)
693{
694 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
695 {
696 switch (inventoryItem->ledState)
697 {
698 case LedState::OFF:
699 sensorJson["IndicatorLED"] = "Off";
700 break;
701 case LedState::ON:
702 sensorJson["IndicatorLED"] = "Lit";
703 break;
704 case LedState::BLINK:
705 sensorJson["IndicatorLED"] = "Blinking";
706 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000707 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500708 break;
709 }
710 }
711}
712
James Feist34dd1792019-05-17 14:10:54 -0700713/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100714 * @brief Builds a json sensor representation of a sensor.
715 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500716 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100717 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700718 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
719 * @param propertiesDict A dictionary of the properties to build the sensor
720 * from.
721 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500722 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
723 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100724 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700725inline void objectPropertiesToJson(
726 std::string_view sensorName, std::string_view sensorType,
727 std::string_view chassisSubNode,
728 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000729 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700730{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700731 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500732 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800733 std::string subNodeEscaped(sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700734 subNodeEscaped.erase(
735 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
736 subNodeEscaped.end());
737
738 // For sensors in SensorCollection we set Id instead of MemberId,
739 // including power sensors.
740 subNodeEscaped += '_';
741 subNodeEscaped += sensorName;
742 sensorJson["Id"] = std::move(subNodeEscaped);
743
Ed Tanous1d7c0052022-08-09 12:32:26 -0700744 std::string sensorNameEs(sensorName);
745 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
746 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500747 }
748 else if (sensorType != "power")
749 {
750 // Set MemberId and Name for non-power sensors. For PowerSupplies and
751 // PowerControl, those properties have more general values because
752 // multiple sensors can be stored in the same JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700753 std::string sensorNameEs(sensorName);
754 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
755 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500756 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700757
Ed Tanous81ce6092020-12-17 16:54:55 +0000758 sensorJson["Status"]["State"] = getState(inventoryItem);
759 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700760 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700761
762 // Parameter to set to override the type we get from dbus, and force it to
763 // int, regardless of what is available. This is used for schemas like fan,
764 // that require integers, not floats.
765 bool forceToInt = false;
766
Anthony Wilson3929aca2019-07-19 15:42:33 -0500767 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700768 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500769 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000770 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000771
Ed Tanous0ec8b832022-03-14 14:56:47 -0700772 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
773 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500774 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000775 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
776 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500777 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000778 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500779 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000780 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500781 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000782
Ed Tanous1d7c0052022-08-09 12:32:26 -0700783 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000784 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200785 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000786 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
787 << sensorType;
788 }
789 else
790 {
791 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200792 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500793 }
794 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700795 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500796 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000797 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700798 // TODO(ed) Documentation says that path should be type fan_tach,
799 // implementation seems to implement fan
800 }
801 else if (sensorType == "fan" || sensorType == "fan_tach")
802 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500803 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000804 sensorJson["ReadingUnits"] = "RPM";
805 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
806 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700807 forceToInt = true;
808 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700809 else if (sensorType == "fan_pwm")
810 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500811 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000812 sensorJson["ReadingUnits"] = "Percent";
813 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
814 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700815 forceToInt = true;
816 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700817 else if (sensorType == "voltage")
818 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500819 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000820 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700821 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700822 else if (sensorType == "power")
823 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700824 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000825 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000826 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500827 // Put multiple "sensors" into a single PowerControl, so have
828 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000829 sensorJson["MemberId"] = "0";
830 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500831 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000832 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700833 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700834 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500835 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700836 }
837 else
838 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500839 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700840 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700841 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700842 else
843 {
844 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
845 return;
846 }
847 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500848 std::vector<
849 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
850 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700851 properties.reserve(7);
852
853 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600854
Ed Tanous1d7c0052022-08-09 12:32:26 -0700855 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500856 {
857 properties.emplace_back(
858 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
859 "/Thresholds/UpperCaution/Reading"_json_pointer);
860 properties.emplace_back(
861 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
862 "/Thresholds/LowerCaution/Reading"_json_pointer);
863 properties.emplace_back(
864 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
865 "/Thresholds/UpperCritical/Reading"_json_pointer);
866 properties.emplace_back(
867 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
868 "/Thresholds/LowerCritical/Reading"_json_pointer);
869 }
870 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600871 {
872 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500873 "WarningHigh",
874 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600875 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500876 "WarningLow",
877 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600878 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500879 "CriticalHigh",
880 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600881 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500882 "CriticalLow",
883 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600884 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700885
Ed Tanous2474adf2018-09-05 16:31:16 -0700886 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
887
Ed Tanous1d7c0052022-08-09 12:32:26 -0700888 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500889 {
890 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500891 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500892 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500893 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800894 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
895 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500896 }
897 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700898 {
899 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500900 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500902 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500904 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 {
906 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500907 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700908 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500909 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 }
911
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 for (const std::tuple<const char*, const char*,
913 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700914 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700915 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700917 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800919 continue;
920 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700921
922 // The property we want to set may be nested json, so use
923 // a json_pointer for easy indexing into the json structure.
924 const nlohmann::json::json_pointer& key = std::get<2>(p);
925
Ed Tanous1d7c0052022-08-09 12:32:26 -0700926 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700927 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800928 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700929 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700930 continue;
931 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700932 if (forceToInt)
933 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700934 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700935 }
936 else
937 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700938 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939 }
940 }
941 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100942}
943
Ed Tanous1d7c0052022-08-09 12:32:26 -0700944/**
945 * @brief Builds a json sensor representation of a sensor.
946 * @param sensorName The name of the sensor to be built
947 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
948 * build
949 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
950 * @param interfacesDict A dictionary of the interfaces and properties of said
951 * interfaces to be built from
952 * @param sensorJson The json object to fill
953 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
954 * be nullptr if no associated inventory item was found.
955 */
956inline void objectInterfacesToJson(
957 const std::string& sensorName, const std::string& sensorType,
958 const std::string& chassisSubNode,
959 const dbus::utility::DBusInteracesMap& interfacesDict,
960 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
961{
962
963 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
Ed Tanous002d39b2022-05-31 08:59:27 -07001116 size_t minNumNeeded =
1117 collection->empty()
1118 ? 0
1119 : collection->size() - *allowedFailures;
1120 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;
Willy Tueddfc432022-09-26 16:46:38 +00001124 boost::urls::url url = crow::utility::urlFromPieces(
1125 "redfish", "v1", "Chassis", sensorsAsyncResp->chassisId,
1126 sensorsAsyncResp->chassisSubNode);
1127 url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1128 .to_string());
1129 redundancy["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -07001130 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1131 redundancy["MinNumNeeded"] = minNumNeeded;
Ed Tanous002d39b2022-05-31 08:59:27 -07001132 redundancy["Mode"] = "N+m";
1133 redundancy["Name"] = name;
1134 redundancy["RedundancySet"] = redfishCollection;
1135 redundancy["Status"]["Health"] = health;
1136 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001137
Ed Tanous002d39b2022-05-31 08:59:27 -07001138 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001139 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001140 });
1141 }
George Liue99073f2022-12-09 11:06:16 +08001142 });
James Feist8bd25cc2019-03-15 15:14:00 -07001143}
1144
Ed Tanousb5a76932020-09-29 16:16:58 -07001145inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001146 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001147{
zhanghch058d1b46d2021-04-01 11:18:24 +08001148 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001149 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001150 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001151 {
1152 sensorHeaders = {"Voltages", "PowerSupplies"};
1153 }
1154 for (const std::string& sensorGroup : sensorHeaders)
1155 {
1156 nlohmann::json::iterator entry = response.find(sensorGroup);
1157 if (entry != response.end())
1158 {
1159 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001160 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001161 return c1["Name"] < c2["Name"];
1162 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001163
1164 // add the index counts to the end of each entry
1165 size_t count = 0;
1166 for (nlohmann::json& sensorJson : *entry)
1167 {
1168 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1169 if (odata == sensorJson.end())
1170 {
1171 continue;
1172 }
1173 std::string* value = odata->get_ptr<std::string*>();
1174 if (value != nullptr)
1175 {
Willy Tueddfc432022-09-26 16:46:38 +00001176 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001177 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001178 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001179 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001180 }
1181 }
1182 }
1183 }
1184}
1185
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001186/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001187 * @brief Finds the inventory item with the specified object path.
1188 * @param inventoryItems D-Bus inventory items associated with sensors.
1189 * @param invItemObjPath D-Bus object path of inventory item.
1190 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001191 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001192inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001193 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001194 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001195{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001196 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001197 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001198 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001199 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001200 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001201 }
1202 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001203 return nullptr;
1204}
1205
1206/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001207 * @brief Finds the inventory item associated with the specified sensor.
1208 * @param inventoryItems D-Bus inventory items associated with sensors.
1209 * @param sensorObjPath D-Bus object path of sensor.
1210 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001211 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001212inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001213 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001214 const std::string& sensorObjPath)
1215{
1216 for (InventoryItem& inventoryItem : *inventoryItems)
1217 {
1218 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1219 {
1220 return &inventoryItem;
1221 }
1222 }
1223 return nullptr;
1224}
1225
1226/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001227 * @brief Finds the inventory item associated with the specified led path.
1228 * @param inventoryItems D-Bus inventory items associated with sensors.
1229 * @param ledObjPath D-Bus object path of led.
1230 * @return Inventory item within vector, or nullptr if no match found.
1231 */
1232inline InventoryItem*
1233 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1234 const std::string& ledObjPath)
1235{
1236 for (InventoryItem& inventoryItem : inventoryItems)
1237 {
1238 if (inventoryItem.ledObjectPath == ledObjPath)
1239 {
1240 return &inventoryItem;
1241 }
1242 }
1243 return nullptr;
1244}
1245
1246/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001247 * @brief Adds inventory item and associated sensor to specified vector.
1248 *
1249 * Adds a new InventoryItem to the vector if necessary. Searches for an
1250 * existing InventoryItem with the specified object path. If not found, one is
1251 * added to the vector.
1252 *
1253 * Next, the specified sensor is added to the set of sensors associated with the
1254 * InventoryItem.
1255 *
1256 * @param inventoryItems D-Bus inventory items associated with sensors.
1257 * @param invItemObjPath D-Bus object path of inventory item.
1258 * @param sensorObjPath D-Bus object path of sensor
1259 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001260inline void addInventoryItem(
1261 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1262 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001263{
1264 // Look for inventory item in vector
1265 InventoryItem* inventoryItem =
1266 findInventoryItem(inventoryItems, invItemObjPath);
1267
1268 // If inventory item doesn't exist in vector, add it
1269 if (inventoryItem == nullptr)
1270 {
1271 inventoryItems->emplace_back(invItemObjPath);
1272 inventoryItem = &(inventoryItems->back());
1273 }
1274
1275 // Add sensor to set of sensors associated with inventory item
1276 inventoryItem->sensors.emplace(sensorObjPath);
1277}
1278
1279/**
1280 * @brief Stores D-Bus data in the specified inventory item.
1281 *
1282 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1283 * specified InventoryItem.
1284 *
1285 * This data is later used to provide sensor property values in the JSON
1286 * response.
1287 *
1288 * @param inventoryItem Inventory item where data will be stored.
1289 * @param interfacesDict Map containing D-Bus interfaces and their properties
1290 * for the specified inventory item.
1291 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001292inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001293 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001294 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001295{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001296 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001297
Ed Tanous9eb808c2022-01-25 10:19:23 -08001298 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001299 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001300 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001301 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001302 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001303 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001304 if (name == "Present")
1305 {
1306 const bool* value = std::get_if<bool>(&dbusValue);
1307 if (value != nullptr)
1308 {
1309 inventoryItem.isPresent = *value;
1310 }
1311 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001312 }
1313 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001314 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001315
Ed Tanous711ac7a2021-12-20 09:34:41 -08001316 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001317 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001318 inventoryItem.isPowerSupply = true;
1319 }
1320
1321 // Get properties from Inventory.Decorator.Asset interface
1322 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1323 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001324 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001325 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001326 if (name == "Manufacturer")
1327 {
1328 const std::string* value =
1329 std::get_if<std::string>(&dbusValue);
1330 if (value != nullptr)
1331 {
1332 inventoryItem.manufacturer = *value;
1333 }
1334 }
1335 if (name == "Model")
1336 {
1337 const std::string* value =
1338 std::get_if<std::string>(&dbusValue);
1339 if (value != nullptr)
1340 {
1341 inventoryItem.model = *value;
1342 }
1343 }
1344 if (name == "SerialNumber")
1345 {
1346 const std::string* value =
1347 std::get_if<std::string>(&dbusValue);
1348 if (value != nullptr)
1349 {
1350 inventoryItem.serialNumber = *value;
1351 }
1352 }
1353 if (name == "PartNumber")
1354 {
1355 const std::string* value =
1356 std::get_if<std::string>(&dbusValue);
1357 if (value != nullptr)
1358 {
1359 inventoryItem.partNumber = *value;
1360 }
1361 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001362 }
1363 }
1364
Ed Tanous711ac7a2021-12-20 09:34:41 -08001365 if (interface ==
1366 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001367 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001368 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001369 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001370 if (name == "Functional")
1371 {
1372 const bool* value = std::get_if<bool>(&dbusValue);
1373 if (value != nullptr)
1374 {
1375 inventoryItem.isFunctional = *value;
1376 }
1377 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001378 }
1379 }
1380 }
1381}
1382
1383/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001384 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001385 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001386 * Uses the specified connections (services) to obtain D-Bus data for inventory
1387 * items associated with sensors. Stores the resulting data in the
1388 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001389 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001390 * This data is later used to provide sensor property values in the JSON
1391 * response.
1392 *
1393 * Finds the inventory item data asynchronously. Invokes callback when data has
1394 * been obtained.
1395 *
1396 * The callback must have the following signature:
1397 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001398 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001399 * @endcode
1400 *
1401 * This function is called recursively, obtaining data asynchronously from one
1402 * connection in each call. This ensures the callback is not invoked until the
1403 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001404 *
1405 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001406 * @param inventoryItems D-Bus inventory items associated with sensors.
1407 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001408 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001409 * @param callback Callback to invoke when inventory data has been obtained.
1410 * @param invConnectionsIndex Current index in invConnections. Only specified
1411 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001412 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001413template <typename Callback>
1414static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001415 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001416 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001417 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1418 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001419{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001420 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001421
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001422 // If no more connections left, call callback
1423 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001424 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001425 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1427 return;
1428 }
1429
1430 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001431 auto it = invConnections->begin();
1432 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001433 if (it != invConnections->end())
1434 {
1435 const std::string& invConnection = *it;
1436
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001437 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001438 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1439 callback{std::forward<Callback>(callback)},
1440 invConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001441 const boost::system::error_code& ec,
Ed Tanousd0090732022-10-04 17:22:56 -07001442 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001443 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001444 if (ec)
1445 {
1446 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001447 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001448 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001449 return;
1450 }
1451
1452 // Loop through returned object paths
1453 for (const auto& objDictEntry : resp)
1454 {
1455 const std::string& objPath =
1456 static_cast<const std::string&>(objDictEntry.first);
1457
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001458 // If this object path is one of the specified inventory items
1459 InventoryItem* inventoryItem =
1460 findInventoryItem(inventoryItems, objPath);
1461 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001462 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001463 // Store inventory data in InventoryItem
1464 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001465 }
1466 }
1467
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001468 // Recurse to get inventory item data from next connection
1469 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001470 invConnections, std::move(callback),
1471 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001472
1473 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001474 };
1475
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001476 // Get all object paths and their interfaces for current connection
1477 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001478 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001479 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001480 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1481 }
1482
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001483 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001484}
1485
1486/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001487 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001488 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001489 * Gets the D-Bus connections (services) that provide data for the inventory
1490 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001491 *
1492 * Finds the connections asynchronously. Invokes callback when information has
1493 * been obtained.
1494 *
1495 * The callback must have the following signature:
1496 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001497 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498 * @endcode
1499 *
1500 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001501 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001502 * @param callback Callback to invoke when connections have been obtained.
1503 */
1504template <typename Callback>
1505static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001506 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1507 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001508 Callback&& callback)
1509{
1510 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1511
1512 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001513 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001514 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001515 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1516 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001517 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1518
George Liue99073f2022-12-09 11:06:16 +08001519 // Make call to ObjectMapper to find all inventory items
1520 dbus::utility::getSubTree(
1521 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001522 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1523 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001524 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001525 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001526 // Response handler for parsing output from GetSubTree
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001527 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1528 if (ec)
1529 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001530 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001531 BMCWEB_LOG_ERROR
1532 << "getInventoryItemsConnections respHandler DBus error " << ec;
1533 return;
1534 }
1535
1536 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001537 std::shared_ptr<std::set<std::string>> invConnections =
1538 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001539
1540 // Loop through objects from GetSubTree
1541 for (const std::pair<
1542 std::string,
1543 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1544 object : subtree)
1545 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001546 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001547 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001548 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001549 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001550 // Store all connections to inventory item
1551 for (const std::pair<std::string, std::vector<std::string>>&
1552 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001553 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001554 const std::string& invConnection = objData.first;
1555 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001556 }
1557 }
1558 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001559
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 callback(invConnections);
1561 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001562 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001563 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1564}
1565
1566/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001567 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001568 *
1569 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001570 * inventory items. Then finds the associations from those inventory items to
1571 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001572 *
1573 * Finds the inventory items asynchronously. Invokes callback when information
1574 * has been obtained.
1575 *
1576 * The callback must have the following signature:
1577 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001578 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001579 * @endcode
1580 *
1581 * @param sensorsAsyncResp Pointer to object holding response data.
1582 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001583 * implements ObjectManager.
1584 * @param callback Callback to invoke when inventory items have been obtained.
1585 */
1586template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001587static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001588 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001589 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 Callback&& callback)
1591{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001592 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001593
1594 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001595 auto respHandler =
1596 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001597 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001598 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001599 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001600 if (ec)
1601 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001602 BMCWEB_LOG_ERROR
1603 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001604 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001605 return;
1606 }
1607
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001608 // Create vector to hold list of inventory items
1609 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1610 std::make_shared<std::vector<InventoryItem>>();
1611
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001612 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 std::string sensorAssocPath;
1614 sensorAssocPath.reserve(128); // avoid memory allocations
1615 for (const auto& objDictEntry : resp)
1616 {
1617 const std::string& objPath =
1618 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001619
1620 // If path is inventory association for one of the specified sensors
1621 for (const std::string& sensorName : *sensorNames)
1622 {
1623 sensorAssocPath = sensorName;
1624 sensorAssocPath += "/inventory";
1625 if (objPath == sensorAssocPath)
1626 {
1627 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001628 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001629 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001630 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001632 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001634 if (valueName == "endpoints")
1635 {
1636 const std::vector<std::string>* endpoints =
1637 std::get_if<std::vector<std::string>>(
1638 &value);
1639 if ((endpoints != nullptr) &&
1640 !endpoints->empty())
1641 {
1642 // Add inventory item to vector
1643 const std::string& invItemPath =
1644 endpoints->front();
1645 addInventoryItem(inventoryItems,
1646 invItemPath,
1647 sensorName);
1648 }
1649 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001650 }
1651 }
1652 }
1653 break;
1654 }
1655 }
1656 }
1657
Anthony Wilsond5005492019-07-31 16:34:17 -05001658 // Now loop through the returned object paths again, this time to
1659 // find the leds associated with the inventory items we just found
1660 std::string inventoryAssocPath;
1661 inventoryAssocPath.reserve(128); // avoid memory allocations
1662 for (const auto& objDictEntry : resp)
1663 {
1664 const std::string& objPath =
1665 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001666
1667 for (InventoryItem& inventoryItem : *inventoryItems)
1668 {
1669 inventoryAssocPath = inventoryItem.objectPath;
1670 inventoryAssocPath += "/leds";
1671 if (objPath == inventoryAssocPath)
1672 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001673 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001674 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001675 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001676 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001677 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001678 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001679 if (valueName == "endpoints")
1680 {
1681 const std::vector<std::string>* endpoints =
1682 std::get_if<std::vector<std::string>>(
1683 &value);
1684 if ((endpoints != nullptr) &&
1685 !endpoints->empty())
1686 {
1687 // Add inventory item to vector
1688 // Store LED path in inventory item
1689 const std::string& ledPath =
1690 endpoints->front();
1691 inventoryItem.ledObjectPath = ledPath;
1692 }
1693 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001694 }
1695 }
1696 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001697
Anthony Wilsond5005492019-07-31 16:34:17 -05001698 break;
1699 }
1700 }
1701 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001702 callback(inventoryItems);
1703 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001704 };
1705
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001706 // Call GetManagedObjects on the ObjectMapper to get all associations
1707 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001708 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001709 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1710
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001711 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001712}
1713
1714/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001715 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1716 *
1717 * Uses the specified connections (services) to obtain D-Bus data for inventory
1718 * item leds associated with sensors. Stores the resulting data in the
1719 * inventoryItems vector.
1720 *
1721 * This data is later used to provide sensor property values in the JSON
1722 * response.
1723 *
1724 * Finds the inventory item led data asynchronously. Invokes callback when data
1725 * has been obtained.
1726 *
1727 * The callback must have the following signature:
1728 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001729 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001730 * @endcode
1731 *
1732 * This function is called recursively, obtaining data asynchronously from one
1733 * connection in each call. This ensures the callback is not invoked until the
1734 * last asynchronous function has completed.
1735 *
1736 * @param sensorsAsyncResp Pointer to object holding response data.
1737 * @param inventoryItems D-Bus inventory items associated with sensors.
1738 * @param ledConnections Connections that provide data for the inventory leds.
1739 * @param callback Callback to invoke when inventory data has been obtained.
1740 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1741 * in recursive calls to this function.
1742 */
1743template <typename Callback>
1744void getInventoryLedData(
1745 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1746 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001747 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001748 Callback&& callback, size_t ledConnectionsIndex = 0)
1749{
1750 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1751
1752 // If no more connections left, call callback
1753 if (ledConnectionsIndex >= ledConnections->size())
1754 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001755 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001756 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1757 return;
1758 }
1759
1760 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001761 auto it = ledConnections->begin();
1762 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001763 if (it != ledConnections->end())
1764 {
1765 const std::string& ledPath = (*it).first;
1766 const std::string& ledConnection = (*it).second;
1767 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001768 auto respHandler =
1769 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001770 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001771 const boost::system::error_code& ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001772 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1773 if (ec)
1774 {
1775 BMCWEB_LOG_ERROR
1776 << "getInventoryLedData respHandler DBus error " << ec;
1777 messages::internalError(sensorsAsyncResp->asyncResp->res);
1778 return;
1779 }
1780
1781 BMCWEB_LOG_DEBUG << "Led state: " << state;
1782 // Find inventory item with this LED object path
1783 InventoryItem* inventoryItem =
1784 findInventoryItemForLed(*inventoryItems, ledPath);
1785 if (inventoryItem != nullptr)
1786 {
1787 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001788 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001789 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001790 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001791 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001792 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001793 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001794 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001795 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001796 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001797 {
1798 inventoryItem->ledState = LedState::OFF;
1799 }
1800 else
1801 {
1802 inventoryItem->ledState = LedState::UNKNOWN;
1803 }
1804 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001805
Ed Tanous002d39b2022-05-31 08:59:27 -07001806 // Recurse to get LED data from next connection
1807 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1808 ledConnections, std::move(callback),
1809 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001810
Ed Tanous002d39b2022-05-31 08:59:27 -07001811 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1812 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001813
1814 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001815 sdbusplus::asio::getProperty<std::string>(
1816 *crow::connections::systemBus, ledConnection, ledPath,
1817 "xyz.openbmc_project.Led.Physical", "State",
1818 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001819 }
1820
1821 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1822}
1823
1824/**
1825 * @brief Gets LED data for LEDs associated with given inventory items.
1826 *
1827 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1828 * associated with the specified inventory items. Then gets the LED data from
1829 * each connection and stores it in the inventory item.
1830 *
1831 * This data is later used to provide sensor property values in the JSON
1832 * response.
1833 *
1834 * Finds the LED data asynchronously. Invokes callback when information has
1835 * been obtained.
1836 *
1837 * The callback must have the following signature:
1838 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001839 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001840 * @endcode
1841 *
1842 * @param sensorsAsyncResp Pointer to object holding response data.
1843 * @param inventoryItems D-Bus inventory items associated with sensors.
1844 * @param callback Callback to invoke when inventory items have been obtained.
1845 */
1846template <typename Callback>
1847void getInventoryLeds(
1848 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1849 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1850 Callback&& callback)
1851{
1852 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1853
1854 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001855 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001856 "xyz.openbmc_project.Led.Physical"};
1857
George Liue99073f2022-12-09 11:06:16 +08001858 // Make call to ObjectMapper to find all inventory items
1859 dbus::utility::getSubTree(
1860 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001861 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1862 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001863 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001864 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001865 // Response handler for parsing output from GetSubTree
Anthony Wilsond5005492019-07-31 16:34:17 -05001866 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1867 if (ec)
1868 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001869 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001870 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1871 << ec;
1872 return;
1873 }
1874
1875 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001876 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1877 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001878
1879 // Loop through objects from GetSubTree
1880 for (const std::pair<
1881 std::string,
1882 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1883 object : subtree)
1884 {
1885 // Check if object path is LED for one of the specified inventory
1886 // items
1887 const std::string& ledPath = object.first;
1888 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1889 {
1890 // Add mapping from ledPath to connection
1891 const std::string& connection = object.second.begin()->first;
1892 (*ledConnections)[ledPath] = connection;
1893 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1894 << connection;
1895 }
1896 }
1897
1898 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1899 std::move(callback));
1900 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001901 });
Anthony Wilsond5005492019-07-31 16:34:17 -05001902 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1903}
1904
1905/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001906 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1907 *
1908 * Uses the specified connections (services) (currently assumes just one) to
1909 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1910 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1911 *
1912 * This data is later used to provide sensor property values in the JSON
1913 * response.
1914 *
1915 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1916 * when data has been obtained.
1917 *
1918 * The callback must have the following signature:
1919 * @code
1920 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1921 * @endcode
1922 *
1923 * @param sensorsAsyncResp Pointer to object holding response data.
1924 * @param inventoryItems D-Bus inventory items associated with sensors.
1925 * @param psAttributesConnections Connections that provide data for the Power
1926 * Supply Attributes
1927 * @param callback Callback to invoke when data has been obtained.
1928 */
1929template <typename Callback>
1930void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001931 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001932 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001933 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001934 Callback&& callback)
1935{
1936 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1937
1938 if (psAttributesConnections.empty())
1939 {
1940 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1941 callback(inventoryItems);
1942 return;
1943 }
1944
1945 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001946 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001947
1948 const std::string& psAttributesPath = (*it).first;
1949 const std::string& psAttributesConnection = (*it).second;
1950
1951 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001952 auto respHandler =
1953 [sensorsAsyncResp, inventoryItems,
1954 callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001955 const boost::system::error_code& ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001956 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1957 if (ec)
1958 {
1959 BMCWEB_LOG_ERROR
1960 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001961 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001962 return;
1963 }
1964
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001965 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1966 // Store value in Power Supply Inventory Items
1967 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001968 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001969 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001970 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001971 inventoryItem.powerSupplyEfficiencyPercent =
1972 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001973 }
1974 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001975
1976 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1977 callback(inventoryItems);
1978 };
1979
1980 // Get the DeratingFactor property for the PowerSupplyAttributes
1981 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001982 sdbusplus::asio::getProperty<uint32_t>(
1983 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1984 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
1985 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05001986
1987 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
1988}
1989
1990/**
1991 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
1992 *
1993 * Gets the D-Bus connection (service) that provides Power Supply Attributes
1994 * data. Then gets the Power Supply Attributes data from the connection
1995 * (currently just assumes 1 connection) and stores the data in the inventory
1996 * item.
1997 *
1998 * This data is later used to provide sensor property values in the JSON
1999 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2000 *
2001 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2002 * when information has been obtained.
2003 *
2004 * The callback must have the following signature:
2005 * @code
2006 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2007 * @endcode
2008 *
2009 * @param sensorsAsyncResp Pointer to object holding response data.
2010 * @param inventoryItems D-Bus inventory items associated with sensors.
2011 * @param callback Callback to invoke when data has been obtained.
2012 */
2013template <typename Callback>
2014void getPowerSupplyAttributes(
2015 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2016 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2017 Callback&& callback)
2018{
2019 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2020
2021 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002022 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002023 {
2024 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2025 callback(inventoryItems);
2026 return;
2027 }
2028
George Liue99073f2022-12-09 11:06:16 +08002029 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002030 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2031
George Liue99073f2022-12-09 11:06:16 +08002032 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2033 dbus::utility::getSubTree(
2034 "/xyz/openbmc_project", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002035 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2036 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002037 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002038 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002039 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07002040 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2041 if (ec)
2042 {
2043 messages::internalError(sensorsAsyncResp->asyncResp->res);
2044 BMCWEB_LOG_ERROR
2045 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2046 return;
2047 }
2048 if (subtree.empty())
2049 {
2050 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2051 callback(inventoryItems);
2052 return;
2053 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002054
Ed Tanous002d39b2022-05-31 08:59:27 -07002055 // Currently we only support 1 power supply attribute, use this for
2056 // all the power supplies. Build map of object path to connection.
2057 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002058 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002059
Ed Tanous002d39b2022-05-31 08:59:27 -07002060 if (subtree[0].first.empty() || subtree[0].second.empty())
2061 {
2062 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2063 callback(inventoryItems);
2064 return;
2065 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002066
Ed Tanous002d39b2022-05-31 08:59:27 -07002067 const std::string& psAttributesPath = subtree[0].first;
2068 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002069
Ed Tanous002d39b2022-05-31 08:59:27 -07002070 if (connection.empty())
2071 {
2072 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2073 callback(inventoryItems);
2074 return;
2075 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002076
Ed Tanous002d39b2022-05-31 08:59:27 -07002077 psAttributesConnections[psAttributesPath] = connection;
2078 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2079 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002080
Ed Tanous002d39b2022-05-31 08:59:27 -07002081 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2082 psAttributesConnections,
2083 std::move(callback));
2084 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08002085 });
Gunnar Mills42cbe532019-08-15 15:26:54 -05002086 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2087}
2088
2089/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002090 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002091 *
2092 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002093 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002094 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002095 * This data is later used to provide sensor property values in the JSON
2096 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002097 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002098 * Finds the inventory items asynchronously. Invokes callback when the
2099 * inventory items have been obtained.
2100 *
2101 * The callback must have the following signature:
2102 * @code
2103 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2104 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002105 *
2106 * @param sensorsAsyncResp Pointer to object holding response data.
2107 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002108 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002109 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002110 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002111template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002112static void
2113 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2114 const std::shared_ptr<std::set<std::string>> sensorNames,
2115 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002116{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002117 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2118 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002119 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002120 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002121 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2122 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002123 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002124 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002125 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002126 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2127 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2128 callback{std::move(callback)}]() {
2129 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002130
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2132 callback{std::move(callback)}]() {
2133 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2134 // Find Power Supply Attributes and get the data
2135 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2136 std::move(callback));
2137 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002138 };
2139
Ed Tanous002d39b2022-05-31 08:59:27 -07002140 // Find led connections and get the data
2141 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2142 std::move(getInventoryLedsCb));
2143 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2144 };
2145
2146 // Get inventory item data from connections
2147 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002148 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002149 std::move(getInventoryItemsDataCb));
2150 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002151 };
2152
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 // Get connections that provide inventory item data
2154 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2155 std::move(getInventoryItemsConnectionsCb));
2156 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2157 };
2158
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002159 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002160 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002161 std::move(getInventoryItemAssociationsCb));
2162 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2163}
2164
2165/**
2166 * @brief Returns JSON PowerSupply object for the specified inventory item.
2167 *
2168 * Searches for a JSON PowerSupply object that matches the specified inventory
2169 * item. If one is not found, a new PowerSupply object is added to the JSON
2170 * array.
2171 *
2172 * Multiple sensors are often associated with one power supply inventory item.
2173 * As a result, multiple sensor values are stored in one JSON PowerSupply
2174 * object.
2175 *
2176 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2177 * @param inventoryItem Inventory item for the power supply.
2178 * @param chassisId Chassis that contains the power supply.
2179 * @return JSON PowerSupply object for the specified inventory item.
2180 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002181inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002182 const InventoryItem& inventoryItem,
2183 const std::string& chassisId)
2184{
2185 // Check if matching PowerSupply object already exists in JSON array
2186 for (nlohmann::json& powerSupply : powerSupplyArray)
2187 {
George Liu3e35c762023-03-08 16:56:38 +08002188 if (powerSupply["Name"] == inventoryItem.name)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002189 {
2190 return powerSupply;
2191 }
2192 }
2193
2194 // Add new PowerSupply object to JSON array
2195 powerSupplyArray.push_back({});
2196 nlohmann::json& powerSupply = powerSupplyArray.back();
Willy Tueddfc432022-09-26 16:46:38 +00002197 boost::urls::url url = crow::utility::urlFromPieces(
2198 "redfish", "v1", "Chassis", chassisId, "Power");
2199 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2200 powerSupply["@odata.id"] = std::move(url);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002201 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2202 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2203 powerSupply["Model"] = inventoryItem.model;
2204 powerSupply["PartNumber"] = inventoryItem.partNumber;
2205 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002206 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002207
Gunnar Mills42cbe532019-08-15 15:26:54 -05002208 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2209 {
2210 powerSupply["EfficiencyPercent"] =
2211 inventoryItem.powerSupplyEfficiencyPercent;
2212 }
2213
2214 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002215 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2216 powerSupply["Status"]["Health"] = health;
2217
2218 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002219}
2220
2221/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002222 * @brief Gets the values of the specified sensors.
2223 *
2224 * Stores the results as JSON in the SensorsAsyncResp.
2225 *
2226 * Gets the sensor values asynchronously. Stores the results later when the
2227 * information has been obtained.
2228 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002229 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002230 *
2231 * To minimize the number of DBus calls, the DBus method
2232 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2233 * values of all sensors provided by a connection (service).
2234 *
2235 * The connections set contains all the connections that provide sensor values.
2236 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002237 * The InventoryItem vector contains D-Bus inventory items associated with the
2238 * sensors. Inventory item data is needed for some Redfish sensor properties.
2239 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002240 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002241 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002242 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002243 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002244 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002245 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002246inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002247 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002248 const std::shared_ptr<std::set<std::string>>& sensorNames,
2249 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002250 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002251{
2252 BMCWEB_LOG_DEBUG << "getSensorData enter";
2253 // Get managed objects from all services exposing sensors
2254 for (const std::string& connection : connections)
2255 {
2256 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002257 auto getManagedObjectsCb =
2258 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002259 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002260 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002261 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2262 if (ec)
2263 {
2264 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002265 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002266 return;
2267 }
2268 // Go through all objects and update response with sensor data
2269 for (const auto& objDictEntry : resp)
2270 {
2271 const std::string& objPath =
2272 static_cast<const std::string&>(objDictEntry.first);
2273 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2274 << objPath;
2275
Shawn McCarneyde629b62019-03-08 10:42:51 -06002276 std::vector<std::string> split;
2277 // Reserve space for
2278 // /xyz/openbmc_project/sensors/<name>/<subname>
2279 split.reserve(6);
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002280 // NOLINTNEXTLINE
2281 bmcweb::split(split, objPath, '/');
Shawn McCarneyde629b62019-03-08 10:42:51 -06002282 if (split.size() < 6)
2283 {
2284 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2285 << objPath;
2286 continue;
2287 }
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002288 // These indexes aren't intuitive, as split puts an empty
Shawn McCarneyde629b62019-03-08 10:42:51 -06002289 // string at the beginning
2290 const std::string& sensorType = split[4];
2291 const std::string& sensorName = split[5];
2292 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2293 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002294 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002295 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002296 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002297 continue;
2298 }
2299
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002300 // Find inventory item (if any) associated with sensor
2301 InventoryItem* inventoryItem =
2302 findInventoryItemForSensor(inventoryItems, objPath);
2303
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002304 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002305 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002306
2307 nlohmann::json* sensorJson = nullptr;
2308
Nan Zhou928fefb2022-03-28 08:45:00 -07002309 if (sensorSchema == sensors::node::sensors &&
2310 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002311 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002312 std::string sensorTypeEscaped(sensorType);
2313 sensorTypeEscaped.erase(
2314 std::remove(sensorTypeEscaped.begin(),
2315 sensorTypeEscaped.end(), '_'),
2316 sensorTypeEscaped.end());
2317 std::string sensorId(sensorTypeEscaped);
2318 sensorId += "_";
2319 sensorId += sensorName;
2320
zhanghch058d1b46d2021-04-01 11:18:24 +08002321 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002322 crow::utility::urlFromPieces(
2323 "redfish", "v1", "Chassis",
2324 sensorsAsyncResp->chassisId,
2325 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002326 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002327 }
2328 else
2329 {
Ed Tanous271584a2019-07-09 16:24:22 -07002330 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002331 if (sensorsAsyncResp->efficientExpand)
2332 {
2333 fieldName = "Members";
2334 }
2335 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002336 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002337 fieldName = "Temperatures";
2338 }
2339 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2340 sensorType == "fan_pwm")
2341 {
2342 fieldName = "Fans";
2343 }
2344 else if (sensorType == "voltage")
2345 {
2346 fieldName = "Voltages";
2347 }
2348 else if (sensorType == "power")
2349 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002350 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002351 {
2352 fieldName = "PowerControl";
2353 }
2354 else if ((inventoryItem != nullptr) &&
2355 (inventoryItem->isPowerSupply))
2356 {
2357 fieldName = "PowerSupplies";
2358 }
2359 else
2360 {
2361 // Other power sensors are in SensorCollection
2362 continue;
2363 }
2364 }
2365 else
2366 {
2367 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2368 << sensorType;
2369 continue;
2370 }
2371
2372 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002373 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002374 if (fieldName == "PowerControl")
2375 {
2376 if (tempArray.empty())
2377 {
2378 // Put multiple "sensors" into a single
2379 // PowerControl. Follows MemberId naming and
2380 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002381 nlohmann::json::object_t power;
Willy Tueddfc432022-09-26 16:46:38 +00002382 boost::urls::url url = crow::utility::urlFromPieces(
2383 "redfish", "v1", "Chassis",
2384 sensorsAsyncResp->chassisId,
2385 sensorsAsyncResp->chassisSubNode);
2386 url.set_fragment((""_json_pointer / fieldName / "0")
2387 .to_string());
2388 power["@odata.id"] = std::move(url);
Ed Tanous14766872022-03-15 10:44:42 -07002389 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002390 }
2391 sensorJson = &(tempArray.back());
2392 }
2393 else if (fieldName == "PowerSupplies")
2394 {
2395 if (inventoryItem != nullptr)
2396 {
2397 sensorJson =
2398 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002399 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002400 }
2401 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002402 else if (fieldName == "Members")
2403 {
Ed Tanous677bb752022-09-15 10:52:19 -07002404 std::string sensorTypeEscaped(sensorType);
2405 sensorTypeEscaped.erase(
2406 std::remove(sensorTypeEscaped.begin(),
2407 sensorTypeEscaped.end(), '_'),
2408 sensorTypeEscaped.end());
2409 std::string sensorId(sensorTypeEscaped);
2410 sensorId += "_";
2411 sensorId += sensorName;
2412
Ed Tanous14766872022-03-15 10:44:42 -07002413 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002414 member["@odata.id"] = crow::utility::urlFromPieces(
2415 "redfish", "v1", "Chassis",
2416 sensorsAsyncResp->chassisId,
2417 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002418 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002419 sensorJson = &(tempArray.back());
2420 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002421 else
2422 {
Ed Tanous14766872022-03-15 10:44:42 -07002423 nlohmann::json::object_t member;
Willy Tueddfc432022-09-26 16:46:38 +00002424 boost::urls::url url = crow::utility::urlFromPieces(
2425 "redfish", "v1", "Chassis",
2426 sensorsAsyncResp->chassisId,
2427 sensorsAsyncResp->chassisSubNode);
2428 url.set_fragment(
2429 (""_json_pointer / fieldName).to_string());
2430 member["@odata.id"] = std::move(url);
Ed Tanous14766872022-03-15 10:44:42 -07002431 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002432 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002433 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002434 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002435
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002436 if (sensorJson != nullptr)
2437 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002438 objectInterfacesToJson(sensorName, sensorType,
2439 sensorsAsyncResp->chassisSubNode,
2440 objDictEntry.second, *sensorJson,
2441 inventoryItem);
2442
2443 std::string path = "/xyz/openbmc_project/sensors/";
2444 path += sensorType;
2445 path += "/";
2446 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002447 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002448 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002449 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002450 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002451 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002452 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002453 if (sensorsAsyncResp->chassisSubNode ==
2454 sensors::node::sensors &&
2455 sensorsAsyncResp->efficientExpand)
2456 {
2457 sensorsAsyncResp->asyncResp->res
2458 .jsonValue["Members@odata.count"] =
2459 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2460 .size();
2461 }
2462 else if (sensorsAsyncResp->chassisSubNode ==
2463 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002464 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002465 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002466 }
James Feist8bd25cc2019-03-15 15:14:00 -07002467 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002468 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2469 };
2470
Shawn McCarneyde629b62019-03-08 10:42:51 -06002471 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002472 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002473 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002474 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002475 BMCWEB_LOG_DEBUG << "getSensorData exit";
2476}
2477
Nan Zhoufe04d492022-06-22 17:10:41 +00002478inline void
2479 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2480 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002481{
Nan Zhoufe04d492022-06-22 17:10:41 +00002482 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2483 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002484 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002485 auto getInventoryItemsCb =
2486 [sensorsAsyncResp, sensorNames,
2487 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2488 inventoryItems) {
2489 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2490 // Get sensor data and store results in JSON
2491 getSensorData(sensorsAsyncResp, sensorNames, connections,
2492 inventoryItems);
2493 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002494 };
2495
Ed Tanousd0090732022-10-04 17:22:56 -07002496 // Get inventory items associated with sensors
2497 getInventoryItems(sensorsAsyncResp, sensorNames,
2498 std::move(getInventoryItemsCb));
2499
Ed Tanous002d39b2022-05-31 08:59:27 -07002500 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2501 };
2502
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002503 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002504 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002505}
2506
Shawn McCarneyde629b62019-03-08 10:42:51 -06002507/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002508 * @brief Entry point for retrieving sensors data related to requested
2509 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002510 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002511 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002512inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002513 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002514{
2515 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002516 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002517 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002518 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002519 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2520 processSensorList(sensorsAsyncResp, sensorNames);
2521 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2522 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002523 // SensorCollection doesn't contain the Redundancy property
2524 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2525 {
2526 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2527 nlohmann::json::array();
2528 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002529 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002530 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2531 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2532 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002533 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002534}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002535
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302536/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002537 * @brief Find the requested sensorName in the list of all sensors supplied by
2538 * the chassis node
2539 *
2540 * @param sensorName The sensor name supplied in the PATCH request
2541 * @param sensorsList The list of sensors managed by the chassis node
2542 * @param sensorsModified The list of sensors that were found as a result of
2543 * repeated calls to this function
2544 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002545inline bool
2546 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002547 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002548 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002549{
Nan Zhoufe04d492022-06-22 17:10:41 +00002550 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002551 {
George Liu28aa8de2021-02-01 15:13:30 +08002552 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002553 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002554 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002555 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002556 continue;
2557 }
2558 if (thisSensorName == sensorName)
2559 {
2560 sensorsModified.emplace(chassisSensor);
2561 return true;
2562 }
2563 }
2564 return false;
2565}
2566
Ed Tanousc71d6122022-11-29 14:10:32 -08002567inline std::pair<std::string, std::string>
2568 splitSensorNameAndType(std::string_view sensorId)
2569{
2570 size_t index = sensorId.find('_');
2571 if (index == std::string::npos)
2572 {
2573 return std::make_pair<std::string, std::string>("", "");
2574 }
2575 std::string sensorType{sensorId.substr(0, index)};
2576 std::string sensorName{sensorId.substr(index + 1)};
2577 // fan_pwm and fan_tach need special handling
2578 if (sensorType == "fantach" || sensorType == "fanpwm")
2579 {
2580 sensorType.insert(3, 1, '_');
2581 }
2582 return std::make_pair(sensorType, sensorName);
2583}
2584
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002585/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302586 * @brief Entry point for overriding sensor values of given sensor
2587 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002588 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002589 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002590 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302591 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002592inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002593 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002594 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002595 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302596{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002597 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002598 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302599
Ed Tanous543f4402022-01-06 13:12:53 -08002600 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302601 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302602 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002603 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302604 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302605 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302606 if (collectionItems.first == "Temperatures")
2607 {
2608 propertyValueName = "ReadingCelsius";
2609 }
2610 else if (collectionItems.first == "Fans")
2611 {
2612 propertyValueName = "Reading";
2613 }
2614 else
2615 {
2616 propertyValueName = "ReadingVolts";
2617 }
2618 for (auto& item : collectionItems.second)
2619 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002620 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2621 "MemberId", memberId, propertyValueName,
2622 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302623 {
2624 return;
2625 }
2626 overrideMap.emplace(memberId,
2627 std::make_pair(value, collectionItems.first));
2628 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302629 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002630
Ed Tanous002d39b2022-05-31 08:59:27 -07002631 auto getChassisSensorListCb =
2632 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002633 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002634 // Match sensor names in the PATCH request to those managed by the
2635 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002636 const std::shared_ptr<std::set<std::string>> sensorNames =
2637 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302638 for (const auto& item : overrideMap)
2639 {
2640 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002641 std::pair<std::string, std::string> sensorNameType =
2642 splitSensorNameAndType(sensor);
2643 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2644 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302645 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302646 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002647 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302648 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302649 return;
2650 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302651 }
2652 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002653 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002654 [sensorAsyncResp,
2655 overrideMap](const std::set<std::string>& /*connections*/,
2656 const std::set<std::pair<std::string, std::string>>&
2657 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002658 if (objectsWithConnection.size() != overrideMap.size())
2659 {
2660 BMCWEB_LOG_INFO
2661 << "Unable to find all objects with proper connection "
2662 << objectsWithConnection.size() << " requested "
2663 << overrideMap.size() << "\n";
2664 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2665 sensorAsyncResp->chassisSubNode ==
2666 sensors::node::thermal
2667 ? "Temperatures"
2668 : "Voltages",
2669 "Count");
2670 return;
2671 }
2672 for (const auto& item : objectsWithConnection)
2673 {
2674 sdbusplus::message::object_path path(item.first);
2675 std::string sensorName = path.filename();
2676 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302677 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002678 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302679 return;
2680 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302681
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002682 const auto& iterator = overrideMap.find(sensorName);
2683 if (iterator == overrideMap.end())
2684 {
2685 BMCWEB_LOG_INFO << "Unable to find sensor object"
2686 << item.first << "\n";
2687 messages::internalError(sensorAsyncResp->asyncResp->res);
2688 return;
2689 }
2690 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002691 [sensorAsyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002692 if (ec)
2693 {
2694 if (ec.value() ==
2695 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002696 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002697 BMCWEB_LOG_WARNING
2698 << "Manufacturing mode is not Enabled...can't "
2699 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002700
Ed Tanous002d39b2022-05-31 08:59:27 -07002701 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002702 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002703 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002704 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002705 BMCWEB_LOG_DEBUG
2706 << "setOverrideValueStatus DBUS error: " << ec;
2707 messages::internalError(
2708 sensorAsyncResp->asyncResp->res);
2709 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002710 },
2711 item.second, item.first, "org.freedesktop.DBus.Properties",
2712 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002713 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002714 }
2715 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302716 // Get object with connection for the given sensor name
2717 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2718 std::move(getObjectsWithConnectionCb));
2719 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302720 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002721 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2722 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2723 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302724}
2725
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002726/**
2727 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2728 * path of the sensor.
2729 *
2730 * Function builds valid Redfish response for sensor query of given chassis and
2731 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2732 * it to caller in a callback.
2733 *
2734 * @param chassis Chassis for which retrieval should be performed
2735 * @param node Node (group) of sensors. See sensors::node for supported values
2736 * @param mapComplete Callback to be called with retrieval result
2737 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002738inline void retrieveUriToDbusMap(const std::string& chassis,
2739 const std::string& node,
2740 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002741{
Ed Tanous02da7c52022-02-27 00:09:02 -08002742 decltype(sensors::paths)::const_iterator pathIt =
2743 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2744 [&node](auto&& val) { return val.first == node; });
2745 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002746 {
2747 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2748 mapComplete(boost::beast::http::status::bad_request, {});
2749 return;
2750 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002751
Nan Zhou72374eb2022-01-27 17:06:51 -08002752 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002753 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2754 const boost::beast::http::status status,
2755 const std::map<std::string, std::string>& uriToDbus) {
2756 mapCompleteCb(status, uriToDbus);
2757 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002758
2759 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002760 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002761 getChassisData(resp);
2762}
2763
Nan Zhoubacb2162022-04-06 11:28:32 -07002764namespace sensors
2765{
Nan Zhou928fefb2022-03-28 08:45:00 -07002766
Nan Zhoubacb2162022-04-06 11:28:32 -07002767inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002768 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2769 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002770 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002771{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002772 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002773
Ed Tanousc1d019a2022-08-06 09:36:06 -07002774 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2775 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002776 {
2777 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2778
2779 sdbusplus::message::object_path path(sensor);
2780 std::string sensorName = path.filename();
2781 if (sensorName.empty())
2782 {
2783 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002784 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002785 return;
2786 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002787 std::string type = path.parent_path().filename();
2788 // fan_tach has an underscore in it, so remove it to "normalize" the
2789 // type in the URI
2790 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2791
Ed Tanous14766872022-03-15 10:44:42 -07002792 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002793 std::string id = type;
2794 id += "_";
2795 id += sensorName;
2796 member["@odata.id"] = crow::utility::urlFromPieces(
2797 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2798
Ed Tanous14766872022-03-15 10:44:42 -07002799 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002800 }
2801
Ed Tanousc1d019a2022-08-06 09:36:06 -07002802 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002803 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2804}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002805
Nan Zhoude167a62022-06-01 04:47:45 +00002806inline void
2807 handleSensorCollectionGet(App& app, const crow::Request& req,
2808 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2809 const std::string& chassisId)
2810{
2811 query_param::QueryCapabilities capabilities = {
2812 .canDelegateExpandLevel = 1,
2813 };
2814 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002815 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002816 delegatedQuery, capabilities))
2817 {
2818 return;
2819 }
2820
2821 if (delegatedQuery.expandType != query_param::ExpandType::None)
2822 {
2823 // we perform efficient expand.
2824 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2825 aResp, chassisId, sensors::dbus::sensorPaths,
2826 sensors::node::sensors,
2827 /*efficientExpand=*/true);
2828 getChassisData(asyncResp);
2829
2830 BMCWEB_LOG_DEBUG
2831 << "SensorCollection doGet exit via efficient expand handler";
2832 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002833 }
Nan Zhoude167a62022-06-01 04:47:45 +00002834
Nan Zhoude167a62022-06-01 04:47:45 +00002835 // We get all sensors as hyperlinkes in the chassis (this
2836 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002837 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2838 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2839 sensors::node::sensors));
2840}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002841
Ed Tanousc1d019a2022-08-06 09:36:06 -07002842inline void
2843 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2844 const std::string& sensorPath,
2845 const ::dbus::utility::MapperGetObject& mapperResponse)
2846{
2847 if (mapperResponse.size() != 1)
2848 {
2849 messages::internalError(asyncResp->res);
2850 return;
2851 }
2852 const auto& valueIface = *mapperResponse.begin();
2853 const std::string& connectionName = valueIface.first;
2854 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2855 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002856
2857 sdbusplus::asio::getAllProperties(
2858 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002859 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002860 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002861 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2862 if (ec)
2863 {
2864 messages::internalError(asyncResp->res);
2865 return;
2866 }
2867 sdbusplus::message::object_path path(sensorPath);
2868 std::string name = path.filename();
2869 path = path.parent_path();
2870 std::string type = path.filename();
2871 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2872 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002873 });
Nan Zhoude167a62022-06-01 04:47:45 +00002874}
2875
Nan Zhoue6bd8462022-06-01 04:35:35 +00002876inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002877 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002878 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002879 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002880{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002881 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002882 {
2883 return;
2884 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002885 std::pair<std::string, std::string> nameType =
2886 splitSensorNameAndType(sensorId);
2887 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002888 {
2889 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2890 return;
2891 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002892
Ed Tanous677bb752022-09-15 10:52:19 -07002893 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2894 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002895
Nan Zhoue6bd8462022-06-01 04:35:35 +00002896 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002897
George Liu2b731192023-01-11 16:27:13 +08002898 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002899 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002900 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2901 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002902 // Get a list of all of the sensors that implement Sensor.Value
2903 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002904 ::dbus::utility::getDbusObject(
2905 sensorPath, interfaces,
Ed Tanousc71d6122022-11-29 14:10:32 -08002906 [asyncResp,
George Liu2b731192023-01-11 16:27:13 +08002907 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002908 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002909 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2910 if (ec)
2911 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002912 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002913 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2914 << "Dbus error " << ec;
2915 return;
2916 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002917 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002918 BMCWEB_LOG_DEBUG << "respHandler1 exit";
George Liu2b731192023-01-11 16:27:13 +08002919 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002920}
2921
Nan Zhoubacb2162022-04-06 11:28:32 -07002922} // namespace sensors
2923
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002924inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002925{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002926 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002927 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002928 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002929 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002930}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002931
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002932inline void requestRoutesSensor(App& app)
2933{
2934 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002935 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002936 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002937 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002938}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002939
Ed Tanous1abe55e2018-09-05 08:30:59 -07002940} // namespace redfish