blob: e53a7d280908d6bb60474800c1620d4a1d648951 [file] [log] [blame]
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous0ec8b832022-03-14 14:56:47 -070018#include "generated/enums/sensor.hpp"
19
John Edward Broadbent7e860f12021-04-08 15:57:16 -070020#include <app.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070021#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070022#include <boost/algorithm/string/find.hpp>
23#include <boost/algorithm/string/predicate.hpp>
Ed Tanousc71d6122022-11-29 14:10:32 -080024#include <boost/algorithm/string/replace.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010025#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010026#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070027#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080028#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070029#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070030#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070031#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020032#include <sdbusplus/unpack_properties.hpp>
33#include <utils/dbus_utils.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053034#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070035#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050036
37#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000038#include <iterator>
39#include <map>
40#include <set>
Ed Tanousb5a76932020-09-29 16:16:58 -070041#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080042#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010043
Ed Tanous1abe55e2018-09-05 08:30:59 -070044namespace redfish
45{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010046
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020047namespace sensors
48{
49namespace node
50{
51static constexpr std::string_view power = "Power";
52static constexpr std::string_view sensors = "Sensors";
53static constexpr std::string_view thermal = "Thermal";
54} // namespace node
55
Ed Tanous02da7c52022-02-27 00:09:02 -080056// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020057namespace dbus
58{
Ed Tanous4ee8e212022-05-28 09:42:51 -070059static auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080060 "/xyz/openbmc_project/sensors/voltage",
61 "/xyz/openbmc_project/sensors/power"
62});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000063
Ed Tanous4ee8e212022-05-28 09:42:51 -070064static auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080065 "/xyz/openbmc_project/sensors/power",
66 "/xyz/openbmc_project/sensors/current",
67 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070068 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080069#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080070 "/xyz/openbmc_project/sensors/voltage",
71 "/xyz/openbmc_project/sensors/fan_tach",
72 "/xyz/openbmc_project/sensors/temperature",
73 "/xyz/openbmc_project/sensors/fan_pwm",
74 "/xyz/openbmc_project/sensors/altitude",
75 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080076#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080077 "/xyz/openbmc_project/sensors/utilization"
78});
79
Ed Tanous4ee8e212022-05-28 09:42:51 -070080static auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080081 "/xyz/openbmc_project/sensors/fan_tach",
82 "/xyz/openbmc_project/sensors/temperature",
83 "/xyz/openbmc_project/sensors/fan_pwm"
84});
85
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000086} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080087// clang-format on
88
89using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
90static constexpr std::array<sensorPair, 3> paths = {
91 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
92 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
93 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000094
Ed Tanous0ec8b832022-03-14 14:56:47 -070095inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000096{
97 if (sensorType == "voltage")
98 {
Ed Tanous0ec8b832022-03-14 14:56:47 -070099 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000100 }
101 if (sensorType == "power")
102 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700103 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000104 }
105 if (sensorType == "current")
106 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700107 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000108 }
109 if (sensorType == "fan_tach")
110 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700111 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000112 }
113 if (sensorType == "temperature")
114 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700115 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000116 }
117 if (sensorType == "fan_pwm" || sensorType == "utilization")
118 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700119 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000120 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600121 if (sensorType == "humidity")
122 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700123 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600124 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000125 if (sensorType == "altitude")
126 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700127 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000128 }
129 if (sensorType == "airflow")
130 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700131 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000132 }
133 if (sensorType == "energy")
134 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700135 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000136 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700137 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000138}
139
Ed Tanous1d7c0052022-08-09 12:32:26 -0700140inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000141{
142 if (sensorType == "voltage")
143 {
144 return "V";
145 }
146 if (sensorType == "power")
147 {
148 return "W";
149 }
150 if (sensorType == "current")
151 {
152 return "A";
153 }
154 if (sensorType == "fan_tach")
155 {
156 return "RPM";
157 }
158 if (sensorType == "temperature")
159 {
160 return "Cel";
161 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600162 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
163 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000164 {
165 return "%";
166 }
167 if (sensorType == "altitude")
168 {
169 return "m";
170 }
171 if (sensorType == "airflow")
172 {
173 return "cft_i/min";
174 }
175 if (sensorType == "energy")
176 {
177 return "J";
178 }
179 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200180}
181} // namespace sensors
182
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100183/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200184 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100185 * Gathers data needed for response processing after async calls are done
186 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700187class SensorsAsyncResp
188{
189 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200190 using DataCompleteCb = std::function<void(
191 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000192 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200193
194 struct SensorData
195 {
196 const std::string name;
197 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200198 const std::string dbusPath;
199 };
200
Ed Tanous8a592812022-06-04 09:06:59 -0700201 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800202 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800203 std::span<std::string_view> typesIn,
204 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700205 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700206 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
207 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500208 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200209
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200210 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700211 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800212 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800213 std::span<std::string_view> typesIn,
214 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200215 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700216 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700217 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
218 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200219 dataComplete{std::move(creationComplete)}
220 {}
221
Nan Zhou928fefb2022-03-28 08:45:00 -0700222 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700223 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700224 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800225 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700226 const std::string_view& subNode, bool efficientExpandIn) :
227 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700228 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700229 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700230 {}
231
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 ~SensorsAsyncResp()
233 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800234 if (asyncResp->res.result() ==
235 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 {
237 // Reset the json object to clear out any data that made it in
238 // before the error happened todo(ed) handle error condition with
239 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800240 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200242
243 if (dataComplete && metadata)
244 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000245 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800246 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200247 {
248 for (auto& sensor : *metadata)
249 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700250 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200251 }
252 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800253 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200254 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700255 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100256
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800257 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
258 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
259 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
260 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
261
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200262 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700263 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200264 {
265 if (metadata)
266 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700267 metadata->emplace_back(SensorData{
268 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200269 }
270 }
271
272 void updateUri(const std::string& name, const std::string& uri)
273 {
274 if (metadata)
275 {
276 for (auto& sensor : *metadata)
277 {
278 if (sensor.name == name)
279 {
280 sensor.uri = uri;
281 }
282 }
283 }
284 }
285
zhanghch058d1b46d2021-04-01 11:18:24 +0800286 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200287 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800288 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200289 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700290 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200291
292 private:
293 std::optional<std::vector<SensorData>> metadata;
294 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100295};
296
297/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500298 * Possible states for physical inventory leds
299 */
300enum class LedState
301{
302 OFF,
303 ON,
304 BLINK,
305 UNKNOWN
306};
307
308/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500309 * D-Bus inventory item associated with one or more sensors.
310 */
311class InventoryItem
312{
313 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700314 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500315 {
316 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800317 sdbusplus::message::object_path path(objectPath);
318 name = path.filename();
319 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500320 {
George Liu28aa8de2021-02-01 15:13:30 +0800321 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500322 }
323 }
324
325 std::string objectPath;
326 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800327 bool isPresent = true;
328 bool isFunctional = true;
329 bool isPowerSupply = false;
330 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500331 std::string manufacturer;
332 std::string model;
333 std::string partNumber;
334 std::string serialNumber;
335 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500336 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800337 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500338};
339
340/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530341 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200342 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100343 * @param sensorNames Sensors retrieved from chassis
344 * @param callback Callback for processing gathered connections
345 */
346template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530347void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000348 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000349 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530350 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700351{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530352 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700353 const std::string path = "/xyz/openbmc_project/sensors";
354 const std::array<std::string, 1> interfaces = {
355 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100356
Ed Tanous1abe55e2018-09-05 08:30:59 -0700357 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700358 auto respHandler =
359 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
360 sensorNames](const boost::system::error_code ec,
361 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530362 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700363 if (ec)
364 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800365 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530366 BMCWEB_LOG_ERROR
367 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700368 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100369 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100370
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
372
373 // Make unique list of connections only for requested sensor types and
374 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000375 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530376 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700378 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
379 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380 {
381 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
382 }
383
384 for (const std::pair<
385 std::string,
386 std::vector<std::pair<std::string, std::vector<std::string>>>>&
387 object : subtree)
388 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700389 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700391 for (const std::pair<std::string, std::vector<std::string>>&
392 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700393 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700394 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
395 connections.insert(objData.first);
396 objectsWithConnection.insert(
397 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700398 }
399 }
400 }
401 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530402 callback(std::move(connections), std::move(objectsWithConnection));
403 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700405 // Make call to ObjectMapper to find all sensors objects
406 crow::connections::systemBus->async_method_call(
407 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
408 "/xyz/openbmc_project/object_mapper",
409 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530410 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
411}
412
413/**
414 * @brief Create connections necessary for sensors
415 * @param SensorsAsyncResp Pointer to object holding response data
416 * @param sensorNames Sensors retrieved from chassis
417 * @param callback Callback for processing gathered connections
418 */
419template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000420void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
421 const std::shared_ptr<std::set<std::string>> sensorNames,
422 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530423{
424 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000425 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530426 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700427 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000428 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530429 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100430}
431
432/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700433 * @brief Shrinks the list of sensors for processing
434 * @param SensorsAysncResp The class holding the Redfish response
435 * @param allSensors A list of all the sensors associated to the
436 * chassis element (i.e. baseboard, front panel, etc...)
437 * @param activeSensors A list that is a reduction of the incoming
438 * allSensors list. Eliminate Thermal sensors when a Power request is
439 * made, and eliminate Power sensors when a Thermal request is made.
440 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000441inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700442 crow::Response& res, std::string_view chassisSubNode,
443 std::span<std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700444 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000445 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700446{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447 if ((allSensors == nullptr) || (activeSensors == nullptr))
448 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700449 messages::resourceNotFound(res, chassisSubNode,
450 chassisSubNode == sensors::node::thermal
451 ? "Temperatures"
452 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700453
454 return;
455 }
456 if (allSensors->empty())
457 {
458 // Nothing to do, the activeSensors object is also empty
459 return;
460 }
461
Ed Tanous7f1cc262022-08-09 13:33:57 -0700462 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700463 {
464 for (const std::string& sensor : *allSensors)
465 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700466 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700467 {
468 activeSensors->emplace(sensor);
469 }
470 }
471 }
472}
473
Ed Tanous7f1cc262022-08-09 13:33:57 -0700474/*
475 *Populates the top level collection for a given subnode. Populates
476 *SensorCollection, Power, or Thermal schemas.
477 *
478 * */
479inline void populateChassisNode(nlohmann::json& jsonValue,
480 std::string_view chassisSubNode)
481{
482 if (chassisSubNode == sensors::node::power)
483 {
484 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
485 }
486 else if (chassisSubNode == sensors::node::thermal)
487 {
488 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
489 jsonValue["Fans"] = nlohmann::json::array();
490 jsonValue["Temperatures"] = nlohmann::json::array();
491 }
492 else if (chassisSubNode == sensors::node::sensors)
493 {
494 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
495 jsonValue["Description"] = "Collection of Sensors for this Chassis";
496 jsonValue["Members"] = nlohmann::json::array();
497 jsonValue["Members@odata.count"] = 0;
498 }
499
500 if (chassisSubNode != sensors::node::sensors)
501 {
502 jsonValue["Id"] = chassisSubNode;
503 }
504 jsonValue["Name"] = chassisSubNode;
505}
506
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700507/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100508 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200509 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100510 * @param callback Callback for next step in gathered sensor processing
511 */
512template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700513void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
514 std::string_view chassisId, std::string_view chassisSubNode,
515 std::span<std::string_view> sensorTypes, Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700516{
517 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500518 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700519 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500520 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700521 auto respHandler =
Ed Tanous7f1cc262022-08-09 13:33:57 -0700522 [callback{std::forward<Callback>(callback)}, asyncResp,
523 chassisIdStr{std::string(chassisId)},
524 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
Ed Tanous002d39b2022-05-31 08:59:27 -0700525 const boost::system::error_code ec,
526 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700527 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
528 if (ec)
529 {
530 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700531 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 return;
533 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700534 const std::string* chassisPath = nullptr;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700535 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700536 {
George Liu28aa8de2021-02-01 15:13:30 +0800537 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700538 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800539 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542 continue;
543 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700544 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700546 chassisPath = &chassis;
547 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700549 }
550 if (chassisPath == nullptr)
551 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700552 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700553 return;
554 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700555 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556
Ed Tanous7f1cc262022-08-09 13:33:57 -0700557 asyncResp->res.jsonValue["@odata.id"] =
558 "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500559
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500560 // Get the list of all sensors for this Chassis element
561 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700562 sdbusplus::asio::getProperty<std::vector<std::string>>(
563 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
564 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700565 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800566 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700567 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700568 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700569 if (e)
570 {
571 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700572 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700573 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700575 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700576 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000577 const std::shared_ptr<std::set<std::string>> culledSensorList =
578 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700579 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
580 &nodeSensorList, culledSensorList);
581 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700583 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100584 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100585
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700586 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700587 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700588 respHandler, "xyz.openbmc_project.ObjectMapper",
589 "/xyz/openbmc_project/object_mapper",
590 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700591 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700592 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100593}
594
595/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500596 * @brief Returns the Redfish State value for the specified inventory item.
597 * @param inventoryItem D-Bus inventory item associated with a sensor.
598 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700599 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000600inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500601{
602 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
603 {
604 return "Absent";
605 }
James Feist34dd1792019-05-17 14:10:54 -0700606
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500607 return "Enabled";
608}
609
610/**
611 * @brief Returns the Redfish Health value for the specified sensor.
612 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700613 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500614 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
615 * be nullptr if no associated inventory item was found.
616 * @return Health value for sensor.
617 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700618inline std::string getHealth(nlohmann::json& sensorJson,
619 const dbus::utility::DBusPropertiesMap& valuesDict,
620 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700621{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500622 // Get current health value (if any) in the sensor JSON object. Some JSON
623 // objects contain multiple sensors (such as PowerSupplies). We want to set
624 // the overall health to be the most severe of any of the sensors.
625 std::string currentHealth;
626 auto statusIt = sensorJson.find("Status");
627 if (statusIt != sensorJson.end())
628 {
629 auto healthIt = statusIt->find("Health");
630 if (healthIt != statusIt->end())
631 {
632 std::string* health = healthIt->get_ptr<std::string*>();
633 if (health != nullptr)
634 {
635 currentHealth = *health;
636 }
637 }
638 }
639
640 // If current health in JSON object is already Critical, return that. This
641 // should override the sensor health, which might be less severe.
642 if (currentHealth == "Critical")
643 {
644 return "Critical";
645 }
646
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200647 const bool* criticalAlarmHigh = nullptr;
648 const bool* criticalAlarmLow = nullptr;
649 const bool* warningAlarmHigh = nullptr;
650 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800651
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200652 const bool success = sdbusplus::unpackPropertiesNoThrow(
653 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
654 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
655 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
656 warningAlarmLow);
657
658 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700659 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200660 // Check if sensor has critical threshold alarm
661 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
662 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700663 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200664 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700665 }
666 }
667
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500668 // Check if associated inventory item is not functional
669 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
670 {
671 return "Critical";
672 }
673
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200674 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500675 // should override the sensor status, which might be less severe.
676 if (currentHealth == "Warning")
677 {
678 return "Warning";
679 }
680
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200681 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700682 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200683 // Check if sensor has warning threshold alarm
684 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
685 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700686 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200687 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700688 }
689 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500690
James Feist34dd1792019-05-17 14:10:54 -0700691 return "OK";
692}
693
Ed Tanous23a21a12020-07-25 04:45:05 +0000694inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500695 const InventoryItem* inventoryItem)
696{
697 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
698 {
699 switch (inventoryItem->ledState)
700 {
701 case LedState::OFF:
702 sensorJson["IndicatorLED"] = "Off";
703 break;
704 case LedState::ON:
705 sensorJson["IndicatorLED"] = "Lit";
706 break;
707 case LedState::BLINK:
708 sensorJson["IndicatorLED"] = "Blinking";
709 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000710 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500711 break;
712 }
713 }
714}
715
James Feist34dd1792019-05-17 14:10:54 -0700716/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100717 * @brief Builds a json sensor representation of a sensor.
718 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500719 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100720 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700721 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
722 * @param propertiesDict A dictionary of the properties to build the sensor
723 * from.
724 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500725 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
726 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100727 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700728inline void objectPropertiesToJson(
729 std::string_view sensorName, std::string_view sensorType,
730 std::string_view chassisSubNode,
731 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000732 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700734 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500735 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800736 std::string subNodeEscaped(sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700737 subNodeEscaped.erase(
738 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
739 subNodeEscaped.end());
740
741 // For sensors in SensorCollection we set Id instead of MemberId,
742 // including power sensors.
743 subNodeEscaped += '_';
744 subNodeEscaped += sensorName;
745 sensorJson["Id"] = std::move(subNodeEscaped);
746
Ed Tanous1d7c0052022-08-09 12:32:26 -0700747 std::string sensorNameEs(sensorName);
748 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
749 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500750 }
751 else if (sensorType != "power")
752 {
753 // Set MemberId and Name for non-power sensors. For PowerSupplies and
754 // PowerControl, those properties have more general values because
755 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000756 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700757 std::string sensorNameEs(sensorName);
758 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
759 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500760 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700761
Ed Tanous81ce6092020-12-17 16:54:55 +0000762 sensorJson["Status"]["State"] = getState(inventoryItem);
763 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700764 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765
766 // Parameter to set to override the type we get from dbus, and force it to
767 // int, regardless of what is available. This is used for schemas like fan,
768 // that require integers, not floats.
769 bool forceToInt = false;
770
Anthony Wilson3929aca2019-07-19 15:42:33 -0500771 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700772 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500773 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000774 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000775
Ed Tanous0ec8b832022-03-14 14:56:47 -0700776 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
777 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500778 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000779 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
780 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500781 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000782 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500783 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000784 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500785 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000786
Ed Tanous1d7c0052022-08-09 12:32:26 -0700787 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000788 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200789 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000790 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
791 << sensorType;
792 }
793 else
794 {
795 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200796 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500797 }
798 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700799 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500800 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000801 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700802 // TODO(ed) Documentation says that path should be type fan_tach,
803 // implementation seems to implement fan
804 }
805 else if (sensorType == "fan" || sensorType == "fan_tach")
806 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500807 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000808 sensorJson["ReadingUnits"] = "RPM";
809 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
810 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700811 forceToInt = true;
812 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700813 else if (sensorType == "fan_pwm")
814 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500815 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000816 sensorJson["ReadingUnits"] = "Percent";
817 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
818 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700819 forceToInt = true;
820 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700821 else if (sensorType == "voltage")
822 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500823 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000824 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700826 else if (sensorType == "power")
827 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700828 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000829 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000830 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500831 // Put multiple "sensors" into a single PowerControl, so have
832 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000833 sensorJson["MemberId"] = "0";
834 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500835 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000836 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700837 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700838 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500839 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700840 }
841 else
842 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500843 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700844 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700845 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700846 else
847 {
848 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
849 return;
850 }
851 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500852 std::vector<
853 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
854 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700855 properties.reserve(7);
856
857 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600858
Ed Tanous1d7c0052022-08-09 12:32:26 -0700859 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500860 {
861 properties.emplace_back(
862 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
863 "/Thresholds/UpperCaution/Reading"_json_pointer);
864 properties.emplace_back(
865 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
866 "/Thresholds/LowerCaution/Reading"_json_pointer);
867 properties.emplace_back(
868 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
869 "/Thresholds/UpperCritical/Reading"_json_pointer);
870 properties.emplace_back(
871 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
872 "/Thresholds/LowerCritical/Reading"_json_pointer);
873 }
874 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600875 {
876 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500877 "WarningHigh",
878 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600879 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500880 "WarningLow",
881 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600882 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500883 "CriticalHigh",
884 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600885 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500886 "CriticalLow",
887 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600888 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700889
Ed Tanous2474adf2018-09-05 16:31:16 -0700890 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
891
Ed Tanous1d7c0052022-08-09 12:32:26 -0700892 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500893 {
894 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500895 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500896 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500897 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800898 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
899 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500900 }
901 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700902 {
903 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500904 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500906 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500908 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 {
910 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500911 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700912 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500913 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700914 }
915
Anthony Wilson3929aca2019-07-19 15:42:33 -0500916 for (const std::tuple<const char*, const char*,
917 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700919 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700920 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700921 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700922 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800923 continue;
924 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700925
926 // The property we want to set may be nested json, so use
927 // a json_pointer for easy indexing into the json structure.
928 const nlohmann::json::json_pointer& key = std::get<2>(p);
929
Ed Tanous1d7c0052022-08-09 12:32:26 -0700930 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700931 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800932 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700933 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700934 continue;
935 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700936 if (forceToInt)
937 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700938 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700939 }
940 else
941 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700942 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943 }
944 }
945 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100946}
947
Ed Tanous1d7c0052022-08-09 12:32:26 -0700948/**
949 * @brief Builds a json sensor representation of a sensor.
950 * @param sensorName The name of the sensor to be built
951 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
952 * build
953 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
954 * @param interfacesDict A dictionary of the interfaces and properties of said
955 * interfaces to be built from
956 * @param sensorJson The json object to fill
957 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
958 * be nullptr if no associated inventory item was found.
959 */
960inline void objectInterfacesToJson(
961 const std::string& sensorName, const std::string& sensorType,
962 const std::string& chassisSubNode,
963 const dbus::utility::DBusInteracesMap& interfacesDict,
964 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
965{
966
967 for (const auto& [interface, valuesDict] : interfacesDict)
968 {
969 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
970 valuesDict, sensorJson, inventoryItem);
971 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700972 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700973}
974
Ed Tanousb5a76932020-09-29 16:16:58 -0700975inline void populateFanRedundancy(
976 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700977{
978 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -0800979 [sensorsAsyncResp](
980 const boost::system::error_code ec,
981 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700982 if (ec)
983 {
984 return; // don't have to have this interface
985 }
986 for (const std::pair<
987 std::string,
988 std::vector<std::pair<std::string, std::vector<std::string>>>>&
989 pathPair : resp)
990 {
991 const std::string& path = pathPair.first;
992 const std::vector<std::pair<std::string, std::vector<std::string>>>&
993 objDict = pathPair.second;
994 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700995 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700996 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700997 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700998
999 const std::string& owner = objDict.begin()->first;
1000 sdbusplus::asio::getProperty<std::vector<std::string>>(
1001 *crow::connections::systemBus,
1002 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1003 "xyz.openbmc_project.Association", "endpoints",
1004 [path, owner,
1005 sensorsAsyncResp](const boost::system::error_code e,
1006 const std::vector<std::string>& endpoints) {
1007 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001008 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001009 return; // if they don't have an association we
1010 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001011 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001012 auto found =
1013 std::find_if(endpoints.begin(), endpoints.end(),
1014 [sensorsAsyncResp](const std::string& entry) {
1015 return entry.find(sensorsAsyncResp->chassisId) !=
1016 std::string::npos;
1017 });
James Feist8bd25cc2019-03-15 15:14:00 -07001018
Ed Tanous002d39b2022-05-31 08:59:27 -07001019 if (found == endpoints.end())
1020 {
1021 return;
1022 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001023 sdbusplus::asio::getAllProperties(
1024 *crow::connections::systemBus, owner, path,
1025 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001026 [path, sensorsAsyncResp](
1027 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001028 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001029 if (err)
1030 {
1031 return; // don't have to have this
1032 // interface
1033 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001034
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001035 const uint8_t* allowedFailures = nullptr;
1036 const std::vector<std::string>* collection = nullptr;
1037 const std::string* status = nullptr;
1038
1039 const bool success = sdbusplus::unpackPropertiesNoThrow(
1040 dbus_utils::UnpackErrorPrinter(), ret,
1041 "AllowedFailures", allowedFailures, "Collection",
1042 collection, "Status", status);
1043
1044 if (!success)
1045 {
1046 messages::internalError(
1047 sensorsAsyncResp->asyncResp->res);
1048 return;
1049 }
1050
1051 if (allowedFailures == nullptr || collection == nullptr ||
1052 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001053 {
1054 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1055 messages::internalError(
1056 sensorsAsyncResp->asyncResp->res);
1057 return;
1058 }
1059
Ed Tanous002d39b2022-05-31 08:59:27 -07001060 sdbusplus::message::object_path objectPath(path);
1061 std::string name = objectPath.filename();
1062 if (name.empty())
1063 {
1064 // this should be impossible
1065 messages::internalError(
1066 sensorsAsyncResp->asyncResp->res);
1067 return;
1068 }
1069 std::replace(name.begin(), name.end(), '_', ' ');
1070
1071 std::string health;
1072
Ed Tanous11ba3972022-07-11 09:50:41 -07001073 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001074 {
1075 health = "OK";
1076 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001077 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001078 {
1079 health = "Warning";
1080 }
1081 else
1082 {
1083 health = "Critical";
1084 }
1085 nlohmann::json::array_t redfishCollection;
1086 const auto& fanRedfish =
1087 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1088 for (const std::string& item : *collection)
1089 {
Ed Tanous8a592812022-06-04 09:06:59 -07001090 sdbusplus::message::object_path itemPath(item);
1091 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001092 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001093 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001094 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001095 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001096 /*
1097 todo(ed): merge patch that fixes the names
1098 std::replace(itemName.begin(),
1099 itemName.end(), '_', ' ');*/
1100 auto schemaItem =
1101 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1102 [itemName](const nlohmann::json& fan) {
1103 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001104 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001105 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001106 {
Ed Tanous8a592812022-06-04 09:06:59 -07001107 nlohmann::json::object_t collectionId;
1108 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001109 (*schemaItem)["@odata.id"];
1110 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001111 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001112 }
1113 else
1114 {
1115 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1116 messages::internalError(
1117 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001118 return;
1119 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 }
James Feist8bd25cc2019-03-15 15:14:00 -07001121
Ed Tanous002d39b2022-05-31 08:59:27 -07001122 size_t minNumNeeded =
1123 collection->empty()
1124 ? 0
1125 : collection->size() - *allowedFailures;
1126 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1127 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001128
Ed Tanous002d39b2022-05-31 08:59:27 -07001129 nlohmann::json::object_t redundancy;
1130 redundancy["@odata.id"] =
1131 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1132 "/" + sensorsAsyncResp->chassisSubNode +
1133 "#/Redundancy/" + std::to_string(jResp.size());
1134 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1135 redundancy["MinNumNeeded"] = minNumNeeded;
1136 redundancy["MemberId"] = name;
1137 redundancy["Mode"] = "N+m";
1138 redundancy["Name"] = name;
1139 redundancy["RedundancySet"] = redfishCollection;
1140 redundancy["Status"]["Health"] = health;
1141 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001142
Ed Tanous002d39b2022-05-31 08:59:27 -07001143 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001144 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001145 });
1146 }
James Feist8bd25cc2019-03-15 15:14:00 -07001147 },
1148 "xyz.openbmc_project.ObjectMapper",
1149 "/xyz/openbmc_project/object_mapper",
1150 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1151 "/xyz/openbmc_project/control", 2,
1152 std::array<const char*, 1>{
1153 "xyz.openbmc_project.Control.FanRedundancy"});
1154}
1155
Ed Tanousb5a76932020-09-29 16:16:58 -07001156inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001157 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001158{
zhanghch058d1b46d2021-04-01 11:18:24 +08001159 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001160 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001161 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001162 {
1163 sensorHeaders = {"Voltages", "PowerSupplies"};
1164 }
1165 for (const std::string& sensorGroup : sensorHeaders)
1166 {
1167 nlohmann::json::iterator entry = response.find(sensorGroup);
1168 if (entry != response.end())
1169 {
1170 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001171 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 return c1["Name"] < c2["Name"];
1173 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001174
1175 // add the index counts to the end of each entry
1176 size_t count = 0;
1177 for (nlohmann::json& sensorJson : *entry)
1178 {
1179 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1180 if (odata == sensorJson.end())
1181 {
1182 continue;
1183 }
1184 std::string* value = odata->get_ptr<std::string*>();
1185 if (value != nullptr)
1186 {
1187 *value += std::to_string(count);
1188 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001189 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001190 }
1191 }
1192 }
1193 }
1194}
1195
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001196/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001197 * @brief Finds the inventory item with the specified object path.
1198 * @param inventoryItems D-Bus inventory items associated with sensors.
1199 * @param invItemObjPath D-Bus object path of inventory item.
1200 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001201 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001202inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001203 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001204 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001205{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001206 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001207 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001208 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001209 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001210 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001211 }
1212 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001213 return nullptr;
1214}
1215
1216/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001217 * @brief Finds the inventory item associated with the specified sensor.
1218 * @param inventoryItems D-Bus inventory items associated with sensors.
1219 * @param sensorObjPath D-Bus object path of sensor.
1220 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001221 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001222inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001223 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001224 const std::string& sensorObjPath)
1225{
1226 for (InventoryItem& inventoryItem : *inventoryItems)
1227 {
1228 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1229 {
1230 return &inventoryItem;
1231 }
1232 }
1233 return nullptr;
1234}
1235
1236/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001237 * @brief Finds the inventory item associated with the specified led path.
1238 * @param inventoryItems D-Bus inventory items associated with sensors.
1239 * @param ledObjPath D-Bus object path of led.
1240 * @return Inventory item within vector, or nullptr if no match found.
1241 */
1242inline InventoryItem*
1243 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1244 const std::string& ledObjPath)
1245{
1246 for (InventoryItem& inventoryItem : inventoryItems)
1247 {
1248 if (inventoryItem.ledObjectPath == ledObjPath)
1249 {
1250 return &inventoryItem;
1251 }
1252 }
1253 return nullptr;
1254}
1255
1256/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001257 * @brief Adds inventory item and associated sensor to specified vector.
1258 *
1259 * Adds a new InventoryItem to the vector if necessary. Searches for an
1260 * existing InventoryItem with the specified object path. If not found, one is
1261 * added to the vector.
1262 *
1263 * Next, the specified sensor is added to the set of sensors associated with the
1264 * InventoryItem.
1265 *
1266 * @param inventoryItems D-Bus inventory items associated with sensors.
1267 * @param invItemObjPath D-Bus object path of inventory item.
1268 * @param sensorObjPath D-Bus object path of sensor
1269 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001270inline void addInventoryItem(
1271 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1272 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001273{
1274 // Look for inventory item in vector
1275 InventoryItem* inventoryItem =
1276 findInventoryItem(inventoryItems, invItemObjPath);
1277
1278 // If inventory item doesn't exist in vector, add it
1279 if (inventoryItem == nullptr)
1280 {
1281 inventoryItems->emplace_back(invItemObjPath);
1282 inventoryItem = &(inventoryItems->back());
1283 }
1284
1285 // Add sensor to set of sensors associated with inventory item
1286 inventoryItem->sensors.emplace(sensorObjPath);
1287}
1288
1289/**
1290 * @brief Stores D-Bus data in the specified inventory item.
1291 *
1292 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1293 * specified InventoryItem.
1294 *
1295 * This data is later used to provide sensor property values in the JSON
1296 * response.
1297 *
1298 * @param inventoryItem Inventory item where data will be stored.
1299 * @param interfacesDict Map containing D-Bus interfaces and their properties
1300 * for the specified inventory item.
1301 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001302inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001303 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001304 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001305{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001306 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001307
Ed Tanous9eb808c2022-01-25 10:19:23 -08001308 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001309 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001310 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001311 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001312 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001313 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001314 if (name == "Present")
1315 {
1316 const bool* value = std::get_if<bool>(&dbusValue);
1317 if (value != nullptr)
1318 {
1319 inventoryItem.isPresent = *value;
1320 }
1321 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001322 }
1323 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001324 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001325
Ed Tanous711ac7a2021-12-20 09:34:41 -08001326 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001327 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001328 inventoryItem.isPowerSupply = true;
1329 }
1330
1331 // Get properties from Inventory.Decorator.Asset interface
1332 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1333 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001334 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001335 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001336 if (name == "Manufacturer")
1337 {
1338 const std::string* value =
1339 std::get_if<std::string>(&dbusValue);
1340 if (value != nullptr)
1341 {
1342 inventoryItem.manufacturer = *value;
1343 }
1344 }
1345 if (name == "Model")
1346 {
1347 const std::string* value =
1348 std::get_if<std::string>(&dbusValue);
1349 if (value != nullptr)
1350 {
1351 inventoryItem.model = *value;
1352 }
1353 }
1354 if (name == "SerialNumber")
1355 {
1356 const std::string* value =
1357 std::get_if<std::string>(&dbusValue);
1358 if (value != nullptr)
1359 {
1360 inventoryItem.serialNumber = *value;
1361 }
1362 }
1363 if (name == "PartNumber")
1364 {
1365 const std::string* value =
1366 std::get_if<std::string>(&dbusValue);
1367 if (value != nullptr)
1368 {
1369 inventoryItem.partNumber = *value;
1370 }
1371 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001372 }
1373 }
1374
Ed Tanous711ac7a2021-12-20 09:34:41 -08001375 if (interface ==
1376 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001377 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001378 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001379 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001380 if (name == "Functional")
1381 {
1382 const bool* value = std::get_if<bool>(&dbusValue);
1383 if (value != nullptr)
1384 {
1385 inventoryItem.isFunctional = *value;
1386 }
1387 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001388 }
1389 }
1390 }
1391}
1392
1393/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001394 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001395 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001396 * Uses the specified connections (services) to obtain D-Bus data for inventory
1397 * items associated with sensors. Stores the resulting data in the
1398 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001399 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001400 * This data is later used to provide sensor property values in the JSON
1401 * response.
1402 *
1403 * Finds the inventory item data asynchronously. Invokes callback when data has
1404 * been obtained.
1405 *
1406 * The callback must have the following signature:
1407 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001408 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001409 * @endcode
1410 *
1411 * This function is called recursively, obtaining data asynchronously from one
1412 * connection in each call. This ensures the callback is not invoked until the
1413 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001414 *
1415 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001416 * @param inventoryItems D-Bus inventory items associated with sensors.
1417 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001418 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001419 * @param callback Callback to invoke when inventory data has been obtained.
1420 * @param invConnectionsIndex Current index in invConnections. Only specified
1421 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001422 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001423template <typename Callback>
1424static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001425 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001427 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1428 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001429{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001430 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001431
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001432 // If no more connections left, call callback
1433 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001434 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001435 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001436 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1437 return;
1438 }
1439
1440 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001441 auto it = invConnections->begin();
1442 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001443 if (it != invConnections->end())
1444 {
1445 const std::string& invConnection = *it;
1446
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001447 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001448 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1449 callback{std::forward<Callback>(callback)},
1450 invConnectionsIndex](
1451 const boost::system::error_code ec,
1452 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001453 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001454 if (ec)
1455 {
1456 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001457 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001458 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001459 return;
1460 }
1461
1462 // Loop through returned object paths
1463 for (const auto& objDictEntry : resp)
1464 {
1465 const std::string& objPath =
1466 static_cast<const std::string&>(objDictEntry.first);
1467
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001468 // If this object path is one of the specified inventory items
1469 InventoryItem* inventoryItem =
1470 findInventoryItem(inventoryItems, objPath);
1471 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001472 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001473 // Store inventory data in InventoryItem
1474 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001475 }
1476 }
1477
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001478 // Recurse to get inventory item data from next connection
1479 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001480 invConnections, std::move(callback),
1481 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001482
1483 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001484 };
1485
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001486 // Get all object paths and their interfaces for current connection
1487 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001488 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001489 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001490 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1491 }
1492
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001493 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001494}
1495
1496/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001497 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001499 * Gets the D-Bus connections (services) that provide data for the inventory
1500 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001501 *
1502 * Finds the connections asynchronously. Invokes callback when information has
1503 * been obtained.
1504 *
1505 * The callback must have the following signature:
1506 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001507 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001508 * @endcode
1509 *
1510 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001511 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001512 * @param callback Callback to invoke when connections have been obtained.
1513 */
1514template <typename Callback>
1515static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001516 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1517 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001518 Callback&& callback)
1519{
1520 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1521
1522 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001523 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001524 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001525 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1526 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001527 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1528
1529 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001530 auto respHandler =
1531 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1532 inventoryItems](
1533 const boost::system::error_code ec,
1534 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001535 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1536 if (ec)
1537 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001538 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001539 BMCWEB_LOG_ERROR
1540 << "getInventoryItemsConnections respHandler DBus error " << ec;
1541 return;
1542 }
1543
1544 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001545 std::shared_ptr<std::set<std::string>> invConnections =
1546 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001547
1548 // Loop through objects from GetSubTree
1549 for (const std::pair<
1550 std::string,
1551 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1552 object : subtree)
1553 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001554 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001556 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001557 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001558 // Store all connections to inventory item
1559 for (const std::pair<std::string, std::vector<std::string>>&
1560 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001561 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001562 const std::string& invConnection = objData.first;
1563 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001564 }
1565 }
1566 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001567
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001568 callback(invConnections);
1569 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1570 };
1571
1572 // Make call to ObjectMapper to find all inventory items
1573 crow::connections::systemBus->async_method_call(
1574 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1575 "/xyz/openbmc_project/object_mapper",
1576 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1577 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1578}
1579
1580/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001581 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001582 *
1583 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001584 * inventory items. Then finds the associations from those inventory items to
1585 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001586 *
1587 * Finds the inventory items asynchronously. Invokes callback when information
1588 * has been obtained.
1589 *
1590 * The callback must have the following signature:
1591 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001592 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001593 * @endcode
1594 *
1595 * @param sensorsAsyncResp Pointer to object holding response data.
1596 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001597 * implements ObjectManager.
1598 * @param callback Callback to invoke when inventory items have been obtained.
1599 */
1600template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001601static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001602 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001603 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001604 Callback&& callback)
1605{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001607
1608 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001609 auto respHandler =
1610 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1611 sensorNames](const boost::system::error_code ec,
1612 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001613 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001614 if (ec)
1615 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001616 BMCWEB_LOG_ERROR
1617 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001618 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001619 return;
1620 }
1621
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001622 // Create vector to hold list of inventory items
1623 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1624 std::make_shared<std::vector<InventoryItem>>();
1625
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001627 std::string sensorAssocPath;
1628 sensorAssocPath.reserve(128); // avoid memory allocations
1629 for (const auto& objDictEntry : resp)
1630 {
1631 const std::string& objPath =
1632 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633
1634 // If path is inventory association for one of the specified sensors
1635 for (const std::string& sensorName : *sensorNames)
1636 {
1637 sensorAssocPath = sensorName;
1638 sensorAssocPath += "/inventory";
1639 if (objPath == sensorAssocPath)
1640 {
1641 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001642 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001643 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001644 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001645 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001646 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001647 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001648 if (valueName == "endpoints")
1649 {
1650 const std::vector<std::string>* endpoints =
1651 std::get_if<std::vector<std::string>>(
1652 &value);
1653 if ((endpoints != nullptr) &&
1654 !endpoints->empty())
1655 {
1656 // Add inventory item to vector
1657 const std::string& invItemPath =
1658 endpoints->front();
1659 addInventoryItem(inventoryItems,
1660 invItemPath,
1661 sensorName);
1662 }
1663 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001664 }
1665 }
1666 }
1667 break;
1668 }
1669 }
1670 }
1671
Anthony Wilsond5005492019-07-31 16:34:17 -05001672 // Now loop through the returned object paths again, this time to
1673 // find the leds associated with the inventory items we just found
1674 std::string inventoryAssocPath;
1675 inventoryAssocPath.reserve(128); // avoid memory allocations
1676 for (const auto& objDictEntry : resp)
1677 {
1678 const std::string& objPath =
1679 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001680
1681 for (InventoryItem& inventoryItem : *inventoryItems)
1682 {
1683 inventoryAssocPath = inventoryItem.objectPath;
1684 inventoryAssocPath += "/leds";
1685 if (objPath == inventoryAssocPath)
1686 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001687 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001688 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001689 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001690 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001691 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001692 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001693 if (valueName == "endpoints")
1694 {
1695 const std::vector<std::string>* endpoints =
1696 std::get_if<std::vector<std::string>>(
1697 &value);
1698 if ((endpoints != nullptr) &&
1699 !endpoints->empty())
1700 {
1701 // Add inventory item to vector
1702 // Store LED path in inventory item
1703 const std::string& ledPath =
1704 endpoints->front();
1705 inventoryItem.ledObjectPath = ledPath;
1706 }
1707 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001708 }
1709 }
1710 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001711
Anthony Wilsond5005492019-07-31 16:34:17 -05001712 break;
1713 }
1714 }
1715 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001716 callback(inventoryItems);
1717 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001718 };
1719
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001720 // Call GetManagedObjects on the ObjectMapper to get all associations
1721 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001722 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001723 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1724
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001725 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001726}
1727
1728/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001729 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1730 *
1731 * Uses the specified connections (services) to obtain D-Bus data for inventory
1732 * item leds associated with sensors. Stores the resulting data in the
1733 * inventoryItems vector.
1734 *
1735 * This data is later used to provide sensor property values in the JSON
1736 * response.
1737 *
1738 * Finds the inventory item led data asynchronously. Invokes callback when data
1739 * has been obtained.
1740 *
1741 * The callback must have the following signature:
1742 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001743 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001744 * @endcode
1745 *
1746 * This function is called recursively, obtaining data asynchronously from one
1747 * connection in each call. This ensures the callback is not invoked until the
1748 * last asynchronous function has completed.
1749 *
1750 * @param sensorsAsyncResp Pointer to object holding response data.
1751 * @param inventoryItems D-Bus inventory items associated with sensors.
1752 * @param ledConnections Connections that provide data for the inventory leds.
1753 * @param callback Callback to invoke when inventory data has been obtained.
1754 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1755 * in recursive calls to this function.
1756 */
1757template <typename Callback>
1758void getInventoryLedData(
1759 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1760 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001761 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001762 Callback&& callback, size_t ledConnectionsIndex = 0)
1763{
1764 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1765
1766 // If no more connections left, call callback
1767 if (ledConnectionsIndex >= ledConnections->size())
1768 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001769 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001770 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1771 return;
1772 }
1773
1774 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001775 auto it = ledConnections->begin();
1776 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001777 if (it != ledConnections->end())
1778 {
1779 const std::string& ledPath = (*it).first;
1780 const std::string& ledConnection = (*it).second;
1781 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001782 auto respHandler =
1783 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001784 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001785 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001786 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1787 if (ec)
1788 {
1789 BMCWEB_LOG_ERROR
1790 << "getInventoryLedData respHandler DBus error " << ec;
1791 messages::internalError(sensorsAsyncResp->asyncResp->res);
1792 return;
1793 }
1794
1795 BMCWEB_LOG_DEBUG << "Led state: " << state;
1796 // Find inventory item with this LED object path
1797 InventoryItem* inventoryItem =
1798 findInventoryItemForLed(*inventoryItems, ledPath);
1799 if (inventoryItem != nullptr)
1800 {
1801 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001802 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001803 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001804 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001805 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001806 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001807 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001808 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001809 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001810 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001811 {
1812 inventoryItem->ledState = LedState::OFF;
1813 }
1814 else
1815 {
1816 inventoryItem->ledState = LedState::UNKNOWN;
1817 }
1818 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001819
Ed Tanous002d39b2022-05-31 08:59:27 -07001820 // Recurse to get LED data from next connection
1821 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1822 ledConnections, std::move(callback),
1823 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001824
Ed Tanous002d39b2022-05-31 08:59:27 -07001825 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1826 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001827
1828 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001829 sdbusplus::asio::getProperty<std::string>(
1830 *crow::connections::systemBus, ledConnection, ledPath,
1831 "xyz.openbmc_project.Led.Physical", "State",
1832 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001833 }
1834
1835 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1836}
1837
1838/**
1839 * @brief Gets LED data for LEDs associated with given inventory items.
1840 *
1841 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1842 * associated with the specified inventory items. Then gets the LED data from
1843 * each connection and stores it in the inventory item.
1844 *
1845 * This data is later used to provide sensor property values in the JSON
1846 * response.
1847 *
1848 * Finds the LED data asynchronously. Invokes callback when information has
1849 * been obtained.
1850 *
1851 * The callback must have the following signature:
1852 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001853 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001854 * @endcode
1855 *
1856 * @param sensorsAsyncResp Pointer to object holding response data.
1857 * @param inventoryItems D-Bus inventory items associated with sensors.
1858 * @param callback Callback to invoke when inventory items have been obtained.
1859 */
1860template <typename Callback>
1861void getInventoryLeds(
1862 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1863 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1864 Callback&& callback)
1865{
1866 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1867
1868 const std::string path = "/xyz/openbmc_project";
1869 const std::array<std::string, 1> interfaces = {
1870 "xyz.openbmc_project.Led.Physical"};
1871
1872 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001873 auto respHandler =
1874 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1875 inventoryItems](
1876 const boost::system::error_code ec,
1877 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001878 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1879 if (ec)
1880 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001881 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001882 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1883 << ec;
1884 return;
1885 }
1886
1887 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001888 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1889 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001890
1891 // Loop through objects from GetSubTree
1892 for (const std::pair<
1893 std::string,
1894 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1895 object : subtree)
1896 {
1897 // Check if object path is LED for one of the specified inventory
1898 // items
1899 const std::string& ledPath = object.first;
1900 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1901 {
1902 // Add mapping from ledPath to connection
1903 const std::string& connection = object.second.begin()->first;
1904 (*ledConnections)[ledPath] = connection;
1905 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1906 << connection;
1907 }
1908 }
1909
1910 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1911 std::move(callback));
1912 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1913 };
1914 // Make call to ObjectMapper to find all inventory items
1915 crow::connections::systemBus->async_method_call(
1916 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1917 "/xyz/openbmc_project/object_mapper",
1918 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1919 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1920}
1921
1922/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001923 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1924 *
1925 * Uses the specified connections (services) (currently assumes just one) to
1926 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1927 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1928 *
1929 * This data is later used to provide sensor property values in the JSON
1930 * response.
1931 *
1932 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1933 * when data has been obtained.
1934 *
1935 * The callback must have the following signature:
1936 * @code
1937 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1938 * @endcode
1939 *
1940 * @param sensorsAsyncResp Pointer to object holding response data.
1941 * @param inventoryItems D-Bus inventory items associated with sensors.
1942 * @param psAttributesConnections Connections that provide data for the Power
1943 * Supply Attributes
1944 * @param callback Callback to invoke when data has been obtained.
1945 */
1946template <typename Callback>
1947void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001948 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001949 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001950 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001951 Callback&& callback)
1952{
1953 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1954
1955 if (psAttributesConnections.empty())
1956 {
1957 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1958 callback(inventoryItems);
1959 return;
1960 }
1961
1962 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001963 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001964
1965 const std::string& psAttributesPath = (*it).first;
1966 const std::string& psAttributesConnection = (*it).second;
1967
1968 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001969 auto respHandler =
1970 [sensorsAsyncResp, inventoryItems,
1971 callback{std::forward<Callback>(callback)}](
1972 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001973 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1974 if (ec)
1975 {
1976 BMCWEB_LOG_ERROR
1977 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001978 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001979 return;
1980 }
1981
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001982 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1983 // Store value in Power Supply Inventory Items
1984 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001985 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001986 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001987 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001988 inventoryItem.powerSupplyEfficiencyPercent =
1989 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001990 }
1991 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001992
1993 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1994 callback(inventoryItems);
1995 };
1996
1997 // Get the DeratingFactor property for the PowerSupplyAttributes
1998 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001999 sdbusplus::asio::getProperty<uint32_t>(
2000 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2001 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2002 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002003
2004 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2005}
2006
2007/**
2008 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2009 *
2010 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2011 * data. Then gets the Power Supply Attributes data from the connection
2012 * (currently just assumes 1 connection) and stores the data in the inventory
2013 * item.
2014 *
2015 * This data is later used to provide sensor property values in the JSON
2016 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2017 *
2018 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2019 * when information has been obtained.
2020 *
2021 * The callback must have the following signature:
2022 * @code
2023 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2024 * @endcode
2025 *
2026 * @param sensorsAsyncResp Pointer to object holding response data.
2027 * @param inventoryItems D-Bus inventory items associated with sensors.
2028 * @param callback Callback to invoke when data has been obtained.
2029 */
2030template <typename Callback>
2031void getPowerSupplyAttributes(
2032 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2033 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2034 Callback&& callback)
2035{
2036 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2037
2038 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002039 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002040 {
2041 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2042 callback(inventoryItems);
2043 return;
2044 }
2045
2046 const std::array<std::string, 1> interfaces = {
2047 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2048
2049 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002050 auto respHandler =
2051 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2052 inventoryItems](
2053 const boost::system::error_code ec,
2054 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002055 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2056 if (ec)
2057 {
2058 messages::internalError(sensorsAsyncResp->asyncResp->res);
2059 BMCWEB_LOG_ERROR
2060 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2061 return;
2062 }
2063 if (subtree.empty())
2064 {
2065 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2066 callback(inventoryItems);
2067 return;
2068 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002069
Ed Tanous002d39b2022-05-31 08:59:27 -07002070 // Currently we only support 1 power supply attribute, use this for
2071 // all the power supplies. Build map of object path to connection.
2072 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002073 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002074
Ed Tanous002d39b2022-05-31 08:59:27 -07002075 if (subtree[0].first.empty() || subtree[0].second.empty())
2076 {
2077 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2078 callback(inventoryItems);
2079 return;
2080 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002081
Ed Tanous002d39b2022-05-31 08:59:27 -07002082 const std::string& psAttributesPath = subtree[0].first;
2083 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002084
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 if (connection.empty())
2086 {
2087 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2088 callback(inventoryItems);
2089 return;
2090 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002091
Ed Tanous002d39b2022-05-31 08:59:27 -07002092 psAttributesConnections[psAttributesPath] = connection;
2093 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2094 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002095
Ed Tanous002d39b2022-05-31 08:59:27 -07002096 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2097 psAttributesConnections,
2098 std::move(callback));
2099 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2100 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002101 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2102 crow::connections::systemBus->async_method_call(
2103 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2104 "/xyz/openbmc_project/object_mapper",
2105 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2106 "/xyz/openbmc_project", 0, interfaces);
2107 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2108}
2109
2110/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002111 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002112 *
2113 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002114 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002115 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002116 * This data is later used to provide sensor property values in the JSON
2117 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002118 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002119 * Finds the inventory items asynchronously. Invokes callback when the
2120 * inventory items have been obtained.
2121 *
2122 * The callback must have the following signature:
2123 * @code
2124 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2125 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002126 *
2127 * @param sensorsAsyncResp Pointer to object holding response data.
2128 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002129 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002130 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002131 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002132template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002133static void
2134 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2135 const std::shared_ptr<std::set<std::string>> sensorNames,
2136 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002137{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002138 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2139 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002140 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002141 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002142 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2143 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002144 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002145 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002146 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2148 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2149 callback{std::move(callback)}]() {
2150 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002151
Ed Tanous002d39b2022-05-31 08:59:27 -07002152 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2153 callback{std::move(callback)}]() {
2154 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2155 // Find Power Supply Attributes and get the data
2156 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2157 std::move(callback));
2158 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002159 };
2160
Ed Tanous002d39b2022-05-31 08:59:27 -07002161 // Find led connections and get the data
2162 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2163 std::move(getInventoryLedsCb));
2164 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2165 };
2166
2167 // Get inventory item data from connections
2168 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002169 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002170 std::move(getInventoryItemsDataCb));
2171 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002172 };
2173
Ed Tanous002d39b2022-05-31 08:59:27 -07002174 // Get connections that provide inventory item data
2175 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2176 std::move(getInventoryItemsConnectionsCb));
2177 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2178 };
2179
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002180 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002181 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002182 std::move(getInventoryItemAssociationsCb));
2183 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2184}
2185
2186/**
2187 * @brief Returns JSON PowerSupply object for the specified inventory item.
2188 *
2189 * Searches for a JSON PowerSupply object that matches the specified inventory
2190 * item. If one is not found, a new PowerSupply object is added to the JSON
2191 * array.
2192 *
2193 * Multiple sensors are often associated with one power supply inventory item.
2194 * As a result, multiple sensor values are stored in one JSON PowerSupply
2195 * object.
2196 *
2197 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2198 * @param inventoryItem Inventory item for the power supply.
2199 * @param chassisId Chassis that contains the power supply.
2200 * @return JSON PowerSupply object for the specified inventory item.
2201 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002202inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002203 const InventoryItem& inventoryItem,
2204 const std::string& chassisId)
2205{
2206 // Check if matching PowerSupply object already exists in JSON array
2207 for (nlohmann::json& powerSupply : powerSupplyArray)
2208 {
2209 if (powerSupply["MemberId"] == inventoryItem.name)
2210 {
2211 return powerSupply;
2212 }
2213 }
2214
2215 // Add new PowerSupply object to JSON array
2216 powerSupplyArray.push_back({});
2217 nlohmann::json& powerSupply = powerSupplyArray.back();
2218 powerSupply["@odata.id"] =
2219 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2220 powerSupply["MemberId"] = inventoryItem.name;
2221 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2222 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2223 powerSupply["Model"] = inventoryItem.model;
2224 powerSupply["PartNumber"] = inventoryItem.partNumber;
2225 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002226 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002227
Gunnar Mills42cbe532019-08-15 15:26:54 -05002228 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2229 {
2230 powerSupply["EfficiencyPercent"] =
2231 inventoryItem.powerSupplyEfficiencyPercent;
2232 }
2233
2234 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002235 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2236 powerSupply["Status"]["Health"] = health;
2237
2238 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002239}
2240
2241/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002242 * @brief Gets the values of the specified sensors.
2243 *
2244 * Stores the results as JSON in the SensorsAsyncResp.
2245 *
2246 * Gets the sensor values asynchronously. Stores the results later when the
2247 * information has been obtained.
2248 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002249 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002250 *
2251 * To minimize the number of DBus calls, the DBus method
2252 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2253 * values of all sensors provided by a connection (service).
2254 *
2255 * The connections set contains all the connections that provide sensor values.
2256 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002257 * The InventoryItem vector contains D-Bus inventory items associated with the
2258 * sensors. Inventory item data is needed for some Redfish sensor properties.
2259 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002260 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002261 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002262 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002263 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002264 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002265 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002266inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002267 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002268 const std::shared_ptr<std::set<std::string>>& sensorNames,
2269 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002270 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002271{
2272 BMCWEB_LOG_DEBUG << "getSensorData enter";
2273 // Get managed objects from all services exposing sensors
2274 for (const std::string& connection : connections)
2275 {
2276 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002277 auto getManagedObjectsCb =
2278 [sensorsAsyncResp, sensorNames,
2279 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002280 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002281 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2282 if (ec)
2283 {
2284 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002285 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002286 return;
2287 }
2288 // Go through all objects and update response with sensor data
2289 for (const auto& objDictEntry : resp)
2290 {
2291 const std::string& objPath =
2292 static_cast<const std::string&>(objDictEntry.first);
2293 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2294 << objPath;
2295
Shawn McCarneyde629b62019-03-08 10:42:51 -06002296 std::vector<std::string> split;
2297 // Reserve space for
2298 // /xyz/openbmc_project/sensors/<name>/<subname>
2299 split.reserve(6);
2300 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2301 if (split.size() < 6)
2302 {
2303 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2304 << objPath;
2305 continue;
2306 }
2307 // These indexes aren't intuitive, as boost::split puts an empty
2308 // string at the beginning
2309 const std::string& sensorType = split[4];
2310 const std::string& sensorName = split[5];
2311 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2312 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002313 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002314 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002315 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002316 continue;
2317 }
2318
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002319 // Find inventory item (if any) associated with sensor
2320 InventoryItem* inventoryItem =
2321 findInventoryItemForSensor(inventoryItems, objPath);
2322
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002323 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002324 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002325
2326 nlohmann::json* sensorJson = nullptr;
2327
Nan Zhou928fefb2022-03-28 08:45:00 -07002328 if (sensorSchema == sensors::node::sensors &&
2329 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002330 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002331 std::string sensorTypeEscaped(sensorType);
2332 sensorTypeEscaped.erase(
2333 std::remove(sensorTypeEscaped.begin(),
2334 sensorTypeEscaped.end(), '_'),
2335 sensorTypeEscaped.end());
2336 std::string sensorId(sensorTypeEscaped);
2337 sensorId += "_";
2338 sensorId += sensorName;
2339
zhanghch058d1b46d2021-04-01 11:18:24 +08002340 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002341 crow::utility::urlFromPieces(
2342 "redfish", "v1", "Chassis",
2343 sensorsAsyncResp->chassisId,
2344 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002345 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002346 }
2347 else
2348 {
Ed Tanous271584a2019-07-09 16:24:22 -07002349 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002350 if (sensorsAsyncResp->efficientExpand)
2351 {
2352 fieldName = "Members";
2353 }
2354 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002355 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002356 fieldName = "Temperatures";
2357 }
2358 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2359 sensorType == "fan_pwm")
2360 {
2361 fieldName = "Fans";
2362 }
2363 else if (sensorType == "voltage")
2364 {
2365 fieldName = "Voltages";
2366 }
2367 else if (sensorType == "power")
2368 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002369 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002370 {
2371 fieldName = "PowerControl";
2372 }
2373 else if ((inventoryItem != nullptr) &&
2374 (inventoryItem->isPowerSupply))
2375 {
2376 fieldName = "PowerSupplies";
2377 }
2378 else
2379 {
2380 // Other power sensors are in SensorCollection
2381 continue;
2382 }
2383 }
2384 else
2385 {
2386 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2387 << sensorType;
2388 continue;
2389 }
2390
2391 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002392 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002393 if (fieldName == "PowerControl")
2394 {
2395 if (tempArray.empty())
2396 {
2397 // Put multiple "sensors" into a single
2398 // PowerControl. Follows MemberId naming and
2399 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002400 nlohmann::json::object_t power;
2401 power["@odata.id"] =
2402 "/redfish/v1/Chassis/" +
2403 sensorsAsyncResp->chassisId + "/" +
2404 sensorsAsyncResp->chassisSubNode + "#/" +
2405 fieldName + "/0";
2406 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002407 }
2408 sensorJson = &(tempArray.back());
2409 }
2410 else if (fieldName == "PowerSupplies")
2411 {
2412 if (inventoryItem != nullptr)
2413 {
2414 sensorJson =
2415 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002416 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002417 }
2418 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002419 else if (fieldName == "Members")
2420 {
Ed Tanous677bb752022-09-15 10:52:19 -07002421 std::string sensorTypeEscaped(sensorType);
2422 sensorTypeEscaped.erase(
2423 std::remove(sensorTypeEscaped.begin(),
2424 sensorTypeEscaped.end(), '_'),
2425 sensorTypeEscaped.end());
2426 std::string sensorId(sensorTypeEscaped);
2427 sensorId += "_";
2428 sensorId += sensorName;
2429
Ed Tanous14766872022-03-15 10:44:42 -07002430 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002431 member["@odata.id"] = crow::utility::urlFromPieces(
2432 "redfish", "v1", "Chassis",
2433 sensorsAsyncResp->chassisId,
2434 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002435 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002436 sensorJson = &(tempArray.back());
2437 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002438 else
2439 {
Ed Tanous14766872022-03-15 10:44:42 -07002440 nlohmann::json::object_t member;
2441 member["@odata.id"] = "/redfish/v1/Chassis/" +
2442 sensorsAsyncResp->chassisId +
2443 "/" +
2444 sensorsAsyncResp->chassisSubNode +
2445 "#/" + fieldName + "/";
2446 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002447 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002448 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002449 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002450
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002451 if (sensorJson != nullptr)
2452 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002453 objectInterfacesToJson(sensorName, sensorType,
2454 sensorsAsyncResp->chassisSubNode,
2455 objDictEntry.second, *sensorJson,
2456 inventoryItem);
2457
2458 std::string path = "/xyz/openbmc_project/sensors/";
2459 path += sensorType;
2460 path += "/";
2461 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002462 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002463 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002464 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002465 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002466 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002467 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002468 if (sensorsAsyncResp->chassisSubNode ==
2469 sensors::node::sensors &&
2470 sensorsAsyncResp->efficientExpand)
2471 {
2472 sensorsAsyncResp->asyncResp->res
2473 .jsonValue["Members@odata.count"] =
2474 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2475 .size();
2476 }
2477 else if (sensorsAsyncResp->chassisSubNode ==
2478 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002479 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002480 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002481 }
James Feist8bd25cc2019-03-15 15:14:00 -07002482 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002483 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2484 };
2485
Shawn McCarneyde629b62019-03-08 10:42:51 -06002486 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002487 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002488 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002489 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002490 BMCWEB_LOG_DEBUG << "getSensorData exit";
2491}
2492
Nan Zhoufe04d492022-06-22 17:10:41 +00002493inline void
2494 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2495 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002496{
Nan Zhoufe04d492022-06-22 17:10:41 +00002497 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2498 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002499 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002500 auto getInventoryItemsCb =
2501 [sensorsAsyncResp, sensorNames,
2502 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2503 inventoryItems) {
2504 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2505 // Get sensor data and store results in JSON
2506 getSensorData(sensorsAsyncResp, sensorNames, connections,
2507 inventoryItems);
2508 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002509 };
2510
Ed Tanousd0090732022-10-04 17:22:56 -07002511 // Get inventory items associated with sensors
2512 getInventoryItems(sensorsAsyncResp, sensorNames,
2513 std::move(getInventoryItemsCb));
2514
Ed Tanous002d39b2022-05-31 08:59:27 -07002515 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2516 };
2517
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002518 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002519 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002520}
2521
Shawn McCarneyde629b62019-03-08 10:42:51 -06002522/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002523 * @brief Entry point for retrieving sensors data related to requested
2524 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002525 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002526 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002527inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002528 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002529{
2530 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002531 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002532 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002533 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002534 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2535 processSensorList(sensorsAsyncResp, sensorNames);
2536 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2537 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002538 // SensorCollection doesn't contain the Redundancy property
2539 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2540 {
2541 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2542 nlohmann::json::array();
2543 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002544 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002545 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2546 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2547 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002548 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002549}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002550
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302551/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002552 * @brief Find the requested sensorName in the list of all sensors supplied by
2553 * the chassis node
2554 *
2555 * @param sensorName The sensor name supplied in the PATCH request
2556 * @param sensorsList The list of sensors managed by the chassis node
2557 * @param sensorsModified The list of sensors that were found as a result of
2558 * repeated calls to this function
2559 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002560inline bool
2561 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002562 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002563 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002564{
Nan Zhoufe04d492022-06-22 17:10:41 +00002565 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002566 {
George Liu28aa8de2021-02-01 15:13:30 +08002567 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002568 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002569 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002570 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002571 continue;
2572 }
2573 if (thisSensorName == sensorName)
2574 {
2575 sensorsModified.emplace(chassisSensor);
2576 return true;
2577 }
2578 }
2579 return false;
2580}
2581
Ed Tanousc71d6122022-11-29 14:10:32 -08002582inline std::pair<std::string, std::string>
2583 splitSensorNameAndType(std::string_view sensorId)
2584{
2585 size_t index = sensorId.find('_');
2586 if (index == std::string::npos)
2587 {
2588 return std::make_pair<std::string, std::string>("", "");
2589 }
2590 std::string sensorType{sensorId.substr(0, index)};
2591 std::string sensorName{sensorId.substr(index + 1)};
2592 // fan_pwm and fan_tach need special handling
2593 if (sensorType == "fantach" || sensorType == "fanpwm")
2594 {
2595 sensorType.insert(3, 1, '_');
2596 }
2597 return std::make_pair(sensorType, sensorName);
2598}
2599
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002600/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302601 * @brief Entry point for overriding sensor values of given sensor
2602 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002603 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002604 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002605 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302606 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002607inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002608 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002609 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002610 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302611{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002612 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002613 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302614
Ed Tanous543f4402022-01-06 13:12:53 -08002615 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302616 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302617 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002618 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302619 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302620 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302621 if (collectionItems.first == "Temperatures")
2622 {
2623 propertyValueName = "ReadingCelsius";
2624 }
2625 else if (collectionItems.first == "Fans")
2626 {
2627 propertyValueName = "Reading";
2628 }
2629 else
2630 {
2631 propertyValueName = "ReadingVolts";
2632 }
2633 for (auto& item : collectionItems.second)
2634 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002635 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2636 "MemberId", memberId, propertyValueName,
2637 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302638 {
2639 return;
2640 }
2641 overrideMap.emplace(memberId,
2642 std::make_pair(value, collectionItems.first));
2643 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302644 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002645
Ed Tanous002d39b2022-05-31 08:59:27 -07002646 auto getChassisSensorListCb =
2647 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002648 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002649 // Match sensor names in the PATCH request to those managed by the
2650 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002651 const std::shared_ptr<std::set<std::string>> sensorNames =
2652 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302653 for (const auto& item : overrideMap)
2654 {
2655 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002656 std::pair<std::string, std::string> sensorNameType =
2657 splitSensorNameAndType(sensor);
2658 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2659 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302660 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302661 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002662 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302663 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302664 return;
2665 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302666 }
2667 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002668 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002669 [sensorAsyncResp,
2670 overrideMap](const std::set<std::string>& /*connections*/,
2671 const std::set<std::pair<std::string, std::string>>&
2672 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002673 if (objectsWithConnection.size() != overrideMap.size())
2674 {
2675 BMCWEB_LOG_INFO
2676 << "Unable to find all objects with proper connection "
2677 << objectsWithConnection.size() << " requested "
2678 << overrideMap.size() << "\n";
2679 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2680 sensorAsyncResp->chassisSubNode ==
2681 sensors::node::thermal
2682 ? "Temperatures"
2683 : "Voltages",
2684 "Count");
2685 return;
2686 }
2687 for (const auto& item : objectsWithConnection)
2688 {
2689 sdbusplus::message::object_path path(item.first);
2690 std::string sensorName = path.filename();
2691 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302692 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002693 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302694 return;
2695 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302696
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002697 const auto& iterator = overrideMap.find(sensorName);
2698 if (iterator == overrideMap.end())
2699 {
2700 BMCWEB_LOG_INFO << "Unable to find sensor object"
2701 << item.first << "\n";
2702 messages::internalError(sensorAsyncResp->asyncResp->res);
2703 return;
2704 }
2705 crow::connections::systemBus->async_method_call(
2706 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002707 if (ec)
2708 {
2709 if (ec.value() ==
2710 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002711 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002712 BMCWEB_LOG_WARNING
2713 << "Manufacturing mode is not Enabled...can't "
2714 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002715
Ed Tanous002d39b2022-05-31 08:59:27 -07002716 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002717 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002718 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002719 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002720 BMCWEB_LOG_DEBUG
2721 << "setOverrideValueStatus DBUS error: " << ec;
2722 messages::internalError(
2723 sensorAsyncResp->asyncResp->res);
2724 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002725 },
2726 item.second, item.first, "org.freedesktop.DBus.Properties",
2727 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002728 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002729 }
2730 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302731 // Get object with connection for the given sensor name
2732 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2733 std::move(getObjectsWithConnectionCb));
2734 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302735 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002736 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2737 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2738 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302739}
2740
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002741/**
2742 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2743 * path of the sensor.
2744 *
2745 * Function builds valid Redfish response for sensor query of given chassis and
2746 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2747 * it to caller in a callback.
2748 *
2749 * @param chassis Chassis for which retrieval should be performed
2750 * @param node Node (group) of sensors. See sensors::node for supported values
2751 * @param mapComplete Callback to be called with retrieval result
2752 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002753inline void retrieveUriToDbusMap(const std::string& chassis,
2754 const std::string& node,
2755 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002756{
Ed Tanous02da7c52022-02-27 00:09:02 -08002757 decltype(sensors::paths)::const_iterator pathIt =
2758 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2759 [&node](auto&& val) { return val.first == node; });
2760 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002761 {
2762 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2763 mapComplete(boost::beast::http::status::bad_request, {});
2764 return;
2765 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002766
Nan Zhou72374eb2022-01-27 17:06:51 -08002767 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002768 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2769 const boost::beast::http::status status,
2770 const std::map<std::string, std::string>& uriToDbus) {
2771 mapCompleteCb(status, uriToDbus);
2772 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002773
2774 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002775 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002776 getChassisData(resp);
2777}
2778
Nan Zhoubacb2162022-04-06 11:28:32 -07002779namespace sensors
2780{
Nan Zhou928fefb2022-03-28 08:45:00 -07002781
Nan Zhoubacb2162022-04-06 11:28:32 -07002782inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002783 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2784 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002785 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002786{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002787 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002788
Ed Tanousc1d019a2022-08-06 09:36:06 -07002789 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2790 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002791 {
2792 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2793
2794 sdbusplus::message::object_path path(sensor);
2795 std::string sensorName = path.filename();
2796 if (sensorName.empty())
2797 {
2798 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002799 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002800 return;
2801 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002802 std::string type = path.parent_path().filename();
2803 // fan_tach has an underscore in it, so remove it to "normalize" the
2804 // type in the URI
2805 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2806
Ed Tanous14766872022-03-15 10:44:42 -07002807 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002808 std::string id = type;
2809 id += "_";
2810 id += sensorName;
2811 member["@odata.id"] = crow::utility::urlFromPieces(
2812 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2813
Ed Tanous14766872022-03-15 10:44:42 -07002814 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002815 }
2816
Ed Tanousc1d019a2022-08-06 09:36:06 -07002817 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002818 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2819}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002820
Nan Zhoude167a62022-06-01 04:47:45 +00002821inline void
2822 handleSensorCollectionGet(App& app, const crow::Request& req,
2823 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2824 const std::string& chassisId)
2825{
2826 query_param::QueryCapabilities capabilities = {
2827 .canDelegateExpandLevel = 1,
2828 };
2829 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002830 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002831 delegatedQuery, capabilities))
2832 {
2833 return;
2834 }
2835
2836 if (delegatedQuery.expandType != query_param::ExpandType::None)
2837 {
2838 // we perform efficient expand.
2839 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2840 aResp, chassisId, sensors::dbus::sensorPaths,
2841 sensors::node::sensors,
2842 /*efficientExpand=*/true);
2843 getChassisData(asyncResp);
2844
2845 BMCWEB_LOG_DEBUG
2846 << "SensorCollection doGet exit via efficient expand handler";
2847 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002848 }
Nan Zhoude167a62022-06-01 04:47:45 +00002849
Nan Zhoude167a62022-06-01 04:47:45 +00002850 // We get all sensors as hyperlinkes in the chassis (this
2851 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002852 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2853 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2854 sensors::node::sensors));
2855}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002856
Ed Tanousc1d019a2022-08-06 09:36:06 -07002857inline void
2858 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2859 const std::string& sensorPath,
2860 const ::dbus::utility::MapperGetObject& mapperResponse)
2861{
2862 if (mapperResponse.size() != 1)
2863 {
2864 messages::internalError(asyncResp->res);
2865 return;
2866 }
2867 const auto& valueIface = *mapperResponse.begin();
2868 const std::string& connectionName = valueIface.first;
2869 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2870 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002871
2872 sdbusplus::asio::getAllProperties(
2873 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002874 [asyncResp,
2875 sensorPath](const boost::system::error_code ec,
2876 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2877 if (ec)
2878 {
2879 messages::internalError(asyncResp->res);
2880 return;
2881 }
2882 sdbusplus::message::object_path path(sensorPath);
2883 std::string name = path.filename();
2884 path = path.parent_path();
2885 std::string type = path.filename();
2886 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2887 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002888 });
Nan Zhoude167a62022-06-01 04:47:45 +00002889}
2890
Nan Zhoue6bd8462022-06-01 04:35:35 +00002891inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002892 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002893 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002894 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002895{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002896 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002897 {
2898 return;
2899 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002900 std::pair<std::string, std::string> nameType =
2901 splitSensorNameAndType(sensorId);
2902 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002903 {
2904 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2905 return;
2906 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002907
Ed Tanous677bb752022-09-15 10:52:19 -07002908 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2909 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002910
Nan Zhoue6bd8462022-06-01 04:35:35 +00002911 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002912
2913 const std::array<const char*, 1> interfaces = {
2914 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002915 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2916 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002917 // Get a list of all of the sensors that implement Sensor.Value
2918 // and get the path and service name associated with the sensor
2919 crow::connections::systemBus->async_method_call(
Ed Tanousc71d6122022-11-29 14:10:32 -08002920 [asyncResp,
2921 sensorPath](const boost::system::error_code ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002922 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002923 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2924 if (ec)
2925 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002926 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002927 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2928 << "Dbus error " << ec;
2929 return;
2930 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002931 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002932 BMCWEB_LOG_DEBUG << "respHandler1 exit";
2933 },
2934 "xyz.openbmc_project.ObjectMapper",
2935 "/xyz/openbmc_project/object_mapper",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002936 "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
2937 interfaces);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002938}
2939
Nan Zhoubacb2162022-04-06 11:28:32 -07002940} // namespace sensors
2941
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002942inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002943{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002944 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002945 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002946 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002947 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002948}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002949
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002950inline void requestRoutesSensor(App& app)
2951{
2952 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002953 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002954 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002955 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002956}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002957
Ed Tanous1abe55e2018-09-05 08:30:59 -07002958} // namespace redfish