blob: 6be9e86bc1c1a3df554126e3cadf8ce5715c4518 [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"
24#include "utils/dbus_utils.hpp"
25#include "utils/json_utils.hpp"
26#include "utils/query_param.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070027
Ed Tanous11ba3972022-07-11 09:50:41 -070028#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070029#include <boost/algorithm/string/find.hpp>
30#include <boost/algorithm/string/predicate.hpp>
Ed Tanousc71d6122022-11-29 14:10:32 -080031#include <boost/algorithm/string/replace.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010032#include <boost/algorithm/string/split.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
Ed Tanous7f1cc262022-08-09 13:33:57 -0700562 asyncResp->res.jsonValue["@odata.id"] =
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";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700567 sdbusplus::asio::getProperty<std::vector<std::string>>(
568 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
569 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700570 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800571 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700572 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700573 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 if (e)
575 {
576 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700577 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700578 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700580 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000582 const std::shared_ptr<std::set<std::string>> culledSensorList =
583 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700584 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
585 &nodeSensorList, culledSensorList);
586 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700587 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700588 });
George Liu7a1dbc42022-12-07 16:03:22 +0800589 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700590 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100591}
592
593/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500594 * @brief Returns the Redfish State value for the specified inventory item.
595 * @param inventoryItem D-Bus inventory item associated with a sensor.
596 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700597 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000598inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500599{
600 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
601 {
602 return "Absent";
603 }
James Feist34dd1792019-05-17 14:10:54 -0700604
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500605 return "Enabled";
606}
607
608/**
609 * @brief Returns the Redfish Health value for the specified sensor.
610 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700611 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500612 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
613 * be nullptr if no associated inventory item was found.
614 * @return Health value for sensor.
615 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700616inline std::string getHealth(nlohmann::json& sensorJson,
617 const dbus::utility::DBusPropertiesMap& valuesDict,
618 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700619{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500620 // Get current health value (if any) in the sensor JSON object. Some JSON
621 // objects contain multiple sensors (such as PowerSupplies). We want to set
622 // the overall health to be the most severe of any of the sensors.
623 std::string currentHealth;
624 auto statusIt = sensorJson.find("Status");
625 if (statusIt != sensorJson.end())
626 {
627 auto healthIt = statusIt->find("Health");
628 if (healthIt != statusIt->end())
629 {
630 std::string* health = healthIt->get_ptr<std::string*>();
631 if (health != nullptr)
632 {
633 currentHealth = *health;
634 }
635 }
636 }
637
638 // If current health in JSON object is already Critical, return that. This
639 // should override the sensor health, which might be less severe.
640 if (currentHealth == "Critical")
641 {
642 return "Critical";
643 }
644
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200645 const bool* criticalAlarmHigh = nullptr;
646 const bool* criticalAlarmLow = nullptr;
647 const bool* warningAlarmHigh = nullptr;
648 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800649
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200650 const bool success = sdbusplus::unpackPropertiesNoThrow(
651 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
652 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
653 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
654 warningAlarmLow);
655
656 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700657 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200658 // Check if sensor has critical threshold alarm
659 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
660 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700661 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200662 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700663 }
664 }
665
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500666 // Check if associated inventory item is not functional
667 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
668 {
669 return "Critical";
670 }
671
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200672 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500673 // should override the sensor status, which might be less severe.
674 if (currentHealth == "Warning")
675 {
676 return "Warning";
677 }
678
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200679 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700680 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200681 // Check if sensor has warning threshold alarm
682 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
683 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700684 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200685 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700686 }
687 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500688
James Feist34dd1792019-05-17 14:10:54 -0700689 return "OK";
690}
691
Ed Tanous23a21a12020-07-25 04:45:05 +0000692inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500693 const InventoryItem* inventoryItem)
694{
695 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
696 {
697 switch (inventoryItem->ledState)
698 {
699 case LedState::OFF:
700 sensorJson["IndicatorLED"] = "Off";
701 break;
702 case LedState::ON:
703 sensorJson["IndicatorLED"] = "Lit";
704 break;
705 case LedState::BLINK:
706 sensorJson["IndicatorLED"] = "Blinking";
707 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000708 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500709 break;
710 }
711 }
712}
713
James Feist34dd1792019-05-17 14:10:54 -0700714/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100715 * @brief Builds a json sensor representation of a sensor.
716 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500717 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100718 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700719 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
720 * @param propertiesDict A dictionary of the properties to build the sensor
721 * from.
722 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500723 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
724 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100725 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700726inline void objectPropertiesToJson(
727 std::string_view sensorName, std::string_view sensorType,
728 std::string_view chassisSubNode,
729 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000730 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700731{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700732 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500733 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800734 std::string subNodeEscaped(sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700735 subNodeEscaped.erase(
736 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
737 subNodeEscaped.end());
738
739 // For sensors in SensorCollection we set Id instead of MemberId,
740 // including power sensors.
741 subNodeEscaped += '_';
742 subNodeEscaped += sensorName;
743 sensorJson["Id"] = std::move(subNodeEscaped);
744
Ed Tanous1d7c0052022-08-09 12:32:26 -0700745 std::string sensorNameEs(sensorName);
746 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
747 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500748 }
749 else if (sensorType != "power")
750 {
751 // Set MemberId and Name for non-power sensors. For PowerSupplies and
752 // PowerControl, those properties have more general values because
753 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000754 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700755 std::string sensorNameEs(sensorName);
756 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
757 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500758 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700759
Ed Tanous81ce6092020-12-17 16:54:55 +0000760 sensorJson["Status"]["State"] = getState(inventoryItem);
761 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700762 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700763
764 // Parameter to set to override the type we get from dbus, and force it to
765 // int, regardless of what is available. This is used for schemas like fan,
766 // that require integers, not floats.
767 bool forceToInt = false;
768
Anthony Wilson3929aca2019-07-19 15:42:33 -0500769 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700770 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500771 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000772 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000773
Ed Tanous0ec8b832022-03-14 14:56:47 -0700774 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
775 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500776 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000777 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
778 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500779 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000780 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500781 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000782 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500783 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000784
Ed Tanous1d7c0052022-08-09 12:32:26 -0700785 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000786 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200787 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000788 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
789 << sensorType;
790 }
791 else
792 {
793 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200794 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500795 }
796 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700797 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500798 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000799 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800 // TODO(ed) Documentation says that path should be type fan_tach,
801 // implementation seems to implement fan
802 }
803 else if (sensorType == "fan" || sensorType == "fan_tach")
804 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500805 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000806 sensorJson["ReadingUnits"] = "RPM";
807 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
808 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700809 forceToInt = true;
810 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700811 else if (sensorType == "fan_pwm")
812 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500813 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000814 sensorJson["ReadingUnits"] = "Percent";
815 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
816 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700817 forceToInt = true;
818 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819 else if (sensorType == "voltage")
820 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500821 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000822 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700824 else if (sensorType == "power")
825 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700826 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000827 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000828 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500829 // Put multiple "sensors" into a single PowerControl, so have
830 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000831 sensorJson["MemberId"] = "0";
832 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500833 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000834 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700835 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700836 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500837 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700838 }
839 else
840 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500841 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700842 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700843 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700844 else
845 {
846 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
847 return;
848 }
849 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500850 std::vector<
851 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
852 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 properties.reserve(7);
854
855 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600856
Ed Tanous1d7c0052022-08-09 12:32:26 -0700857 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500858 {
859 properties.emplace_back(
860 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
861 "/Thresholds/UpperCaution/Reading"_json_pointer);
862 properties.emplace_back(
863 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
864 "/Thresholds/LowerCaution/Reading"_json_pointer);
865 properties.emplace_back(
866 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
867 "/Thresholds/UpperCritical/Reading"_json_pointer);
868 properties.emplace_back(
869 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
870 "/Thresholds/LowerCritical/Reading"_json_pointer);
871 }
872 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600873 {
874 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500875 "WarningHigh",
876 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600877 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500878 "WarningLow",
879 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600880 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500881 "CriticalHigh",
882 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600883 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500884 "CriticalLow",
885 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600886 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887
Ed Tanous2474adf2018-09-05 16:31:16 -0700888 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
889
Ed Tanous1d7c0052022-08-09 12:32:26 -0700890 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500891 {
892 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500893 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500894 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500895 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800896 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
897 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500898 }
899 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700900 {
901 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500902 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500904 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500906 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907 {
908 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500909 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500911 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700912 }
913
Anthony Wilson3929aca2019-07-19 15:42:33 -0500914 for (const std::tuple<const char*, const char*,
915 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700917 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700919 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700920 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800921 continue;
922 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700923
924 // The property we want to set may be nested json, so use
925 // a json_pointer for easy indexing into the json structure.
926 const nlohmann::json::json_pointer& key = std::get<2>(p);
927
Ed Tanous1d7c0052022-08-09 12:32:26 -0700928 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700929 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800930 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700931 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700932 continue;
933 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700934 if (forceToInt)
935 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700936 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700937 }
938 else
939 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700940 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 }
942 }
943 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100944}
945
Ed Tanous1d7c0052022-08-09 12:32:26 -0700946/**
947 * @brief Builds a json sensor representation of a sensor.
948 * @param sensorName The name of the sensor to be built
949 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
950 * build
951 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
952 * @param interfacesDict A dictionary of the interfaces and properties of said
953 * interfaces to be built from
954 * @param sensorJson The json object to fill
955 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
956 * be nullptr if no associated inventory item was found.
957 */
958inline void objectInterfacesToJson(
959 const std::string& sensorName, const std::string& sensorType,
960 const std::string& chassisSubNode,
961 const dbus::utility::DBusInteracesMap& interfacesDict,
962 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
963{
964
965 for (const auto& [interface, valuesDict] : interfacesDict)
966 {
967 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
968 valuesDict, sensorJson, inventoryItem);
969 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700970 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700971}
972
Ed Tanousb5a76932020-09-29 16:16:58 -0700973inline void populateFanRedundancy(
974 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700975{
George Liue99073f2022-12-09 11:06:16 +0800976 constexpr std::array<std::string_view, 1> interfaces = {
977 "xyz.openbmc_project.Control.FanRedundancy"};
978 dbus::utility::getSubTree(
979 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800980 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +0800981 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800982 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700983 if (ec)
984 {
985 return; // don't have to have this interface
986 }
987 for (const std::pair<
988 std::string,
989 std::vector<std::pair<std::string, std::vector<std::string>>>>&
990 pathPair : resp)
991 {
992 const std::string& path = pathPair.first;
993 const std::vector<std::pair<std::string, std::vector<std::string>>>&
994 objDict = pathPair.second;
995 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700996 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700997 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700998 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700999
1000 const std::string& owner = objDict.begin()->first;
1001 sdbusplus::asio::getProperty<std::vector<std::string>>(
1002 *crow::connections::systemBus,
1003 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1004 "xyz.openbmc_project.Association", "endpoints",
1005 [path, owner,
1006 sensorsAsyncResp](const boost::system::error_code e,
1007 const std::vector<std::string>& endpoints) {
1008 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001009 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001010 return; // if they don't have an association we
1011 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001012 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001013 auto found =
1014 std::find_if(endpoints.begin(), endpoints.end(),
1015 [sensorsAsyncResp](const std::string& entry) {
1016 return entry.find(sensorsAsyncResp->chassisId) !=
1017 std::string::npos;
1018 });
James Feist8bd25cc2019-03-15 15:14:00 -07001019
Ed Tanous002d39b2022-05-31 08:59:27 -07001020 if (found == endpoints.end())
1021 {
1022 return;
1023 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001024 sdbusplus::asio::getAllProperties(
1025 *crow::connections::systemBus, owner, path,
1026 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001027 [path, sensorsAsyncResp](
1028 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001029 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001030 if (err)
1031 {
1032 return; // don't have to have this
1033 // interface
1034 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001035
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001036 const uint8_t* allowedFailures = nullptr;
1037 const std::vector<std::string>* collection = nullptr;
1038 const std::string* status = nullptr;
1039
1040 const bool success = sdbusplus::unpackPropertiesNoThrow(
1041 dbus_utils::UnpackErrorPrinter(), ret,
1042 "AllowedFailures", allowedFailures, "Collection",
1043 collection, "Status", status);
1044
1045 if (!success)
1046 {
1047 messages::internalError(
1048 sensorsAsyncResp->asyncResp->res);
1049 return;
1050 }
1051
1052 if (allowedFailures == nullptr || collection == nullptr ||
1053 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001054 {
1055 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1056 messages::internalError(
1057 sensorsAsyncResp->asyncResp->res);
1058 return;
1059 }
1060
Ed Tanous002d39b2022-05-31 08:59:27 -07001061 sdbusplus::message::object_path objectPath(path);
1062 std::string name = objectPath.filename();
1063 if (name.empty())
1064 {
1065 // this should be impossible
1066 messages::internalError(
1067 sensorsAsyncResp->asyncResp->res);
1068 return;
1069 }
1070 std::replace(name.begin(), name.end(), '_', ' ');
1071
1072 std::string health;
1073
Ed Tanous11ba3972022-07-11 09:50:41 -07001074 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001075 {
1076 health = "OK";
1077 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001078 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001079 {
1080 health = "Warning";
1081 }
1082 else
1083 {
1084 health = "Critical";
1085 }
1086 nlohmann::json::array_t redfishCollection;
1087 const auto& fanRedfish =
1088 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1089 for (const std::string& item : *collection)
1090 {
Ed Tanous8a592812022-06-04 09:06:59 -07001091 sdbusplus::message::object_path itemPath(item);
1092 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001093 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001094 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001096 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001097 /*
1098 todo(ed): merge patch that fixes the names
1099 std::replace(itemName.begin(),
1100 itemName.end(), '_', ' ');*/
1101 auto schemaItem =
1102 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1103 [itemName](const nlohmann::json& fan) {
1104 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001105 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001106 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001107 {
Ed Tanous8a592812022-06-04 09:06:59 -07001108 nlohmann::json::object_t collectionId;
1109 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001110 (*schemaItem)["@odata.id"];
1111 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001112 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 }
1114 else
1115 {
1116 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1117 messages::internalError(
1118 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001119 return;
1120 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 }
James Feist8bd25cc2019-03-15 15:14:00 -07001122
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 size_t minNumNeeded =
1124 collection->empty()
1125 ? 0
1126 : collection->size() - *allowedFailures;
1127 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1128 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001129
Ed Tanous002d39b2022-05-31 08:59:27 -07001130 nlohmann::json::object_t redundancy;
1131 redundancy["@odata.id"] =
1132 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1133 "/" + sensorsAsyncResp->chassisSubNode +
1134 "#/Redundancy/" + std::to_string(jResp.size());
1135 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1136 redundancy["MinNumNeeded"] = minNumNeeded;
1137 redundancy["MemberId"] = name;
1138 redundancy["Mode"] = "N+m";
1139 redundancy["Name"] = name;
1140 redundancy["RedundancySet"] = redfishCollection;
1141 redundancy["Status"]["Health"] = health;
1142 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001143
Ed Tanous002d39b2022-05-31 08:59:27 -07001144 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001145 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001146 });
1147 }
George Liue99073f2022-12-09 11:06:16 +08001148 });
James Feist8bd25cc2019-03-15 15:14:00 -07001149}
1150
Ed Tanousb5a76932020-09-29 16:16:58 -07001151inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001152 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001153{
zhanghch058d1b46d2021-04-01 11:18:24 +08001154 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001155 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001156 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001157 {
1158 sensorHeaders = {"Voltages", "PowerSupplies"};
1159 }
1160 for (const std::string& sensorGroup : sensorHeaders)
1161 {
1162 nlohmann::json::iterator entry = response.find(sensorGroup);
1163 if (entry != response.end())
1164 {
1165 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001166 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001167 return c1["Name"] < c2["Name"];
1168 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001169
1170 // add the index counts to the end of each entry
1171 size_t count = 0;
1172 for (nlohmann::json& sensorJson : *entry)
1173 {
1174 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1175 if (odata == sensorJson.end())
1176 {
1177 continue;
1178 }
1179 std::string* value = odata->get_ptr<std::string*>();
1180 if (value != nullptr)
1181 {
1182 *value += std::to_string(count);
1183 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001184 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001185 }
1186 }
1187 }
1188 }
1189}
1190
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001191/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001192 * @brief Finds the inventory item with the specified object path.
1193 * @param inventoryItems D-Bus inventory items associated with sensors.
1194 * @param invItemObjPath D-Bus object path of inventory item.
1195 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001196 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001197inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001198 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001199 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001200{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001201 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001202 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001203 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001204 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001205 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001206 }
1207 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001208 return nullptr;
1209}
1210
1211/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001212 * @brief Finds the inventory item associated with the specified sensor.
1213 * @param inventoryItems D-Bus inventory items associated with sensors.
1214 * @param sensorObjPath D-Bus object path of sensor.
1215 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001216 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001217inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001218 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001219 const std::string& sensorObjPath)
1220{
1221 for (InventoryItem& inventoryItem : *inventoryItems)
1222 {
1223 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1224 {
1225 return &inventoryItem;
1226 }
1227 }
1228 return nullptr;
1229}
1230
1231/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001232 * @brief Finds the inventory item associated with the specified led path.
1233 * @param inventoryItems D-Bus inventory items associated with sensors.
1234 * @param ledObjPath D-Bus object path of led.
1235 * @return Inventory item within vector, or nullptr if no match found.
1236 */
1237inline InventoryItem*
1238 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1239 const std::string& ledObjPath)
1240{
1241 for (InventoryItem& inventoryItem : inventoryItems)
1242 {
1243 if (inventoryItem.ledObjectPath == ledObjPath)
1244 {
1245 return &inventoryItem;
1246 }
1247 }
1248 return nullptr;
1249}
1250
1251/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001252 * @brief Adds inventory item and associated sensor to specified vector.
1253 *
1254 * Adds a new InventoryItem to the vector if necessary. Searches for an
1255 * existing InventoryItem with the specified object path. If not found, one is
1256 * added to the vector.
1257 *
1258 * Next, the specified sensor is added to the set of sensors associated with the
1259 * InventoryItem.
1260 *
1261 * @param inventoryItems D-Bus inventory items associated with sensors.
1262 * @param invItemObjPath D-Bus object path of inventory item.
1263 * @param sensorObjPath D-Bus object path of sensor
1264 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001265inline void addInventoryItem(
1266 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1267 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001268{
1269 // Look for inventory item in vector
1270 InventoryItem* inventoryItem =
1271 findInventoryItem(inventoryItems, invItemObjPath);
1272
1273 // If inventory item doesn't exist in vector, add it
1274 if (inventoryItem == nullptr)
1275 {
1276 inventoryItems->emplace_back(invItemObjPath);
1277 inventoryItem = &(inventoryItems->back());
1278 }
1279
1280 // Add sensor to set of sensors associated with inventory item
1281 inventoryItem->sensors.emplace(sensorObjPath);
1282}
1283
1284/**
1285 * @brief Stores D-Bus data in the specified inventory item.
1286 *
1287 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1288 * specified InventoryItem.
1289 *
1290 * This data is later used to provide sensor property values in the JSON
1291 * response.
1292 *
1293 * @param inventoryItem Inventory item where data will be stored.
1294 * @param interfacesDict Map containing D-Bus interfaces and their properties
1295 * for the specified inventory item.
1296 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001297inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001298 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001299 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001300{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001301 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001302
Ed Tanous9eb808c2022-01-25 10:19:23 -08001303 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001304 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001305 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001306 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001307 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001308 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001309 if (name == "Present")
1310 {
1311 const bool* value = std::get_if<bool>(&dbusValue);
1312 if (value != nullptr)
1313 {
1314 inventoryItem.isPresent = *value;
1315 }
1316 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001317 }
1318 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001319 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001320
Ed Tanous711ac7a2021-12-20 09:34:41 -08001321 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001322 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001323 inventoryItem.isPowerSupply = true;
1324 }
1325
1326 // Get properties from Inventory.Decorator.Asset interface
1327 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1328 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001329 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001330 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001331 if (name == "Manufacturer")
1332 {
1333 const std::string* value =
1334 std::get_if<std::string>(&dbusValue);
1335 if (value != nullptr)
1336 {
1337 inventoryItem.manufacturer = *value;
1338 }
1339 }
1340 if (name == "Model")
1341 {
1342 const std::string* value =
1343 std::get_if<std::string>(&dbusValue);
1344 if (value != nullptr)
1345 {
1346 inventoryItem.model = *value;
1347 }
1348 }
1349 if (name == "SerialNumber")
1350 {
1351 const std::string* value =
1352 std::get_if<std::string>(&dbusValue);
1353 if (value != nullptr)
1354 {
1355 inventoryItem.serialNumber = *value;
1356 }
1357 }
1358 if (name == "PartNumber")
1359 {
1360 const std::string* value =
1361 std::get_if<std::string>(&dbusValue);
1362 if (value != nullptr)
1363 {
1364 inventoryItem.partNumber = *value;
1365 }
1366 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001367 }
1368 }
1369
Ed Tanous711ac7a2021-12-20 09:34:41 -08001370 if (interface ==
1371 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001372 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001373 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001374 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001375 if (name == "Functional")
1376 {
1377 const bool* value = std::get_if<bool>(&dbusValue);
1378 if (value != nullptr)
1379 {
1380 inventoryItem.isFunctional = *value;
1381 }
1382 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001383 }
1384 }
1385 }
1386}
1387
1388/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001389 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001390 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001391 * Uses the specified connections (services) to obtain D-Bus data for inventory
1392 * items associated with sensors. Stores the resulting data in the
1393 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001394 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001395 * This data is later used to provide sensor property values in the JSON
1396 * response.
1397 *
1398 * Finds the inventory item data asynchronously. Invokes callback when data has
1399 * been obtained.
1400 *
1401 * The callback must have the following signature:
1402 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001403 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001404 * @endcode
1405 *
1406 * This function is called recursively, obtaining data asynchronously from one
1407 * connection in each call. This ensures the callback is not invoked until the
1408 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001409 *
1410 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001411 * @param inventoryItems D-Bus inventory items associated with sensors.
1412 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001413 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001414 * @param callback Callback to invoke when inventory data has been obtained.
1415 * @param invConnectionsIndex Current index in invConnections. Only specified
1416 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001417 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001418template <typename Callback>
1419static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001420 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001422 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1423 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001424{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001425 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001426
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001427 // If no more connections left, call callback
1428 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001429 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001430 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001431 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1432 return;
1433 }
1434
1435 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001436 auto it = invConnections->begin();
1437 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001438 if (it != invConnections->end())
1439 {
1440 const std::string& invConnection = *it;
1441
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001442 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001443 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1444 callback{std::forward<Callback>(callback)},
1445 invConnectionsIndex](
1446 const boost::system::error_code ec,
1447 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001448 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001449 if (ec)
1450 {
1451 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001452 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001453 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001454 return;
1455 }
1456
1457 // Loop through returned object paths
1458 for (const auto& objDictEntry : resp)
1459 {
1460 const std::string& objPath =
1461 static_cast<const std::string&>(objDictEntry.first);
1462
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001463 // If this object path is one of the specified inventory items
1464 InventoryItem* inventoryItem =
1465 findInventoryItem(inventoryItems, objPath);
1466 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001467 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001468 // Store inventory data in InventoryItem
1469 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001470 }
1471 }
1472
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001473 // Recurse to get inventory item data from next connection
1474 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001475 invConnections, std::move(callback),
1476 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001477
1478 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001479 };
1480
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001481 // Get all object paths and their interfaces for current connection
1482 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001483 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001484 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001485 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1486 }
1487
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001488 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001489}
1490
1491/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001492 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001493 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001494 * Gets the D-Bus connections (services) that provide data for the inventory
1495 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001496 *
1497 * Finds the connections asynchronously. Invokes callback when information has
1498 * been obtained.
1499 *
1500 * The callback must have the following signature:
1501 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001502 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001503 * @endcode
1504 *
1505 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001506 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001507 * @param callback Callback to invoke when connections have been obtained.
1508 */
1509template <typename Callback>
1510static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001511 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1512 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001513 Callback&& callback)
1514{
1515 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1516
1517 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001518 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001519 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001520 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1521 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001522 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1523
George Liue99073f2022-12-09 11:06:16 +08001524 // Make call to ObjectMapper to find all inventory items
1525 dbus::utility::getSubTree(
1526 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001527 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1528 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001529 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001530 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001531 // Response handler for parsing output from GetSubTree
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001532 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1533 if (ec)
1534 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001535 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001536 BMCWEB_LOG_ERROR
1537 << "getInventoryItemsConnections respHandler DBus error " << ec;
1538 return;
1539 }
1540
1541 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001542 std::shared_ptr<std::set<std::string>> invConnections =
1543 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001544
1545 // Loop through objects from GetSubTree
1546 for (const std::pair<
1547 std::string,
1548 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1549 object : subtree)
1550 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001551 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001552 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001553 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001555 // Store all connections to inventory item
1556 for (const std::pair<std::string, std::vector<std::string>>&
1557 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001558 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001559 const std::string& invConnection = objData.first;
1560 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001561 }
1562 }
1563 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001564
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001565 callback(invConnections);
1566 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001567 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001568 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1569}
1570
1571/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001572 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001573 *
1574 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001575 * inventory items. Then finds the associations from those inventory items to
1576 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001577 *
1578 * Finds the inventory items asynchronously. Invokes callback when information
1579 * has been obtained.
1580 *
1581 * The callback must have the following signature:
1582 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001583 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001584 * @endcode
1585 *
1586 * @param sensorsAsyncResp Pointer to object holding response data.
1587 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001588 * implements ObjectManager.
1589 * @param callback Callback to invoke when inventory items have been obtained.
1590 */
1591template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001592static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001593 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001594 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001595 Callback&& callback)
1596{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001597 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001598
1599 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001600 auto respHandler =
1601 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1602 sensorNames](const boost::system::error_code ec,
1603 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001604 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001605 if (ec)
1606 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001607 BMCWEB_LOG_ERROR
1608 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001609 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001610 return;
1611 }
1612
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001613 // Create vector to hold list of inventory items
1614 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1615 std::make_shared<std::vector<InventoryItem>>();
1616
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001617 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618 std::string sensorAssocPath;
1619 sensorAssocPath.reserve(128); // avoid memory allocations
1620 for (const auto& objDictEntry : resp)
1621 {
1622 const std::string& objPath =
1623 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001624
1625 // If path is inventory association for one of the specified sensors
1626 for (const std::string& sensorName : *sensorNames)
1627 {
1628 sensorAssocPath = sensorName;
1629 sensorAssocPath += "/inventory";
1630 if (objPath == sensorAssocPath)
1631 {
1632 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001633 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001634 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001635 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001636 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001637 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001638 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001639 if (valueName == "endpoints")
1640 {
1641 const std::vector<std::string>* endpoints =
1642 std::get_if<std::vector<std::string>>(
1643 &value);
1644 if ((endpoints != nullptr) &&
1645 !endpoints->empty())
1646 {
1647 // Add inventory item to vector
1648 const std::string& invItemPath =
1649 endpoints->front();
1650 addInventoryItem(inventoryItems,
1651 invItemPath,
1652 sensorName);
1653 }
1654 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001655 }
1656 }
1657 }
1658 break;
1659 }
1660 }
1661 }
1662
Anthony Wilsond5005492019-07-31 16:34:17 -05001663 // Now loop through the returned object paths again, this time to
1664 // find the leds associated with the inventory items we just found
1665 std::string inventoryAssocPath;
1666 inventoryAssocPath.reserve(128); // avoid memory allocations
1667 for (const auto& objDictEntry : resp)
1668 {
1669 const std::string& objPath =
1670 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001671
1672 for (InventoryItem& inventoryItem : *inventoryItems)
1673 {
1674 inventoryAssocPath = inventoryItem.objectPath;
1675 inventoryAssocPath += "/leds";
1676 if (objPath == inventoryAssocPath)
1677 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001678 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001679 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001680 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001681 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001682 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001683 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001684 if (valueName == "endpoints")
1685 {
1686 const std::vector<std::string>* endpoints =
1687 std::get_if<std::vector<std::string>>(
1688 &value);
1689 if ((endpoints != nullptr) &&
1690 !endpoints->empty())
1691 {
1692 // Add inventory item to vector
1693 // Store LED path in inventory item
1694 const std::string& ledPath =
1695 endpoints->front();
1696 inventoryItem.ledObjectPath = ledPath;
1697 }
1698 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001699 }
1700 }
1701 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001702
Anthony Wilsond5005492019-07-31 16:34:17 -05001703 break;
1704 }
1705 }
1706 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001707 callback(inventoryItems);
1708 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001709 };
1710
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001711 // Call GetManagedObjects on the ObjectMapper to get all associations
1712 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001713 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001714 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1715
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001716 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001717}
1718
1719/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001720 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1721 *
1722 * Uses the specified connections (services) to obtain D-Bus data for inventory
1723 * item leds associated with sensors. Stores the resulting data in the
1724 * inventoryItems vector.
1725 *
1726 * This data is later used to provide sensor property values in the JSON
1727 * response.
1728 *
1729 * Finds the inventory item led data asynchronously. Invokes callback when data
1730 * has been obtained.
1731 *
1732 * The callback must have the following signature:
1733 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001734 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001735 * @endcode
1736 *
1737 * This function is called recursively, obtaining data asynchronously from one
1738 * connection in each call. This ensures the callback is not invoked until the
1739 * last asynchronous function has completed.
1740 *
1741 * @param sensorsAsyncResp Pointer to object holding response data.
1742 * @param inventoryItems D-Bus inventory items associated with sensors.
1743 * @param ledConnections Connections that provide data for the inventory leds.
1744 * @param callback Callback to invoke when inventory data has been obtained.
1745 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1746 * in recursive calls to this function.
1747 */
1748template <typename Callback>
1749void getInventoryLedData(
1750 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1751 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001752 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001753 Callback&& callback, size_t ledConnectionsIndex = 0)
1754{
1755 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1756
1757 // If no more connections left, call callback
1758 if (ledConnectionsIndex >= ledConnections->size())
1759 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001760 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001761 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1762 return;
1763 }
1764
1765 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001766 auto it = ledConnections->begin();
1767 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001768 if (it != ledConnections->end())
1769 {
1770 const std::string& ledPath = (*it).first;
1771 const std::string& ledConnection = (*it).second;
1772 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001773 auto respHandler =
1774 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001775 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001776 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001777 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1778 if (ec)
1779 {
1780 BMCWEB_LOG_ERROR
1781 << "getInventoryLedData respHandler DBus error " << ec;
1782 messages::internalError(sensorsAsyncResp->asyncResp->res);
1783 return;
1784 }
1785
1786 BMCWEB_LOG_DEBUG << "Led state: " << state;
1787 // Find inventory item with this LED object path
1788 InventoryItem* inventoryItem =
1789 findInventoryItemForLed(*inventoryItems, ledPath);
1790 if (inventoryItem != nullptr)
1791 {
1792 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001793 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001794 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001796 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001797 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001798 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001799 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001800 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001801 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 {
1803 inventoryItem->ledState = LedState::OFF;
1804 }
1805 else
1806 {
1807 inventoryItem->ledState = LedState::UNKNOWN;
1808 }
1809 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001810
Ed Tanous002d39b2022-05-31 08:59:27 -07001811 // Recurse to get LED data from next connection
1812 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1813 ledConnections, std::move(callback),
1814 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001815
Ed Tanous002d39b2022-05-31 08:59:27 -07001816 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1817 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001818
1819 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001820 sdbusplus::asio::getProperty<std::string>(
1821 *crow::connections::systemBus, ledConnection, ledPath,
1822 "xyz.openbmc_project.Led.Physical", "State",
1823 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001824 }
1825
1826 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1827}
1828
1829/**
1830 * @brief Gets LED data for LEDs associated with given inventory items.
1831 *
1832 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1833 * associated with the specified inventory items. Then gets the LED data from
1834 * each connection and stores it in the inventory item.
1835 *
1836 * This data is later used to provide sensor property values in the JSON
1837 * response.
1838 *
1839 * Finds the LED data asynchronously. Invokes callback when information has
1840 * been obtained.
1841 *
1842 * The callback must have the following signature:
1843 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001844 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001845 * @endcode
1846 *
1847 * @param sensorsAsyncResp Pointer to object holding response data.
1848 * @param inventoryItems D-Bus inventory items associated with sensors.
1849 * @param callback Callback to invoke when inventory items have been obtained.
1850 */
1851template <typename Callback>
1852void getInventoryLeds(
1853 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1854 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1855 Callback&& callback)
1856{
1857 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1858
1859 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001860 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001861 "xyz.openbmc_project.Led.Physical"};
1862
George Liue99073f2022-12-09 11:06:16 +08001863 // Make call to ObjectMapper to find all inventory items
1864 dbus::utility::getSubTree(
1865 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001866 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1867 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001868 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001869 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001870 // Response handler for parsing output from GetSubTree
Anthony Wilsond5005492019-07-31 16:34:17 -05001871 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1872 if (ec)
1873 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001874 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001875 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1876 << ec;
1877 return;
1878 }
1879
1880 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001881 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1882 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001883
1884 // Loop through objects from GetSubTree
1885 for (const std::pair<
1886 std::string,
1887 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1888 object : subtree)
1889 {
1890 // Check if object path is LED for one of the specified inventory
1891 // items
1892 const std::string& ledPath = object.first;
1893 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1894 {
1895 // Add mapping from ledPath to connection
1896 const std::string& connection = object.second.begin()->first;
1897 (*ledConnections)[ledPath] = connection;
1898 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1899 << connection;
1900 }
1901 }
1902
1903 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1904 std::move(callback));
1905 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001906 });
Anthony Wilsond5005492019-07-31 16:34:17 -05001907 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1908}
1909
1910/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001911 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1912 *
1913 * Uses the specified connections (services) (currently assumes just one) to
1914 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1915 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1916 *
1917 * This data is later used to provide sensor property values in the JSON
1918 * response.
1919 *
1920 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1921 * when data has been obtained.
1922 *
1923 * The callback must have the following signature:
1924 * @code
1925 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1926 * @endcode
1927 *
1928 * @param sensorsAsyncResp Pointer to object holding response data.
1929 * @param inventoryItems D-Bus inventory items associated with sensors.
1930 * @param psAttributesConnections Connections that provide data for the Power
1931 * Supply Attributes
1932 * @param callback Callback to invoke when data has been obtained.
1933 */
1934template <typename Callback>
1935void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001936 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001937 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001938 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001939 Callback&& callback)
1940{
1941 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1942
1943 if (psAttributesConnections.empty())
1944 {
1945 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1946 callback(inventoryItems);
1947 return;
1948 }
1949
1950 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001951 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001952
1953 const std::string& psAttributesPath = (*it).first;
1954 const std::string& psAttributesConnection = (*it).second;
1955
1956 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001957 auto respHandler =
1958 [sensorsAsyncResp, inventoryItems,
1959 callback{std::forward<Callback>(callback)}](
1960 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001961 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1962 if (ec)
1963 {
1964 BMCWEB_LOG_ERROR
1965 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001966 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001967 return;
1968 }
1969
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001970 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1971 // Store value in Power Supply Inventory Items
1972 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001973 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001974 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001975 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001976 inventoryItem.powerSupplyEfficiencyPercent =
1977 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001978 }
1979 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001980
1981 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1982 callback(inventoryItems);
1983 };
1984
1985 // Get the DeratingFactor property for the PowerSupplyAttributes
1986 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001987 sdbusplus::asio::getProperty<uint32_t>(
1988 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1989 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
1990 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05001991
1992 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
1993}
1994
1995/**
1996 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
1997 *
1998 * Gets the D-Bus connection (service) that provides Power Supply Attributes
1999 * data. Then gets the Power Supply Attributes data from the connection
2000 * (currently just assumes 1 connection) and stores the data in the inventory
2001 * item.
2002 *
2003 * This data is later used to provide sensor property values in the JSON
2004 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2005 *
2006 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2007 * when information has been obtained.
2008 *
2009 * The callback must have the following signature:
2010 * @code
2011 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2012 * @endcode
2013 *
2014 * @param sensorsAsyncResp Pointer to object holding response data.
2015 * @param inventoryItems D-Bus inventory items associated with sensors.
2016 * @param callback Callback to invoke when data has been obtained.
2017 */
2018template <typename Callback>
2019void getPowerSupplyAttributes(
2020 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2021 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2022 Callback&& callback)
2023{
2024 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2025
2026 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002027 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002028 {
2029 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2030 callback(inventoryItems);
2031 return;
2032 }
2033
George Liue99073f2022-12-09 11:06:16 +08002034 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002035 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2036
George Liue99073f2022-12-09 11:06:16 +08002037 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2038 dbus::utility::getSubTree(
2039 "/xyz/openbmc_project", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002040 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2041 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002042 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002043 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002044 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07002045 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2046 if (ec)
2047 {
2048 messages::internalError(sensorsAsyncResp->asyncResp->res);
2049 BMCWEB_LOG_ERROR
2050 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2051 return;
2052 }
2053 if (subtree.empty())
2054 {
2055 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2056 callback(inventoryItems);
2057 return;
2058 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002059
Ed Tanous002d39b2022-05-31 08:59:27 -07002060 // Currently we only support 1 power supply attribute, use this for
2061 // all the power supplies. Build map of object path to connection.
2062 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002063 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002064
Ed Tanous002d39b2022-05-31 08:59:27 -07002065 if (subtree[0].first.empty() || subtree[0].second.empty())
2066 {
2067 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2068 callback(inventoryItems);
2069 return;
2070 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002071
Ed Tanous002d39b2022-05-31 08:59:27 -07002072 const std::string& psAttributesPath = subtree[0].first;
2073 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002074
Ed Tanous002d39b2022-05-31 08:59:27 -07002075 if (connection.empty())
2076 {
2077 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2078 callback(inventoryItems);
2079 return;
2080 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002081
Ed Tanous002d39b2022-05-31 08:59:27 -07002082 psAttributesConnections[psAttributesPath] = connection;
2083 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2084 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002085
Ed Tanous002d39b2022-05-31 08:59:27 -07002086 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2087 psAttributesConnections,
2088 std::move(callback));
2089 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08002090 });
Gunnar Mills42cbe532019-08-15 15:26:54 -05002091 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2092}
2093
2094/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002095 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002096 *
2097 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002098 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002099 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002100 * This data is later used to provide sensor property values in the JSON
2101 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002102 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002103 * Finds the inventory items asynchronously. Invokes callback when the
2104 * inventory items have been obtained.
2105 *
2106 * The callback must have the following signature:
2107 * @code
2108 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2109 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002110 *
2111 * @param sensorsAsyncResp Pointer to object holding response data.
2112 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002113 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002114 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002115 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002116template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002117static void
2118 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2119 const std::shared_ptr<std::set<std::string>> sensorNames,
2120 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002121{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002122 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2123 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002124 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002125 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002126 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2127 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002128 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002130 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2132 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2133 callback{std::move(callback)}]() {
2134 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002135
Ed Tanous002d39b2022-05-31 08:59:27 -07002136 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2137 callback{std::move(callback)}]() {
2138 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2139 // Find Power Supply Attributes and get the data
2140 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2141 std::move(callback));
2142 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002143 };
2144
Ed Tanous002d39b2022-05-31 08:59:27 -07002145 // Find led connections and get the data
2146 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2147 std::move(getInventoryLedsCb));
2148 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2149 };
2150
2151 // Get inventory item data from connections
2152 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002153 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002154 std::move(getInventoryItemsDataCb));
2155 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002156 };
2157
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 // Get connections that provide inventory item data
2159 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2160 std::move(getInventoryItemsConnectionsCb));
2161 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2162 };
2163
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002164 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002165 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002166 std::move(getInventoryItemAssociationsCb));
2167 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2168}
2169
2170/**
2171 * @brief Returns JSON PowerSupply object for the specified inventory item.
2172 *
2173 * Searches for a JSON PowerSupply object that matches the specified inventory
2174 * item. If one is not found, a new PowerSupply object is added to the JSON
2175 * array.
2176 *
2177 * Multiple sensors are often associated with one power supply inventory item.
2178 * As a result, multiple sensor values are stored in one JSON PowerSupply
2179 * object.
2180 *
2181 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2182 * @param inventoryItem Inventory item for the power supply.
2183 * @param chassisId Chassis that contains the power supply.
2184 * @return JSON PowerSupply object for the specified inventory item.
2185 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002186inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002187 const InventoryItem& inventoryItem,
2188 const std::string& chassisId)
2189{
2190 // Check if matching PowerSupply object already exists in JSON array
2191 for (nlohmann::json& powerSupply : powerSupplyArray)
2192 {
2193 if (powerSupply["MemberId"] == inventoryItem.name)
2194 {
2195 return powerSupply;
2196 }
2197 }
2198
2199 // Add new PowerSupply object to JSON array
2200 powerSupplyArray.push_back({});
2201 nlohmann::json& powerSupply = powerSupplyArray.back();
2202 powerSupply["@odata.id"] =
2203 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2204 powerSupply["MemberId"] = inventoryItem.name;
2205 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2206 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2207 powerSupply["Model"] = inventoryItem.model;
2208 powerSupply["PartNumber"] = inventoryItem.partNumber;
2209 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002210 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002211
Gunnar Mills42cbe532019-08-15 15:26:54 -05002212 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2213 {
2214 powerSupply["EfficiencyPercent"] =
2215 inventoryItem.powerSupplyEfficiencyPercent;
2216 }
2217
2218 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002219 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2220 powerSupply["Status"]["Health"] = health;
2221
2222 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002223}
2224
2225/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002226 * @brief Gets the values of the specified sensors.
2227 *
2228 * Stores the results as JSON in the SensorsAsyncResp.
2229 *
2230 * Gets the sensor values asynchronously. Stores the results later when the
2231 * information has been obtained.
2232 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002233 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002234 *
2235 * To minimize the number of DBus calls, the DBus method
2236 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2237 * values of all sensors provided by a connection (service).
2238 *
2239 * The connections set contains all the connections that provide sensor values.
2240 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002241 * The InventoryItem vector contains D-Bus inventory items associated with the
2242 * sensors. Inventory item data is needed for some Redfish sensor properties.
2243 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002244 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002245 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002246 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002247 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002248 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002249 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002250inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002251 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002252 const std::shared_ptr<std::set<std::string>>& sensorNames,
2253 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002254 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002255{
2256 BMCWEB_LOG_DEBUG << "getSensorData enter";
2257 // Get managed objects from all services exposing sensors
2258 for (const std::string& connection : connections)
2259 {
2260 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002261 auto getManagedObjectsCb =
2262 [sensorsAsyncResp, sensorNames,
2263 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002264 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002265 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2266 if (ec)
2267 {
2268 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002269 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002270 return;
2271 }
2272 // Go through all objects and update response with sensor data
2273 for (const auto& objDictEntry : resp)
2274 {
2275 const std::string& objPath =
2276 static_cast<const std::string&>(objDictEntry.first);
2277 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2278 << objPath;
2279
Shawn McCarneyde629b62019-03-08 10:42:51 -06002280 std::vector<std::string> split;
2281 // Reserve space for
2282 // /xyz/openbmc_project/sensors/<name>/<subname>
2283 split.reserve(6);
2284 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2285 if (split.size() < 6)
2286 {
2287 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2288 << objPath;
2289 continue;
2290 }
2291 // These indexes aren't intuitive, as boost::split puts an empty
2292 // string at the beginning
2293 const std::string& sensorType = split[4];
2294 const std::string& sensorName = split[5];
2295 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2296 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002297 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002298 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002299 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002300 continue;
2301 }
2302
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002303 // Find inventory item (if any) associated with sensor
2304 InventoryItem* inventoryItem =
2305 findInventoryItemForSensor(inventoryItems, objPath);
2306
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002307 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002308 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002309
2310 nlohmann::json* sensorJson = nullptr;
2311
Nan Zhou928fefb2022-03-28 08:45:00 -07002312 if (sensorSchema == sensors::node::sensors &&
2313 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002314 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002315 std::string sensorTypeEscaped(sensorType);
2316 sensorTypeEscaped.erase(
2317 std::remove(sensorTypeEscaped.begin(),
2318 sensorTypeEscaped.end(), '_'),
2319 sensorTypeEscaped.end());
2320 std::string sensorId(sensorTypeEscaped);
2321 sensorId += "_";
2322 sensorId += sensorName;
2323
zhanghch058d1b46d2021-04-01 11:18:24 +08002324 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002325 crow::utility::urlFromPieces(
2326 "redfish", "v1", "Chassis",
2327 sensorsAsyncResp->chassisId,
2328 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002329 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002330 }
2331 else
2332 {
Ed Tanous271584a2019-07-09 16:24:22 -07002333 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002334 if (sensorsAsyncResp->efficientExpand)
2335 {
2336 fieldName = "Members";
2337 }
2338 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002339 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002340 fieldName = "Temperatures";
2341 }
2342 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2343 sensorType == "fan_pwm")
2344 {
2345 fieldName = "Fans";
2346 }
2347 else if (sensorType == "voltage")
2348 {
2349 fieldName = "Voltages";
2350 }
2351 else if (sensorType == "power")
2352 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002353 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002354 {
2355 fieldName = "PowerControl";
2356 }
2357 else if ((inventoryItem != nullptr) &&
2358 (inventoryItem->isPowerSupply))
2359 {
2360 fieldName = "PowerSupplies";
2361 }
2362 else
2363 {
2364 // Other power sensors are in SensorCollection
2365 continue;
2366 }
2367 }
2368 else
2369 {
2370 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2371 << sensorType;
2372 continue;
2373 }
2374
2375 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002376 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002377 if (fieldName == "PowerControl")
2378 {
2379 if (tempArray.empty())
2380 {
2381 // Put multiple "sensors" into a single
2382 // PowerControl. Follows MemberId naming and
2383 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002384 nlohmann::json::object_t power;
2385 power["@odata.id"] =
2386 "/redfish/v1/Chassis/" +
2387 sensorsAsyncResp->chassisId + "/" +
2388 sensorsAsyncResp->chassisSubNode + "#/" +
2389 fieldName + "/0";
2390 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002391 }
2392 sensorJson = &(tempArray.back());
2393 }
2394 else if (fieldName == "PowerSupplies")
2395 {
2396 if (inventoryItem != nullptr)
2397 {
2398 sensorJson =
2399 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002400 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002401 }
2402 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002403 else if (fieldName == "Members")
2404 {
Ed Tanous677bb752022-09-15 10:52:19 -07002405 std::string sensorTypeEscaped(sensorType);
2406 sensorTypeEscaped.erase(
2407 std::remove(sensorTypeEscaped.begin(),
2408 sensorTypeEscaped.end(), '_'),
2409 sensorTypeEscaped.end());
2410 std::string sensorId(sensorTypeEscaped);
2411 sensorId += "_";
2412 sensorId += sensorName;
2413
Ed Tanous14766872022-03-15 10:44:42 -07002414 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002415 member["@odata.id"] = crow::utility::urlFromPieces(
2416 "redfish", "v1", "Chassis",
2417 sensorsAsyncResp->chassisId,
2418 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002419 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002420 sensorJson = &(tempArray.back());
2421 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002422 else
2423 {
Ed Tanous14766872022-03-15 10:44:42 -07002424 nlohmann::json::object_t member;
2425 member["@odata.id"] = "/redfish/v1/Chassis/" +
2426 sensorsAsyncResp->chassisId +
2427 "/" +
2428 sensorsAsyncResp->chassisSubNode +
2429 "#/" + fieldName + "/";
2430 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002431 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002432 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002433 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002434
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002435 if (sensorJson != nullptr)
2436 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002437 objectInterfacesToJson(sensorName, sensorType,
2438 sensorsAsyncResp->chassisSubNode,
2439 objDictEntry.second, *sensorJson,
2440 inventoryItem);
2441
2442 std::string path = "/xyz/openbmc_project/sensors/";
2443 path += sensorType;
2444 path += "/";
2445 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002446 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002447 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002448 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002449 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002450 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002451 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002452 if (sensorsAsyncResp->chassisSubNode ==
2453 sensors::node::sensors &&
2454 sensorsAsyncResp->efficientExpand)
2455 {
2456 sensorsAsyncResp->asyncResp->res
2457 .jsonValue["Members@odata.count"] =
2458 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2459 .size();
2460 }
2461 else if (sensorsAsyncResp->chassisSubNode ==
2462 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002463 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002464 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002465 }
James Feist8bd25cc2019-03-15 15:14:00 -07002466 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002467 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2468 };
2469
Shawn McCarneyde629b62019-03-08 10:42:51 -06002470 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002471 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002472 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002473 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002474 BMCWEB_LOG_DEBUG << "getSensorData exit";
2475}
2476
Nan Zhoufe04d492022-06-22 17:10:41 +00002477inline void
2478 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2479 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002480{
Nan Zhoufe04d492022-06-22 17:10:41 +00002481 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2482 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002483 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002484 auto getInventoryItemsCb =
2485 [sensorsAsyncResp, sensorNames,
2486 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2487 inventoryItems) {
2488 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2489 // Get sensor data and store results in JSON
2490 getSensorData(sensorsAsyncResp, sensorNames, connections,
2491 inventoryItems);
2492 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002493 };
2494
Ed Tanousd0090732022-10-04 17:22:56 -07002495 // Get inventory items associated with sensors
2496 getInventoryItems(sensorsAsyncResp, sensorNames,
2497 std::move(getInventoryItemsCb));
2498
Ed Tanous002d39b2022-05-31 08:59:27 -07002499 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2500 };
2501
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002502 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002503 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002504}
2505
Shawn McCarneyde629b62019-03-08 10:42:51 -06002506/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002507 * @brief Entry point for retrieving sensors data related to requested
2508 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002509 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002510 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002511inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002512 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002513{
2514 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002515 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002516 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002517 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002518 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2519 processSensorList(sensorsAsyncResp, sensorNames);
2520 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2521 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002522 // SensorCollection doesn't contain the Redundancy property
2523 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2524 {
2525 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2526 nlohmann::json::array();
2527 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002528 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002529 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2530 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2531 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002532 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002533}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002534
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302535/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002536 * @brief Find the requested sensorName in the list of all sensors supplied by
2537 * the chassis node
2538 *
2539 * @param sensorName The sensor name supplied in the PATCH request
2540 * @param sensorsList The list of sensors managed by the chassis node
2541 * @param sensorsModified The list of sensors that were found as a result of
2542 * repeated calls to this function
2543 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002544inline bool
2545 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002546 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002547 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002548{
Nan Zhoufe04d492022-06-22 17:10:41 +00002549 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002550 {
George Liu28aa8de2021-02-01 15:13:30 +08002551 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002552 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002553 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002554 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002555 continue;
2556 }
2557 if (thisSensorName == sensorName)
2558 {
2559 sensorsModified.emplace(chassisSensor);
2560 return true;
2561 }
2562 }
2563 return false;
2564}
2565
Ed Tanousc71d6122022-11-29 14:10:32 -08002566inline std::pair<std::string, std::string>
2567 splitSensorNameAndType(std::string_view sensorId)
2568{
2569 size_t index = sensorId.find('_');
2570 if (index == std::string::npos)
2571 {
2572 return std::make_pair<std::string, std::string>("", "");
2573 }
2574 std::string sensorType{sensorId.substr(0, index)};
2575 std::string sensorName{sensorId.substr(index + 1)};
2576 // fan_pwm and fan_tach need special handling
2577 if (sensorType == "fantach" || sensorType == "fanpwm")
2578 {
2579 sensorType.insert(3, 1, '_');
2580 }
2581 return std::make_pair(sensorType, sensorName);
2582}
2583
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002584/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302585 * @brief Entry point for overriding sensor values of given sensor
2586 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002587 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002588 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002589 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302590 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002591inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002592 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002593 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002594 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302595{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002596 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002597 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302598
Ed Tanous543f4402022-01-06 13:12:53 -08002599 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302600 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302601 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002602 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302603 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302604 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302605 if (collectionItems.first == "Temperatures")
2606 {
2607 propertyValueName = "ReadingCelsius";
2608 }
2609 else if (collectionItems.first == "Fans")
2610 {
2611 propertyValueName = "Reading";
2612 }
2613 else
2614 {
2615 propertyValueName = "ReadingVolts";
2616 }
2617 for (auto& item : collectionItems.second)
2618 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002619 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2620 "MemberId", memberId, propertyValueName,
2621 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302622 {
2623 return;
2624 }
2625 overrideMap.emplace(memberId,
2626 std::make_pair(value, collectionItems.first));
2627 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302628 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002629
Ed Tanous002d39b2022-05-31 08:59:27 -07002630 auto getChassisSensorListCb =
2631 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002632 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002633 // Match sensor names in the PATCH request to those managed by the
2634 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002635 const std::shared_ptr<std::set<std::string>> sensorNames =
2636 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302637 for (const auto& item : overrideMap)
2638 {
2639 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002640 std::pair<std::string, std::string> sensorNameType =
2641 splitSensorNameAndType(sensor);
2642 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2643 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302644 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302645 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002646 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302647 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302648 return;
2649 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302650 }
2651 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002652 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002653 [sensorAsyncResp,
2654 overrideMap](const std::set<std::string>& /*connections*/,
2655 const std::set<std::pair<std::string, std::string>>&
2656 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002657 if (objectsWithConnection.size() != overrideMap.size())
2658 {
2659 BMCWEB_LOG_INFO
2660 << "Unable to find all objects with proper connection "
2661 << objectsWithConnection.size() << " requested "
2662 << overrideMap.size() << "\n";
2663 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2664 sensorAsyncResp->chassisSubNode ==
2665 sensors::node::thermal
2666 ? "Temperatures"
2667 : "Voltages",
2668 "Count");
2669 return;
2670 }
2671 for (const auto& item : objectsWithConnection)
2672 {
2673 sdbusplus::message::object_path path(item.first);
2674 std::string sensorName = path.filename();
2675 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302676 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002677 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302678 return;
2679 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302680
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002681 const auto& iterator = overrideMap.find(sensorName);
2682 if (iterator == overrideMap.end())
2683 {
2684 BMCWEB_LOG_INFO << "Unable to find sensor object"
2685 << item.first << "\n";
2686 messages::internalError(sensorAsyncResp->asyncResp->res);
2687 return;
2688 }
2689 crow::connections::systemBus->async_method_call(
2690 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002691 if (ec)
2692 {
2693 if (ec.value() ==
2694 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002695 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002696 BMCWEB_LOG_WARNING
2697 << "Manufacturing mode is not Enabled...can't "
2698 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002699
Ed Tanous002d39b2022-05-31 08:59:27 -07002700 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002701 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002702 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002703 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002704 BMCWEB_LOG_DEBUG
2705 << "setOverrideValueStatus DBUS error: " << ec;
2706 messages::internalError(
2707 sensorAsyncResp->asyncResp->res);
2708 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002709 },
2710 item.second, item.first, "org.freedesktop.DBus.Properties",
2711 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002712 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002713 }
2714 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302715 // Get object with connection for the given sensor name
2716 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2717 std::move(getObjectsWithConnectionCb));
2718 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302719 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002720 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2721 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2722 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302723}
2724
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002725/**
2726 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2727 * path of the sensor.
2728 *
2729 * Function builds valid Redfish response for sensor query of given chassis and
2730 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2731 * it to caller in a callback.
2732 *
2733 * @param chassis Chassis for which retrieval should be performed
2734 * @param node Node (group) of sensors. See sensors::node for supported values
2735 * @param mapComplete Callback to be called with retrieval result
2736 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002737inline void retrieveUriToDbusMap(const std::string& chassis,
2738 const std::string& node,
2739 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002740{
Ed Tanous02da7c52022-02-27 00:09:02 -08002741 decltype(sensors::paths)::const_iterator pathIt =
2742 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2743 [&node](auto&& val) { return val.first == node; });
2744 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002745 {
2746 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2747 mapComplete(boost::beast::http::status::bad_request, {});
2748 return;
2749 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002750
Nan Zhou72374eb2022-01-27 17:06:51 -08002751 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002752 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2753 const boost::beast::http::status status,
2754 const std::map<std::string, std::string>& uriToDbus) {
2755 mapCompleteCb(status, uriToDbus);
2756 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002757
2758 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002759 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002760 getChassisData(resp);
2761}
2762
Nan Zhoubacb2162022-04-06 11:28:32 -07002763namespace sensors
2764{
Nan Zhou928fefb2022-03-28 08:45:00 -07002765
Nan Zhoubacb2162022-04-06 11:28:32 -07002766inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002767 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2768 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002769 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002770{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002771 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002772
Ed Tanousc1d019a2022-08-06 09:36:06 -07002773 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2774 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002775 {
2776 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2777
2778 sdbusplus::message::object_path path(sensor);
2779 std::string sensorName = path.filename();
2780 if (sensorName.empty())
2781 {
2782 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002783 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002784 return;
2785 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002786 std::string type = path.parent_path().filename();
2787 // fan_tach has an underscore in it, so remove it to "normalize" the
2788 // type in the URI
2789 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2790
Ed Tanous14766872022-03-15 10:44:42 -07002791 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002792 std::string id = type;
2793 id += "_";
2794 id += sensorName;
2795 member["@odata.id"] = crow::utility::urlFromPieces(
2796 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2797
Ed Tanous14766872022-03-15 10:44:42 -07002798 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002799 }
2800
Ed Tanousc1d019a2022-08-06 09:36:06 -07002801 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002802 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2803}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002804
Nan Zhoude167a62022-06-01 04:47:45 +00002805inline void
2806 handleSensorCollectionGet(App& app, const crow::Request& req,
2807 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2808 const std::string& chassisId)
2809{
2810 query_param::QueryCapabilities capabilities = {
2811 .canDelegateExpandLevel = 1,
2812 };
2813 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002814 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002815 delegatedQuery, capabilities))
2816 {
2817 return;
2818 }
2819
2820 if (delegatedQuery.expandType != query_param::ExpandType::None)
2821 {
2822 // we perform efficient expand.
2823 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2824 aResp, chassisId, sensors::dbus::sensorPaths,
2825 sensors::node::sensors,
2826 /*efficientExpand=*/true);
2827 getChassisData(asyncResp);
2828
2829 BMCWEB_LOG_DEBUG
2830 << "SensorCollection doGet exit via efficient expand handler";
2831 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002832 }
Nan Zhoude167a62022-06-01 04:47:45 +00002833
Nan Zhoude167a62022-06-01 04:47:45 +00002834 // We get all sensors as hyperlinkes in the chassis (this
2835 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002836 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2837 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2838 sensors::node::sensors));
2839}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002840
Ed Tanousc1d019a2022-08-06 09:36:06 -07002841inline void
2842 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2843 const std::string& sensorPath,
2844 const ::dbus::utility::MapperGetObject& mapperResponse)
2845{
2846 if (mapperResponse.size() != 1)
2847 {
2848 messages::internalError(asyncResp->res);
2849 return;
2850 }
2851 const auto& valueIface = *mapperResponse.begin();
2852 const std::string& connectionName = valueIface.first;
2853 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2854 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002855
2856 sdbusplus::asio::getAllProperties(
2857 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002858 [asyncResp,
2859 sensorPath](const boost::system::error_code ec,
2860 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2861 if (ec)
2862 {
2863 messages::internalError(asyncResp->res);
2864 return;
2865 }
2866 sdbusplus::message::object_path path(sensorPath);
2867 std::string name = path.filename();
2868 path = path.parent_path();
2869 std::string type = path.filename();
2870 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2871 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002872 });
Nan Zhoude167a62022-06-01 04:47:45 +00002873}
2874
Nan Zhoue6bd8462022-06-01 04:35:35 +00002875inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002876 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002877 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002878 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002879{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002880 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002881 {
2882 return;
2883 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002884 std::pair<std::string, std::string> nameType =
2885 splitSensorNameAndType(sensorId);
2886 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002887 {
2888 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2889 return;
2890 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002891
Ed Tanous677bb752022-09-15 10:52:19 -07002892 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2893 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002894
Nan Zhoue6bd8462022-06-01 04:35:35 +00002895 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002896
George Liu2b731192023-01-11 16:27:13 +08002897 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002898 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002899 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2900 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002901 // Get a list of all of the sensors that implement Sensor.Value
2902 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002903 ::dbus::utility::getDbusObject(
2904 sensorPath, interfaces,
Ed Tanousc71d6122022-11-29 14:10:32 -08002905 [asyncResp,
George Liu2b731192023-01-11 16:27:13 +08002906 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002907 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002908 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2909 if (ec)
2910 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002911 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002912 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2913 << "Dbus error " << ec;
2914 return;
2915 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002916 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002917 BMCWEB_LOG_DEBUG << "respHandler1 exit";
George Liu2b731192023-01-11 16:27:13 +08002918 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002919}
2920
Nan Zhoubacb2162022-04-06 11:28:32 -07002921} // namespace sensors
2922
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002923inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002924{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002925 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002926 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002927 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002928 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002929}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002930
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002931inline void requestRoutesSensor(App& app)
2932{
2933 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002934 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002935 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002936 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002937}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002938
Ed Tanous1abe55e2018-09-05 08:30:59 -07002939} // namespace redfish