blob: cf63bd70786fddf4717ed8700fee7c2e739b2f14 [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
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070019#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070020#include <boost/algorithm/string/find.hpp>
21#include <boost/algorithm/string/predicate.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010022#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010023#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070024#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080025#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070026#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070027#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070028#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020029#include <sdbusplus/unpack_properties.hpp>
30#include <utils/dbus_utils.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053031#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070032#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033
34#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000035#include <iterator>
36#include <map>
37#include <set>
Ed Tanousb5a76932020-09-29 16:16:58 -070038#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080039#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010040
Ed Tanous1abe55e2018-09-05 08:30:59 -070041namespace redfish
42{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010043
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020044namespace sensors
45{
46namespace node
47{
48static constexpr std::string_view power = "Power";
49static constexpr std::string_view sensors = "Sensors";
50static constexpr std::string_view thermal = "Thermal";
51} // namespace node
52
Ed Tanous02da7c52022-02-27 00:09:02 -080053// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020054namespace dbus
55{
Ed Tanous4ee8e212022-05-28 09:42:51 -070056static auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080057 "/xyz/openbmc_project/sensors/voltage",
58 "/xyz/openbmc_project/sensors/power"
59});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000060
Ed Tanous4ee8e212022-05-28 09:42:51 -070061static auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080062 "/xyz/openbmc_project/sensors/power",
63 "/xyz/openbmc_project/sensors/current",
64 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070065 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080066#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080067 "/xyz/openbmc_project/sensors/voltage",
68 "/xyz/openbmc_project/sensors/fan_tach",
69 "/xyz/openbmc_project/sensors/temperature",
70 "/xyz/openbmc_project/sensors/fan_pwm",
71 "/xyz/openbmc_project/sensors/altitude",
72 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080073#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080074 "/xyz/openbmc_project/sensors/utilization"
75});
76
Ed Tanous4ee8e212022-05-28 09:42:51 -070077static auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080078 "/xyz/openbmc_project/sensors/fan_tach",
79 "/xyz/openbmc_project/sensors/temperature",
80 "/xyz/openbmc_project/sensors/fan_pwm"
81});
82
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000083} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080084// clang-format on
85
86using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
87static constexpr std::array<sensorPair, 3> paths = {
88 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
89 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
90 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000091
Ed Tanous1d7c0052022-08-09 12:32:26 -070092inline std::string_view toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000093{
94 if (sensorType == "voltage")
95 {
96 return "Voltage";
97 }
98 if (sensorType == "power")
99 {
100 return "Power";
101 }
102 if (sensorType == "current")
103 {
104 return "Current";
105 }
106 if (sensorType == "fan_tach")
107 {
108 return "Rotational";
109 }
110 if (sensorType == "temperature")
111 {
112 return "Temperature";
113 }
114 if (sensorType == "fan_pwm" || sensorType == "utilization")
115 {
116 return "Percent";
117 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600118 if (sensorType == "humidity")
119 {
120 return "Humidity";
121 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000122 if (sensorType == "altitude")
123 {
124 return "Altitude";
125 }
126 if (sensorType == "airflow")
127 {
128 return "AirFlow";
129 }
130 if (sensorType == "energy")
131 {
132 return "EnergyJoules";
133 }
134 return "";
135}
136
Ed Tanous1d7c0052022-08-09 12:32:26 -0700137inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000138{
139 if (sensorType == "voltage")
140 {
141 return "V";
142 }
143 if (sensorType == "power")
144 {
145 return "W";
146 }
147 if (sensorType == "current")
148 {
149 return "A";
150 }
151 if (sensorType == "fan_tach")
152 {
153 return "RPM";
154 }
155 if (sensorType == "temperature")
156 {
157 return "Cel";
158 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600159 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
160 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000161 {
162 return "%";
163 }
164 if (sensorType == "altitude")
165 {
166 return "m";
167 }
168 if (sensorType == "airflow")
169 {
170 return "cft_i/min";
171 }
172 if (sensorType == "energy")
173 {
174 return "J";
175 }
176 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200177}
178} // namespace sensors
179
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100180/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200181 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100182 * Gathers data needed for response processing after async calls are done
183 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184class SensorsAsyncResp
185{
186 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200187 using DataCompleteCb = std::function<void(
188 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000189 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200190
191 struct SensorData
192 {
193 const std::string name;
194 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200195 const std::string dbusPath;
196 };
197
Ed Tanous8a592812022-06-04 09:06:59 -0700198 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800199 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800200 std::span<std::string_view> typesIn,
201 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700202 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700203 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
204 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200206
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200207 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700208 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800209 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800210 std::span<std::string_view> typesIn,
211 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200212 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700213 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700214 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
215 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200216 dataComplete{std::move(creationComplete)}
217 {}
218
Nan Zhou928fefb2022-03-28 08:45:00 -0700219 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700220 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700221 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800222 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700223 const std::string_view& subNode, bool efficientExpandIn) :
224 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700225 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700226 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700227 {}
228
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229 ~SensorsAsyncResp()
230 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800231 if (asyncResp->res.result() ==
232 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700233 {
234 // Reset the json object to clear out any data that made it in
235 // before the error happened todo(ed) handle error condition with
236 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800237 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200239
240 if (dataComplete && metadata)
241 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000242 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800243 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200244 {
245 for (auto& sensor : *metadata)
246 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700247 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200248 }
249 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200251 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100253
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800254 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
255 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
256 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
257 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
258
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200259 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700260 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200261 {
262 if (metadata)
263 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700264 metadata->emplace_back(SensorData{
265 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200266 }
267 }
268
269 void updateUri(const std::string& name, const std::string& uri)
270 {
271 if (metadata)
272 {
273 for (auto& sensor : *metadata)
274 {
275 if (sensor.name == name)
276 {
277 sensor.uri = uri;
278 }
279 }
280 }
281 }
282
zhanghch058d1b46d2021-04-01 11:18:24 +0800283 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200284 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800285 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200286 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700287 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200288
289 private:
290 std::optional<std::vector<SensorData>> metadata;
291 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100292};
293
294/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500295 * Possible states for physical inventory leds
296 */
297enum class LedState
298{
299 OFF,
300 ON,
301 BLINK,
302 UNKNOWN
303};
304
305/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500306 * D-Bus inventory item associated with one or more sensors.
307 */
308class InventoryItem
309{
310 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700311 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500312 {
313 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800314 sdbusplus::message::object_path path(objectPath);
315 name = path.filename();
316 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500317 {
George Liu28aa8de2021-02-01 15:13:30 +0800318 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500319 }
320 }
321
322 std::string objectPath;
323 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800324 bool isPresent = true;
325 bool isFunctional = true;
326 bool isPowerSupply = false;
327 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500328 std::string manufacturer;
329 std::string model;
330 std::string partNumber;
331 std::string serialNumber;
332 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500333 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800334 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500335};
336
337/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530338 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200339 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100340 * @param sensorNames Sensors retrieved from chassis
341 * @param callback Callback for processing gathered connections
342 */
343template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530344void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000345 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000346 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530347 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700348{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530349 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350 const std::string path = "/xyz/openbmc_project/sensors";
351 const std::array<std::string, 1> interfaces = {
352 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100353
Ed Tanous1abe55e2018-09-05 08:30:59 -0700354 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700355 auto respHandler =
356 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
357 sensorNames](const boost::system::error_code ec,
358 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530359 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700360 if (ec)
361 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800362 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530363 BMCWEB_LOG_ERROR
364 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700365 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100366 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100367
Ed Tanous1abe55e2018-09-05 08:30:59 -0700368 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
369
370 // Make unique list of connections only for requested sensor types and
371 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000372 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530373 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700374
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700375 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
376 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377 {
378 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
379 }
380
381 for (const std::pair<
382 std::string,
383 std::vector<std::pair<std::string, std::vector<std::string>>>>&
384 object : subtree)
385 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700386 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700387 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700388 for (const std::pair<std::string, std::vector<std::string>>&
389 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700391 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
392 connections.insert(objData.first);
393 objectsWithConnection.insert(
394 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700395 }
396 }
397 }
398 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530399 callback(std::move(connections), std::move(objectsWithConnection));
400 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 // Make call to ObjectMapper to find all sensors objects
403 crow::connections::systemBus->async_method_call(
404 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
405 "/xyz/openbmc_project/object_mapper",
406 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530407 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
408}
409
410/**
411 * @brief Create connections necessary for sensors
412 * @param SensorsAsyncResp Pointer to object holding response data
413 * @param sensorNames Sensors retrieved from chassis
414 * @param callback Callback for processing gathered connections
415 */
416template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000417void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
418 const std::shared_ptr<std::set<std::string>> sensorNames,
419 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530420{
421 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000422 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530423 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700424 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000425 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530426 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100427}
428
429/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700430 * @brief Shrinks the list of sensors for processing
431 * @param SensorsAysncResp The class holding the Redfish response
432 * @param allSensors A list of all the sensors associated to the
433 * chassis element (i.e. baseboard, front panel, etc...)
434 * @param activeSensors A list that is a reduction of the incoming
435 * allSensors list. Eliminate Thermal sensors when a Power request is
436 * made, and eliminate Power sensors when a Thermal request is made.
437 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000438inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700439 crow::Response& res, std::string_view chassisSubNode,
440 std::span<std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700441 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000442 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700443{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700444 if ((allSensors == nullptr) || (activeSensors == nullptr))
445 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700446 messages::resourceNotFound(res, chassisSubNode,
447 chassisSubNode == sensors::node::thermal
448 ? "Temperatures"
449 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700450
451 return;
452 }
453 if (allSensors->empty())
454 {
455 // Nothing to do, the activeSensors object is also empty
456 return;
457 }
458
Ed Tanous7f1cc262022-08-09 13:33:57 -0700459 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700460 {
461 for (const std::string& sensor : *allSensors)
462 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700463 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700464 {
465 activeSensors->emplace(sensor);
466 }
467 }
468 }
469}
470
Ed Tanous7f1cc262022-08-09 13:33:57 -0700471/*
472 *Populates the top level collection for a given subnode. Populates
473 *SensorCollection, Power, or Thermal schemas.
474 *
475 * */
476inline void populateChassisNode(nlohmann::json& jsonValue,
477 std::string_view chassisSubNode)
478{
479 if (chassisSubNode == sensors::node::power)
480 {
481 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
482 }
483 else if (chassisSubNode == sensors::node::thermal)
484 {
485 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
486 jsonValue["Fans"] = nlohmann::json::array();
487 jsonValue["Temperatures"] = nlohmann::json::array();
488 }
489 else if (chassisSubNode == sensors::node::sensors)
490 {
491 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
492 jsonValue["Description"] = "Collection of Sensors for this Chassis";
493 jsonValue["Members"] = nlohmann::json::array();
494 jsonValue["Members@odata.count"] = 0;
495 }
496
497 if (chassisSubNode != sensors::node::sensors)
498 {
499 jsonValue["Id"] = chassisSubNode;
500 }
501 jsonValue["Name"] = chassisSubNode;
502}
503
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700504/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100505 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200506 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100507 * @param callback Callback for next step in gathered sensor processing
508 */
509template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700510void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
511 std::string_view chassisId, std::string_view chassisSubNode,
512 std::span<std::string_view> sensorTypes, Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700513{
514 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500515 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700516 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500517 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700518 auto respHandler =
Ed Tanous7f1cc262022-08-09 13:33:57 -0700519 [callback{std::forward<Callback>(callback)}, asyncResp,
520 chassisIdStr{std::string(chassisId)},
521 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
Ed Tanous002d39b2022-05-31 08:59:27 -0700522 const boost::system::error_code ec,
523 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700524 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
525 if (ec)
526 {
527 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700528 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 return;
530 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700531 const std::string* chassisPath = nullptr;
532 std::string chassisName;
533 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700534 {
George Liu28aa8de2021-02-01 15:13:30 +0800535 sdbusplus::message::object_path path(chassis);
536 chassisName = path.filename();
537 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700538 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700539 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 continue;
541 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700542 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700544 chassisPath = &chassis;
545 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700547 }
548 if (chassisPath == nullptr)
549 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700550 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700551 return;
552 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700553 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700554
Ed Tanous7f1cc262022-08-09 13:33:57 -0700555 asyncResp->res.jsonValue["@odata.id"] =
556 "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500557
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500558 // Get the list of all sensors for this Chassis element
559 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700560 sdbusplus::asio::getProperty<std::vector<std::string>>(
561 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
562 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700563 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800564 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700565 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700566 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700567 if (e)
568 {
569 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700570 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700571 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700572 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700573 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000575 const std::shared_ptr<std::set<std::string>> culledSensorList =
576 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700577 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
578 &nodeSensorList, culledSensorList);
579 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700581 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100582 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100583
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700584 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700585 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700586 respHandler, "xyz.openbmc_project.ObjectMapper",
587 "/xyz/openbmc_project/object_mapper",
588 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700589 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700590 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100591}
592
593/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600594 * @brief Finds all DBus object paths that implement ObjectManager.
595 *
596 * Creates a mapping from the associated connection name to the object path.
597 *
598 * Finds the object paths asynchronously. Invokes callback when information has
599 * been obtained.
600 *
601 * The callback must have the following signature:
602 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +0000603 * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600604 * @endcode
605 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700606 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600607 * @param callback Callback to invoke when object paths obtained.
608 */
609template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700610void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000611 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700612 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600613{
614 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
615 const std::array<std::string, 1> interfaces = {
616 "org.freedesktop.DBus.ObjectManager"};
617
618 // Response handler for GetSubTree DBus method
Ed Tanous002d39b2022-05-31 08:59:27 -0700619 auto respHandler =
620 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
621 const boost::system::error_code ec,
622 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600623 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
624 if (ec)
625 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800626 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600627 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
628 << ec;
629 return;
630 }
631
632 // Loop over returned object paths
Nan Zhoufe04d492022-06-22 17:10:41 +0000633 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
634 std::make_shared<std::map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600635 for (const std::pair<
636 std::string,
637 std::vector<std::pair<std::string, std::vector<std::string>>>>&
638 object : subtree)
639 {
640 // Loop over connections for current object path
641 const std::string& objectPath = object.first;
642 for (const std::pair<std::string, std::vector<std::string>>&
643 objData : object.second)
644 {
645 // Add mapping from connection to object path
646 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500647 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600648 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
649 << objectPath;
650 }
651 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500652 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600653 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
654 };
655
656 // Query mapper for all DBus object paths that implement ObjectManager
657 crow::connections::systemBus->async_method_call(
658 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
659 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700660 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600661 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
662}
663
664/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500665 * @brief Returns the Redfish State value for the specified inventory item.
666 * @param inventoryItem D-Bus inventory item associated with a sensor.
667 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700668 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000669inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500670{
671 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
672 {
673 return "Absent";
674 }
James Feist34dd1792019-05-17 14:10:54 -0700675
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500676 return "Enabled";
677}
678
679/**
680 * @brief Returns the Redfish Health value for the specified sensor.
681 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700682 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500683 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
684 * be nullptr if no associated inventory item was found.
685 * @return Health value for sensor.
686 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700687inline std::string getHealth(nlohmann::json& sensorJson,
688 const dbus::utility::DBusPropertiesMap& valuesDict,
689 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700690{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500691 // Get current health value (if any) in the sensor JSON object. Some JSON
692 // objects contain multiple sensors (such as PowerSupplies). We want to set
693 // the overall health to be the most severe of any of the sensors.
694 std::string currentHealth;
695 auto statusIt = sensorJson.find("Status");
696 if (statusIt != sensorJson.end())
697 {
698 auto healthIt = statusIt->find("Health");
699 if (healthIt != statusIt->end())
700 {
701 std::string* health = healthIt->get_ptr<std::string*>();
702 if (health != nullptr)
703 {
704 currentHealth = *health;
705 }
706 }
707 }
708
709 // If current health in JSON object is already Critical, return that. This
710 // should override the sensor health, which might be less severe.
711 if (currentHealth == "Critical")
712 {
713 return "Critical";
714 }
715
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200716 const bool* criticalAlarmHigh = nullptr;
717 const bool* criticalAlarmLow = nullptr;
718 const bool* warningAlarmHigh = nullptr;
719 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800720
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200721 const bool success = sdbusplus::unpackPropertiesNoThrow(
722 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
723 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
724 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
725 warningAlarmLow);
726
727 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700728 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200729 // Check if sensor has critical threshold alarm
730 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
731 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700732 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200733 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700734 }
735 }
736
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500737 // Check if associated inventory item is not functional
738 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
739 {
740 return "Critical";
741 }
742
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200743 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500744 // should override the sensor status, which might be less severe.
745 if (currentHealth == "Warning")
746 {
747 return "Warning";
748 }
749
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200750 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700751 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200752 // Check if sensor has warning threshold alarm
753 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
754 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700755 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200756 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700757 }
758 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500759
James Feist34dd1792019-05-17 14:10:54 -0700760 return "OK";
761}
762
Ed Tanous23a21a12020-07-25 04:45:05 +0000763inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500764 const InventoryItem* inventoryItem)
765{
766 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
767 {
768 switch (inventoryItem->ledState)
769 {
770 case LedState::OFF:
771 sensorJson["IndicatorLED"] = "Off";
772 break;
773 case LedState::ON:
774 sensorJson["IndicatorLED"] = "Lit";
775 break;
776 case LedState::BLINK:
777 sensorJson["IndicatorLED"] = "Blinking";
778 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000779 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500780 break;
781 }
782 }
783}
784
James Feist34dd1792019-05-17 14:10:54 -0700785/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100786 * @brief Builds a json sensor representation of a sensor.
787 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500788 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100789 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700790 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
791 * @param propertiesDict A dictionary of the properties to build the sensor
792 * from.
793 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500794 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
795 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100796 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700797inline void objectPropertiesToJson(
798 std::string_view sensorName, std::string_view sensorType,
799 std::string_view chassisSubNode,
800 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000801 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700802{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700803 // Assume values exist as is (10^0 == 1) if no scale exists
804 int64_t scaleMultiplier = 0;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200805
806 const int64_t* scale = nullptr;
807
808 const bool success = sdbusplus::unpackPropertiesNoThrow(
809 dbus_utils::UnpackErrorPrinter(), propertiesDict, "Scale", scale);
810
811 if (success && scale != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700812 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200813 scaleMultiplier = *scale;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100814 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700815
Ed Tanous1d7c0052022-08-09 12:32:26 -0700816 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500817 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700818 std::string subNodeEscaped(chassisSubNode);
819 subNodeEscaped.erase(
820 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
821 subNodeEscaped.end());
822
823 // For sensors in SensorCollection we set Id instead of MemberId,
824 // including power sensors.
825 subNodeEscaped += '_';
826 subNodeEscaped += sensorName;
827 sensorJson["Id"] = std::move(subNodeEscaped);
828
Ed Tanous1d7c0052022-08-09 12:32:26 -0700829 std::string sensorNameEs(sensorName);
830 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
831 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500832 }
833 else if (sensorType != "power")
834 {
835 // Set MemberId and Name for non-power sensors. For PowerSupplies and
836 // PowerControl, those properties have more general values because
837 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000838 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700839 std::string sensorNameEs(sensorName);
840 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
841 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500842 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700843
Ed Tanous81ce6092020-12-17 16:54:55 +0000844 sensorJson["Status"]["State"] = getState(inventoryItem);
845 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700846 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700847
848 // Parameter to set to override the type we get from dbus, and force it to
849 // int, regardless of what is available. This is used for schemas like fan,
850 // that require integers, not floats.
851 bool forceToInt = false;
852
Anthony Wilson3929aca2019-07-19 15:42:33 -0500853 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700854 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500855 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000856 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000857
Ed Tanous1d7c0052022-08-09 12:32:26 -0700858 std::string_view readingType = sensors::toReadingType(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000859 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500860 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000861 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
862 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500863 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000864 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500865 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000866 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500867 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000868
Ed Tanous1d7c0052022-08-09 12:32:26 -0700869 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000870 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200871 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000872 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
873 << sensorType;
874 }
875 else
876 {
877 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200878 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500879 }
880 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500882 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000883 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700884 // TODO(ed) Documentation says that path should be type fan_tach,
885 // implementation seems to implement fan
886 }
887 else if (sensorType == "fan" || sensorType == "fan_tach")
888 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500889 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000890 sensorJson["ReadingUnits"] = "RPM";
891 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
892 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700893 forceToInt = true;
894 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700895 else if (sensorType == "fan_pwm")
896 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500897 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000898 sensorJson["ReadingUnits"] = "Percent";
899 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
900 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700901 forceToInt = true;
902 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 else if (sensorType == "voltage")
904 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500905 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000906 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700908 else if (sensorType == "power")
909 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700910 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000911 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000912 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500913 // Put multiple "sensors" into a single PowerControl, so have
914 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000915 sensorJson["MemberId"] = "0";
916 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500917 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000918 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700919 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700920 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500921 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700922 }
923 else
924 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500925 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700926 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700927 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700928 else
929 {
930 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
931 return;
932 }
933 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500934 std::vector<
935 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
936 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700937 properties.reserve(7);
938
939 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600940
Ed Tanous1d7c0052022-08-09 12:32:26 -0700941 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500942 {
943 properties.emplace_back(
944 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
945 "/Thresholds/UpperCaution/Reading"_json_pointer);
946 properties.emplace_back(
947 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
948 "/Thresholds/LowerCaution/Reading"_json_pointer);
949 properties.emplace_back(
950 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
951 "/Thresholds/UpperCritical/Reading"_json_pointer);
952 properties.emplace_back(
953 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
954 "/Thresholds/LowerCritical/Reading"_json_pointer);
955 }
956 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600957 {
958 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500959 "WarningHigh",
960 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600961 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500962 "WarningLow",
963 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600964 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500965 "CriticalHigh",
966 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600967 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500968 "CriticalLow",
969 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600970 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700971
Ed Tanous2474adf2018-09-05 16:31:16 -0700972 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
973
Ed Tanous1d7c0052022-08-09 12:32:26 -0700974 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500975 {
976 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500977 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500978 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500979 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500980 }
981 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700982 {
983 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500984 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700985 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500986 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500988 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 {
990 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500991 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700992 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500993 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700994 }
995
Anthony Wilson3929aca2019-07-19 15:42:33 -0500996 for (const std::tuple<const char*, const char*,
997 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700998 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700999 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001000 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07001001 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001003 continue;
1004 }
Ed Tanous1d7c0052022-08-09 12:32:26 -07001005
1006 // The property we want to set may be nested json, so use
1007 // a json_pointer for easy indexing into the json structure.
1008 const nlohmann::json::json_pointer& key = std::get<2>(p);
1009
1010 // Attempt to pull the int64 directly
1011 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
1012
1013 const double* doubleValue = std::get_if<double>(&valueVariant);
1014 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
1015 double temp = 0.0;
1016 if (int64Value != nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001017 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07001018 temp = static_cast<double>(*int64Value);
1019 }
1020 else if (doubleValue != nullptr)
1021 {
1022 temp = *doubleValue;
1023 }
1024 else if (uValue != nullptr)
1025 {
1026 temp = *uValue;
1027 }
1028 else
1029 {
1030 BMCWEB_LOG_ERROR
1031 << "Got value interface that wasn't int or double";
1032 continue;
1033 }
1034 temp = temp * std::pow(10, scaleMultiplier);
1035 if (forceToInt)
1036 {
1037 sensorJson[key] = static_cast<int64_t>(temp);
1038 }
1039 else
1040 {
1041 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001042 }
1043 }
1044 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001045}
1046
Ed Tanous1d7c0052022-08-09 12:32:26 -07001047/**
1048 * @brief Builds a json sensor representation of a sensor.
1049 * @param sensorName The name of the sensor to be built
1050 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
1051 * build
1052 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
1053 * @param interfacesDict A dictionary of the interfaces and properties of said
1054 * interfaces to be built from
1055 * @param sensorJson The json object to fill
1056 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1057 * be nullptr if no associated inventory item was found.
1058 */
1059inline void objectInterfacesToJson(
1060 const std::string& sensorName, const std::string& sensorType,
1061 const std::string& chassisSubNode,
1062 const dbus::utility::DBusInteracesMap& interfacesDict,
1063 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1064{
1065
1066 for (const auto& [interface, valuesDict] : interfacesDict)
1067 {
1068 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1069 valuesDict, sensorJson, inventoryItem);
1070 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07001071 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -07001072}
1073
Ed Tanousb5a76932020-09-29 16:16:58 -07001074inline void populateFanRedundancy(
1075 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001076{
1077 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001078 [sensorsAsyncResp](
1079 const boost::system::error_code ec,
1080 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001081 if (ec)
1082 {
1083 return; // don't have to have this interface
1084 }
1085 for (const std::pair<
1086 std::string,
1087 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1088 pathPair : resp)
1089 {
1090 const std::string& path = pathPair.first;
1091 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1092 objDict = pathPair.second;
1093 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001094 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001096 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001097
1098 const std::string& owner = objDict.begin()->first;
1099 sdbusplus::asio::getProperty<std::vector<std::string>>(
1100 *crow::connections::systemBus,
1101 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1102 "xyz.openbmc_project.Association", "endpoints",
1103 [path, owner,
1104 sensorsAsyncResp](const boost::system::error_code e,
1105 const std::vector<std::string>& endpoints) {
1106 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001107 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001108 return; // if they don't have an association we
1109 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001110 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001111 auto found =
1112 std::find_if(endpoints.begin(), endpoints.end(),
1113 [sensorsAsyncResp](const std::string& entry) {
1114 return entry.find(sensorsAsyncResp->chassisId) !=
1115 std::string::npos;
1116 });
James Feist8bd25cc2019-03-15 15:14:00 -07001117
Ed Tanous002d39b2022-05-31 08:59:27 -07001118 if (found == endpoints.end())
1119 {
1120 return;
1121 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001122 sdbusplus::asio::getAllProperties(
1123 *crow::connections::systemBus, owner, path,
1124 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001125 [path, sensorsAsyncResp](
1126 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001127 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 if (err)
1129 {
1130 return; // don't have to have this
1131 // interface
1132 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001133
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001134 const uint8_t* allowedFailures = nullptr;
1135 const std::vector<std::string>* collection = nullptr;
1136 const std::string* status = nullptr;
1137
1138 const bool success = sdbusplus::unpackPropertiesNoThrow(
1139 dbus_utils::UnpackErrorPrinter(), ret,
1140 "AllowedFailures", allowedFailures, "Collection",
1141 collection, "Status", status);
1142
1143 if (!success)
1144 {
1145 messages::internalError(
1146 sensorsAsyncResp->asyncResp->res);
1147 return;
1148 }
1149
1150 if (allowedFailures == nullptr || collection == nullptr ||
1151 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001152 {
1153 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1154 messages::internalError(
1155 sensorsAsyncResp->asyncResp->res);
1156 return;
1157 }
1158
Ed Tanous002d39b2022-05-31 08:59:27 -07001159 sdbusplus::message::object_path objectPath(path);
1160 std::string name = objectPath.filename();
1161 if (name.empty())
1162 {
1163 // this should be impossible
1164 messages::internalError(
1165 sensorsAsyncResp->asyncResp->res);
1166 return;
1167 }
1168 std::replace(name.begin(), name.end(), '_', ' ');
1169
1170 std::string health;
1171
Ed Tanous11ba3972022-07-11 09:50:41 -07001172 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001173 {
1174 health = "OK";
1175 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001176 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001177 {
1178 health = "Warning";
1179 }
1180 else
1181 {
1182 health = "Critical";
1183 }
1184 nlohmann::json::array_t redfishCollection;
1185 const auto& fanRedfish =
1186 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1187 for (const std::string& item : *collection)
1188 {
Ed Tanous8a592812022-06-04 09:06:59 -07001189 sdbusplus::message::object_path itemPath(item);
1190 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001191 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001192 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001193 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001194 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001195 /*
1196 todo(ed): merge patch that fixes the names
1197 std::replace(itemName.begin(),
1198 itemName.end(), '_', ' ');*/
1199 auto schemaItem =
1200 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1201 [itemName](const nlohmann::json& fan) {
1202 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001203 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001204 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001205 {
Ed Tanous8a592812022-06-04 09:06:59 -07001206 nlohmann::json::object_t collectionId;
1207 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001208 (*schemaItem)["@odata.id"];
1209 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001210 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001211 }
1212 else
1213 {
1214 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1215 messages::internalError(
1216 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001217 return;
1218 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001219 }
James Feist8bd25cc2019-03-15 15:14:00 -07001220
Ed Tanous002d39b2022-05-31 08:59:27 -07001221 size_t minNumNeeded =
1222 collection->empty()
1223 ? 0
1224 : collection->size() - *allowedFailures;
1225 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1226 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001227
Ed Tanous002d39b2022-05-31 08:59:27 -07001228 nlohmann::json::object_t redundancy;
1229 redundancy["@odata.id"] =
1230 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1231 "/" + sensorsAsyncResp->chassisSubNode +
1232 "#/Redundancy/" + std::to_string(jResp.size());
1233 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1234 redundancy["MinNumNeeded"] = minNumNeeded;
1235 redundancy["MemberId"] = name;
1236 redundancy["Mode"] = "N+m";
1237 redundancy["Name"] = name;
1238 redundancy["RedundancySet"] = redfishCollection;
1239 redundancy["Status"]["Health"] = health;
1240 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001241
Ed Tanous002d39b2022-05-31 08:59:27 -07001242 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001243 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001244 });
1245 }
James Feist8bd25cc2019-03-15 15:14:00 -07001246 },
1247 "xyz.openbmc_project.ObjectMapper",
1248 "/xyz/openbmc_project/object_mapper",
1249 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1250 "/xyz/openbmc_project/control", 2,
1251 std::array<const char*, 1>{
1252 "xyz.openbmc_project.Control.FanRedundancy"});
1253}
1254
Ed Tanousb5a76932020-09-29 16:16:58 -07001255inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001256 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001257{
zhanghch058d1b46d2021-04-01 11:18:24 +08001258 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001259 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001260 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001261 {
1262 sensorHeaders = {"Voltages", "PowerSupplies"};
1263 }
1264 for (const std::string& sensorGroup : sensorHeaders)
1265 {
1266 nlohmann::json::iterator entry = response.find(sensorGroup);
1267 if (entry != response.end())
1268 {
1269 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001270 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001271 return c1["Name"] < c2["Name"];
1272 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001273
1274 // add the index counts to the end of each entry
1275 size_t count = 0;
1276 for (nlohmann::json& sensorJson : *entry)
1277 {
1278 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1279 if (odata == sensorJson.end())
1280 {
1281 continue;
1282 }
1283 std::string* value = odata->get_ptr<std::string*>();
1284 if (value != nullptr)
1285 {
1286 *value += std::to_string(count);
1287 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001288 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001289 }
1290 }
1291 }
1292 }
1293}
1294
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001295/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001296 * @brief Finds the inventory item with the specified object path.
1297 * @param inventoryItems D-Bus inventory items associated with sensors.
1298 * @param invItemObjPath D-Bus object path of inventory item.
1299 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001300 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001301inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001302 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001303 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001304{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001305 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001306 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001307 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001308 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001309 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001310 }
1311 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001312 return nullptr;
1313}
1314
1315/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001316 * @brief Finds the inventory item associated with the specified sensor.
1317 * @param inventoryItems D-Bus inventory items associated with sensors.
1318 * @param sensorObjPath D-Bus object path of sensor.
1319 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001320 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001321inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001322 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001323 const std::string& sensorObjPath)
1324{
1325 for (InventoryItem& inventoryItem : *inventoryItems)
1326 {
1327 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1328 {
1329 return &inventoryItem;
1330 }
1331 }
1332 return nullptr;
1333}
1334
1335/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001336 * @brief Finds the inventory item associated with the specified led path.
1337 * @param inventoryItems D-Bus inventory items associated with sensors.
1338 * @param ledObjPath D-Bus object path of led.
1339 * @return Inventory item within vector, or nullptr if no match found.
1340 */
1341inline InventoryItem*
1342 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1343 const std::string& ledObjPath)
1344{
1345 for (InventoryItem& inventoryItem : inventoryItems)
1346 {
1347 if (inventoryItem.ledObjectPath == ledObjPath)
1348 {
1349 return &inventoryItem;
1350 }
1351 }
1352 return nullptr;
1353}
1354
1355/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001356 * @brief Adds inventory item and associated sensor to specified vector.
1357 *
1358 * Adds a new InventoryItem to the vector if necessary. Searches for an
1359 * existing InventoryItem with the specified object path. If not found, one is
1360 * added to the vector.
1361 *
1362 * Next, the specified sensor is added to the set of sensors associated with the
1363 * InventoryItem.
1364 *
1365 * @param inventoryItems D-Bus inventory items associated with sensors.
1366 * @param invItemObjPath D-Bus object path of inventory item.
1367 * @param sensorObjPath D-Bus object path of sensor
1368 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001369inline void addInventoryItem(
1370 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1371 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001372{
1373 // Look for inventory item in vector
1374 InventoryItem* inventoryItem =
1375 findInventoryItem(inventoryItems, invItemObjPath);
1376
1377 // If inventory item doesn't exist in vector, add it
1378 if (inventoryItem == nullptr)
1379 {
1380 inventoryItems->emplace_back(invItemObjPath);
1381 inventoryItem = &(inventoryItems->back());
1382 }
1383
1384 // Add sensor to set of sensors associated with inventory item
1385 inventoryItem->sensors.emplace(sensorObjPath);
1386}
1387
1388/**
1389 * @brief Stores D-Bus data in the specified inventory item.
1390 *
1391 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1392 * specified InventoryItem.
1393 *
1394 * This data is later used to provide sensor property values in the JSON
1395 * response.
1396 *
1397 * @param inventoryItem Inventory item where data will be stored.
1398 * @param interfacesDict Map containing D-Bus interfaces and their properties
1399 * for the specified inventory item.
1400 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001401inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001402 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001403 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001404{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001405 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001406
Ed Tanous9eb808c2022-01-25 10:19:23 -08001407 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001408 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001409 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001410 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001411 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001412 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001413 if (name == "Present")
1414 {
1415 const bool* value = std::get_if<bool>(&dbusValue);
1416 if (value != nullptr)
1417 {
1418 inventoryItem.isPresent = *value;
1419 }
1420 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421 }
1422 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001423 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001424
Ed Tanous711ac7a2021-12-20 09:34:41 -08001425 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001427 inventoryItem.isPowerSupply = true;
1428 }
1429
1430 // Get properties from Inventory.Decorator.Asset interface
1431 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1432 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001433 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001434 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001435 if (name == "Manufacturer")
1436 {
1437 const std::string* value =
1438 std::get_if<std::string>(&dbusValue);
1439 if (value != nullptr)
1440 {
1441 inventoryItem.manufacturer = *value;
1442 }
1443 }
1444 if (name == "Model")
1445 {
1446 const std::string* value =
1447 std::get_if<std::string>(&dbusValue);
1448 if (value != nullptr)
1449 {
1450 inventoryItem.model = *value;
1451 }
1452 }
1453 if (name == "SerialNumber")
1454 {
1455 const std::string* value =
1456 std::get_if<std::string>(&dbusValue);
1457 if (value != nullptr)
1458 {
1459 inventoryItem.serialNumber = *value;
1460 }
1461 }
1462 if (name == "PartNumber")
1463 {
1464 const std::string* value =
1465 std::get_if<std::string>(&dbusValue);
1466 if (value != nullptr)
1467 {
1468 inventoryItem.partNumber = *value;
1469 }
1470 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001471 }
1472 }
1473
Ed Tanous711ac7a2021-12-20 09:34:41 -08001474 if (interface ==
1475 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001476 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001477 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001478 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001479 if (name == "Functional")
1480 {
1481 const bool* value = std::get_if<bool>(&dbusValue);
1482 if (value != nullptr)
1483 {
1484 inventoryItem.isFunctional = *value;
1485 }
1486 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001487 }
1488 }
1489 }
1490}
1491
1492/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001493 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001494 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001495 * Uses the specified connections (services) to obtain D-Bus data for inventory
1496 * items associated with sensors. Stores the resulting data in the
1497 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001499 * This data is later used to provide sensor property values in the JSON
1500 * response.
1501 *
1502 * Finds the inventory item data asynchronously. Invokes callback when data has
1503 * been obtained.
1504 *
1505 * The callback must have the following signature:
1506 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001507 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001508 * @endcode
1509 *
1510 * This function is called recursively, obtaining data asynchronously from one
1511 * connection in each call. This ensures the callback is not invoked until the
1512 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001513 *
1514 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001515 * @param inventoryItems D-Bus inventory items associated with sensors.
1516 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001517 * @param objectMgrPaths Mappings from connection name to DBus object path that
1518 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001519 * @param callback Callback to invoke when inventory data has been obtained.
1520 * @param invConnectionsIndex Current index in invConnections. Only specified
1521 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001522 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001523template <typename Callback>
1524static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001525 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001526 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001527 std::shared_ptr<std::set<std::string>> invConnections,
1528 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001529 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001530{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001531 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001532
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001533 // If no more connections left, call callback
1534 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001535 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001536 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001537 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1538 return;
1539 }
1540
1541 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001542 auto it = invConnections->begin();
1543 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001544 if (it != invConnections->end())
1545 {
1546 const std::string& invConnection = *it;
1547
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001548 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001549 auto respHandler =
1550 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
Ed Tanous02cad962022-06-30 16:50:15 -07001551 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1552 const boost::system::error_code ec,
1553 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001554 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 if (ec)
1556 {
1557 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001558 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001559 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 return;
1561 }
1562
1563 // Loop through returned object paths
1564 for (const auto& objDictEntry : resp)
1565 {
1566 const std::string& objPath =
1567 static_cast<const std::string&>(objDictEntry.first);
1568
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001569 // If this object path is one of the specified inventory items
1570 InventoryItem* inventoryItem =
1571 findInventoryItem(inventoryItems, objPath);
1572 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001573 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001574 // Store inventory data in InventoryItem
1575 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001576 }
1577 }
1578
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579 // Recurse to get inventory item data from next connection
1580 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1581 invConnections, objectMgrPaths,
1582 std::move(callback), invConnectionsIndex + 1);
1583
1584 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001585 };
1586
1587 // Find DBus object path that implements ObjectManager for the current
1588 // connection. If no mapping found, default to "/".
1589 auto iter = objectMgrPaths->find(invConnection);
1590 const std::string& objectMgrPath =
1591 (iter != objectMgrPaths->end()) ? iter->second : "/";
1592 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1593 << objectMgrPath;
1594
1595 // Get all object paths and their interfaces for current connection
1596 crow::connections::systemBus->async_method_call(
1597 std::move(respHandler), invConnection, objectMgrPath,
1598 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1599 }
1600
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001601 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001602}
1603
1604/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001605 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001606 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001607 * Gets the D-Bus connections (services) that provide data for the inventory
1608 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001609 *
1610 * Finds the connections asynchronously. Invokes callback when information has
1611 * been obtained.
1612 *
1613 * The callback must have the following signature:
1614 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001615 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001616 * @endcode
1617 *
1618 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001619 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001620 * @param callback Callback to invoke when connections have been obtained.
1621 */
1622template <typename Callback>
1623static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001624 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1625 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626 Callback&& callback)
1627{
1628 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1629
1630 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001631 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001632 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001633 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1634 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001635 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1636
1637 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001638 auto respHandler =
1639 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1640 inventoryItems](
1641 const boost::system::error_code ec,
1642 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001643 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1644 if (ec)
1645 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001646 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001647 BMCWEB_LOG_ERROR
1648 << "getInventoryItemsConnections respHandler DBus error " << ec;
1649 return;
1650 }
1651
1652 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001653 std::shared_ptr<std::set<std::string>> invConnections =
1654 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001655
1656 // Loop through objects from GetSubTree
1657 for (const std::pair<
1658 std::string,
1659 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1660 object : subtree)
1661 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001662 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001663 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001664 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001665 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001666 // Store all connections to inventory item
1667 for (const std::pair<std::string, std::vector<std::string>>&
1668 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001669 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001670 const std::string& invConnection = objData.first;
1671 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001672 }
1673 }
1674 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001675
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001676 callback(invConnections);
1677 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1678 };
1679
1680 // Make call to ObjectMapper to find all inventory items
1681 crow::connections::systemBus->async_method_call(
1682 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1683 "/xyz/openbmc_project/object_mapper",
1684 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1685 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1686}
1687
1688/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001689 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001690 *
1691 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001692 * inventory items. Then finds the associations from those inventory items to
1693 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001694 *
1695 * Finds the inventory items asynchronously. Invokes callback when information
1696 * has been obtained.
1697 *
1698 * The callback must have the following signature:
1699 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001700 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001701 * @endcode
1702 *
1703 * @param sensorsAsyncResp Pointer to object holding response data.
1704 * @param sensorNames All sensors within the current chassis.
1705 * @param objectMgrPaths Mappings from connection name to DBus object path that
1706 * implements ObjectManager.
1707 * @param callback Callback to invoke when inventory items have been obtained.
1708 */
1709template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001710static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001711 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001712 const std::shared_ptr<std::set<std::string>>& sensorNames,
1713 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001714 Callback&& callback)
1715{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001716 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001717
1718 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001719 auto respHandler =
1720 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1721 sensorNames](const boost::system::error_code ec,
1722 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001723 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001724 if (ec)
1725 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001726 BMCWEB_LOG_ERROR
1727 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001728 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001729 return;
1730 }
1731
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001732 // Create vector to hold list of inventory items
1733 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1734 std::make_shared<std::vector<InventoryItem>>();
1735
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001736 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001737 std::string sensorAssocPath;
1738 sensorAssocPath.reserve(128); // avoid memory allocations
1739 for (const auto& objDictEntry : resp)
1740 {
1741 const std::string& objPath =
1742 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001743
1744 // If path is inventory association for one of the specified sensors
1745 for (const std::string& sensorName : *sensorNames)
1746 {
1747 sensorAssocPath = sensorName;
1748 sensorAssocPath += "/inventory";
1749 if (objPath == sensorAssocPath)
1750 {
1751 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001752 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001753 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001754 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001755 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001756 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001757 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001758 if (valueName == "endpoints")
1759 {
1760 const std::vector<std::string>* endpoints =
1761 std::get_if<std::vector<std::string>>(
1762 &value);
1763 if ((endpoints != nullptr) &&
1764 !endpoints->empty())
1765 {
1766 // Add inventory item to vector
1767 const std::string& invItemPath =
1768 endpoints->front();
1769 addInventoryItem(inventoryItems,
1770 invItemPath,
1771 sensorName);
1772 }
1773 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001774 }
1775 }
1776 }
1777 break;
1778 }
1779 }
1780 }
1781
Anthony Wilsond5005492019-07-31 16:34:17 -05001782 // Now loop through the returned object paths again, this time to
1783 // find the leds associated with the inventory items we just found
1784 std::string inventoryAssocPath;
1785 inventoryAssocPath.reserve(128); // avoid memory allocations
1786 for (const auto& objDictEntry : resp)
1787 {
1788 const std::string& objPath =
1789 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001790
1791 for (InventoryItem& inventoryItem : *inventoryItems)
1792 {
1793 inventoryAssocPath = inventoryItem.objectPath;
1794 inventoryAssocPath += "/leds";
1795 if (objPath == inventoryAssocPath)
1796 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001797 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001798 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001799 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001800 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001801 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001802 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001803 if (valueName == "endpoints")
1804 {
1805 const std::vector<std::string>* endpoints =
1806 std::get_if<std::vector<std::string>>(
1807 &value);
1808 if ((endpoints != nullptr) &&
1809 !endpoints->empty())
1810 {
1811 // Add inventory item to vector
1812 // Store LED path in inventory item
1813 const std::string& ledPath =
1814 endpoints->front();
1815 inventoryItem.ledObjectPath = ledPath;
1816 }
1817 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001818 }
1819 }
1820 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001821
Anthony Wilsond5005492019-07-31 16:34:17 -05001822 break;
1823 }
1824 }
1825 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001826 callback(inventoryItems);
1827 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001828 };
1829
1830 // Find DBus object path that implements ObjectManager for ObjectMapper
1831 std::string connection = "xyz.openbmc_project.ObjectMapper";
1832 auto iter = objectMgrPaths->find(connection);
1833 const std::string& objectMgrPath =
1834 (iter != objectMgrPaths->end()) ? iter->second : "/";
1835 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1836 << objectMgrPath;
1837
1838 // Call GetManagedObjects on the ObjectMapper to get all associations
1839 crow::connections::systemBus->async_method_call(
1840 std::move(respHandler), connection, objectMgrPath,
1841 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1842
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001843 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001844}
1845
1846/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001847 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1848 *
1849 * Uses the specified connections (services) to obtain D-Bus data for inventory
1850 * item leds associated with sensors. Stores the resulting data in the
1851 * inventoryItems vector.
1852 *
1853 * This data is later used to provide sensor property values in the JSON
1854 * response.
1855 *
1856 * Finds the inventory item led data asynchronously. Invokes callback when data
1857 * has been obtained.
1858 *
1859 * The callback must have the following signature:
1860 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001861 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001862 * @endcode
1863 *
1864 * This function is called recursively, obtaining data asynchronously from one
1865 * connection in each call. This ensures the callback is not invoked until the
1866 * last asynchronous function has completed.
1867 *
1868 * @param sensorsAsyncResp Pointer to object holding response data.
1869 * @param inventoryItems D-Bus inventory items associated with sensors.
1870 * @param ledConnections Connections that provide data for the inventory leds.
1871 * @param callback Callback to invoke when inventory data has been obtained.
1872 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1873 * in recursive calls to this function.
1874 */
1875template <typename Callback>
1876void getInventoryLedData(
1877 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1878 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001879 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001880 Callback&& callback, size_t ledConnectionsIndex = 0)
1881{
1882 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1883
1884 // If no more connections left, call callback
1885 if (ledConnectionsIndex >= ledConnections->size())
1886 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001887 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001888 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1889 return;
1890 }
1891
1892 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001893 auto it = ledConnections->begin();
1894 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001895 if (it != ledConnections->end())
1896 {
1897 const std::string& ledPath = (*it).first;
1898 const std::string& ledConnection = (*it).second;
1899 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001900 auto respHandler =
1901 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001902 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001903 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001904 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1905 if (ec)
1906 {
1907 BMCWEB_LOG_ERROR
1908 << "getInventoryLedData respHandler DBus error " << ec;
1909 messages::internalError(sensorsAsyncResp->asyncResp->res);
1910 return;
1911 }
1912
1913 BMCWEB_LOG_DEBUG << "Led state: " << state;
1914 // Find inventory item with this LED object path
1915 InventoryItem* inventoryItem =
1916 findInventoryItemForLed(*inventoryItems, ledPath);
1917 if (inventoryItem != nullptr)
1918 {
1919 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001920 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001921 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001923 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001924 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001925 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001926 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001927 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001928 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001929 {
1930 inventoryItem->ledState = LedState::OFF;
1931 }
1932 else
1933 {
1934 inventoryItem->ledState = LedState::UNKNOWN;
1935 }
1936 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001937
Ed Tanous002d39b2022-05-31 08:59:27 -07001938 // Recurse to get LED data from next connection
1939 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1940 ledConnections, std::move(callback),
1941 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001942
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1944 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001945
1946 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001947 sdbusplus::asio::getProperty<std::string>(
1948 *crow::connections::systemBus, ledConnection, ledPath,
1949 "xyz.openbmc_project.Led.Physical", "State",
1950 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001951 }
1952
1953 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1954}
1955
1956/**
1957 * @brief Gets LED data for LEDs associated with given inventory items.
1958 *
1959 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1960 * associated with the specified inventory items. Then gets the LED data from
1961 * each connection and stores it in the inventory item.
1962 *
1963 * This data is later used to provide sensor property values in the JSON
1964 * response.
1965 *
1966 * Finds the LED data asynchronously. Invokes callback when information has
1967 * been obtained.
1968 *
1969 * The callback must have the following signature:
1970 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001971 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001972 * @endcode
1973 *
1974 * @param sensorsAsyncResp Pointer to object holding response data.
1975 * @param inventoryItems D-Bus inventory items associated with sensors.
1976 * @param callback Callback to invoke when inventory items have been obtained.
1977 */
1978template <typename Callback>
1979void getInventoryLeds(
1980 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1981 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1982 Callback&& callback)
1983{
1984 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1985
1986 const std::string path = "/xyz/openbmc_project";
1987 const std::array<std::string, 1> interfaces = {
1988 "xyz.openbmc_project.Led.Physical"};
1989
1990 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 auto respHandler =
1992 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1993 inventoryItems](
1994 const boost::system::error_code ec,
1995 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001996 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1997 if (ec)
1998 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001999 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002000 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2001 << ec;
2002 return;
2003 }
2004
2005 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00002006 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
2007 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05002008
2009 // Loop through objects from GetSubTree
2010 for (const std::pair<
2011 std::string,
2012 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2013 object : subtree)
2014 {
2015 // Check if object path is LED for one of the specified inventory
2016 // items
2017 const std::string& ledPath = object.first;
2018 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2019 {
2020 // Add mapping from ledPath to connection
2021 const std::string& connection = object.second.begin()->first;
2022 (*ledConnections)[ledPath] = connection;
2023 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2024 << connection;
2025 }
2026 }
2027
2028 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2029 std::move(callback));
2030 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2031 };
2032 // Make call to ObjectMapper to find all inventory items
2033 crow::connections::systemBus->async_method_call(
2034 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2035 "/xyz/openbmc_project/object_mapper",
2036 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2037 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2038}
2039
2040/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002041 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2042 *
2043 * Uses the specified connections (services) (currently assumes just one) to
2044 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2045 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2046 *
2047 * This data is later used to provide sensor property values in the JSON
2048 * response.
2049 *
2050 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2051 * when data has been obtained.
2052 *
2053 * The callback must have the following signature:
2054 * @code
2055 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2056 * @endcode
2057 *
2058 * @param sensorsAsyncResp Pointer to object holding response data.
2059 * @param inventoryItems D-Bus inventory items associated with sensors.
2060 * @param psAttributesConnections Connections that provide data for the Power
2061 * Supply Attributes
2062 * @param callback Callback to invoke when data has been obtained.
2063 */
2064template <typename Callback>
2065void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002066 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002067 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002068 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002069 Callback&& callback)
2070{
2071 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2072
2073 if (psAttributesConnections.empty())
2074 {
2075 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2076 callback(inventoryItems);
2077 return;
2078 }
2079
2080 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002081 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002082
2083 const std::string& psAttributesPath = (*it).first;
2084 const std::string& psAttributesConnection = (*it).second;
2085
2086 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002087 auto respHandler =
2088 [sensorsAsyncResp, inventoryItems,
2089 callback{std::forward<Callback>(callback)}](
2090 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002091 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2092 if (ec)
2093 {
2094 BMCWEB_LOG_ERROR
2095 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002096 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002097 return;
2098 }
2099
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002100 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2101 // Store value in Power Supply Inventory Items
2102 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002103 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002104 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002105 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002106 inventoryItem.powerSupplyEfficiencyPercent =
2107 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002108 }
2109 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002110
2111 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2112 callback(inventoryItems);
2113 };
2114
2115 // Get the DeratingFactor property for the PowerSupplyAttributes
2116 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002117 sdbusplus::asio::getProperty<uint32_t>(
2118 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2119 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2120 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002121
2122 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2123}
2124
2125/**
2126 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2127 *
2128 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2129 * data. Then gets the Power Supply Attributes data from the connection
2130 * (currently just assumes 1 connection) and stores the data in the inventory
2131 * item.
2132 *
2133 * This data is later used to provide sensor property values in the JSON
2134 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2135 *
2136 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2137 * when information has been obtained.
2138 *
2139 * The callback must have the following signature:
2140 * @code
2141 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2142 * @endcode
2143 *
2144 * @param sensorsAsyncResp Pointer to object holding response data.
2145 * @param inventoryItems D-Bus inventory items associated with sensors.
2146 * @param callback Callback to invoke when data has been obtained.
2147 */
2148template <typename Callback>
2149void getPowerSupplyAttributes(
2150 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2151 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2152 Callback&& callback)
2153{
2154 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2155
2156 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002157 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002158 {
2159 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2160 callback(inventoryItems);
2161 return;
2162 }
2163
2164 const std::array<std::string, 1> interfaces = {
2165 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2166
2167 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002168 auto respHandler =
2169 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2170 inventoryItems](
2171 const boost::system::error_code ec,
2172 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2174 if (ec)
2175 {
2176 messages::internalError(sensorsAsyncResp->asyncResp->res);
2177 BMCWEB_LOG_ERROR
2178 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2179 return;
2180 }
2181 if (subtree.empty())
2182 {
2183 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2184 callback(inventoryItems);
2185 return;
2186 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002187
Ed Tanous002d39b2022-05-31 08:59:27 -07002188 // Currently we only support 1 power supply attribute, use this for
2189 // all the power supplies. Build map of object path to connection.
2190 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002191 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002192
Ed Tanous002d39b2022-05-31 08:59:27 -07002193 if (subtree[0].first.empty() || subtree[0].second.empty())
2194 {
2195 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2196 callback(inventoryItems);
2197 return;
2198 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002199
Ed Tanous002d39b2022-05-31 08:59:27 -07002200 const std::string& psAttributesPath = subtree[0].first;
2201 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002202
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 if (connection.empty())
2204 {
2205 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2206 callback(inventoryItems);
2207 return;
2208 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002209
Ed Tanous002d39b2022-05-31 08:59:27 -07002210 psAttributesConnections[psAttributesPath] = connection;
2211 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2212 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002213
Ed Tanous002d39b2022-05-31 08:59:27 -07002214 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2215 psAttributesConnections,
2216 std::move(callback));
2217 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2218 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002219 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2220 crow::connections::systemBus->async_method_call(
2221 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2222 "/xyz/openbmc_project/object_mapper",
2223 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2224 "/xyz/openbmc_project", 0, interfaces);
2225 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2226}
2227
2228/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002229 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002230 *
2231 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002232 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002233 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002234 * This data is later used to provide sensor property values in the JSON
2235 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002236 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002237 * Finds the inventory items asynchronously. Invokes callback when the
2238 * inventory items have been obtained.
2239 *
2240 * The callback must have the following signature:
2241 * @code
2242 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2243 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002244 *
2245 * @param sensorsAsyncResp Pointer to object holding response data.
2246 * @param sensorNames All sensors within the current chassis.
2247 * @param objectMgrPaths Mappings from connection name to DBus object path that
2248 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002249 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002250 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002251template <typename Callback>
2252static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002253 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002254 const std::shared_ptr<std::set<std::string>> sensorNames,
2255 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002257{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002258 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2259 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002260 [sensorsAsyncResp, objectMgrPaths,
2261 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002262 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002263 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2264 auto getInventoryItemsConnectionsCb =
2265 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2266 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002267 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002268 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2269 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2270 callback{std::move(callback)}]() {
2271 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002272
Ed Tanous002d39b2022-05-31 08:59:27 -07002273 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2274 callback{std::move(callback)}]() {
2275 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2276 // Find Power Supply Attributes and get the data
2277 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2278 std::move(callback));
2279 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002280 };
2281
Ed Tanous002d39b2022-05-31 08:59:27 -07002282 // Find led connections and get the data
2283 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2284 std::move(getInventoryLedsCb));
2285 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2286 };
2287
2288 // Get inventory item data from connections
2289 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2290 invConnections, objectMgrPaths,
2291 std::move(getInventoryItemsDataCb));
2292 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002293 };
2294
Ed Tanous002d39b2022-05-31 08:59:27 -07002295 // Get connections that provide inventory item data
2296 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2297 std::move(getInventoryItemsConnectionsCb));
2298 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2299 };
2300
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002301 // Get associations from sensors to inventory items
2302 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2303 std::move(getInventoryItemAssociationsCb));
2304 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2305}
2306
2307/**
2308 * @brief Returns JSON PowerSupply object for the specified inventory item.
2309 *
2310 * Searches for a JSON PowerSupply object that matches the specified inventory
2311 * item. If one is not found, a new PowerSupply object is added to the JSON
2312 * array.
2313 *
2314 * Multiple sensors are often associated with one power supply inventory item.
2315 * As a result, multiple sensor values are stored in one JSON PowerSupply
2316 * object.
2317 *
2318 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2319 * @param inventoryItem Inventory item for the power supply.
2320 * @param chassisId Chassis that contains the power supply.
2321 * @return JSON PowerSupply object for the specified inventory item.
2322 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002323inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002324 const InventoryItem& inventoryItem,
2325 const std::string& chassisId)
2326{
2327 // Check if matching PowerSupply object already exists in JSON array
2328 for (nlohmann::json& powerSupply : powerSupplyArray)
2329 {
2330 if (powerSupply["MemberId"] == inventoryItem.name)
2331 {
2332 return powerSupply;
2333 }
2334 }
2335
2336 // Add new PowerSupply object to JSON array
2337 powerSupplyArray.push_back({});
2338 nlohmann::json& powerSupply = powerSupplyArray.back();
2339 powerSupply["@odata.id"] =
2340 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2341 powerSupply["MemberId"] = inventoryItem.name;
2342 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2343 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2344 powerSupply["Model"] = inventoryItem.model;
2345 powerSupply["PartNumber"] = inventoryItem.partNumber;
2346 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002347 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002348
Gunnar Mills42cbe532019-08-15 15:26:54 -05002349 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2350 {
2351 powerSupply["EfficiencyPercent"] =
2352 inventoryItem.powerSupplyEfficiencyPercent;
2353 }
2354
2355 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002356 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2357 powerSupply["Status"]["Health"] = health;
2358
2359 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002360}
2361
2362/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002363 * @brief Gets the values of the specified sensors.
2364 *
2365 * Stores the results as JSON in the SensorsAsyncResp.
2366 *
2367 * Gets the sensor values asynchronously. Stores the results later when the
2368 * information has been obtained.
2369 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002370 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002371 *
2372 * To minimize the number of DBus calls, the DBus method
2373 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2374 * values of all sensors provided by a connection (service).
2375 *
2376 * The connections set contains all the connections that provide sensor values.
2377 *
2378 * The objectMgrPaths map contains mappings from a connection name to the
2379 * corresponding DBus object path that implements ObjectManager.
2380 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002381 * The InventoryItem vector contains D-Bus inventory items associated with the
2382 * sensors. Inventory item data is needed for some Redfish sensor properties.
2383 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002384 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002385 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002386 * @param connections Connections that provide sensor values.
2387 * @param objectMgrPaths Mappings from connection name to DBus object path that
2388 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002389 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002390 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002391inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002392 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002393 const std::shared_ptr<std::set<std::string>>& sensorNames,
2394 const std::set<std::string>& connections,
2395 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002396 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002397{
2398 BMCWEB_LOG_DEBUG << "getSensorData enter";
2399 // Get managed objects from all services exposing sensors
2400 for (const std::string& connection : connections)
2401 {
2402 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002403 auto getManagedObjectsCb =
2404 [sensorsAsyncResp, sensorNames,
2405 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002406 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002407 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2408 if (ec)
2409 {
2410 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002411 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002412 return;
2413 }
2414 // Go through all objects and update response with sensor data
2415 for (const auto& objDictEntry : resp)
2416 {
2417 const std::string& objPath =
2418 static_cast<const std::string&>(objDictEntry.first);
2419 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2420 << objPath;
2421
Shawn McCarneyde629b62019-03-08 10:42:51 -06002422 std::vector<std::string> split;
2423 // Reserve space for
2424 // /xyz/openbmc_project/sensors/<name>/<subname>
2425 split.reserve(6);
2426 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2427 if (split.size() < 6)
2428 {
2429 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2430 << objPath;
2431 continue;
2432 }
2433 // These indexes aren't intuitive, as boost::split puts an empty
2434 // string at the beginning
2435 const std::string& sensorType = split[4];
2436 const std::string& sensorName = split[5];
2437 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2438 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002439 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002440 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002441 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002442 continue;
2443 }
2444
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002445 // Find inventory item (if any) associated with sensor
2446 InventoryItem* inventoryItem =
2447 findInventoryItemForSensor(inventoryItems, objPath);
2448
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002449 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002450 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002451
2452 nlohmann::json* sensorJson = nullptr;
2453
Nan Zhou928fefb2022-03-28 08:45:00 -07002454 if (sensorSchema == sensors::node::sensors &&
2455 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002456 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002457 std::string sensorTypeEscaped(sensorType);
2458 sensorTypeEscaped.erase(
2459 std::remove(sensorTypeEscaped.begin(),
2460 sensorTypeEscaped.end(), '_'),
2461 sensorTypeEscaped.end());
2462 std::string sensorId(sensorTypeEscaped);
2463 sensorId += "_";
2464 sensorId += sensorName;
2465
zhanghch058d1b46d2021-04-01 11:18:24 +08002466 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002467 crow::utility::urlFromPieces(
2468 "redfish", "v1", "Chassis",
2469 sensorsAsyncResp->chassisId,
2470 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002471 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002472 }
2473 else
2474 {
Ed Tanous271584a2019-07-09 16:24:22 -07002475 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002476 if (sensorsAsyncResp->efficientExpand)
2477 {
2478 fieldName = "Members";
2479 }
2480 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002481 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002482 fieldName = "Temperatures";
2483 }
2484 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2485 sensorType == "fan_pwm")
2486 {
2487 fieldName = "Fans";
2488 }
2489 else if (sensorType == "voltage")
2490 {
2491 fieldName = "Voltages";
2492 }
2493 else if (sensorType == "power")
2494 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002495 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002496 {
2497 fieldName = "PowerControl";
2498 }
2499 else if ((inventoryItem != nullptr) &&
2500 (inventoryItem->isPowerSupply))
2501 {
2502 fieldName = "PowerSupplies";
2503 }
2504 else
2505 {
2506 // Other power sensors are in SensorCollection
2507 continue;
2508 }
2509 }
2510 else
2511 {
2512 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2513 << sensorType;
2514 continue;
2515 }
2516
2517 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002518 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002519 if (fieldName == "PowerControl")
2520 {
2521 if (tempArray.empty())
2522 {
2523 // Put multiple "sensors" into a single
2524 // PowerControl. Follows MemberId naming and
2525 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002526 nlohmann::json::object_t power;
2527 power["@odata.id"] =
2528 "/redfish/v1/Chassis/" +
2529 sensorsAsyncResp->chassisId + "/" +
2530 sensorsAsyncResp->chassisSubNode + "#/" +
2531 fieldName + "/0";
2532 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002533 }
2534 sensorJson = &(tempArray.back());
2535 }
2536 else if (fieldName == "PowerSupplies")
2537 {
2538 if (inventoryItem != nullptr)
2539 {
2540 sensorJson =
2541 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002542 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002543 }
2544 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002545 else if (fieldName == "Members")
2546 {
Ed Tanous14766872022-03-15 10:44:42 -07002547 nlohmann::json::object_t member;
2548 member["@odata.id"] =
2549 "/redfish/v1/Chassis/" +
2550 sensorsAsyncResp->chassisId + "/" +
2551 sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2552 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002553 sensorJson = &(tempArray.back());
2554 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002555 else
2556 {
Ed Tanous14766872022-03-15 10:44:42 -07002557 nlohmann::json::object_t member;
2558 member["@odata.id"] = "/redfish/v1/Chassis/" +
2559 sensorsAsyncResp->chassisId +
2560 "/" +
2561 sensorsAsyncResp->chassisSubNode +
2562 "#/" + fieldName + "/";
2563 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002564 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002565 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002566 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002567
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002568 if (sensorJson != nullptr)
2569 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002570 objectInterfacesToJson(sensorName, sensorType,
2571 sensorsAsyncResp->chassisSubNode,
2572 objDictEntry.second, *sensorJson,
2573 inventoryItem);
2574
2575 std::string path = "/xyz/openbmc_project/sensors/";
2576 path += sensorType;
2577 path += "/";
2578 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002579 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002580 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002581 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002582 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002583 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002584 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002585 if (sensorsAsyncResp->chassisSubNode ==
2586 sensors::node::sensors &&
2587 sensorsAsyncResp->efficientExpand)
2588 {
2589 sensorsAsyncResp->asyncResp->res
2590 .jsonValue["Members@odata.count"] =
2591 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2592 .size();
2593 }
2594 else if (sensorsAsyncResp->chassisSubNode ==
2595 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002596 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002597 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002598 }
James Feist8bd25cc2019-03-15 15:14:00 -07002599 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002600 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2601 };
2602
2603 // Find DBus object path that implements ObjectManager for the current
2604 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002605 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002606 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002607 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002608 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2609 << objectMgrPath;
2610
2611 crow::connections::systemBus->async_method_call(
2612 getManagedObjectsCb, connection, objectMgrPath,
2613 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002614 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002615 BMCWEB_LOG_DEBUG << "getSensorData exit";
2616}
2617
Nan Zhoufe04d492022-06-22 17:10:41 +00002618inline void
2619 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2620 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002621{
Nan Zhoufe04d492022-06-22 17:10:41 +00002622 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2623 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002624 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2625 auto getObjectManagerPathsCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002626 [sensorsAsyncResp, sensorNames, connections](
2627 const std::shared_ptr<std::map<std::string, std::string>>&
2628 objectMgrPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002629 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2630 auto getInventoryItemsCb =
2631 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2632 const std::shared_ptr<std::vector<InventoryItem>>&
2633 inventoryItems) {
2634 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2635 // Get sensor data and store results in JSON
2636 getSensorData(sensorsAsyncResp, sensorNames, connections,
2637 objectMgrPaths, inventoryItems);
2638 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2639 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002640
Ed Tanous002d39b2022-05-31 08:59:27 -07002641 // Get inventory items associated with sensors
2642 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2643 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002644
Ed Tanous002d39b2022-05-31 08:59:27 -07002645 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002646 };
2647
Ed Tanous002d39b2022-05-31 08:59:27 -07002648 // Get mapping from connection names to the DBus object
2649 // paths that implement the ObjectManager interface
2650 getObjectManagerPaths(sensorsAsyncResp,
2651 std::move(getObjectManagerPathsCb));
2652 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2653 };
2654
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002655 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002656 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002657}
2658
Shawn McCarneyde629b62019-03-08 10:42:51 -06002659/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002660 * @brief Entry point for retrieving sensors data related to requested
2661 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002662 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002663 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002664inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002665 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002666{
2667 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002668 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002669 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002670 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002671 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2672 processSensorList(sensorsAsyncResp, sensorNames);
2673 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2674 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002675 // SensorCollection doesn't contain the Redundancy property
2676 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2677 {
2678 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2679 nlohmann::json::array();
2680 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002681 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002682 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2683 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2684 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002685 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002686}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002687
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302688/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002689 * @brief Find the requested sensorName in the list of all sensors supplied by
2690 * the chassis node
2691 *
2692 * @param sensorName The sensor name supplied in the PATCH request
2693 * @param sensorsList The list of sensors managed by the chassis node
2694 * @param sensorsModified The list of sensors that were found as a result of
2695 * repeated calls to this function
2696 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002697inline bool
2698 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002699 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002700 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002701{
Nan Zhoufe04d492022-06-22 17:10:41 +00002702 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002703 {
George Liu28aa8de2021-02-01 15:13:30 +08002704 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002705 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002706 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002707 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002708 continue;
2709 }
2710 if (thisSensorName == sensorName)
2711 {
2712 sensorsModified.emplace(chassisSensor);
2713 return true;
2714 }
2715 }
2716 return false;
2717}
2718
2719/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302720 * @brief Entry point for overriding sensor values of given sensor
2721 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002722 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002723 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002724 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302725 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002726inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002727 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002728 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002729 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302730{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002731 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002732 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302733
Ed Tanous543f4402022-01-06 13:12:53 -08002734 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302735 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302736 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002737 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302738 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302739 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302740 if (collectionItems.first == "Temperatures")
2741 {
2742 propertyValueName = "ReadingCelsius";
2743 }
2744 else if (collectionItems.first == "Fans")
2745 {
2746 propertyValueName = "Reading";
2747 }
2748 else
2749 {
2750 propertyValueName = "ReadingVolts";
2751 }
2752 for (auto& item : collectionItems.second)
2753 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002754 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2755 "MemberId", memberId, propertyValueName,
2756 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302757 {
2758 return;
2759 }
2760 overrideMap.emplace(memberId,
2761 std::make_pair(value, collectionItems.first));
2762 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302763 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002764
Ed Tanous002d39b2022-05-31 08:59:27 -07002765 auto getChassisSensorListCb =
2766 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002767 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002768 // Match sensor names in the PATCH request to those managed by the
2769 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002770 const std::shared_ptr<std::set<std::string>> sensorNames =
2771 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302772 for (const auto& item : overrideMap)
2773 {
2774 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002775 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2776 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302777 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302778 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002779 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302780 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302781 return;
2782 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302783 }
2784 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002785 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002786 [sensorAsyncResp,
2787 overrideMap](const std::set<std::string>& /*connections*/,
2788 const std::set<std::pair<std::string, std::string>>&
2789 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002790 if (objectsWithConnection.size() != overrideMap.size())
2791 {
2792 BMCWEB_LOG_INFO
2793 << "Unable to find all objects with proper connection "
2794 << objectsWithConnection.size() << " requested "
2795 << overrideMap.size() << "\n";
2796 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2797 sensorAsyncResp->chassisSubNode ==
2798 sensors::node::thermal
2799 ? "Temperatures"
2800 : "Voltages",
2801 "Count");
2802 return;
2803 }
2804 for (const auto& item : objectsWithConnection)
2805 {
2806 sdbusplus::message::object_path path(item.first);
2807 std::string sensorName = path.filename();
2808 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302809 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002810 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302811 return;
2812 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302813
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002814 const auto& iterator = overrideMap.find(sensorName);
2815 if (iterator == overrideMap.end())
2816 {
2817 BMCWEB_LOG_INFO << "Unable to find sensor object"
2818 << item.first << "\n";
2819 messages::internalError(sensorAsyncResp->asyncResp->res);
2820 return;
2821 }
2822 crow::connections::systemBus->async_method_call(
2823 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002824 if (ec)
2825 {
2826 if (ec.value() ==
2827 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002828 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002829 BMCWEB_LOG_WARNING
2830 << "Manufacturing mode is not Enabled...can't "
2831 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002832
Ed Tanous002d39b2022-05-31 08:59:27 -07002833 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002834 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002835 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002836 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002837 BMCWEB_LOG_DEBUG
2838 << "setOverrideValueStatus DBUS error: " << ec;
2839 messages::internalError(
2840 sensorAsyncResp->asyncResp->res);
2841 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002842 },
2843 item.second, item.first, "org.freedesktop.DBus.Properties",
2844 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002845 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002846 }
2847 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302848 // Get object with connection for the given sensor name
2849 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2850 std::move(getObjectsWithConnectionCb));
2851 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302852 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002853 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2854 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2855 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302856}
2857
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002858/**
2859 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2860 * path of the sensor.
2861 *
2862 * Function builds valid Redfish response for sensor query of given chassis and
2863 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2864 * it to caller in a callback.
2865 *
2866 * @param chassis Chassis for which retrieval should be performed
2867 * @param node Node (group) of sensors. See sensors::node for supported values
2868 * @param mapComplete Callback to be called with retrieval result
2869 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002870inline void retrieveUriToDbusMap(const std::string& chassis,
2871 const std::string& node,
2872 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002873{
Ed Tanous02da7c52022-02-27 00:09:02 -08002874 decltype(sensors::paths)::const_iterator pathIt =
2875 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2876 [&node](auto&& val) { return val.first == node; });
2877 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002878 {
2879 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2880 mapComplete(boost::beast::http::status::bad_request, {});
2881 return;
2882 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002883
Nan Zhou72374eb2022-01-27 17:06:51 -08002884 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002885 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2886 const boost::beast::http::status status,
2887 const std::map<std::string, std::string>& uriToDbus) {
2888 mapCompleteCb(status, uriToDbus);
2889 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002890
2891 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002892 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002893 getChassisData(resp);
2894}
2895
Nan Zhoubacb2162022-04-06 11:28:32 -07002896namespace sensors
2897{
Nan Zhou928fefb2022-03-28 08:45:00 -07002898
Nan Zhoubacb2162022-04-06 11:28:32 -07002899inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002900 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2901 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002902 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002903{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002904 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002905
Ed Tanousc1d019a2022-08-06 09:36:06 -07002906 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2907 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002908 {
2909 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2910
2911 sdbusplus::message::object_path path(sensor);
2912 std::string sensorName = path.filename();
2913 if (sensorName.empty())
2914 {
2915 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002916 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002917 return;
2918 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002919 std::string type = path.parent_path().filename();
2920 // fan_tach has an underscore in it, so remove it to "normalize" the
2921 // type in the URI
2922 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2923
Ed Tanous14766872022-03-15 10:44:42 -07002924 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002925 std::string id = type;
2926 id += "_";
2927 id += sensorName;
2928 member["@odata.id"] = crow::utility::urlFromPieces(
2929 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2930
Ed Tanous14766872022-03-15 10:44:42 -07002931 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002932 }
2933
Ed Tanousc1d019a2022-08-06 09:36:06 -07002934 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002935 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2936}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002937
Nan Zhoude167a62022-06-01 04:47:45 +00002938inline void
2939 handleSensorCollectionGet(App& app, const crow::Request& req,
2940 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2941 const std::string& chassisId)
2942{
2943 query_param::QueryCapabilities capabilities = {
2944 .canDelegateExpandLevel = 1,
2945 };
2946 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002947 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002948 delegatedQuery, capabilities))
2949 {
2950 return;
2951 }
2952
2953 if (delegatedQuery.expandType != query_param::ExpandType::None)
2954 {
2955 // we perform efficient expand.
2956 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2957 aResp, chassisId, sensors::dbus::sensorPaths,
2958 sensors::node::sensors,
2959 /*efficientExpand=*/true);
2960 getChassisData(asyncResp);
2961
2962 BMCWEB_LOG_DEBUG
2963 << "SensorCollection doGet exit via efficient expand handler";
2964 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002965 }
Nan Zhoude167a62022-06-01 04:47:45 +00002966
Nan Zhoude167a62022-06-01 04:47:45 +00002967 // We get all sensors as hyperlinkes in the chassis (this
2968 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002969 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2970 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2971 sensors::node::sensors));
2972}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002973
Ed Tanousc1d019a2022-08-06 09:36:06 -07002974inline void
2975 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2976 const std::string& sensorPath,
2977 const ::dbus::utility::MapperGetObject& mapperResponse)
2978{
2979 if (mapperResponse.size() != 1)
2980 {
2981 messages::internalError(asyncResp->res);
2982 return;
2983 }
2984 const auto& valueIface = *mapperResponse.begin();
2985 const std::string& connectionName = valueIface.first;
2986 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2987 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002988
2989 sdbusplus::asio::getAllProperties(
2990 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002991 [asyncResp,
2992 sensorPath](const boost::system::error_code ec,
2993 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2994 if (ec)
2995 {
2996 messages::internalError(asyncResp->res);
2997 return;
2998 }
2999 sdbusplus::message::object_path path(sensorPath);
3000 std::string name = path.filename();
3001 path = path.parent_path();
3002 std::string type = path.filename();
3003 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
3004 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02003005 });
Nan Zhoude167a62022-06-01 04:47:45 +00003006}
3007
Nan Zhoue6bd8462022-06-01 04:35:35 +00003008inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07003009 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3010 const std::string& /*chassisId*/,
3011 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00003012{
Ed Tanousc1d019a2022-08-06 09:36:06 -07003013 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00003014 {
3015 return;
3016 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07003017 size_t index = sensorId.find('_');
3018 if (index == std::string::npos)
3019 {
3020 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
3021 return;
3022 }
3023 std::string sensorType = sensorId.substr(0, index);
3024 std::string sensorName = sensorId.substr(index + 1);
3025 // fan_pwm and fan_tach need special handling
3026 if (sensorType == "fantach" || sensorType == "fanpwm")
3027 {
3028 sensorType.insert(3, 1, '_');
3029 }
3030
Nan Zhoue6bd8462022-06-01 04:35:35 +00003031 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00003032
3033 const std::array<const char*, 1> interfaces = {
3034 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc1d019a2022-08-06 09:36:06 -07003035 std::string sensorPath =
3036 "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName;
Nan Zhoue6bd8462022-06-01 04:35:35 +00003037 // Get a list of all of the sensors that implement Sensor.Value
3038 // and get the path and service name associated with the sensor
3039 crow::connections::systemBus->async_method_call(
Ed Tanousc1d019a2022-08-06 09:36:06 -07003040 [asyncResp, sensorPath,
Nan Zhoue6bd8462022-06-01 04:35:35 +00003041 sensorName](const boost::system::error_code ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07003042 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00003043 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3044 if (ec)
3045 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07003046 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003047 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3048 << "Dbus error " << ec;
3049 return;
3050 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07003051 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003052 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3053 },
3054 "xyz.openbmc_project.ObjectMapper",
3055 "/xyz/openbmc_project/object_mapper",
Ed Tanousc1d019a2022-08-06 09:36:06 -07003056 "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
3057 interfaces);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003058}
3059
Nan Zhoubacb2162022-04-06 11:28:32 -07003060} // namespace sensors
3061
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003062inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003063{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003064 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003065 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003066 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003067 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003068}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003069
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003070inline void requestRoutesSensor(App& app)
3071{
3072 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003073 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003074 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003075 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003076}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003077
Ed Tanous1abe55e2018-09-05 08:30:59 -07003078} // namespace redfish