blob: d0485f0136de2a5a34e7f7a235724d6a83837cce [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
716 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800717
Ed Tanous1d7c0052022-08-09 12:32:26 -0700718 for (const auto& [valueName, value] : valuesDict)
James Feist34dd1792019-05-17 14:10:54 -0700719 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700720 if (valueName == "CriticalAlarmHigh" || valueName == "CriticalAlarmLow")
James Feist34dd1792019-05-17 14:10:54 -0700721 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700722 const bool* asserted = std::get_if<bool>(&value);
723 if (asserted == nullptr)
James Feist34dd1792019-05-17 14:10:54 -0700724 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700725 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
726 }
727 else if (*asserted)
728 {
729 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700730 }
731 }
732 }
733
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500734 // Check if associated inventory item is not functional
735 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
736 {
737 return "Critical";
738 }
739
740 // If current health in JSON object is already Warning, return that. This
741 // should override the sensor status, which might be less severe.
742 if (currentHealth == "Warning")
743 {
744 return "Warning";
745 }
746
747 // Check if sensor has warning threshold alarm
Ed Tanous1d7c0052022-08-09 12:32:26 -0700748 for (const auto& [valueName, value] : valuesDict)
James Feist34dd1792019-05-17 14:10:54 -0700749 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700750 if (valueName == "WarningAlarmHigh" || valueName == "WarningAlarmLow")
James Feist34dd1792019-05-17 14:10:54 -0700751 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700752 const bool* asserted = std::get_if<bool>(&value);
753 if (asserted == nullptr)
James Feist34dd1792019-05-17 14:10:54 -0700754 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700755 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
756 }
757 else if (*asserted)
758 {
759 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700760 }
761 }
762 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500763
James Feist34dd1792019-05-17 14:10:54 -0700764 return "OK";
765}
766
Ed Tanous23a21a12020-07-25 04:45:05 +0000767inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500768 const InventoryItem* inventoryItem)
769{
770 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
771 {
772 switch (inventoryItem->ledState)
773 {
774 case LedState::OFF:
775 sensorJson["IndicatorLED"] = "Off";
776 break;
777 case LedState::ON:
778 sensorJson["IndicatorLED"] = "Lit";
779 break;
780 case LedState::BLINK:
781 sensorJson["IndicatorLED"] = "Blinking";
782 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000783 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500784 break;
785 }
786 }
787}
788
James Feist34dd1792019-05-17 14:10:54 -0700789/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100790 * @brief Builds a json sensor representation of a sensor.
791 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500792 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100793 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700794 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
795 * @param propertiesDict A dictionary of the properties to build the sensor
796 * from.
797 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500798 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
799 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100800 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700801inline void objectPropertiesToJson(
802 std::string_view sensorName, std::string_view sensorType,
803 std::string_view chassisSubNode,
804 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000805 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700806{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700807 // Assume values exist as is (10^0 == 1) if no scale exists
808 int64_t scaleMultiplier = 0;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700809 for (const auto& [valueName, value] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700810 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700811 if (valueName == "Scale")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700812 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700813 const int64_t* int64Value = std::get_if<int64_t>(&value);
814 if (int64Value != nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800815 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700816 scaleMultiplier = *int64Value;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800817 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100818 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100819 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700820
Ed Tanous1d7c0052022-08-09 12:32:26 -0700821 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500822 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700823 std::string subNodeEscaped(chassisSubNode);
824 subNodeEscaped.erase(
825 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
826 subNodeEscaped.end());
827
828 // For sensors in SensorCollection we set Id instead of MemberId,
829 // including power sensors.
830 subNodeEscaped += '_';
831 subNodeEscaped += sensorName;
832 sensorJson["Id"] = std::move(subNodeEscaped);
833
Ed Tanous1d7c0052022-08-09 12:32:26 -0700834 std::string sensorNameEs(sensorName);
835 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
836 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500837 }
838 else if (sensorType != "power")
839 {
840 // Set MemberId and Name for non-power sensors. For PowerSupplies and
841 // PowerControl, those properties have more general values because
842 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000843 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700844 std::string sensorNameEs(sensorName);
845 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
846 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500847 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700848
Ed Tanous81ce6092020-12-17 16:54:55 +0000849 sensorJson["Status"]["State"] = getState(inventoryItem);
850 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700851 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852
853 // Parameter to set to override the type we get from dbus, and force it to
854 // int, regardless of what is available. This is used for schemas like fan,
855 // that require integers, not floats.
856 bool forceToInt = false;
857
Anthony Wilson3929aca2019-07-19 15:42:33 -0500858 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700859 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500860 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000861 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000862
Ed Tanous1d7c0052022-08-09 12:32:26 -0700863 std::string_view readingType = sensors::toReadingType(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000864 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500865 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000866 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
867 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500868 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000869 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500870 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000871 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500872 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000873
Ed Tanous1d7c0052022-08-09 12:32:26 -0700874 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000875 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200876 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000877 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
878 << sensorType;
879 }
880 else
881 {
882 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200883 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500884 }
885 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700886 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500887 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000888 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700889 // TODO(ed) Documentation says that path should be type fan_tach,
890 // implementation seems to implement fan
891 }
892 else if (sensorType == "fan" || sensorType == "fan_tach")
893 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500894 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000895 sensorJson["ReadingUnits"] = "RPM";
896 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
897 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700898 forceToInt = true;
899 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700900 else if (sensorType == "fan_pwm")
901 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500902 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000903 sensorJson["ReadingUnits"] = "Percent";
904 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
905 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700906 forceToInt = true;
907 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700908 else if (sensorType == "voltage")
909 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500910 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000911 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700912 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700913 else if (sensorType == "power")
914 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700915 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000916 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000917 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500918 // Put multiple "sensors" into a single PowerControl, so have
919 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000920 sensorJson["MemberId"] = "0";
921 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500922 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000923 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700924 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700925 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500926 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700927 }
928 else
929 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500930 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700931 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700932 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700933 else
934 {
935 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
936 return;
937 }
938 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500939 std::vector<
940 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
941 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 properties.reserve(7);
943
944 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600945
Ed Tanous1d7c0052022-08-09 12:32:26 -0700946 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500947 {
948 properties.emplace_back(
949 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
950 "/Thresholds/UpperCaution/Reading"_json_pointer);
951 properties.emplace_back(
952 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
953 "/Thresholds/LowerCaution/Reading"_json_pointer);
954 properties.emplace_back(
955 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
956 "/Thresholds/UpperCritical/Reading"_json_pointer);
957 properties.emplace_back(
958 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
959 "/Thresholds/LowerCritical/Reading"_json_pointer);
960 }
961 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600962 {
963 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500964 "WarningHigh",
965 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600966 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500967 "WarningLow",
968 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600969 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500970 "CriticalHigh",
971 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600972 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500973 "CriticalLow",
974 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600975 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700976
Ed Tanous2474adf2018-09-05 16:31:16 -0700977 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
978
Ed Tanous1d7c0052022-08-09 12:32:26 -0700979 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500980 {
981 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500982 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500983 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500984 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500985 }
986 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 {
988 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500989 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500991 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700992 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500993 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700994 {
995 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500996 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700997 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500998 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700999 }
1000
Anthony Wilson3929aca2019-07-19 15:42:33 -05001001 for (const std::tuple<const char*, const char*,
1002 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001003 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07001004 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001005 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07001006 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001007 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001008 continue;
1009 }
Ed Tanous1d7c0052022-08-09 12:32:26 -07001010
1011 // The property we want to set may be nested json, so use
1012 // a json_pointer for easy indexing into the json structure.
1013 const nlohmann::json::json_pointer& key = std::get<2>(p);
1014
1015 // Attempt to pull the int64 directly
1016 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
1017
1018 const double* doubleValue = std::get_if<double>(&valueVariant);
1019 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
1020 double temp = 0.0;
1021 if (int64Value != nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001022 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07001023 temp = static_cast<double>(*int64Value);
1024 }
1025 else if (doubleValue != nullptr)
1026 {
1027 temp = *doubleValue;
1028 }
1029 else if (uValue != nullptr)
1030 {
1031 temp = *uValue;
1032 }
1033 else
1034 {
1035 BMCWEB_LOG_ERROR
1036 << "Got value interface that wasn't int or double";
1037 continue;
1038 }
1039 temp = temp * std::pow(10, scaleMultiplier);
1040 if (forceToInt)
1041 {
1042 sensorJson[key] = static_cast<int64_t>(temp);
1043 }
1044 else
1045 {
1046 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001047 }
1048 }
1049 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001050}
1051
Ed Tanous1d7c0052022-08-09 12:32:26 -07001052/**
1053 * @brief Builds a json sensor representation of a sensor.
1054 * @param sensorName The name of the sensor to be built
1055 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
1056 * build
1057 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
1058 * @param interfacesDict A dictionary of the interfaces and properties of said
1059 * interfaces to be built from
1060 * @param sensorJson The json object to fill
1061 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1062 * be nullptr if no associated inventory item was found.
1063 */
1064inline void objectInterfacesToJson(
1065 const std::string& sensorName, const std::string& sensorType,
1066 const std::string& chassisSubNode,
1067 const dbus::utility::DBusInteracesMap& interfacesDict,
1068 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1069{
1070
1071 for (const auto& [interface, valuesDict] : interfacesDict)
1072 {
1073 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1074 valuesDict, sensorJson, inventoryItem);
1075 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07001076 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -07001077}
1078
Ed Tanousb5a76932020-09-29 16:16:58 -07001079inline void populateFanRedundancy(
1080 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001081{
1082 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001083 [sensorsAsyncResp](
1084 const boost::system::error_code ec,
1085 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001086 if (ec)
1087 {
1088 return; // don't have to have this interface
1089 }
1090 for (const std::pair<
1091 std::string,
1092 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1093 pathPair : resp)
1094 {
1095 const std::string& path = pathPair.first;
1096 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1097 objDict = pathPair.second;
1098 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001099 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001100 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001101 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001102
1103 const std::string& owner = objDict.begin()->first;
1104 sdbusplus::asio::getProperty<std::vector<std::string>>(
1105 *crow::connections::systemBus,
1106 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1107 "xyz.openbmc_project.Association", "endpoints",
1108 [path, owner,
1109 sensorsAsyncResp](const boost::system::error_code e,
1110 const std::vector<std::string>& endpoints) {
1111 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001112 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 return; // if they don't have an association we
1114 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001115 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001116 auto found =
1117 std::find_if(endpoints.begin(), endpoints.end(),
1118 [sensorsAsyncResp](const std::string& entry) {
1119 return entry.find(sensorsAsyncResp->chassisId) !=
1120 std::string::npos;
1121 });
James Feist8bd25cc2019-03-15 15:14:00 -07001122
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 if (found == endpoints.end())
1124 {
1125 return;
1126 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001127 sdbusplus::asio::getAllProperties(
1128 *crow::connections::systemBus, owner, path,
1129 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001130 [path, sensorsAsyncResp](
1131 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001132 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001133 if (err)
1134 {
1135 return; // don't have to have this
1136 // interface
1137 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001138
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001139 const uint8_t* allowedFailures = nullptr;
1140 const std::vector<std::string>* collection = nullptr;
1141 const std::string* status = nullptr;
1142
1143 const bool success = sdbusplus::unpackPropertiesNoThrow(
1144 dbus_utils::UnpackErrorPrinter(), ret,
1145 "AllowedFailures", allowedFailures, "Collection",
1146 collection, "Status", status);
1147
1148 if (!success)
1149 {
1150 messages::internalError(
1151 sensorsAsyncResp->asyncResp->res);
1152 return;
1153 }
1154
1155 if (allowedFailures == nullptr || collection == nullptr ||
1156 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001157 {
1158 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1159 messages::internalError(
1160 sensorsAsyncResp->asyncResp->res);
1161 return;
1162 }
1163
Ed Tanous002d39b2022-05-31 08:59:27 -07001164 sdbusplus::message::object_path objectPath(path);
1165 std::string name = objectPath.filename();
1166 if (name.empty())
1167 {
1168 // this should be impossible
1169 messages::internalError(
1170 sensorsAsyncResp->asyncResp->res);
1171 return;
1172 }
1173 std::replace(name.begin(), name.end(), '_', ' ');
1174
1175 std::string health;
1176
Ed Tanous11ba3972022-07-11 09:50:41 -07001177 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001178 {
1179 health = "OK";
1180 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001181 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001182 {
1183 health = "Warning";
1184 }
1185 else
1186 {
1187 health = "Critical";
1188 }
1189 nlohmann::json::array_t redfishCollection;
1190 const auto& fanRedfish =
1191 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1192 for (const std::string& item : *collection)
1193 {
Ed Tanous8a592812022-06-04 09:06:59 -07001194 sdbusplus::message::object_path itemPath(item);
1195 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001196 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001197 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001198 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001199 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001200 /*
1201 todo(ed): merge patch that fixes the names
1202 std::replace(itemName.begin(),
1203 itemName.end(), '_', ' ');*/
1204 auto schemaItem =
1205 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1206 [itemName](const nlohmann::json& fan) {
1207 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001208 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001209 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001210 {
Ed Tanous8a592812022-06-04 09:06:59 -07001211 nlohmann::json::object_t collectionId;
1212 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001213 (*schemaItem)["@odata.id"];
1214 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001215 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001216 }
1217 else
1218 {
1219 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1220 messages::internalError(
1221 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001222 return;
1223 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001224 }
James Feist8bd25cc2019-03-15 15:14:00 -07001225
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 size_t minNumNeeded =
1227 collection->empty()
1228 ? 0
1229 : collection->size() - *allowedFailures;
1230 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1231 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001232
Ed Tanous002d39b2022-05-31 08:59:27 -07001233 nlohmann::json::object_t redundancy;
1234 redundancy["@odata.id"] =
1235 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1236 "/" + sensorsAsyncResp->chassisSubNode +
1237 "#/Redundancy/" + std::to_string(jResp.size());
1238 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1239 redundancy["MinNumNeeded"] = minNumNeeded;
1240 redundancy["MemberId"] = name;
1241 redundancy["Mode"] = "N+m";
1242 redundancy["Name"] = name;
1243 redundancy["RedundancySet"] = redfishCollection;
1244 redundancy["Status"]["Health"] = health;
1245 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001246
Ed Tanous002d39b2022-05-31 08:59:27 -07001247 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001248 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001249 });
1250 }
James Feist8bd25cc2019-03-15 15:14:00 -07001251 },
1252 "xyz.openbmc_project.ObjectMapper",
1253 "/xyz/openbmc_project/object_mapper",
1254 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1255 "/xyz/openbmc_project/control", 2,
1256 std::array<const char*, 1>{
1257 "xyz.openbmc_project.Control.FanRedundancy"});
1258}
1259
Ed Tanousb5a76932020-09-29 16:16:58 -07001260inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001261 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001262{
zhanghch058d1b46d2021-04-01 11:18:24 +08001263 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001264 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001265 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001266 {
1267 sensorHeaders = {"Voltages", "PowerSupplies"};
1268 }
1269 for (const std::string& sensorGroup : sensorHeaders)
1270 {
1271 nlohmann::json::iterator entry = response.find(sensorGroup);
1272 if (entry != response.end())
1273 {
1274 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001275 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001276 return c1["Name"] < c2["Name"];
1277 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001278
1279 // add the index counts to the end of each entry
1280 size_t count = 0;
1281 for (nlohmann::json& sensorJson : *entry)
1282 {
1283 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1284 if (odata == sensorJson.end())
1285 {
1286 continue;
1287 }
1288 std::string* value = odata->get_ptr<std::string*>();
1289 if (value != nullptr)
1290 {
1291 *value += std::to_string(count);
1292 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001293 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001294 }
1295 }
1296 }
1297 }
1298}
1299
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001300/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001301 * @brief Finds the inventory item with the specified object path.
1302 * @param inventoryItems D-Bus inventory items associated with sensors.
1303 * @param invItemObjPath D-Bus object path of inventory item.
1304 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001305 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001306inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001307 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001308 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001309{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001310 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001311 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001312 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001313 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001314 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001315 }
1316 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001317 return nullptr;
1318}
1319
1320/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001321 * @brief Finds the inventory item associated with the specified sensor.
1322 * @param inventoryItems D-Bus inventory items associated with sensors.
1323 * @param sensorObjPath D-Bus object path of sensor.
1324 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001325 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001326inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001327 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001328 const std::string& sensorObjPath)
1329{
1330 for (InventoryItem& inventoryItem : *inventoryItems)
1331 {
1332 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1333 {
1334 return &inventoryItem;
1335 }
1336 }
1337 return nullptr;
1338}
1339
1340/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001341 * @brief Finds the inventory item associated with the specified led path.
1342 * @param inventoryItems D-Bus inventory items associated with sensors.
1343 * @param ledObjPath D-Bus object path of led.
1344 * @return Inventory item within vector, or nullptr if no match found.
1345 */
1346inline InventoryItem*
1347 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1348 const std::string& ledObjPath)
1349{
1350 for (InventoryItem& inventoryItem : inventoryItems)
1351 {
1352 if (inventoryItem.ledObjectPath == ledObjPath)
1353 {
1354 return &inventoryItem;
1355 }
1356 }
1357 return nullptr;
1358}
1359
1360/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001361 * @brief Adds inventory item and associated sensor to specified vector.
1362 *
1363 * Adds a new InventoryItem to the vector if necessary. Searches for an
1364 * existing InventoryItem with the specified object path. If not found, one is
1365 * added to the vector.
1366 *
1367 * Next, the specified sensor is added to the set of sensors associated with the
1368 * InventoryItem.
1369 *
1370 * @param inventoryItems D-Bus inventory items associated with sensors.
1371 * @param invItemObjPath D-Bus object path of inventory item.
1372 * @param sensorObjPath D-Bus object path of sensor
1373 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001374inline void addInventoryItem(
1375 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1376 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001377{
1378 // Look for inventory item in vector
1379 InventoryItem* inventoryItem =
1380 findInventoryItem(inventoryItems, invItemObjPath);
1381
1382 // If inventory item doesn't exist in vector, add it
1383 if (inventoryItem == nullptr)
1384 {
1385 inventoryItems->emplace_back(invItemObjPath);
1386 inventoryItem = &(inventoryItems->back());
1387 }
1388
1389 // Add sensor to set of sensors associated with inventory item
1390 inventoryItem->sensors.emplace(sensorObjPath);
1391}
1392
1393/**
1394 * @brief Stores D-Bus data in the specified inventory item.
1395 *
1396 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1397 * specified InventoryItem.
1398 *
1399 * This data is later used to provide sensor property values in the JSON
1400 * response.
1401 *
1402 * @param inventoryItem Inventory item where data will be stored.
1403 * @param interfacesDict Map containing D-Bus interfaces and their properties
1404 * for the specified inventory item.
1405 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001406inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001407 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001408 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001409{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001410 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001411
Ed Tanous9eb808c2022-01-25 10:19:23 -08001412 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001413 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001414 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001415 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001416 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001417 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001418 if (name == "Present")
1419 {
1420 const bool* value = std::get_if<bool>(&dbusValue);
1421 if (value != nullptr)
1422 {
1423 inventoryItem.isPresent = *value;
1424 }
1425 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426 }
1427 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001428 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001429
Ed Tanous711ac7a2021-12-20 09:34:41 -08001430 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001431 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001432 inventoryItem.isPowerSupply = true;
1433 }
1434
1435 // Get properties from Inventory.Decorator.Asset interface
1436 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1437 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001438 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001439 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001440 if (name == "Manufacturer")
1441 {
1442 const std::string* value =
1443 std::get_if<std::string>(&dbusValue);
1444 if (value != nullptr)
1445 {
1446 inventoryItem.manufacturer = *value;
1447 }
1448 }
1449 if (name == "Model")
1450 {
1451 const std::string* value =
1452 std::get_if<std::string>(&dbusValue);
1453 if (value != nullptr)
1454 {
1455 inventoryItem.model = *value;
1456 }
1457 }
1458 if (name == "SerialNumber")
1459 {
1460 const std::string* value =
1461 std::get_if<std::string>(&dbusValue);
1462 if (value != nullptr)
1463 {
1464 inventoryItem.serialNumber = *value;
1465 }
1466 }
1467 if (name == "PartNumber")
1468 {
1469 const std::string* value =
1470 std::get_if<std::string>(&dbusValue);
1471 if (value != nullptr)
1472 {
1473 inventoryItem.partNumber = *value;
1474 }
1475 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001476 }
1477 }
1478
Ed Tanous711ac7a2021-12-20 09:34:41 -08001479 if (interface ==
1480 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001481 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001482 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001484 if (name == "Functional")
1485 {
1486 const bool* value = std::get_if<bool>(&dbusValue);
1487 if (value != nullptr)
1488 {
1489 inventoryItem.isFunctional = *value;
1490 }
1491 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001492 }
1493 }
1494 }
1495}
1496
1497/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001498 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001499 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001500 * Uses the specified connections (services) to obtain D-Bus data for inventory
1501 * items associated with sensors. Stores the resulting data in the
1502 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001503 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001504 * This data is later used to provide sensor property values in the JSON
1505 * response.
1506 *
1507 * Finds the inventory item data asynchronously. Invokes callback when data has
1508 * been obtained.
1509 *
1510 * The callback must have the following signature:
1511 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001512 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001513 * @endcode
1514 *
1515 * This function is called recursively, obtaining data asynchronously from one
1516 * connection in each call. This ensures the callback is not invoked until the
1517 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001518 *
1519 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001520 * @param inventoryItems D-Bus inventory items associated with sensors.
1521 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001522 * @param objectMgrPaths Mappings from connection name to DBus object path that
1523 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001524 * @param callback Callback to invoke when inventory data has been obtained.
1525 * @param invConnectionsIndex Current index in invConnections. Only specified
1526 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001527 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001528template <typename Callback>
1529static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001530 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001531 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001532 std::shared_ptr<std::set<std::string>> invConnections,
1533 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001534 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001535{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001536 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001537
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001538 // If no more connections left, call callback
1539 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001540 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001541 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001542 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1543 return;
1544 }
1545
1546 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001547 auto it = invConnections->begin();
1548 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001549 if (it != invConnections->end())
1550 {
1551 const std::string& invConnection = *it;
1552
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001553 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001554 auto respHandler =
1555 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
Ed Tanous02cad962022-06-30 16:50:15 -07001556 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1557 const boost::system::error_code ec,
1558 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001559 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 if (ec)
1561 {
1562 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001563 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001564 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001565 return;
1566 }
1567
1568 // Loop through returned object paths
1569 for (const auto& objDictEntry : resp)
1570 {
1571 const std::string& objPath =
1572 static_cast<const std::string&>(objDictEntry.first);
1573
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001574 // If this object path is one of the specified inventory items
1575 InventoryItem* inventoryItem =
1576 findInventoryItem(inventoryItems, objPath);
1577 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579 // Store inventory data in InventoryItem
1580 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001581 }
1582 }
1583
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001584 // Recurse to get inventory item data from next connection
1585 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1586 invConnections, objectMgrPaths,
1587 std::move(callback), invConnectionsIndex + 1);
1588
1589 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 };
1591
1592 // Find DBus object path that implements ObjectManager for the current
1593 // connection. If no mapping found, default to "/".
1594 auto iter = objectMgrPaths->find(invConnection);
1595 const std::string& objectMgrPath =
1596 (iter != objectMgrPaths->end()) ? iter->second : "/";
1597 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1598 << objectMgrPath;
1599
1600 // Get all object paths and their interfaces for current connection
1601 crow::connections::systemBus->async_method_call(
1602 std::move(respHandler), invConnection, objectMgrPath,
1603 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1604 }
1605
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001607}
1608
1609/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001610 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001612 * Gets the D-Bus connections (services) that provide data for the inventory
1613 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001614 *
1615 * Finds the connections asynchronously. Invokes callback when information has
1616 * been obtained.
1617 *
1618 * The callback must have the following signature:
1619 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001620 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001621 * @endcode
1622 *
1623 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001624 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 * @param callback Callback to invoke when connections have been obtained.
1626 */
1627template <typename Callback>
1628static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001629 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1630 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631 Callback&& callback)
1632{
1633 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1634
1635 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001636 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001637 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001638 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1639 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001640 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1641
1642 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001643 auto respHandler =
1644 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1645 inventoryItems](
1646 const boost::system::error_code ec,
1647 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001648 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1649 if (ec)
1650 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001651 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001652 BMCWEB_LOG_ERROR
1653 << "getInventoryItemsConnections respHandler DBus error " << ec;
1654 return;
1655 }
1656
1657 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001658 std::shared_ptr<std::set<std::string>> invConnections =
1659 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001660
1661 // Loop through objects from GetSubTree
1662 for (const std::pair<
1663 std::string,
1664 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1665 object : subtree)
1666 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001667 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001668 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001669 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001670 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001671 // Store all connections to inventory item
1672 for (const std::pair<std::string, std::vector<std::string>>&
1673 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001674 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001675 const std::string& invConnection = objData.first;
1676 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001677 }
1678 }
1679 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001680
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001681 callback(invConnections);
1682 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1683 };
1684
1685 // Make call to ObjectMapper to find all inventory items
1686 crow::connections::systemBus->async_method_call(
1687 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1688 "/xyz/openbmc_project/object_mapper",
1689 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1690 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1691}
1692
1693/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001694 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001695 *
1696 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001697 * inventory items. Then finds the associations from those inventory items to
1698 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001699 *
1700 * Finds the inventory items asynchronously. Invokes callback when information
1701 * has been obtained.
1702 *
1703 * The callback must have the following signature:
1704 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001705 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001706 * @endcode
1707 *
1708 * @param sensorsAsyncResp Pointer to object holding response data.
1709 * @param sensorNames All sensors within the current chassis.
1710 * @param objectMgrPaths Mappings from connection name to DBus object path that
1711 * implements ObjectManager.
1712 * @param callback Callback to invoke when inventory items have been obtained.
1713 */
1714template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001715static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001716 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001717 const std::shared_ptr<std::set<std::string>>& sensorNames,
1718 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001719 Callback&& callback)
1720{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001721 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001722
1723 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001724 auto respHandler =
1725 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1726 sensorNames](const boost::system::error_code ec,
1727 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001728 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001729 if (ec)
1730 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001731 BMCWEB_LOG_ERROR
1732 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001733 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001734 return;
1735 }
1736
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001737 // Create vector to hold list of inventory items
1738 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1739 std::make_shared<std::vector<InventoryItem>>();
1740
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001741 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001742 std::string sensorAssocPath;
1743 sensorAssocPath.reserve(128); // avoid memory allocations
1744 for (const auto& objDictEntry : resp)
1745 {
1746 const std::string& objPath =
1747 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001748
1749 // If path is inventory association for one of the specified sensors
1750 for (const std::string& sensorName : *sensorNames)
1751 {
1752 sensorAssocPath = sensorName;
1753 sensorAssocPath += "/inventory";
1754 if (objPath == sensorAssocPath)
1755 {
1756 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001757 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001758 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001759 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001760 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001761 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001762 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001763 if (valueName == "endpoints")
1764 {
1765 const std::vector<std::string>* endpoints =
1766 std::get_if<std::vector<std::string>>(
1767 &value);
1768 if ((endpoints != nullptr) &&
1769 !endpoints->empty())
1770 {
1771 // Add inventory item to vector
1772 const std::string& invItemPath =
1773 endpoints->front();
1774 addInventoryItem(inventoryItems,
1775 invItemPath,
1776 sensorName);
1777 }
1778 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001779 }
1780 }
1781 }
1782 break;
1783 }
1784 }
1785 }
1786
Anthony Wilsond5005492019-07-31 16:34:17 -05001787 // Now loop through the returned object paths again, this time to
1788 // find the leds associated with the inventory items we just found
1789 std::string inventoryAssocPath;
1790 inventoryAssocPath.reserve(128); // avoid memory allocations
1791 for (const auto& objDictEntry : resp)
1792 {
1793 const std::string& objPath =
1794 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001795
1796 for (InventoryItem& inventoryItem : *inventoryItems)
1797 {
1798 inventoryAssocPath = inventoryItem.objectPath;
1799 inventoryAssocPath += "/leds";
1800 if (objPath == inventoryAssocPath)
1801 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001802 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001803 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001804 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001805 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001806 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001807 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001808 if (valueName == "endpoints")
1809 {
1810 const std::vector<std::string>* endpoints =
1811 std::get_if<std::vector<std::string>>(
1812 &value);
1813 if ((endpoints != nullptr) &&
1814 !endpoints->empty())
1815 {
1816 // Add inventory item to vector
1817 // Store LED path in inventory item
1818 const std::string& ledPath =
1819 endpoints->front();
1820 inventoryItem.ledObjectPath = ledPath;
1821 }
1822 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001823 }
1824 }
1825 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001826
Anthony Wilsond5005492019-07-31 16:34:17 -05001827 break;
1828 }
1829 }
1830 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001831 callback(inventoryItems);
1832 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001833 };
1834
1835 // Find DBus object path that implements ObjectManager for ObjectMapper
1836 std::string connection = "xyz.openbmc_project.ObjectMapper";
1837 auto iter = objectMgrPaths->find(connection);
1838 const std::string& objectMgrPath =
1839 (iter != objectMgrPaths->end()) ? iter->second : "/";
1840 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1841 << objectMgrPath;
1842
1843 // Call GetManagedObjects on the ObjectMapper to get all associations
1844 crow::connections::systemBus->async_method_call(
1845 std::move(respHandler), connection, objectMgrPath,
1846 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1847
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001848 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001849}
1850
1851/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001852 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1853 *
1854 * Uses the specified connections (services) to obtain D-Bus data for inventory
1855 * item leds associated with sensors. Stores the resulting data in the
1856 * inventoryItems vector.
1857 *
1858 * This data is later used to provide sensor property values in the JSON
1859 * response.
1860 *
1861 * Finds the inventory item led data asynchronously. Invokes callback when data
1862 * has been obtained.
1863 *
1864 * The callback must have the following signature:
1865 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001866 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001867 * @endcode
1868 *
1869 * This function is called recursively, obtaining data asynchronously from one
1870 * connection in each call. This ensures the callback is not invoked until the
1871 * last asynchronous function has completed.
1872 *
1873 * @param sensorsAsyncResp Pointer to object holding response data.
1874 * @param inventoryItems D-Bus inventory items associated with sensors.
1875 * @param ledConnections Connections that provide data for the inventory leds.
1876 * @param callback Callback to invoke when inventory data has been obtained.
1877 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1878 * in recursive calls to this function.
1879 */
1880template <typename Callback>
1881void getInventoryLedData(
1882 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1883 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001884 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001885 Callback&& callback, size_t ledConnectionsIndex = 0)
1886{
1887 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1888
1889 // If no more connections left, call callback
1890 if (ledConnectionsIndex >= ledConnections->size())
1891 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001892 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001893 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1894 return;
1895 }
1896
1897 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001898 auto it = ledConnections->begin();
1899 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001900 if (it != ledConnections->end())
1901 {
1902 const std::string& ledPath = (*it).first;
1903 const std::string& ledConnection = (*it).second;
1904 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001905 auto respHandler =
1906 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001907 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001908 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1910 if (ec)
1911 {
1912 BMCWEB_LOG_ERROR
1913 << "getInventoryLedData respHandler DBus error " << ec;
1914 messages::internalError(sensorsAsyncResp->asyncResp->res);
1915 return;
1916 }
1917
1918 BMCWEB_LOG_DEBUG << "Led state: " << state;
1919 // Find inventory item with this LED object path
1920 InventoryItem* inventoryItem =
1921 findInventoryItemForLed(*inventoryItems, ledPath);
1922 if (inventoryItem != nullptr)
1923 {
1924 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001925 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001926 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001927 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001928 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001929 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001930 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001931 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001932 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001933 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001934 {
1935 inventoryItem->ledState = LedState::OFF;
1936 }
1937 else
1938 {
1939 inventoryItem->ledState = LedState::UNKNOWN;
1940 }
1941 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001942
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 // Recurse to get LED data from next connection
1944 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1945 ledConnections, std::move(callback),
1946 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001947
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1949 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001950
1951 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001952 sdbusplus::asio::getProperty<std::string>(
1953 *crow::connections::systemBus, ledConnection, ledPath,
1954 "xyz.openbmc_project.Led.Physical", "State",
1955 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001956 }
1957
1958 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1959}
1960
1961/**
1962 * @brief Gets LED data for LEDs associated with given inventory items.
1963 *
1964 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1965 * associated with the specified inventory items. Then gets the LED data from
1966 * each connection and stores it in the inventory item.
1967 *
1968 * This data is later used to provide sensor property values in the JSON
1969 * response.
1970 *
1971 * Finds the LED data asynchronously. Invokes callback when information has
1972 * been obtained.
1973 *
1974 * The callback must have the following signature:
1975 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001976 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001977 * @endcode
1978 *
1979 * @param sensorsAsyncResp Pointer to object holding response data.
1980 * @param inventoryItems D-Bus inventory items associated with sensors.
1981 * @param callback Callback to invoke when inventory items have been obtained.
1982 */
1983template <typename Callback>
1984void getInventoryLeds(
1985 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1986 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1987 Callback&& callback)
1988{
1989 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1990
1991 const std::string path = "/xyz/openbmc_project";
1992 const std::array<std::string, 1> interfaces = {
1993 "xyz.openbmc_project.Led.Physical"};
1994
1995 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001996 auto respHandler =
1997 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1998 inventoryItems](
1999 const boost::system::error_code ec,
2000 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05002001 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2002 if (ec)
2003 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002004 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002005 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2006 << ec;
2007 return;
2008 }
2009
2010 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00002011 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
2012 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05002013
2014 // Loop through objects from GetSubTree
2015 for (const std::pair<
2016 std::string,
2017 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2018 object : subtree)
2019 {
2020 // Check if object path is LED for one of the specified inventory
2021 // items
2022 const std::string& ledPath = object.first;
2023 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2024 {
2025 // Add mapping from ledPath to connection
2026 const std::string& connection = object.second.begin()->first;
2027 (*ledConnections)[ledPath] = connection;
2028 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2029 << connection;
2030 }
2031 }
2032
2033 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2034 std::move(callback));
2035 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2036 };
2037 // Make call to ObjectMapper to find all inventory items
2038 crow::connections::systemBus->async_method_call(
2039 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2040 "/xyz/openbmc_project/object_mapper",
2041 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2042 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2043}
2044
2045/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002046 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2047 *
2048 * Uses the specified connections (services) (currently assumes just one) to
2049 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2050 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2051 *
2052 * This data is later used to provide sensor property values in the JSON
2053 * response.
2054 *
2055 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2056 * when data has been obtained.
2057 *
2058 * The callback must have the following signature:
2059 * @code
2060 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2061 * @endcode
2062 *
2063 * @param sensorsAsyncResp Pointer to object holding response data.
2064 * @param inventoryItems D-Bus inventory items associated with sensors.
2065 * @param psAttributesConnections Connections that provide data for the Power
2066 * Supply Attributes
2067 * @param callback Callback to invoke when data has been obtained.
2068 */
2069template <typename Callback>
2070void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002071 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002072 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002073 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002074 Callback&& callback)
2075{
2076 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2077
2078 if (psAttributesConnections.empty())
2079 {
2080 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2081 callback(inventoryItems);
2082 return;
2083 }
2084
2085 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002086 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002087
2088 const std::string& psAttributesPath = (*it).first;
2089 const std::string& psAttributesConnection = (*it).second;
2090
2091 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002092 auto respHandler =
2093 [sensorsAsyncResp, inventoryItems,
2094 callback{std::forward<Callback>(callback)}](
2095 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002096 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2097 if (ec)
2098 {
2099 BMCWEB_LOG_ERROR
2100 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002101 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002102 return;
2103 }
2104
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002105 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2106 // Store value in Power Supply Inventory Items
2107 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002108 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002109 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002110 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002111 inventoryItem.powerSupplyEfficiencyPercent =
2112 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002113 }
2114 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002115
2116 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2117 callback(inventoryItems);
2118 };
2119
2120 // Get the DeratingFactor property for the PowerSupplyAttributes
2121 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002122 sdbusplus::asio::getProperty<uint32_t>(
2123 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2124 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2125 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002126
2127 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2128}
2129
2130/**
2131 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2132 *
2133 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2134 * data. Then gets the Power Supply Attributes data from the connection
2135 * (currently just assumes 1 connection) and stores the data in the inventory
2136 * item.
2137 *
2138 * This data is later used to provide sensor property values in the JSON
2139 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2140 *
2141 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2142 * when information has been obtained.
2143 *
2144 * The callback must have the following signature:
2145 * @code
2146 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2147 * @endcode
2148 *
2149 * @param sensorsAsyncResp Pointer to object holding response data.
2150 * @param inventoryItems D-Bus inventory items associated with sensors.
2151 * @param callback Callback to invoke when data has been obtained.
2152 */
2153template <typename Callback>
2154void getPowerSupplyAttributes(
2155 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2156 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2157 Callback&& callback)
2158{
2159 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2160
2161 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002162 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002163 {
2164 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2165 callback(inventoryItems);
2166 return;
2167 }
2168
2169 const std::array<std::string, 1> interfaces = {
2170 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2171
2172 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002173 auto respHandler =
2174 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2175 inventoryItems](
2176 const boost::system::error_code ec,
2177 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002178 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2179 if (ec)
2180 {
2181 messages::internalError(sensorsAsyncResp->asyncResp->res);
2182 BMCWEB_LOG_ERROR
2183 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2184 return;
2185 }
2186 if (subtree.empty())
2187 {
2188 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2189 callback(inventoryItems);
2190 return;
2191 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002192
Ed Tanous002d39b2022-05-31 08:59:27 -07002193 // Currently we only support 1 power supply attribute, use this for
2194 // all the power supplies. Build map of object path to connection.
2195 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002196 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002197
Ed Tanous002d39b2022-05-31 08:59:27 -07002198 if (subtree[0].first.empty() || subtree[0].second.empty())
2199 {
2200 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2201 callback(inventoryItems);
2202 return;
2203 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002204
Ed Tanous002d39b2022-05-31 08:59:27 -07002205 const std::string& psAttributesPath = subtree[0].first;
2206 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002207
Ed Tanous002d39b2022-05-31 08:59:27 -07002208 if (connection.empty())
2209 {
2210 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2211 callback(inventoryItems);
2212 return;
2213 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002214
Ed Tanous002d39b2022-05-31 08:59:27 -07002215 psAttributesConnections[psAttributesPath] = connection;
2216 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2217 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002218
Ed Tanous002d39b2022-05-31 08:59:27 -07002219 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2220 psAttributesConnections,
2221 std::move(callback));
2222 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2223 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002224 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2225 crow::connections::systemBus->async_method_call(
2226 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2227 "/xyz/openbmc_project/object_mapper",
2228 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2229 "/xyz/openbmc_project", 0, interfaces);
2230 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2231}
2232
2233/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002234 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002235 *
2236 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002237 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002238 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002239 * This data is later used to provide sensor property values in the JSON
2240 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002241 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002242 * Finds the inventory items asynchronously. Invokes callback when the
2243 * inventory items have been obtained.
2244 *
2245 * The callback must have the following signature:
2246 * @code
2247 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2248 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002249 *
2250 * @param sensorsAsyncResp Pointer to object holding response data.
2251 * @param sensorNames All sensors within the current chassis.
2252 * @param objectMgrPaths Mappings from connection name to DBus object path that
2253 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002254 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002255 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256template <typename Callback>
2257static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002258 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002259 const std::shared_ptr<std::set<std::string>> sensorNames,
2260 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002261 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002262{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002263 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2264 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002265 [sensorsAsyncResp, objectMgrPaths,
2266 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002267 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002268 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2269 auto getInventoryItemsConnectionsCb =
2270 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2271 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002272 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002273 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2274 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2275 callback{std::move(callback)}]() {
2276 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002277
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2279 callback{std::move(callback)}]() {
2280 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2281 // Find Power Supply Attributes and get the data
2282 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2283 std::move(callback));
2284 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002285 };
2286
Ed Tanous002d39b2022-05-31 08:59:27 -07002287 // Find led connections and get the data
2288 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2289 std::move(getInventoryLedsCb));
2290 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2291 };
2292
2293 // Get inventory item data from connections
2294 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2295 invConnections, objectMgrPaths,
2296 std::move(getInventoryItemsDataCb));
2297 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002298 };
2299
Ed Tanous002d39b2022-05-31 08:59:27 -07002300 // Get connections that provide inventory item data
2301 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2302 std::move(getInventoryItemsConnectionsCb));
2303 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2304 };
2305
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002306 // Get associations from sensors to inventory items
2307 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2308 std::move(getInventoryItemAssociationsCb));
2309 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2310}
2311
2312/**
2313 * @brief Returns JSON PowerSupply object for the specified inventory item.
2314 *
2315 * Searches for a JSON PowerSupply object that matches the specified inventory
2316 * item. If one is not found, a new PowerSupply object is added to the JSON
2317 * array.
2318 *
2319 * Multiple sensors are often associated with one power supply inventory item.
2320 * As a result, multiple sensor values are stored in one JSON PowerSupply
2321 * object.
2322 *
2323 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2324 * @param inventoryItem Inventory item for the power supply.
2325 * @param chassisId Chassis that contains the power supply.
2326 * @return JSON PowerSupply object for the specified inventory item.
2327 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002328inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002329 const InventoryItem& inventoryItem,
2330 const std::string& chassisId)
2331{
2332 // Check if matching PowerSupply object already exists in JSON array
2333 for (nlohmann::json& powerSupply : powerSupplyArray)
2334 {
2335 if (powerSupply["MemberId"] == inventoryItem.name)
2336 {
2337 return powerSupply;
2338 }
2339 }
2340
2341 // Add new PowerSupply object to JSON array
2342 powerSupplyArray.push_back({});
2343 nlohmann::json& powerSupply = powerSupplyArray.back();
2344 powerSupply["@odata.id"] =
2345 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2346 powerSupply["MemberId"] = inventoryItem.name;
2347 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2348 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2349 powerSupply["Model"] = inventoryItem.model;
2350 powerSupply["PartNumber"] = inventoryItem.partNumber;
2351 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002352 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002353
Gunnar Mills42cbe532019-08-15 15:26:54 -05002354 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2355 {
2356 powerSupply["EfficiencyPercent"] =
2357 inventoryItem.powerSupplyEfficiencyPercent;
2358 }
2359
2360 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002361 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2362 powerSupply["Status"]["Health"] = health;
2363
2364 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002365}
2366
2367/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002368 * @brief Gets the values of the specified sensors.
2369 *
2370 * Stores the results as JSON in the SensorsAsyncResp.
2371 *
2372 * Gets the sensor values asynchronously. Stores the results later when the
2373 * information has been obtained.
2374 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002375 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002376 *
2377 * To minimize the number of DBus calls, the DBus method
2378 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2379 * values of all sensors provided by a connection (service).
2380 *
2381 * The connections set contains all the connections that provide sensor values.
2382 *
2383 * The objectMgrPaths map contains mappings from a connection name to the
2384 * corresponding DBus object path that implements ObjectManager.
2385 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002386 * The InventoryItem vector contains D-Bus inventory items associated with the
2387 * sensors. Inventory item data is needed for some Redfish sensor properties.
2388 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002389 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002390 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002391 * @param connections Connections that provide sensor values.
2392 * @param objectMgrPaths Mappings from connection name to DBus object path that
2393 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002394 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002395 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002396inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002397 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002398 const std::shared_ptr<std::set<std::string>>& sensorNames,
2399 const std::set<std::string>& connections,
2400 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002401 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002402{
2403 BMCWEB_LOG_DEBUG << "getSensorData enter";
2404 // Get managed objects from all services exposing sensors
2405 for (const std::string& connection : connections)
2406 {
2407 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002408 auto getManagedObjectsCb =
2409 [sensorsAsyncResp, sensorNames,
2410 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002411 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002412 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2413 if (ec)
2414 {
2415 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002416 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002417 return;
2418 }
2419 // Go through all objects and update response with sensor data
2420 for (const auto& objDictEntry : resp)
2421 {
2422 const std::string& objPath =
2423 static_cast<const std::string&>(objDictEntry.first);
2424 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2425 << objPath;
2426
Shawn McCarneyde629b62019-03-08 10:42:51 -06002427 std::vector<std::string> split;
2428 // Reserve space for
2429 // /xyz/openbmc_project/sensors/<name>/<subname>
2430 split.reserve(6);
2431 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2432 if (split.size() < 6)
2433 {
2434 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2435 << objPath;
2436 continue;
2437 }
2438 // These indexes aren't intuitive, as boost::split puts an empty
2439 // string at the beginning
2440 const std::string& sensorType = split[4];
2441 const std::string& sensorName = split[5];
2442 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2443 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002444 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002445 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002446 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002447 continue;
2448 }
2449
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002450 // Find inventory item (if any) associated with sensor
2451 InventoryItem* inventoryItem =
2452 findInventoryItemForSensor(inventoryItems, objPath);
2453
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002454 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002455 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002456
2457 nlohmann::json* sensorJson = nullptr;
2458
Nan Zhou928fefb2022-03-28 08:45:00 -07002459 if (sensorSchema == sensors::node::sensors &&
2460 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002461 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002462 std::string sensorTypeEscaped(sensorType);
2463 sensorTypeEscaped.erase(
2464 std::remove(sensorTypeEscaped.begin(),
2465 sensorTypeEscaped.end(), '_'),
2466 sensorTypeEscaped.end());
2467 std::string sensorId(sensorTypeEscaped);
2468 sensorId += "_";
2469 sensorId += sensorName;
2470
zhanghch058d1b46d2021-04-01 11:18:24 +08002471 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002472 crow::utility::urlFromPieces(
2473 "redfish", "v1", "Chassis",
2474 sensorsAsyncResp->chassisId,
2475 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002476 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002477 }
2478 else
2479 {
Ed Tanous271584a2019-07-09 16:24:22 -07002480 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002481 if (sensorsAsyncResp->efficientExpand)
2482 {
2483 fieldName = "Members";
2484 }
2485 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002486 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002487 fieldName = "Temperatures";
2488 }
2489 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2490 sensorType == "fan_pwm")
2491 {
2492 fieldName = "Fans";
2493 }
2494 else if (sensorType == "voltage")
2495 {
2496 fieldName = "Voltages";
2497 }
2498 else if (sensorType == "power")
2499 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002500 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002501 {
2502 fieldName = "PowerControl";
2503 }
2504 else if ((inventoryItem != nullptr) &&
2505 (inventoryItem->isPowerSupply))
2506 {
2507 fieldName = "PowerSupplies";
2508 }
2509 else
2510 {
2511 // Other power sensors are in SensorCollection
2512 continue;
2513 }
2514 }
2515 else
2516 {
2517 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2518 << sensorType;
2519 continue;
2520 }
2521
2522 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002523 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002524 if (fieldName == "PowerControl")
2525 {
2526 if (tempArray.empty())
2527 {
2528 // Put multiple "sensors" into a single
2529 // PowerControl. Follows MemberId naming and
2530 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002531 nlohmann::json::object_t power;
2532 power["@odata.id"] =
2533 "/redfish/v1/Chassis/" +
2534 sensorsAsyncResp->chassisId + "/" +
2535 sensorsAsyncResp->chassisSubNode + "#/" +
2536 fieldName + "/0";
2537 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002538 }
2539 sensorJson = &(tempArray.back());
2540 }
2541 else if (fieldName == "PowerSupplies")
2542 {
2543 if (inventoryItem != nullptr)
2544 {
2545 sensorJson =
2546 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002547 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002548 }
2549 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002550 else if (fieldName == "Members")
2551 {
Ed Tanous14766872022-03-15 10:44:42 -07002552 nlohmann::json::object_t member;
2553 member["@odata.id"] =
2554 "/redfish/v1/Chassis/" +
2555 sensorsAsyncResp->chassisId + "/" +
2556 sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2557 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002558 sensorJson = &(tempArray.back());
2559 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002560 else
2561 {
Ed Tanous14766872022-03-15 10:44:42 -07002562 nlohmann::json::object_t member;
2563 member["@odata.id"] = "/redfish/v1/Chassis/" +
2564 sensorsAsyncResp->chassisId +
2565 "/" +
2566 sensorsAsyncResp->chassisSubNode +
2567 "#/" + fieldName + "/";
2568 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002569 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002570 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002571 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002572
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002573 if (sensorJson != nullptr)
2574 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002575 objectInterfacesToJson(sensorName, sensorType,
2576 sensorsAsyncResp->chassisSubNode,
2577 objDictEntry.second, *sensorJson,
2578 inventoryItem);
2579
2580 std::string path = "/xyz/openbmc_project/sensors/";
2581 path += sensorType;
2582 path += "/";
2583 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002584 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002585 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002586 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002587 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002588 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002589 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002590 if (sensorsAsyncResp->chassisSubNode ==
2591 sensors::node::sensors &&
2592 sensorsAsyncResp->efficientExpand)
2593 {
2594 sensorsAsyncResp->asyncResp->res
2595 .jsonValue["Members@odata.count"] =
2596 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2597 .size();
2598 }
2599 else if (sensorsAsyncResp->chassisSubNode ==
2600 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002601 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002602 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002603 }
James Feist8bd25cc2019-03-15 15:14:00 -07002604 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002605 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2606 };
2607
2608 // Find DBus object path that implements ObjectManager for the current
2609 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002610 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002611 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002612 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002613 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2614 << objectMgrPath;
2615
2616 crow::connections::systemBus->async_method_call(
2617 getManagedObjectsCb, connection, objectMgrPath,
2618 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002619 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002620 BMCWEB_LOG_DEBUG << "getSensorData exit";
2621}
2622
Nan Zhoufe04d492022-06-22 17:10:41 +00002623inline void
2624 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2625 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002626{
Nan Zhoufe04d492022-06-22 17:10:41 +00002627 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2628 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002629 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2630 auto getObjectManagerPathsCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002631 [sensorsAsyncResp, sensorNames, connections](
2632 const std::shared_ptr<std::map<std::string, std::string>>&
2633 objectMgrPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002634 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2635 auto getInventoryItemsCb =
2636 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2637 const std::shared_ptr<std::vector<InventoryItem>>&
2638 inventoryItems) {
2639 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2640 // Get sensor data and store results in JSON
2641 getSensorData(sensorsAsyncResp, sensorNames, connections,
2642 objectMgrPaths, inventoryItems);
2643 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2644 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002645
Ed Tanous002d39b2022-05-31 08:59:27 -07002646 // Get inventory items associated with sensors
2647 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2648 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002649
Ed Tanous002d39b2022-05-31 08:59:27 -07002650 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002651 };
2652
Ed Tanous002d39b2022-05-31 08:59:27 -07002653 // Get mapping from connection names to the DBus object
2654 // paths that implement the ObjectManager interface
2655 getObjectManagerPaths(sensorsAsyncResp,
2656 std::move(getObjectManagerPathsCb));
2657 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2658 };
2659
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002660 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002661 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002662}
2663
Shawn McCarneyde629b62019-03-08 10:42:51 -06002664/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002665 * @brief Entry point for retrieving sensors data related to requested
2666 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002667 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002668 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002669inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002670 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002671{
2672 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002673 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002674 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002675 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002676 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2677 processSensorList(sensorsAsyncResp, sensorNames);
2678 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2679 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002680 // SensorCollection doesn't contain the Redundancy property
2681 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2682 {
2683 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2684 nlohmann::json::array();
2685 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002686 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002687 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2688 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2689 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002690 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002691}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002692
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302693/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002694 * @brief Find the requested sensorName in the list of all sensors supplied by
2695 * the chassis node
2696 *
2697 * @param sensorName The sensor name supplied in the PATCH request
2698 * @param sensorsList The list of sensors managed by the chassis node
2699 * @param sensorsModified The list of sensors that were found as a result of
2700 * repeated calls to this function
2701 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002702inline bool
2703 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002704 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002705 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002706{
Nan Zhoufe04d492022-06-22 17:10:41 +00002707 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002708 {
George Liu28aa8de2021-02-01 15:13:30 +08002709 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002710 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002711 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002712 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002713 continue;
2714 }
2715 if (thisSensorName == sensorName)
2716 {
2717 sensorsModified.emplace(chassisSensor);
2718 return true;
2719 }
2720 }
2721 return false;
2722}
2723
2724/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302725 * @brief Entry point for overriding sensor values of given sensor
2726 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002727 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002728 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002729 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302730 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002731inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002732 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002733 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002734 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302735{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002736 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002737 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302738
Ed Tanous543f4402022-01-06 13:12:53 -08002739 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302740 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302741 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002742 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302743 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302744 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302745 if (collectionItems.first == "Temperatures")
2746 {
2747 propertyValueName = "ReadingCelsius";
2748 }
2749 else if (collectionItems.first == "Fans")
2750 {
2751 propertyValueName = "Reading";
2752 }
2753 else
2754 {
2755 propertyValueName = "ReadingVolts";
2756 }
2757 for (auto& item : collectionItems.second)
2758 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002759 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2760 "MemberId", memberId, propertyValueName,
2761 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302762 {
2763 return;
2764 }
2765 overrideMap.emplace(memberId,
2766 std::make_pair(value, collectionItems.first));
2767 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302768 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002769
Ed Tanous002d39b2022-05-31 08:59:27 -07002770 auto getChassisSensorListCb =
2771 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002772 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002773 // Match sensor names in the PATCH request to those managed by the
2774 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002775 const std::shared_ptr<std::set<std::string>> sensorNames =
2776 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302777 for (const auto& item : overrideMap)
2778 {
2779 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002780 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2781 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302782 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302783 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002784 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302785 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302786 return;
2787 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302788 }
2789 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002790 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002791 [sensorAsyncResp,
2792 overrideMap](const std::set<std::string>& /*connections*/,
2793 const std::set<std::pair<std::string, std::string>>&
2794 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002795 if (objectsWithConnection.size() != overrideMap.size())
2796 {
2797 BMCWEB_LOG_INFO
2798 << "Unable to find all objects with proper connection "
2799 << objectsWithConnection.size() << " requested "
2800 << overrideMap.size() << "\n";
2801 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2802 sensorAsyncResp->chassisSubNode ==
2803 sensors::node::thermal
2804 ? "Temperatures"
2805 : "Voltages",
2806 "Count");
2807 return;
2808 }
2809 for (const auto& item : objectsWithConnection)
2810 {
2811 sdbusplus::message::object_path path(item.first);
2812 std::string sensorName = path.filename();
2813 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302814 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002815 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302816 return;
2817 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302818
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002819 const auto& iterator = overrideMap.find(sensorName);
2820 if (iterator == overrideMap.end())
2821 {
2822 BMCWEB_LOG_INFO << "Unable to find sensor object"
2823 << item.first << "\n";
2824 messages::internalError(sensorAsyncResp->asyncResp->res);
2825 return;
2826 }
2827 crow::connections::systemBus->async_method_call(
2828 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002829 if (ec)
2830 {
2831 if (ec.value() ==
2832 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002833 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002834 BMCWEB_LOG_WARNING
2835 << "Manufacturing mode is not Enabled...can't "
2836 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002837
Ed Tanous002d39b2022-05-31 08:59:27 -07002838 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002839 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002840 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002841 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002842 BMCWEB_LOG_DEBUG
2843 << "setOverrideValueStatus DBUS error: " << ec;
2844 messages::internalError(
2845 sensorAsyncResp->asyncResp->res);
2846 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002847 },
2848 item.second, item.first, "org.freedesktop.DBus.Properties",
2849 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002850 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002851 }
2852 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302853 // Get object with connection for the given sensor name
2854 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2855 std::move(getObjectsWithConnectionCb));
2856 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302857 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002858 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2859 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2860 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302861}
2862
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002863/**
2864 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2865 * path of the sensor.
2866 *
2867 * Function builds valid Redfish response for sensor query of given chassis and
2868 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2869 * it to caller in a callback.
2870 *
2871 * @param chassis Chassis for which retrieval should be performed
2872 * @param node Node (group) of sensors. See sensors::node for supported values
2873 * @param mapComplete Callback to be called with retrieval result
2874 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002875inline void retrieveUriToDbusMap(const std::string& chassis,
2876 const std::string& node,
2877 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002878{
Ed Tanous02da7c52022-02-27 00:09:02 -08002879 decltype(sensors::paths)::const_iterator pathIt =
2880 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2881 [&node](auto&& val) { return val.first == node; });
2882 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002883 {
2884 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2885 mapComplete(boost::beast::http::status::bad_request, {});
2886 return;
2887 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002888
Nan Zhou72374eb2022-01-27 17:06:51 -08002889 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002890 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2891 const boost::beast::http::status status,
2892 const std::map<std::string, std::string>& uriToDbus) {
2893 mapCompleteCb(status, uriToDbus);
2894 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002895
2896 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002897 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002898 getChassisData(resp);
2899}
2900
Nan Zhoubacb2162022-04-06 11:28:32 -07002901namespace sensors
2902{
Nan Zhou928fefb2022-03-28 08:45:00 -07002903
Nan Zhoubacb2162022-04-06 11:28:32 -07002904inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002905 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2906 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002907 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002908{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002909 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002910
Ed Tanousc1d019a2022-08-06 09:36:06 -07002911 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2912 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002913 {
2914 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2915
2916 sdbusplus::message::object_path path(sensor);
2917 std::string sensorName = path.filename();
2918 if (sensorName.empty())
2919 {
2920 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002921 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002922 return;
2923 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002924 std::string type = path.parent_path().filename();
2925 // fan_tach has an underscore in it, so remove it to "normalize" the
2926 // type in the URI
2927 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2928
Ed Tanous14766872022-03-15 10:44:42 -07002929 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002930 std::string id = type;
2931 id += "_";
2932 id += sensorName;
2933 member["@odata.id"] = crow::utility::urlFromPieces(
2934 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2935
Ed Tanous14766872022-03-15 10:44:42 -07002936 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002937 }
2938
Ed Tanousc1d019a2022-08-06 09:36:06 -07002939 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002940 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2941}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002942
Nan Zhoude167a62022-06-01 04:47:45 +00002943inline void
2944 handleSensorCollectionGet(App& app, const crow::Request& req,
2945 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2946 const std::string& chassisId)
2947{
2948 query_param::QueryCapabilities capabilities = {
2949 .canDelegateExpandLevel = 1,
2950 };
2951 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002952 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002953 delegatedQuery, capabilities))
2954 {
2955 return;
2956 }
2957
2958 if (delegatedQuery.expandType != query_param::ExpandType::None)
2959 {
2960 // we perform efficient expand.
2961 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2962 aResp, chassisId, sensors::dbus::sensorPaths,
2963 sensors::node::sensors,
2964 /*efficientExpand=*/true);
2965 getChassisData(asyncResp);
2966
2967 BMCWEB_LOG_DEBUG
2968 << "SensorCollection doGet exit via efficient expand handler";
2969 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002970 }
Nan Zhoude167a62022-06-01 04:47:45 +00002971
Nan Zhoude167a62022-06-01 04:47:45 +00002972 // We get all sensors as hyperlinkes in the chassis (this
2973 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002974 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2975 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2976 sensors::node::sensors));
2977}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002978
Ed Tanousc1d019a2022-08-06 09:36:06 -07002979inline void
2980 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2981 const std::string& sensorPath,
2982 const ::dbus::utility::MapperGetObject& mapperResponse)
2983{
2984 if (mapperResponse.size() != 1)
2985 {
2986 messages::internalError(asyncResp->res);
2987 return;
2988 }
2989 const auto& valueIface = *mapperResponse.begin();
2990 const std::string& connectionName = valueIface.first;
2991 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2992 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
2993 crow::connections::systemBus->async_method_call(
2994 [asyncResp,
2995 sensorPath](const boost::system::error_code ec,
2996 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2997 if (ec)
2998 {
2999 messages::internalError(asyncResp->res);
3000 return;
3001 }
3002 sdbusplus::message::object_path path(sensorPath);
3003 std::string name = path.filename();
3004 path = path.parent_path();
3005 std::string type = path.filename();
3006 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
3007 asyncResp->res.jsonValue, nullptr);
3008 },
3009 connectionName, sensorPath, "org.freedesktop.DBus.Properties", "GetAll",
3010 "");
Nan Zhoude167a62022-06-01 04:47:45 +00003011}
3012
Nan Zhoue6bd8462022-06-01 04:35:35 +00003013inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07003014 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3015 const std::string& /*chassisId*/,
3016 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00003017{
Ed Tanousc1d019a2022-08-06 09:36:06 -07003018 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00003019 {
3020 return;
3021 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07003022 size_t index = sensorId.find('_');
3023 if (index == std::string::npos)
3024 {
3025 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
3026 return;
3027 }
3028 std::string sensorType = sensorId.substr(0, index);
3029 std::string sensorName = sensorId.substr(index + 1);
3030 // fan_pwm and fan_tach need special handling
3031 if (sensorType == "fantach" || sensorType == "fanpwm")
3032 {
3033 sensorType.insert(3, 1, '_');
3034 }
3035
Nan Zhoue6bd8462022-06-01 04:35:35 +00003036 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00003037
3038 const std::array<const char*, 1> interfaces = {
3039 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc1d019a2022-08-06 09:36:06 -07003040 std::string sensorPath =
3041 "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName;
Nan Zhoue6bd8462022-06-01 04:35:35 +00003042 // Get a list of all of the sensors that implement Sensor.Value
3043 // and get the path and service name associated with the sensor
3044 crow::connections::systemBus->async_method_call(
Ed Tanousc1d019a2022-08-06 09:36:06 -07003045 [asyncResp, sensorPath,
Nan Zhoue6bd8462022-06-01 04:35:35 +00003046 sensorName](const boost::system::error_code ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07003047 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00003048 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3049 if (ec)
3050 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07003051 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003052 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3053 << "Dbus error " << ec;
3054 return;
3055 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07003056 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003057 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3058 },
3059 "xyz.openbmc_project.ObjectMapper",
3060 "/xyz/openbmc_project/object_mapper",
Ed Tanousc1d019a2022-08-06 09:36:06 -07003061 "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
3062 interfaces);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003063}
3064
Nan Zhoubacb2162022-04-06 11:28:32 -07003065} // namespace sensors
3066
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003067inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003068{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003069 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003070 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003071 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003072 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003073}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003074
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003075inline void requestRoutesSensor(App& app)
3076{
3077 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003078 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003079 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003080 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003081}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003082
Ed Tanous1abe55e2018-09-05 08:30:59 -07003083} // namespace redfish