blob: 00b2ba61f6a85fc2f08b6879575d0c821f072b89 [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
George Liu7a1dbc42022-12-07 16:03:22 +080018#include "dbus_utility.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070019#include "generated/enums/sensor.hpp"
20
John Edward Broadbent7e860f12021-04-08 15:57:16 -070021#include <app.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070022#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070023#include <boost/algorithm/string/find.hpp>
24#include <boost/algorithm/string/predicate.hpp>
Ed Tanousc71d6122022-11-29 14:10:32 -080025#include <boost/algorithm/string/replace.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010026#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010027#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070028#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080029#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070030#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070031#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070032#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020033#include <sdbusplus/unpack_properties.hpp>
34#include <utils/dbus_utils.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053035#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070036#include <utils/query_param.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 Tanous4ee8e212022-05-28 09:42:51 -070062static 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 Tanous4ee8e212022-05-28 09:42:51 -070067static 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 Tanous4ee8e212022-05-28 09:42:51 -070083static 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
92using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
93static constexpr std::array<sensorPair, 3> paths = {
94 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
95 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
96 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000097
Ed Tanous0ec8b832022-03-14 14:56:47 -070098inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000099{
100 if (sensorType == "voltage")
101 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700102 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000103 }
104 if (sensorType == "power")
105 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700106 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000107 }
108 if (sensorType == "current")
109 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700110 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000111 }
112 if (sensorType == "fan_tach")
113 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700114 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000115 }
116 if (sensorType == "temperature")
117 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700118 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000119 }
120 if (sensorType == "fan_pwm" || sensorType == "utilization")
121 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700122 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000123 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600124 if (sensorType == "humidity")
125 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700126 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600127 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000128 if (sensorType == "altitude")
129 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700130 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000131 }
132 if (sensorType == "airflow")
133 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700134 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000135 }
136 if (sensorType == "energy")
137 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700138 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000139 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700140 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000141}
142
Ed Tanous1d7c0052022-08-09 12:32:26 -0700143inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000144{
145 if (sensorType == "voltage")
146 {
147 return "V";
148 }
149 if (sensorType == "power")
150 {
151 return "W";
152 }
153 if (sensorType == "current")
154 {
155 return "A";
156 }
157 if (sensorType == "fan_tach")
158 {
159 return "RPM";
160 }
161 if (sensorType == "temperature")
162 {
163 return "Cel";
164 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600165 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
166 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000167 {
168 return "%";
169 }
170 if (sensorType == "altitude")
171 {
172 return "m";
173 }
174 if (sensorType == "airflow")
175 {
176 return "cft_i/min";
177 }
178 if (sensorType == "energy")
179 {
180 return "J";
181 }
182 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200183}
184} // namespace sensors
185
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100186/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200187 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100188 * Gathers data needed for response processing after async calls are done
189 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700190class SensorsAsyncResp
191{
192 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200193 using DataCompleteCb = std::function<void(
194 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000195 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200196
197 struct SensorData
198 {
199 const std::string name;
200 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200201 const std::string dbusPath;
202 };
203
Ed Tanous8a592812022-06-04 09:06:59 -0700204 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800205 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800206 std::span<std::string_view> typesIn,
207 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700208 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700209 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
210 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500211 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200212
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200213 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700214 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800215 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800216 std::span<std::string_view> typesIn,
217 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200218 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700219 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700220 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
221 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200222 dataComplete{std::move(creationComplete)}
223 {}
224
Nan Zhou928fefb2022-03-28 08:45:00 -0700225 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700226 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700227 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800228 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700229 const std::string_view& subNode, bool efficientExpandIn) :
230 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700231 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700232 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700233 {}
234
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 ~SensorsAsyncResp()
236 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800237 if (asyncResp->res.result() ==
238 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 {
240 // Reset the json object to clear out any data that made it in
241 // before the error happened todo(ed) handle error condition with
242 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800243 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700244 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200245
246 if (dataComplete && metadata)
247 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000248 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800249 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200250 {
251 for (auto& sensor : *metadata)
252 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700253 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200254 }
255 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800256 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200257 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700258 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100259
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800260 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
261 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
262 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
263 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
264
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200265 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700266 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200267 {
268 if (metadata)
269 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700270 metadata->emplace_back(SensorData{
271 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200272 }
273 }
274
275 void updateUri(const std::string& name, const std::string& uri)
276 {
277 if (metadata)
278 {
279 for (auto& sensor : *metadata)
280 {
281 if (sensor.name == name)
282 {
283 sensor.uri = uri;
284 }
285 }
286 }
287 }
288
zhanghch058d1b46d2021-04-01 11:18:24 +0800289 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200290 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800291 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200292 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700293 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200294
295 private:
296 std::optional<std::vector<SensorData>> metadata;
297 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100298};
299
300/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500301 * Possible states for physical inventory leds
302 */
303enum class LedState
304{
305 OFF,
306 ON,
307 BLINK,
308 UNKNOWN
309};
310
311/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500312 * D-Bus inventory item associated with one or more sensors.
313 */
314class InventoryItem
315{
316 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700317 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500318 {
319 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800320 sdbusplus::message::object_path path(objectPath);
321 name = path.filename();
322 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500323 {
George Liu28aa8de2021-02-01 15:13:30 +0800324 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500325 }
326 }
327
328 std::string objectPath;
329 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800330 bool isPresent = true;
331 bool isFunctional = true;
332 bool isPowerSupply = false;
333 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500334 std::string manufacturer;
335 std::string model;
336 std::string partNumber;
337 std::string serialNumber;
338 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500339 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800340 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500341};
342
343/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530344 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200345 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100346 * @param sensorNames Sensors retrieved from chassis
347 * @param callback Callback for processing gathered connections
348 */
349template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530350void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000351 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000352 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530353 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700354{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530355 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700356 const std::string path = "/xyz/openbmc_project/sensors";
357 const std::array<std::string, 1> interfaces = {
358 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100359
Ed Tanous1abe55e2018-09-05 08:30:59 -0700360 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700361 auto respHandler =
362 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
363 sensorNames](const boost::system::error_code ec,
364 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530365 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700366 if (ec)
367 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800368 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530369 BMCWEB_LOG_ERROR
370 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100372 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100373
Ed Tanous1abe55e2018-09-05 08:30:59 -0700374 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
375
376 // Make unique list of connections only for requested sensor types and
377 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000378 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530379 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700381 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
382 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700383 {
384 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
385 }
386
387 for (const std::pair<
388 std::string,
389 std::vector<std::pair<std::string, std::vector<std::string>>>>&
390 object : subtree)
391 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700392 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700393 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700394 for (const std::pair<std::string, std::vector<std::string>>&
395 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700396 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700397 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
398 connections.insert(objData.first);
399 objectsWithConnection.insert(
400 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 }
402 }
403 }
404 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530405 callback(std::move(connections), std::move(objectsWithConnection));
406 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700408 // Make call to ObjectMapper to find all sensors objects
409 crow::connections::systemBus->async_method_call(
410 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
411 "/xyz/openbmc_project/object_mapper",
412 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530413 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
414}
415
416/**
417 * @brief Create connections necessary for sensors
418 * @param SensorsAsyncResp Pointer to object holding response data
419 * @param sensorNames Sensors retrieved from chassis
420 * @param callback Callback for processing gathered connections
421 */
422template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000423void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
424 const std::shared_ptr<std::set<std::string>> sensorNames,
425 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530426{
427 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000428 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530429 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700430 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000431 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530432 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100433}
434
435/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700436 * @brief Shrinks the list of sensors for processing
437 * @param SensorsAysncResp The class holding the Redfish response
438 * @param allSensors A list of all the sensors associated to the
439 * chassis element (i.e. baseboard, front panel, etc...)
440 * @param activeSensors A list that is a reduction of the incoming
441 * allSensors list. Eliminate Thermal sensors when a Power request is
442 * made, and eliminate Power sensors when a Thermal request is made.
443 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000444inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700445 crow::Response& res, std::string_view chassisSubNode,
446 std::span<std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000448 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700449{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700450 if ((allSensors == nullptr) || (activeSensors == nullptr))
451 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700452 messages::resourceNotFound(res, chassisSubNode,
453 chassisSubNode == sensors::node::thermal
454 ? "Temperatures"
455 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700456
457 return;
458 }
459 if (allSensors->empty())
460 {
461 // Nothing to do, the activeSensors object is also empty
462 return;
463 }
464
Ed Tanous7f1cc262022-08-09 13:33:57 -0700465 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700466 {
467 for (const std::string& sensor : *allSensors)
468 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700469 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700470 {
471 activeSensors->emplace(sensor);
472 }
473 }
474 }
475}
476
Ed Tanous7f1cc262022-08-09 13:33:57 -0700477/*
478 *Populates the top level collection for a given subnode. Populates
479 *SensorCollection, Power, or Thermal schemas.
480 *
481 * */
482inline void populateChassisNode(nlohmann::json& jsonValue,
483 std::string_view chassisSubNode)
484{
485 if (chassisSubNode == sensors::node::power)
486 {
487 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
488 }
489 else if (chassisSubNode == sensors::node::thermal)
490 {
491 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
492 jsonValue["Fans"] = nlohmann::json::array();
493 jsonValue["Temperatures"] = nlohmann::json::array();
494 }
495 else if (chassisSubNode == sensors::node::sensors)
496 {
497 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
498 jsonValue["Description"] = "Collection of Sensors for this Chassis";
499 jsonValue["Members"] = nlohmann::json::array();
500 jsonValue["Members@odata.count"] = 0;
501 }
502
503 if (chassisSubNode != sensors::node::sensors)
504 {
505 jsonValue["Id"] = chassisSubNode;
506 }
507 jsonValue["Name"] = chassisSubNode;
508}
509
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700510/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100511 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200512 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100513 * @param callback Callback for next step in gathered sensor processing
514 */
515template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700516void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
517 std::string_view chassisId, std::string_view chassisSubNode,
518 std::span<std::string_view> sensorTypes, Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700519{
520 BMCWEB_LOG_DEBUG << "getChassis enter";
George Liu7a1dbc42022-12-07 16:03:22 +0800521 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700522 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500523 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800524
525 // Get the Chassis Collection
526 dbus::utility::getSubTreePaths(
527 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700528 [callback{std::forward<Callback>(callback)}, asyncResp,
529 chassisIdStr{std::string(chassisId)},
530 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800531 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700532 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700533 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
534 if (ec)
535 {
536 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700537 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700538 return;
539 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700540 const std::string* chassisPath = nullptr;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542 {
George Liu28aa8de2021-02-01 15:13:30 +0800543 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700544 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800545 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700547 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 continue;
549 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700550 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700551 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700552 chassisPath = &chassis;
553 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700554 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700555 }
556 if (chassisPath == nullptr)
557 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700558 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700559 return;
560 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700561 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700562
Ed Tanous7f1cc262022-08-09 13:33:57 -0700563 asyncResp->res.jsonValue["@odata.id"] =
564 "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500565
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500566 // Get the list of all sensors for this Chassis element
567 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700568 sdbusplus::asio::getProperty<std::vector<std::string>>(
569 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
570 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700571 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800572 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700573 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700574 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 if (e)
576 {
577 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700578 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700579 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700581 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000583 const std::shared_ptr<std::set<std::string>> culledSensorList =
584 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700585 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
586 &nodeSensorList, culledSensorList);
587 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700588 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700589 });
George Liu7a1dbc42022-12-07 16:03:22 +0800590 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700591 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100592}
593
594/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500595 * @brief Returns the Redfish State value for the specified inventory item.
596 * @param inventoryItem D-Bus inventory item associated with a sensor.
597 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700598 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000599inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500600{
601 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
602 {
603 return "Absent";
604 }
James Feist34dd1792019-05-17 14:10:54 -0700605
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500606 return "Enabled";
607}
608
609/**
610 * @brief Returns the Redfish Health value for the specified sensor.
611 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700612 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500613 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
614 * be nullptr if no associated inventory item was found.
615 * @return Health value for sensor.
616 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700617inline std::string getHealth(nlohmann::json& sensorJson,
618 const dbus::utility::DBusPropertiesMap& valuesDict,
619 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700620{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500621 // Get current health value (if any) in the sensor JSON object. Some JSON
622 // objects contain multiple sensors (such as PowerSupplies). We want to set
623 // the overall health to be the most severe of any of the sensors.
624 std::string currentHealth;
625 auto statusIt = sensorJson.find("Status");
626 if (statusIt != sensorJson.end())
627 {
628 auto healthIt = statusIt->find("Health");
629 if (healthIt != statusIt->end())
630 {
631 std::string* health = healthIt->get_ptr<std::string*>();
632 if (health != nullptr)
633 {
634 currentHealth = *health;
635 }
636 }
637 }
638
639 // If current health in JSON object is already Critical, return that. This
640 // should override the sensor health, which might be less severe.
641 if (currentHealth == "Critical")
642 {
643 return "Critical";
644 }
645
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200646 const bool* criticalAlarmHigh = nullptr;
647 const bool* criticalAlarmLow = nullptr;
648 const bool* warningAlarmHigh = nullptr;
649 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800650
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200651 const bool success = sdbusplus::unpackPropertiesNoThrow(
652 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
653 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
654 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
655 warningAlarmLow);
656
657 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700658 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200659 // Check if sensor has critical threshold alarm
660 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
661 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700662 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200663 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700664 }
665 }
666
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500667 // Check if associated inventory item is not functional
668 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
669 {
670 return "Critical";
671 }
672
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200673 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500674 // should override the sensor status, which might be less severe.
675 if (currentHealth == "Warning")
676 {
677 return "Warning";
678 }
679
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200680 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700681 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200682 // Check if sensor has warning threshold alarm
683 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
684 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700685 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200686 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700687 }
688 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500689
James Feist34dd1792019-05-17 14:10:54 -0700690 return "OK";
691}
692
Ed Tanous23a21a12020-07-25 04:45:05 +0000693inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500694 const InventoryItem* inventoryItem)
695{
696 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
697 {
698 switch (inventoryItem->ledState)
699 {
700 case LedState::OFF:
701 sensorJson["IndicatorLED"] = "Off";
702 break;
703 case LedState::ON:
704 sensorJson["IndicatorLED"] = "Lit";
705 break;
706 case LedState::BLINK:
707 sensorJson["IndicatorLED"] = "Blinking";
708 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000709 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500710 break;
711 }
712 }
713}
714
James Feist34dd1792019-05-17 14:10:54 -0700715/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100716 * @brief Builds a json sensor representation of a sensor.
717 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500718 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100719 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700720 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
721 * @param propertiesDict A dictionary of the properties to build the sensor
722 * from.
723 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500724 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
725 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100726 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700727inline void objectPropertiesToJson(
728 std::string_view sensorName, std::string_view sensorType,
729 std::string_view chassisSubNode,
730 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000731 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700732{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700733 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500734 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800735 std::string subNodeEscaped(sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700736 subNodeEscaped.erase(
737 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
738 subNodeEscaped.end());
739
740 // For sensors in SensorCollection we set Id instead of MemberId,
741 // including power sensors.
742 subNodeEscaped += '_';
743 subNodeEscaped += sensorName;
744 sensorJson["Id"] = std::move(subNodeEscaped);
745
Ed Tanous1d7c0052022-08-09 12:32:26 -0700746 std::string sensorNameEs(sensorName);
747 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
748 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500749 }
750 else if (sensorType != "power")
751 {
752 // Set MemberId and Name for non-power sensors. For PowerSupplies and
753 // PowerControl, those properties have more general values because
754 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000755 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700756 std::string sensorNameEs(sensorName);
757 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
758 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500759 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700760
Ed Tanous81ce6092020-12-17 16:54:55 +0000761 sensorJson["Status"]["State"] = getState(inventoryItem);
762 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700763 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700764
765 // Parameter to set to override the type we get from dbus, and force it to
766 // int, regardless of what is available. This is used for schemas like fan,
767 // that require integers, not floats.
768 bool forceToInt = false;
769
Anthony Wilson3929aca2019-07-19 15:42:33 -0500770 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700771 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500772 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000773 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000774
Ed Tanous0ec8b832022-03-14 14:56:47 -0700775 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
776 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500777 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000778 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
779 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500780 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000781 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500782 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000783 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500784 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000785
Ed Tanous1d7c0052022-08-09 12:32:26 -0700786 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000787 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200788 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000789 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
790 << sensorType;
791 }
792 else
793 {
794 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200795 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500796 }
797 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700798 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500799 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000800 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700801 // TODO(ed) Documentation says that path should be type fan_tach,
802 // implementation seems to implement fan
803 }
804 else if (sensorType == "fan" || sensorType == "fan_tach")
805 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500806 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000807 sensorJson["ReadingUnits"] = "RPM";
808 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
809 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700810 forceToInt = true;
811 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700812 else if (sensorType == "fan_pwm")
813 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500814 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000815 sensorJson["ReadingUnits"] = "Percent";
816 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
817 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700818 forceToInt = true;
819 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700820 else if (sensorType == "voltage")
821 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500822 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000823 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700824 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700825 else if (sensorType == "power")
826 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700827 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000828 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000829 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500830 // Put multiple "sensors" into a single PowerControl, so have
831 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000832 sensorJson["MemberId"] = "0";
833 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500834 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000835 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700836 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700837 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500838 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700839 }
840 else
841 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500842 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700843 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700844 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845 else
846 {
847 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
848 return;
849 }
850 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500851 std::vector<
852 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
853 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700854 properties.reserve(7);
855
856 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600857
Ed Tanous1d7c0052022-08-09 12:32:26 -0700858 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500859 {
860 properties.emplace_back(
861 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
862 "/Thresholds/UpperCaution/Reading"_json_pointer);
863 properties.emplace_back(
864 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
865 "/Thresholds/LowerCaution/Reading"_json_pointer);
866 properties.emplace_back(
867 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
868 "/Thresholds/UpperCritical/Reading"_json_pointer);
869 properties.emplace_back(
870 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
871 "/Thresholds/LowerCritical/Reading"_json_pointer);
872 }
873 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600874 {
875 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500876 "WarningHigh",
877 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600878 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500879 "WarningLow",
880 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600881 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500882 "CriticalHigh",
883 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600884 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500885 "CriticalLow",
886 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600887 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700888
Ed Tanous2474adf2018-09-05 16:31:16 -0700889 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
890
Ed Tanous1d7c0052022-08-09 12:32:26 -0700891 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500892 {
893 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500894 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500895 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500896 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800897 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
898 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500899 }
900 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 {
902 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500903 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500905 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700906 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500907 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700908 {
909 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500910 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700911 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700913 }
914
Anthony Wilson3929aca2019-07-19 15:42:33 -0500915 for (const std::tuple<const char*, const char*,
916 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700918 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700920 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700921 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800922 continue;
923 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700924
925 // The property we want to set may be nested json, so use
926 // a json_pointer for easy indexing into the json structure.
927 const nlohmann::json::json_pointer& key = std::get<2>(p);
928
Ed Tanous1d7c0052022-08-09 12:32:26 -0700929 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700930 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800931 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700932 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700933 continue;
934 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700935 if (forceToInt)
936 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700937 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700938 }
939 else
940 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700941 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 }
943 }
944 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100945}
946
Ed Tanous1d7c0052022-08-09 12:32:26 -0700947/**
948 * @brief Builds a json sensor representation of a sensor.
949 * @param sensorName The name of the sensor to be built
950 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
951 * build
952 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
953 * @param interfacesDict A dictionary of the interfaces and properties of said
954 * interfaces to be built from
955 * @param sensorJson The json object to fill
956 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
957 * be nullptr if no associated inventory item was found.
958 */
959inline void objectInterfacesToJson(
960 const std::string& sensorName, const std::string& sensorType,
961 const std::string& chassisSubNode,
962 const dbus::utility::DBusInteracesMap& interfacesDict,
963 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
964{
965
966 for (const auto& [interface, valuesDict] : interfacesDict)
967 {
968 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
969 valuesDict, sensorJson, inventoryItem);
970 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700971 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700972}
973
Ed Tanousb5a76932020-09-29 16:16:58 -0700974inline void populateFanRedundancy(
975 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700976{
977 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -0800978 [sensorsAsyncResp](
979 const boost::system::error_code ec,
980 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700981 if (ec)
982 {
983 return; // don't have to have this interface
984 }
985 for (const std::pair<
986 std::string,
987 std::vector<std::pair<std::string, std::vector<std::string>>>>&
988 pathPair : resp)
989 {
990 const std::string& path = pathPair.first;
991 const std::vector<std::pair<std::string, std::vector<std::string>>>&
992 objDict = pathPair.second;
993 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700994 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700995 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700996 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700997
998 const std::string& owner = objDict.begin()->first;
999 sdbusplus::asio::getProperty<std::vector<std::string>>(
1000 *crow::connections::systemBus,
1001 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1002 "xyz.openbmc_project.Association", "endpoints",
1003 [path, owner,
1004 sensorsAsyncResp](const boost::system::error_code e,
1005 const std::vector<std::string>& endpoints) {
1006 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001007 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001008 return; // if they don't have an association we
1009 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001010 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001011 auto found =
1012 std::find_if(endpoints.begin(), endpoints.end(),
1013 [sensorsAsyncResp](const std::string& entry) {
1014 return entry.find(sensorsAsyncResp->chassisId) !=
1015 std::string::npos;
1016 });
James Feist8bd25cc2019-03-15 15:14:00 -07001017
Ed Tanous002d39b2022-05-31 08:59:27 -07001018 if (found == endpoints.end())
1019 {
1020 return;
1021 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001022 sdbusplus::asio::getAllProperties(
1023 *crow::connections::systemBus, owner, path,
1024 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001025 [path, sensorsAsyncResp](
1026 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001027 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001028 if (err)
1029 {
1030 return; // don't have to have this
1031 // interface
1032 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001033
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001034 const uint8_t* allowedFailures = nullptr;
1035 const std::vector<std::string>* collection = nullptr;
1036 const std::string* status = nullptr;
1037
1038 const bool success = sdbusplus::unpackPropertiesNoThrow(
1039 dbus_utils::UnpackErrorPrinter(), ret,
1040 "AllowedFailures", allowedFailures, "Collection",
1041 collection, "Status", status);
1042
1043 if (!success)
1044 {
1045 messages::internalError(
1046 sensorsAsyncResp->asyncResp->res);
1047 return;
1048 }
1049
1050 if (allowedFailures == nullptr || collection == nullptr ||
1051 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001052 {
1053 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1054 messages::internalError(
1055 sensorsAsyncResp->asyncResp->res);
1056 return;
1057 }
1058
Ed Tanous002d39b2022-05-31 08:59:27 -07001059 sdbusplus::message::object_path objectPath(path);
1060 std::string name = objectPath.filename();
1061 if (name.empty())
1062 {
1063 // this should be impossible
1064 messages::internalError(
1065 sensorsAsyncResp->asyncResp->res);
1066 return;
1067 }
1068 std::replace(name.begin(), name.end(), '_', ' ');
1069
1070 std::string health;
1071
Ed Tanous11ba3972022-07-11 09:50:41 -07001072 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001073 {
1074 health = "OK";
1075 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001076 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001077 {
1078 health = "Warning";
1079 }
1080 else
1081 {
1082 health = "Critical";
1083 }
1084 nlohmann::json::array_t redfishCollection;
1085 const auto& fanRedfish =
1086 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1087 for (const std::string& item : *collection)
1088 {
Ed Tanous8a592812022-06-04 09:06:59 -07001089 sdbusplus::message::object_path itemPath(item);
1090 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001091 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001092 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001093 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001094 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 /*
1096 todo(ed): merge patch that fixes the names
1097 std::replace(itemName.begin(),
1098 itemName.end(), '_', ' ');*/
1099 auto schemaItem =
1100 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1101 [itemName](const nlohmann::json& fan) {
1102 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001103 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001104 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001105 {
Ed Tanous8a592812022-06-04 09:06:59 -07001106 nlohmann::json::object_t collectionId;
1107 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001108 (*schemaItem)["@odata.id"];
1109 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001110 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001111 }
1112 else
1113 {
1114 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1115 messages::internalError(
1116 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001117 return;
1118 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001119 }
James Feist8bd25cc2019-03-15 15:14:00 -07001120
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 size_t minNumNeeded =
1122 collection->empty()
1123 ? 0
1124 : collection->size() - *allowedFailures;
1125 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1126 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001127
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 nlohmann::json::object_t redundancy;
1129 redundancy["@odata.id"] =
1130 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1131 "/" + sensorsAsyncResp->chassisSubNode +
1132 "#/Redundancy/" + std::to_string(jResp.size());
1133 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1134 redundancy["MinNumNeeded"] = minNumNeeded;
1135 redundancy["MemberId"] = name;
1136 redundancy["Mode"] = "N+m";
1137 redundancy["Name"] = name;
1138 redundancy["RedundancySet"] = redfishCollection;
1139 redundancy["Status"]["Health"] = health;
1140 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001141
Ed Tanous002d39b2022-05-31 08:59:27 -07001142 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001143 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001144 });
1145 }
James Feist8bd25cc2019-03-15 15:14:00 -07001146 },
1147 "xyz.openbmc_project.ObjectMapper",
1148 "/xyz/openbmc_project/object_mapper",
1149 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1150 "/xyz/openbmc_project/control", 2,
1151 std::array<const char*, 1>{
1152 "xyz.openbmc_project.Control.FanRedundancy"});
1153}
1154
Ed Tanousb5a76932020-09-29 16:16:58 -07001155inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001156 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001157{
zhanghch058d1b46d2021-04-01 11:18:24 +08001158 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001159 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001160 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001161 {
1162 sensorHeaders = {"Voltages", "PowerSupplies"};
1163 }
1164 for (const std::string& sensorGroup : sensorHeaders)
1165 {
1166 nlohmann::json::iterator entry = response.find(sensorGroup);
1167 if (entry != response.end())
1168 {
1169 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001170 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001171 return c1["Name"] < c2["Name"];
1172 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001173
1174 // add the index counts to the end of each entry
1175 size_t count = 0;
1176 for (nlohmann::json& sensorJson : *entry)
1177 {
1178 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1179 if (odata == sensorJson.end())
1180 {
1181 continue;
1182 }
1183 std::string* value = odata->get_ptr<std::string*>();
1184 if (value != nullptr)
1185 {
1186 *value += std::to_string(count);
1187 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001188 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001189 }
1190 }
1191 }
1192 }
1193}
1194
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001195/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001196 * @brief Finds the inventory item with the specified object path.
1197 * @param inventoryItems D-Bus inventory items associated with sensors.
1198 * @param invItemObjPath D-Bus object path of inventory item.
1199 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001200 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001201inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001202 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001203 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001204{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001205 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001206 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001207 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001208 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001209 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001210 }
1211 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001212 return nullptr;
1213}
1214
1215/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001216 * @brief Finds the inventory item associated with the specified sensor.
1217 * @param inventoryItems D-Bus inventory items associated with sensors.
1218 * @param sensorObjPath D-Bus object path of sensor.
1219 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001220 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001221inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001222 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001223 const std::string& sensorObjPath)
1224{
1225 for (InventoryItem& inventoryItem : *inventoryItems)
1226 {
1227 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1228 {
1229 return &inventoryItem;
1230 }
1231 }
1232 return nullptr;
1233}
1234
1235/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001236 * @brief Finds the inventory item associated with the specified led path.
1237 * @param inventoryItems D-Bus inventory items associated with sensors.
1238 * @param ledObjPath D-Bus object path of led.
1239 * @return Inventory item within vector, or nullptr if no match found.
1240 */
1241inline InventoryItem*
1242 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1243 const std::string& ledObjPath)
1244{
1245 for (InventoryItem& inventoryItem : inventoryItems)
1246 {
1247 if (inventoryItem.ledObjectPath == ledObjPath)
1248 {
1249 return &inventoryItem;
1250 }
1251 }
1252 return nullptr;
1253}
1254
1255/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001256 * @brief Adds inventory item and associated sensor to specified vector.
1257 *
1258 * Adds a new InventoryItem to the vector if necessary. Searches for an
1259 * existing InventoryItem with the specified object path. If not found, one is
1260 * added to the vector.
1261 *
1262 * Next, the specified sensor is added to the set of sensors associated with the
1263 * InventoryItem.
1264 *
1265 * @param inventoryItems D-Bus inventory items associated with sensors.
1266 * @param invItemObjPath D-Bus object path of inventory item.
1267 * @param sensorObjPath D-Bus object path of sensor
1268 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001269inline void addInventoryItem(
1270 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1271 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001272{
1273 // Look for inventory item in vector
1274 InventoryItem* inventoryItem =
1275 findInventoryItem(inventoryItems, invItemObjPath);
1276
1277 // If inventory item doesn't exist in vector, add it
1278 if (inventoryItem == nullptr)
1279 {
1280 inventoryItems->emplace_back(invItemObjPath);
1281 inventoryItem = &(inventoryItems->back());
1282 }
1283
1284 // Add sensor to set of sensors associated with inventory item
1285 inventoryItem->sensors.emplace(sensorObjPath);
1286}
1287
1288/**
1289 * @brief Stores D-Bus data in the specified inventory item.
1290 *
1291 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1292 * specified InventoryItem.
1293 *
1294 * This data is later used to provide sensor property values in the JSON
1295 * response.
1296 *
1297 * @param inventoryItem Inventory item where data will be stored.
1298 * @param interfacesDict Map containing D-Bus interfaces and their properties
1299 * for the specified inventory item.
1300 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001301inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001302 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001303 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001304{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001305 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001306
Ed Tanous9eb808c2022-01-25 10:19:23 -08001307 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001308 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001309 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001310 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001311 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001312 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001313 if (name == "Present")
1314 {
1315 const bool* value = std::get_if<bool>(&dbusValue);
1316 if (value != nullptr)
1317 {
1318 inventoryItem.isPresent = *value;
1319 }
1320 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001321 }
1322 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001323 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001324
Ed Tanous711ac7a2021-12-20 09:34:41 -08001325 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001326 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001327 inventoryItem.isPowerSupply = true;
1328 }
1329
1330 // Get properties from Inventory.Decorator.Asset interface
1331 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1332 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001333 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001334 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001335 if (name == "Manufacturer")
1336 {
1337 const std::string* value =
1338 std::get_if<std::string>(&dbusValue);
1339 if (value != nullptr)
1340 {
1341 inventoryItem.manufacturer = *value;
1342 }
1343 }
1344 if (name == "Model")
1345 {
1346 const std::string* value =
1347 std::get_if<std::string>(&dbusValue);
1348 if (value != nullptr)
1349 {
1350 inventoryItem.model = *value;
1351 }
1352 }
1353 if (name == "SerialNumber")
1354 {
1355 const std::string* value =
1356 std::get_if<std::string>(&dbusValue);
1357 if (value != nullptr)
1358 {
1359 inventoryItem.serialNumber = *value;
1360 }
1361 }
1362 if (name == "PartNumber")
1363 {
1364 const std::string* value =
1365 std::get_if<std::string>(&dbusValue);
1366 if (value != nullptr)
1367 {
1368 inventoryItem.partNumber = *value;
1369 }
1370 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001371 }
1372 }
1373
Ed Tanous711ac7a2021-12-20 09:34:41 -08001374 if (interface ==
1375 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001376 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001377 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001378 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001379 if (name == "Functional")
1380 {
1381 const bool* value = std::get_if<bool>(&dbusValue);
1382 if (value != nullptr)
1383 {
1384 inventoryItem.isFunctional = *value;
1385 }
1386 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001387 }
1388 }
1389 }
1390}
1391
1392/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001393 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001394 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001395 * Uses the specified connections (services) to obtain D-Bus data for inventory
1396 * items associated with sensors. Stores the resulting data in the
1397 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001398 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001399 * This data is later used to provide sensor property values in the JSON
1400 * response.
1401 *
1402 * Finds the inventory item data asynchronously. Invokes callback when data has
1403 * been obtained.
1404 *
1405 * The callback must have the following signature:
1406 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001407 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001408 * @endcode
1409 *
1410 * This function is called recursively, obtaining data asynchronously from one
1411 * connection in each call. This ensures the callback is not invoked until the
1412 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001413 *
1414 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001415 * @param inventoryItems D-Bus inventory items associated with sensors.
1416 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001417 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001418 * @param callback Callback to invoke when inventory data has been obtained.
1419 * @param invConnectionsIndex Current index in invConnections. Only specified
1420 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001421 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001422template <typename Callback>
1423static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001424 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001425 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001426 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1427 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001428{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001429 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001430
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001431 // If no more connections left, call callback
1432 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001433 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001434 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001435 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1436 return;
1437 }
1438
1439 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001440 auto it = invConnections->begin();
1441 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001442 if (it != invConnections->end())
1443 {
1444 const std::string& invConnection = *it;
1445
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001446 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001447 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1448 callback{std::forward<Callback>(callback)},
1449 invConnectionsIndex](
1450 const boost::system::error_code ec,
1451 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001452 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001453 if (ec)
1454 {
1455 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001456 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001457 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001458 return;
1459 }
1460
1461 // Loop through returned object paths
1462 for (const auto& objDictEntry : resp)
1463 {
1464 const std::string& objPath =
1465 static_cast<const std::string&>(objDictEntry.first);
1466
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001467 // If this object path is one of the specified inventory items
1468 InventoryItem* inventoryItem =
1469 findInventoryItem(inventoryItems, objPath);
1470 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001471 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001472 // Store inventory data in InventoryItem
1473 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001474 }
1475 }
1476
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001477 // Recurse to get inventory item data from next connection
1478 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001479 invConnections, std::move(callback),
1480 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001481
1482 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483 };
1484
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001485 // Get all object paths and their interfaces for current connection
1486 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001487 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001488 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001489 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1490 }
1491
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001492 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001493}
1494
1495/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001497 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001498 * Gets the D-Bus connections (services) that provide data for the inventory
1499 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001500 *
1501 * Finds the connections asynchronously. Invokes callback when information has
1502 * been obtained.
1503 *
1504 * The callback must have the following signature:
1505 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001506 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001507 * @endcode
1508 *
1509 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001510 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001511 * @param callback Callback to invoke when connections have been obtained.
1512 */
1513template <typename Callback>
1514static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001515 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1516 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001517 Callback&& callback)
1518{
1519 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1520
1521 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001522 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001523 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001524 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1525 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001526 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1527
1528 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001529 auto respHandler =
1530 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1531 inventoryItems](
1532 const boost::system::error_code ec,
1533 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1535 if (ec)
1536 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001537 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001538 BMCWEB_LOG_ERROR
1539 << "getInventoryItemsConnections respHandler DBus error " << ec;
1540 return;
1541 }
1542
1543 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001544 std::shared_ptr<std::set<std::string>> invConnections =
1545 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001546
1547 // Loop through objects from GetSubTree
1548 for (const std::pair<
1549 std::string,
1550 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1551 object : subtree)
1552 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001553 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001555 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001556 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001557 // Store all connections to inventory item
1558 for (const std::pair<std::string, std::vector<std::string>>&
1559 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001561 const std::string& invConnection = objData.first;
1562 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001563 }
1564 }
1565 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001566
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001567 callback(invConnections);
1568 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1569 };
1570
1571 // Make call to ObjectMapper to find all inventory items
1572 crow::connections::systemBus->async_method_call(
1573 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1574 "/xyz/openbmc_project/object_mapper",
1575 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1576 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1577}
1578
1579/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001580 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001581 *
1582 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001583 * inventory items. Then finds the associations from those inventory items to
1584 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001585 *
1586 * Finds the inventory items asynchronously. Invokes callback when information
1587 * has been obtained.
1588 *
1589 * The callback must have the following signature:
1590 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001591 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001592 * @endcode
1593 *
1594 * @param sensorsAsyncResp Pointer to object holding response data.
1595 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001596 * implements ObjectManager.
1597 * @param callback Callback to invoke when inventory items have been obtained.
1598 */
1599template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001600static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001601 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001602 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001603 Callback&& callback)
1604{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001605 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001606
1607 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001608 auto respHandler =
1609 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1610 sensorNames](const boost::system::error_code ec,
1611 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001612 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 if (ec)
1614 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001615 BMCWEB_LOG_ERROR
1616 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001617 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618 return;
1619 }
1620
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001621 // Create vector to hold list of inventory items
1622 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1623 std::make_shared<std::vector<InventoryItem>>();
1624
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626 std::string sensorAssocPath;
1627 sensorAssocPath.reserve(128); // avoid memory allocations
1628 for (const auto& objDictEntry : resp)
1629 {
1630 const std::string& objPath =
1631 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001632
1633 // If path is inventory association for one of the specified sensors
1634 for (const std::string& sensorName : *sensorNames)
1635 {
1636 sensorAssocPath = sensorName;
1637 sensorAssocPath += "/inventory";
1638 if (objPath == sensorAssocPath)
1639 {
1640 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001641 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001643 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001644 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001645 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001646 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001647 if (valueName == "endpoints")
1648 {
1649 const std::vector<std::string>* endpoints =
1650 std::get_if<std::vector<std::string>>(
1651 &value);
1652 if ((endpoints != nullptr) &&
1653 !endpoints->empty())
1654 {
1655 // Add inventory item to vector
1656 const std::string& invItemPath =
1657 endpoints->front();
1658 addInventoryItem(inventoryItems,
1659 invItemPath,
1660 sensorName);
1661 }
1662 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001663 }
1664 }
1665 }
1666 break;
1667 }
1668 }
1669 }
1670
Anthony Wilsond5005492019-07-31 16:34:17 -05001671 // Now loop through the returned object paths again, this time to
1672 // find the leds associated with the inventory items we just found
1673 std::string inventoryAssocPath;
1674 inventoryAssocPath.reserve(128); // avoid memory allocations
1675 for (const auto& objDictEntry : resp)
1676 {
1677 const std::string& objPath =
1678 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001679
1680 for (InventoryItem& inventoryItem : *inventoryItems)
1681 {
1682 inventoryAssocPath = inventoryItem.objectPath;
1683 inventoryAssocPath += "/leds";
1684 if (objPath == inventoryAssocPath)
1685 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001686 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001687 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001688 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001689 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001690 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001691 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001692 if (valueName == "endpoints")
1693 {
1694 const std::vector<std::string>* endpoints =
1695 std::get_if<std::vector<std::string>>(
1696 &value);
1697 if ((endpoints != nullptr) &&
1698 !endpoints->empty())
1699 {
1700 // Add inventory item to vector
1701 // Store LED path in inventory item
1702 const std::string& ledPath =
1703 endpoints->front();
1704 inventoryItem.ledObjectPath = ledPath;
1705 }
1706 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001707 }
1708 }
1709 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001710
Anthony Wilsond5005492019-07-31 16:34:17 -05001711 break;
1712 }
1713 }
1714 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001715 callback(inventoryItems);
1716 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001717 };
1718
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001719 // Call GetManagedObjects on the ObjectMapper to get all associations
1720 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001721 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001722 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1723
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001724 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725}
1726
1727/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001728 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1729 *
1730 * Uses the specified connections (services) to obtain D-Bus data for inventory
1731 * item leds associated with sensors. Stores the resulting data in the
1732 * inventoryItems vector.
1733 *
1734 * This data is later used to provide sensor property values in the JSON
1735 * response.
1736 *
1737 * Finds the inventory item led data asynchronously. Invokes callback when data
1738 * has been obtained.
1739 *
1740 * The callback must have the following signature:
1741 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001742 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001743 * @endcode
1744 *
1745 * This function is called recursively, obtaining data asynchronously from one
1746 * connection in each call. This ensures the callback is not invoked until the
1747 * last asynchronous function has completed.
1748 *
1749 * @param sensorsAsyncResp Pointer to object holding response data.
1750 * @param inventoryItems D-Bus inventory items associated with sensors.
1751 * @param ledConnections Connections that provide data for the inventory leds.
1752 * @param callback Callback to invoke when inventory data has been obtained.
1753 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1754 * in recursive calls to this function.
1755 */
1756template <typename Callback>
1757void getInventoryLedData(
1758 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1759 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001760 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001761 Callback&& callback, size_t ledConnectionsIndex = 0)
1762{
1763 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1764
1765 // If no more connections left, call callback
1766 if (ledConnectionsIndex >= ledConnections->size())
1767 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001768 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001769 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1770 return;
1771 }
1772
1773 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001774 auto it = ledConnections->begin();
1775 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001776 if (it != ledConnections->end())
1777 {
1778 const std::string& ledPath = (*it).first;
1779 const std::string& ledConnection = (*it).second;
1780 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001781 auto respHandler =
1782 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001783 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001784 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1786 if (ec)
1787 {
1788 BMCWEB_LOG_ERROR
1789 << "getInventoryLedData respHandler DBus error " << ec;
1790 messages::internalError(sensorsAsyncResp->asyncResp->res);
1791 return;
1792 }
1793
1794 BMCWEB_LOG_DEBUG << "Led state: " << state;
1795 // Find inventory item with this LED object path
1796 InventoryItem* inventoryItem =
1797 findInventoryItemForLed(*inventoryItems, ledPath);
1798 if (inventoryItem != nullptr)
1799 {
1800 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001801 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001802 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001804 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001805 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001806 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001807 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001808 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001809 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001810 {
1811 inventoryItem->ledState = LedState::OFF;
1812 }
1813 else
1814 {
1815 inventoryItem->ledState = LedState::UNKNOWN;
1816 }
1817 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001818
Ed Tanous002d39b2022-05-31 08:59:27 -07001819 // Recurse to get LED data from next connection
1820 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1821 ledConnections, std::move(callback),
1822 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001823
Ed Tanous002d39b2022-05-31 08:59:27 -07001824 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1825 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001826
1827 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001828 sdbusplus::asio::getProperty<std::string>(
1829 *crow::connections::systemBus, ledConnection, ledPath,
1830 "xyz.openbmc_project.Led.Physical", "State",
1831 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001832 }
1833
1834 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1835}
1836
1837/**
1838 * @brief Gets LED data for LEDs associated with given inventory items.
1839 *
1840 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1841 * associated with the specified inventory items. Then gets the LED data from
1842 * each connection and stores it in the inventory item.
1843 *
1844 * This data is later used to provide sensor property values in the JSON
1845 * response.
1846 *
1847 * Finds the LED data asynchronously. Invokes callback when information has
1848 * been obtained.
1849 *
1850 * The callback must have the following signature:
1851 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001852 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001853 * @endcode
1854 *
1855 * @param sensorsAsyncResp Pointer to object holding response data.
1856 * @param inventoryItems D-Bus inventory items associated with sensors.
1857 * @param callback Callback to invoke when inventory items have been obtained.
1858 */
1859template <typename Callback>
1860void getInventoryLeds(
1861 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1862 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1863 Callback&& callback)
1864{
1865 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1866
1867 const std::string path = "/xyz/openbmc_project";
1868 const std::array<std::string, 1> interfaces = {
1869 "xyz.openbmc_project.Led.Physical"};
1870
1871 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001872 auto respHandler =
1873 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1874 inventoryItems](
1875 const boost::system::error_code ec,
1876 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001877 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1878 if (ec)
1879 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001880 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001881 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1882 << ec;
1883 return;
1884 }
1885
1886 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001887 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1888 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001889
1890 // Loop through objects from GetSubTree
1891 for (const std::pair<
1892 std::string,
1893 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1894 object : subtree)
1895 {
1896 // Check if object path is LED for one of the specified inventory
1897 // items
1898 const std::string& ledPath = object.first;
1899 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1900 {
1901 // Add mapping from ledPath to connection
1902 const std::string& connection = object.second.begin()->first;
1903 (*ledConnections)[ledPath] = connection;
1904 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1905 << connection;
1906 }
1907 }
1908
1909 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1910 std::move(callback));
1911 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1912 };
1913 // Make call to ObjectMapper to find all inventory items
1914 crow::connections::systemBus->async_method_call(
1915 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1916 "/xyz/openbmc_project/object_mapper",
1917 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1918 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1919}
1920
1921/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001922 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1923 *
1924 * Uses the specified connections (services) (currently assumes just one) to
1925 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1926 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1927 *
1928 * This data is later used to provide sensor property values in the JSON
1929 * response.
1930 *
1931 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1932 * when data has been obtained.
1933 *
1934 * The callback must have the following signature:
1935 * @code
1936 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1937 * @endcode
1938 *
1939 * @param sensorsAsyncResp Pointer to object holding response data.
1940 * @param inventoryItems D-Bus inventory items associated with sensors.
1941 * @param psAttributesConnections Connections that provide data for the Power
1942 * Supply Attributes
1943 * @param callback Callback to invoke when data has been obtained.
1944 */
1945template <typename Callback>
1946void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001947 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001948 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001949 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001950 Callback&& callback)
1951{
1952 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1953
1954 if (psAttributesConnections.empty())
1955 {
1956 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1957 callback(inventoryItems);
1958 return;
1959 }
1960
1961 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001962 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001963
1964 const std::string& psAttributesPath = (*it).first;
1965 const std::string& psAttributesConnection = (*it).second;
1966
1967 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001968 auto respHandler =
1969 [sensorsAsyncResp, inventoryItems,
1970 callback{std::forward<Callback>(callback)}](
1971 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001972 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1973 if (ec)
1974 {
1975 BMCWEB_LOG_ERROR
1976 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001977 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001978 return;
1979 }
1980
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001981 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1982 // Store value in Power Supply Inventory Items
1983 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001984 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001985 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001986 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001987 inventoryItem.powerSupplyEfficiencyPercent =
1988 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001989 }
1990 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001991
1992 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1993 callback(inventoryItems);
1994 };
1995
1996 // Get the DeratingFactor property for the PowerSupplyAttributes
1997 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001998 sdbusplus::asio::getProperty<uint32_t>(
1999 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2000 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2001 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002002
2003 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2004}
2005
2006/**
2007 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2008 *
2009 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2010 * data. Then gets the Power Supply Attributes data from the connection
2011 * (currently just assumes 1 connection) and stores the data in the inventory
2012 * item.
2013 *
2014 * This data is later used to provide sensor property values in the JSON
2015 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2016 *
2017 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2018 * when information has been obtained.
2019 *
2020 * The callback must have the following signature:
2021 * @code
2022 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2023 * @endcode
2024 *
2025 * @param sensorsAsyncResp Pointer to object holding response data.
2026 * @param inventoryItems D-Bus inventory items associated with sensors.
2027 * @param callback Callback to invoke when data has been obtained.
2028 */
2029template <typename Callback>
2030void getPowerSupplyAttributes(
2031 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2032 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2033 Callback&& callback)
2034{
2035 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2036
2037 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002038 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002039 {
2040 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2041 callback(inventoryItems);
2042 return;
2043 }
2044
2045 const std::array<std::string, 1> interfaces = {
2046 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2047
2048 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002049 auto respHandler =
2050 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2051 inventoryItems](
2052 const boost::system::error_code ec,
2053 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2055 if (ec)
2056 {
2057 messages::internalError(sensorsAsyncResp->asyncResp->res);
2058 BMCWEB_LOG_ERROR
2059 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2060 return;
2061 }
2062 if (subtree.empty())
2063 {
2064 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2065 callback(inventoryItems);
2066 return;
2067 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002068
Ed Tanous002d39b2022-05-31 08:59:27 -07002069 // Currently we only support 1 power supply attribute, use this for
2070 // all the power supplies. Build map of object path to connection.
2071 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002072 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002073
Ed Tanous002d39b2022-05-31 08:59:27 -07002074 if (subtree[0].first.empty() || subtree[0].second.empty())
2075 {
2076 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2077 callback(inventoryItems);
2078 return;
2079 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002080
Ed Tanous002d39b2022-05-31 08:59:27 -07002081 const std::string& psAttributesPath = subtree[0].first;
2082 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002083
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 if (connection.empty())
2085 {
2086 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2087 callback(inventoryItems);
2088 return;
2089 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002090
Ed Tanous002d39b2022-05-31 08:59:27 -07002091 psAttributesConnections[psAttributesPath] = connection;
2092 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2093 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002094
Ed Tanous002d39b2022-05-31 08:59:27 -07002095 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2096 psAttributesConnections,
2097 std::move(callback));
2098 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2099 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002100 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2101 crow::connections::systemBus->async_method_call(
2102 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2103 "/xyz/openbmc_project/object_mapper",
2104 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2105 "/xyz/openbmc_project", 0, interfaces);
2106 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2107}
2108
2109/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002110 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002111 *
2112 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002113 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002114 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002115 * This data is later used to provide sensor property values in the JSON
2116 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002117 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002118 * Finds the inventory items asynchronously. Invokes callback when the
2119 * inventory items have been obtained.
2120 *
2121 * The callback must have the following signature:
2122 * @code
2123 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2124 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002125 *
2126 * @param sensorsAsyncResp Pointer to object holding response data.
2127 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002128 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002129 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002130 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002131template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002132static void
2133 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2134 const std::shared_ptr<std::set<std::string>> sensorNames,
2135 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002136{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002137 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2138 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002139 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002140 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002141 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2142 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002143 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002145 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2147 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2148 callback{std::move(callback)}]() {
2149 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002150
Ed Tanous002d39b2022-05-31 08:59:27 -07002151 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2152 callback{std::move(callback)}]() {
2153 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2154 // Find Power Supply Attributes and get the data
2155 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2156 std::move(callback));
2157 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002158 };
2159
Ed Tanous002d39b2022-05-31 08:59:27 -07002160 // Find led connections and get the data
2161 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2162 std::move(getInventoryLedsCb));
2163 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2164 };
2165
2166 // Get inventory item data from connections
2167 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002168 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002169 std::move(getInventoryItemsDataCb));
2170 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002171 };
2172
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 // Get connections that provide inventory item data
2174 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2175 std::move(getInventoryItemsConnectionsCb));
2176 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2177 };
2178
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002179 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002180 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002181 std::move(getInventoryItemAssociationsCb));
2182 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2183}
2184
2185/**
2186 * @brief Returns JSON PowerSupply object for the specified inventory item.
2187 *
2188 * Searches for a JSON PowerSupply object that matches the specified inventory
2189 * item. If one is not found, a new PowerSupply object is added to the JSON
2190 * array.
2191 *
2192 * Multiple sensors are often associated with one power supply inventory item.
2193 * As a result, multiple sensor values are stored in one JSON PowerSupply
2194 * object.
2195 *
2196 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2197 * @param inventoryItem Inventory item for the power supply.
2198 * @param chassisId Chassis that contains the power supply.
2199 * @return JSON PowerSupply object for the specified inventory item.
2200 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002201inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002202 const InventoryItem& inventoryItem,
2203 const std::string& chassisId)
2204{
2205 // Check if matching PowerSupply object already exists in JSON array
2206 for (nlohmann::json& powerSupply : powerSupplyArray)
2207 {
2208 if (powerSupply["MemberId"] == inventoryItem.name)
2209 {
2210 return powerSupply;
2211 }
2212 }
2213
2214 // Add new PowerSupply object to JSON array
2215 powerSupplyArray.push_back({});
2216 nlohmann::json& powerSupply = powerSupplyArray.back();
2217 powerSupply["@odata.id"] =
2218 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2219 powerSupply["MemberId"] = inventoryItem.name;
2220 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2221 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2222 powerSupply["Model"] = inventoryItem.model;
2223 powerSupply["PartNumber"] = inventoryItem.partNumber;
2224 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002225 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002226
Gunnar Mills42cbe532019-08-15 15:26:54 -05002227 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2228 {
2229 powerSupply["EfficiencyPercent"] =
2230 inventoryItem.powerSupplyEfficiencyPercent;
2231 }
2232
2233 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002234 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2235 powerSupply["Status"]["Health"] = health;
2236
2237 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002238}
2239
2240/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002241 * @brief Gets the values of the specified sensors.
2242 *
2243 * Stores the results as JSON in the SensorsAsyncResp.
2244 *
2245 * Gets the sensor values asynchronously. Stores the results later when the
2246 * information has been obtained.
2247 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002248 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002249 *
2250 * To minimize the number of DBus calls, the DBus method
2251 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2252 * values of all sensors provided by a connection (service).
2253 *
2254 * The connections set contains all the connections that provide sensor values.
2255 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 * The InventoryItem vector contains D-Bus inventory items associated with the
2257 * sensors. Inventory item data is needed for some Redfish sensor properties.
2258 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002259 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002260 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002261 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002262 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002263 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002264 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002265inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002266 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002267 const std::shared_ptr<std::set<std::string>>& sensorNames,
2268 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002269 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002270{
2271 BMCWEB_LOG_DEBUG << "getSensorData enter";
2272 // Get managed objects from all services exposing sensors
2273 for (const std::string& connection : connections)
2274 {
2275 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002276 auto getManagedObjectsCb =
2277 [sensorsAsyncResp, sensorNames,
2278 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002279 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002280 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2281 if (ec)
2282 {
2283 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002284 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002285 return;
2286 }
2287 // Go through all objects and update response with sensor data
2288 for (const auto& objDictEntry : resp)
2289 {
2290 const std::string& objPath =
2291 static_cast<const std::string&>(objDictEntry.first);
2292 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2293 << objPath;
2294
Shawn McCarneyde629b62019-03-08 10:42:51 -06002295 std::vector<std::string> split;
2296 // Reserve space for
2297 // /xyz/openbmc_project/sensors/<name>/<subname>
2298 split.reserve(6);
2299 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2300 if (split.size() < 6)
2301 {
2302 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2303 << objPath;
2304 continue;
2305 }
2306 // These indexes aren't intuitive, as boost::split puts an empty
2307 // string at the beginning
2308 const std::string& sensorType = split[4];
2309 const std::string& sensorName = split[5];
2310 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2311 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002312 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002313 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002314 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002315 continue;
2316 }
2317
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002318 // Find inventory item (if any) associated with sensor
2319 InventoryItem* inventoryItem =
2320 findInventoryItemForSensor(inventoryItems, objPath);
2321
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002322 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002323 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002324
2325 nlohmann::json* sensorJson = nullptr;
2326
Nan Zhou928fefb2022-03-28 08:45:00 -07002327 if (sensorSchema == sensors::node::sensors &&
2328 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002329 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002330 std::string sensorTypeEscaped(sensorType);
2331 sensorTypeEscaped.erase(
2332 std::remove(sensorTypeEscaped.begin(),
2333 sensorTypeEscaped.end(), '_'),
2334 sensorTypeEscaped.end());
2335 std::string sensorId(sensorTypeEscaped);
2336 sensorId += "_";
2337 sensorId += sensorName;
2338
zhanghch058d1b46d2021-04-01 11:18:24 +08002339 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002340 crow::utility::urlFromPieces(
2341 "redfish", "v1", "Chassis",
2342 sensorsAsyncResp->chassisId,
2343 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002344 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002345 }
2346 else
2347 {
Ed Tanous271584a2019-07-09 16:24:22 -07002348 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002349 if (sensorsAsyncResp->efficientExpand)
2350 {
2351 fieldName = "Members";
2352 }
2353 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002354 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002355 fieldName = "Temperatures";
2356 }
2357 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2358 sensorType == "fan_pwm")
2359 {
2360 fieldName = "Fans";
2361 }
2362 else if (sensorType == "voltage")
2363 {
2364 fieldName = "Voltages";
2365 }
2366 else if (sensorType == "power")
2367 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002368 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002369 {
2370 fieldName = "PowerControl";
2371 }
2372 else if ((inventoryItem != nullptr) &&
2373 (inventoryItem->isPowerSupply))
2374 {
2375 fieldName = "PowerSupplies";
2376 }
2377 else
2378 {
2379 // Other power sensors are in SensorCollection
2380 continue;
2381 }
2382 }
2383 else
2384 {
2385 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2386 << sensorType;
2387 continue;
2388 }
2389
2390 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002391 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002392 if (fieldName == "PowerControl")
2393 {
2394 if (tempArray.empty())
2395 {
2396 // Put multiple "sensors" into a single
2397 // PowerControl. Follows MemberId naming and
2398 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002399 nlohmann::json::object_t power;
2400 power["@odata.id"] =
2401 "/redfish/v1/Chassis/" +
2402 sensorsAsyncResp->chassisId + "/" +
2403 sensorsAsyncResp->chassisSubNode + "#/" +
2404 fieldName + "/0";
2405 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002406 }
2407 sensorJson = &(tempArray.back());
2408 }
2409 else if (fieldName == "PowerSupplies")
2410 {
2411 if (inventoryItem != nullptr)
2412 {
2413 sensorJson =
2414 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002415 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002416 }
2417 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002418 else if (fieldName == "Members")
2419 {
Ed Tanous677bb752022-09-15 10:52:19 -07002420 std::string sensorTypeEscaped(sensorType);
2421 sensorTypeEscaped.erase(
2422 std::remove(sensorTypeEscaped.begin(),
2423 sensorTypeEscaped.end(), '_'),
2424 sensorTypeEscaped.end());
2425 std::string sensorId(sensorTypeEscaped);
2426 sensorId += "_";
2427 sensorId += sensorName;
2428
Ed Tanous14766872022-03-15 10:44:42 -07002429 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002430 member["@odata.id"] = crow::utility::urlFromPieces(
2431 "redfish", "v1", "Chassis",
2432 sensorsAsyncResp->chassisId,
2433 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002434 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002435 sensorJson = &(tempArray.back());
2436 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002437 else
2438 {
Ed Tanous14766872022-03-15 10:44:42 -07002439 nlohmann::json::object_t member;
2440 member["@odata.id"] = "/redfish/v1/Chassis/" +
2441 sensorsAsyncResp->chassisId +
2442 "/" +
2443 sensorsAsyncResp->chassisSubNode +
2444 "#/" + fieldName + "/";
2445 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002446 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002447 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002448 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002449
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002450 if (sensorJson != nullptr)
2451 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002452 objectInterfacesToJson(sensorName, sensorType,
2453 sensorsAsyncResp->chassisSubNode,
2454 objDictEntry.second, *sensorJson,
2455 inventoryItem);
2456
2457 std::string path = "/xyz/openbmc_project/sensors/";
2458 path += sensorType;
2459 path += "/";
2460 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002461 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002462 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002463 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002464 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002465 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002466 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002467 if (sensorsAsyncResp->chassisSubNode ==
2468 sensors::node::sensors &&
2469 sensorsAsyncResp->efficientExpand)
2470 {
2471 sensorsAsyncResp->asyncResp->res
2472 .jsonValue["Members@odata.count"] =
2473 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2474 .size();
2475 }
2476 else if (sensorsAsyncResp->chassisSubNode ==
2477 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002478 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002479 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002480 }
James Feist8bd25cc2019-03-15 15:14:00 -07002481 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002482 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2483 };
2484
Shawn McCarneyde629b62019-03-08 10:42:51 -06002485 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002486 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002487 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002488 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002489 BMCWEB_LOG_DEBUG << "getSensorData exit";
2490}
2491
Nan Zhoufe04d492022-06-22 17:10:41 +00002492inline void
2493 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2494 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002495{
Nan Zhoufe04d492022-06-22 17:10:41 +00002496 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2497 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002498 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002499 auto getInventoryItemsCb =
2500 [sensorsAsyncResp, sensorNames,
2501 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2502 inventoryItems) {
2503 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2504 // Get sensor data and store results in JSON
2505 getSensorData(sensorsAsyncResp, sensorNames, connections,
2506 inventoryItems);
2507 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002508 };
2509
Ed Tanousd0090732022-10-04 17:22:56 -07002510 // Get inventory items associated with sensors
2511 getInventoryItems(sensorsAsyncResp, sensorNames,
2512 std::move(getInventoryItemsCb));
2513
Ed Tanous002d39b2022-05-31 08:59:27 -07002514 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2515 };
2516
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002517 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002518 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002519}
2520
Shawn McCarneyde629b62019-03-08 10:42:51 -06002521/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002522 * @brief Entry point for retrieving sensors data related to requested
2523 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002524 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002525 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002526inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002527 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002528{
2529 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002530 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002531 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002532 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002533 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2534 processSensorList(sensorsAsyncResp, sensorNames);
2535 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2536 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002537 // SensorCollection doesn't contain the Redundancy property
2538 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2539 {
2540 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2541 nlohmann::json::array();
2542 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002543 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002544 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2545 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2546 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002547 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002548}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002549
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302550/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002551 * @brief Find the requested sensorName in the list of all sensors supplied by
2552 * the chassis node
2553 *
2554 * @param sensorName The sensor name supplied in the PATCH request
2555 * @param sensorsList The list of sensors managed by the chassis node
2556 * @param sensorsModified The list of sensors that were found as a result of
2557 * repeated calls to this function
2558 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002559inline bool
2560 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002561 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002562 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002563{
Nan Zhoufe04d492022-06-22 17:10:41 +00002564 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002565 {
George Liu28aa8de2021-02-01 15:13:30 +08002566 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002567 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002568 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002569 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002570 continue;
2571 }
2572 if (thisSensorName == sensorName)
2573 {
2574 sensorsModified.emplace(chassisSensor);
2575 return true;
2576 }
2577 }
2578 return false;
2579}
2580
Ed Tanousc71d6122022-11-29 14:10:32 -08002581inline std::pair<std::string, std::string>
2582 splitSensorNameAndType(std::string_view sensorId)
2583{
2584 size_t index = sensorId.find('_');
2585 if (index == std::string::npos)
2586 {
2587 return std::make_pair<std::string, std::string>("", "");
2588 }
2589 std::string sensorType{sensorId.substr(0, index)};
2590 std::string sensorName{sensorId.substr(index + 1)};
2591 // fan_pwm and fan_tach need special handling
2592 if (sensorType == "fantach" || sensorType == "fanpwm")
2593 {
2594 sensorType.insert(3, 1, '_');
2595 }
2596 return std::make_pair(sensorType, sensorName);
2597}
2598
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002599/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302600 * @brief Entry point for overriding sensor values of given sensor
2601 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002602 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002603 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002604 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302605 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002606inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002607 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002608 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002609 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302610{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002611 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002612 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302613
Ed Tanous543f4402022-01-06 13:12:53 -08002614 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302615 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302616 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002617 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302618 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302619 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302620 if (collectionItems.first == "Temperatures")
2621 {
2622 propertyValueName = "ReadingCelsius";
2623 }
2624 else if (collectionItems.first == "Fans")
2625 {
2626 propertyValueName = "Reading";
2627 }
2628 else
2629 {
2630 propertyValueName = "ReadingVolts";
2631 }
2632 for (auto& item : collectionItems.second)
2633 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002634 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2635 "MemberId", memberId, propertyValueName,
2636 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302637 {
2638 return;
2639 }
2640 overrideMap.emplace(memberId,
2641 std::make_pair(value, collectionItems.first));
2642 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302643 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002644
Ed Tanous002d39b2022-05-31 08:59:27 -07002645 auto getChassisSensorListCb =
2646 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002647 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002648 // Match sensor names in the PATCH request to those managed by the
2649 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002650 const std::shared_ptr<std::set<std::string>> sensorNames =
2651 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302652 for (const auto& item : overrideMap)
2653 {
2654 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002655 std::pair<std::string, std::string> sensorNameType =
2656 splitSensorNameAndType(sensor);
2657 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2658 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302659 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302660 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002661 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302662 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302663 return;
2664 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302665 }
2666 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002667 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002668 [sensorAsyncResp,
2669 overrideMap](const std::set<std::string>& /*connections*/,
2670 const std::set<std::pair<std::string, std::string>>&
2671 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002672 if (objectsWithConnection.size() != overrideMap.size())
2673 {
2674 BMCWEB_LOG_INFO
2675 << "Unable to find all objects with proper connection "
2676 << objectsWithConnection.size() << " requested "
2677 << overrideMap.size() << "\n";
2678 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2679 sensorAsyncResp->chassisSubNode ==
2680 sensors::node::thermal
2681 ? "Temperatures"
2682 : "Voltages",
2683 "Count");
2684 return;
2685 }
2686 for (const auto& item : objectsWithConnection)
2687 {
2688 sdbusplus::message::object_path path(item.first);
2689 std::string sensorName = path.filename();
2690 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302691 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002692 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302693 return;
2694 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302695
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002696 const auto& iterator = overrideMap.find(sensorName);
2697 if (iterator == overrideMap.end())
2698 {
2699 BMCWEB_LOG_INFO << "Unable to find sensor object"
2700 << item.first << "\n";
2701 messages::internalError(sensorAsyncResp->asyncResp->res);
2702 return;
2703 }
2704 crow::connections::systemBus->async_method_call(
2705 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002706 if (ec)
2707 {
2708 if (ec.value() ==
2709 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002710 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002711 BMCWEB_LOG_WARNING
2712 << "Manufacturing mode is not Enabled...can't "
2713 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002714
Ed Tanous002d39b2022-05-31 08:59:27 -07002715 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002716 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002717 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002718 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002719 BMCWEB_LOG_DEBUG
2720 << "setOverrideValueStatus DBUS error: " << ec;
2721 messages::internalError(
2722 sensorAsyncResp->asyncResp->res);
2723 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002724 },
2725 item.second, item.first, "org.freedesktop.DBus.Properties",
2726 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002727 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002728 }
2729 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302730 // Get object with connection for the given sensor name
2731 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2732 std::move(getObjectsWithConnectionCb));
2733 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302734 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002735 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2736 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2737 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302738}
2739
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002740/**
2741 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2742 * path of the sensor.
2743 *
2744 * Function builds valid Redfish response for sensor query of given chassis and
2745 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2746 * it to caller in a callback.
2747 *
2748 * @param chassis Chassis for which retrieval should be performed
2749 * @param node Node (group) of sensors. See sensors::node for supported values
2750 * @param mapComplete Callback to be called with retrieval result
2751 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002752inline void retrieveUriToDbusMap(const std::string& chassis,
2753 const std::string& node,
2754 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002755{
Ed Tanous02da7c52022-02-27 00:09:02 -08002756 decltype(sensors::paths)::const_iterator pathIt =
2757 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2758 [&node](auto&& val) { return val.first == node; });
2759 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002760 {
2761 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2762 mapComplete(boost::beast::http::status::bad_request, {});
2763 return;
2764 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002765
Nan Zhou72374eb2022-01-27 17:06:51 -08002766 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002767 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2768 const boost::beast::http::status status,
2769 const std::map<std::string, std::string>& uriToDbus) {
2770 mapCompleteCb(status, uriToDbus);
2771 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002772
2773 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002774 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002775 getChassisData(resp);
2776}
2777
Nan Zhoubacb2162022-04-06 11:28:32 -07002778namespace sensors
2779{
Nan Zhou928fefb2022-03-28 08:45:00 -07002780
Nan Zhoubacb2162022-04-06 11:28:32 -07002781inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002782 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2783 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002784 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002785{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002786 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002787
Ed Tanousc1d019a2022-08-06 09:36:06 -07002788 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2789 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002790 {
2791 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2792
2793 sdbusplus::message::object_path path(sensor);
2794 std::string sensorName = path.filename();
2795 if (sensorName.empty())
2796 {
2797 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002798 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002799 return;
2800 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002801 std::string type = path.parent_path().filename();
2802 // fan_tach has an underscore in it, so remove it to "normalize" the
2803 // type in the URI
2804 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2805
Ed Tanous14766872022-03-15 10:44:42 -07002806 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002807 std::string id = type;
2808 id += "_";
2809 id += sensorName;
2810 member["@odata.id"] = crow::utility::urlFromPieces(
2811 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2812
Ed Tanous14766872022-03-15 10:44:42 -07002813 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002814 }
2815
Ed Tanousc1d019a2022-08-06 09:36:06 -07002816 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002817 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2818}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002819
Nan Zhoude167a62022-06-01 04:47:45 +00002820inline void
2821 handleSensorCollectionGet(App& app, const crow::Request& req,
2822 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2823 const std::string& chassisId)
2824{
2825 query_param::QueryCapabilities capabilities = {
2826 .canDelegateExpandLevel = 1,
2827 };
2828 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002829 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002830 delegatedQuery, capabilities))
2831 {
2832 return;
2833 }
2834
2835 if (delegatedQuery.expandType != query_param::ExpandType::None)
2836 {
2837 // we perform efficient expand.
2838 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2839 aResp, chassisId, sensors::dbus::sensorPaths,
2840 sensors::node::sensors,
2841 /*efficientExpand=*/true);
2842 getChassisData(asyncResp);
2843
2844 BMCWEB_LOG_DEBUG
2845 << "SensorCollection doGet exit via efficient expand handler";
2846 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002847 }
Nan Zhoude167a62022-06-01 04:47:45 +00002848
Nan Zhoude167a62022-06-01 04:47:45 +00002849 // We get all sensors as hyperlinkes in the chassis (this
2850 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002851 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2852 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2853 sensors::node::sensors));
2854}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002855
Ed Tanousc1d019a2022-08-06 09:36:06 -07002856inline void
2857 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2858 const std::string& sensorPath,
2859 const ::dbus::utility::MapperGetObject& mapperResponse)
2860{
2861 if (mapperResponse.size() != 1)
2862 {
2863 messages::internalError(asyncResp->res);
2864 return;
2865 }
2866 const auto& valueIface = *mapperResponse.begin();
2867 const std::string& connectionName = valueIface.first;
2868 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2869 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002870
2871 sdbusplus::asio::getAllProperties(
2872 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002873 [asyncResp,
2874 sensorPath](const boost::system::error_code ec,
2875 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2876 if (ec)
2877 {
2878 messages::internalError(asyncResp->res);
2879 return;
2880 }
2881 sdbusplus::message::object_path path(sensorPath);
2882 std::string name = path.filename();
2883 path = path.parent_path();
2884 std::string type = path.filename();
2885 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2886 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002887 });
Nan Zhoude167a62022-06-01 04:47:45 +00002888}
2889
Nan Zhoue6bd8462022-06-01 04:35:35 +00002890inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002891 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002892 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002893 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002894{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002895 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002896 {
2897 return;
2898 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002899 std::pair<std::string, std::string> nameType =
2900 splitSensorNameAndType(sensorId);
2901 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002902 {
2903 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2904 return;
2905 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002906
Ed Tanous677bb752022-09-15 10:52:19 -07002907 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2908 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002909
Nan Zhoue6bd8462022-06-01 04:35:35 +00002910 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002911
2912 const std::array<const char*, 1> interfaces = {
2913 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002914 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2915 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002916 // Get a list of all of the sensors that implement Sensor.Value
2917 // and get the path and service name associated with the sensor
2918 crow::connections::systemBus->async_method_call(
Ed Tanousc71d6122022-11-29 14:10:32 -08002919 [asyncResp,
2920 sensorPath](const boost::system::error_code ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002921 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002922 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2923 if (ec)
2924 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002925 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002926 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2927 << "Dbus error " << ec;
2928 return;
2929 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002930 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002931 BMCWEB_LOG_DEBUG << "respHandler1 exit";
2932 },
2933 "xyz.openbmc_project.ObjectMapper",
2934 "/xyz/openbmc_project/object_mapper",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002935 "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
2936 interfaces);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002937}
2938
Nan Zhoubacb2162022-04-06 11:28:32 -07002939} // namespace sensors
2940
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002941inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002942{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002943 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002944 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002945 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002946 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002947}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002948
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002949inline void requestRoutesSensor(App& app)
2950{
2951 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002952 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002953 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002954 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002955}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002956
Ed Tanous1abe55e2018-09-05 08:30:59 -07002957} // namespace redfish