blob: 35114bfcfb13ad14d3d2def95d4caa691aabf037 [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
Anthony Wilson95a3eca2019-06-11 10:44:47 -050018#include "node.hpp"
19
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010020#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/split.hpp>
22#include <boost/container/flat_map.hpp>
23#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070024#include <dbus_singleton.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053025#include <utils/json_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
27#include <cmath>
Ed Tanousb5a76932020-09-29 16:16:58 -070028#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080029#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031namespace redfish
32{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010033
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010034using GetSubTreeType = std::vector<
35 std::pair<std::string,
36 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
37
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -050038using SensorVariant =
39 std::variant<int64_t, double, uint32_t, bool, std::string>;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070040
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010041using ManagedObjectsVectorType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070042 sdbusplus::message::object_path,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010043 boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070044 std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010045
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020046namespace sensors
47{
48namespace node
49{
50static constexpr std::string_view power = "Power";
51static constexpr std::string_view sensors = "Sensors";
52static constexpr std::string_view thermal = "Thermal";
53} // namespace node
54
55namespace dbus
56{
57static const boost::container::flat_map<std::string_view,
58 std::vector<const char*>>
59 types = {{node::power,
60 {"/xyz/openbmc_project/sensors/voltage",
61 "/xyz/openbmc_project/sensors/power"}},
62 {node::sensors,
63 {"/xyz/openbmc_project/sensors/power",
64 "/xyz/openbmc_project/sensors/current",
65 "/xyz/openbmc_project/sensors/utilization"}},
66 {node::thermal,
67 {"/xyz/openbmc_project/sensors/fan_tach",
68 "/xyz/openbmc_project/sensors/temperature",
69 "/xyz/openbmc_project/sensors/fan_pwm"}}};
70}
71} // namespace sensors
72
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010073/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020074 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010075 * Gathers data needed for response processing after async calls are done
76 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070077class SensorsAsyncResp
78{
79 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020080 using DataCompleteCb = std::function<void(
81 const boost::beast::http::status status,
82 const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
83
84 struct SensorData
85 {
86 const std::string name;
87 std::string uri;
88 const std::string valueKey;
89 const std::string dbusPath;
90 };
91
Ed Tanous271584a2019-07-09 16:24:22 -070092 SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -070093 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020094 const std::string_view& subNode) :
Ed Tanous43b761d2019-02-13 20:10:56 -080095 res(response),
Ed Tanous271584a2019-07-09 16:24:22 -070096 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050097 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020098
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020099 // Store extra data about sensor mapping and return it in callback
100 SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700101 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200102 const std::string_view& subNode,
103 DataCompleteCb&& creationComplete) :
104 res(response),
105 chassisId(chassisIdIn), types(typesIn),
106 chassisSubNode(subNode), metadata{std::vector<SensorData>()},
107 dataComplete{std::move(creationComplete)}
108 {}
109
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 ~SensorsAsyncResp()
111 {
112 if (res.result() == boost::beast::http::status::internal_server_error)
113 {
114 // Reset the json object to clear out any data that made it in
115 // before the error happened todo(ed) handle error condition with
116 // proper code
117 res.jsonValue = nlohmann::json::object();
118 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200119
120 if (dataComplete && metadata)
121 {
122 boost::container::flat_map<std::string, std::string> map;
123 if (res.result() == boost::beast::http::status::ok)
124 {
125 for (auto& sensor : *metadata)
126 {
127 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
128 sensor.dbusPath));
129 }
130 }
131 dataComplete(res.result(), map);
132 }
133
Ed Tanous1abe55e2018-09-05 08:30:59 -0700134 res.end();
135 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100136
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200137 void addMetadata(const nlohmann::json& sensorObject,
138 const std::string& valueKey, const std::string& dbusPath)
139 {
140 if (metadata)
141 {
142 metadata->emplace_back(SensorData{sensorObject["Name"],
143 sensorObject["@odata.id"],
144 valueKey, dbusPath});
145 }
146 }
147
148 void updateUri(const std::string& name, const std::string& uri)
149 {
150 if (metadata)
151 {
152 for (auto& sensor : *metadata)
153 {
154 if (sensor.name == name)
155 {
156 sensor.uri = uri;
157 }
158 }
159 }
160 }
161
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162 crow::Response& res;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200163 const std::string chassisId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700164 const std::vector<const char*> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200165 const std::string chassisSubNode;
166
167 private:
168 std::optional<std::vector<SensorData>> metadata;
169 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100170};
171
172/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500173 * Possible states for physical inventory leds
174 */
175enum class LedState
176{
177 OFF,
178 ON,
179 BLINK,
180 UNKNOWN
181};
182
183/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500184 * D-Bus inventory item associated with one or more sensors.
185 */
186class InventoryItem
187{
188 public:
189 InventoryItem(const std::string& objPath) :
190 objectPath(objPath), name(), isPresent(true), isFunctional(true),
Gunnar Mills42cbe532019-08-15 15:26:54 -0500191 isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
192 model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""),
Anthony Wilsond5005492019-07-31 16:34:17 -0500193 ledState(LedState::UNKNOWN)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500194 {
195 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800196 sdbusplus::message::object_path path(objectPath);
197 name = path.filename();
198 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500199 {
George Liu28aa8de2021-02-01 15:13:30 +0800200 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500201 }
202 }
203
204 std::string objectPath;
205 std::string name;
206 bool isPresent;
207 bool isFunctional;
208 bool isPowerSupply;
Gunnar Mills42cbe532019-08-15 15:26:54 -0500209 int powerSupplyEfficiencyPercent;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500210 std::string manufacturer;
211 std::string model;
212 std::string partNumber;
213 std::string serialNumber;
214 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500215 std::string ledObjectPath;
216 LedState ledState;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500217};
218
219/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530220 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200221 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100222 * @param sensorNames Sensors retrieved from chassis
223 * @param callback Callback for processing gathered connections
224 */
225template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530226void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000227 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700228 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530229 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530231 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 const std::string path = "/xyz/openbmc_project/sensors";
233 const std::array<std::string, 1> interfaces = {
234 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100235
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 // Response handler for parsing objects subtree
Ed Tanous81ce6092020-12-17 16:54:55 +0000237 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 sensorNames](const boost::system::error_code ec,
239 const GetSubTreeType& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530240 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241 if (ec)
242 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000243 messages::internalError(sensorsAsyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530244 BMCWEB_LOG_ERROR
245 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100247 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100248
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
250
251 // Make unique list of connections only for requested sensor types and
252 // found in the chassis
253 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530254 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700255 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
256 // producers
257 connections.reserve(8);
258
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700259 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
260 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700261 {
262 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
263 }
264
265 for (const std::pair<
266 std::string,
267 std::vector<std::pair<std::string, std::vector<std::string>>>>&
268 object : subtree)
269 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700270 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700271 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700272 for (const std::pair<std::string, std::vector<std::string>>&
273 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700274 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700275 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
276 connections.insert(objData.first);
277 objectsWithConnection.insert(
278 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 }
280 }
281 }
282 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530283 callback(std::move(connections), std::move(objectsWithConnection));
284 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700285 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700286 // Make call to ObjectMapper to find all sensors objects
287 crow::connections::systemBus->async_method_call(
288 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
289 "/xyz/openbmc_project/object_mapper",
290 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530291 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
292}
293
294/**
295 * @brief Create connections necessary for sensors
296 * @param SensorsAsyncResp Pointer to object holding response data
297 * @param sensorNames Sensors retrieved from chassis
298 * @param callback Callback for processing gathered connections
299 */
300template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700301void getConnections(
Ed Tanous81ce6092020-12-17 16:54:55 +0000302 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700303 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
304 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530305{
306 auto objectsWithConnectionCb =
307 [callback](const boost::container::flat_set<std::string>& connections,
308 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700309 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000310 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530311 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100312}
313
314/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700315 * @brief Shrinks the list of sensors for processing
316 * @param SensorsAysncResp The class holding the Redfish response
317 * @param allSensors A list of all the sensors associated to the
318 * chassis element (i.e. baseboard, front panel, etc...)
319 * @param activeSensors A list that is a reduction of the incoming
320 * allSensors list. Eliminate Thermal sensors when a Power request is
321 * made, and eliminate Power sensors when a Thermal request is made.
322 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000323inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000324 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700325 const std::vector<std::string>* allSensors,
Ed Tanousb5a76932020-09-29 16:16:58 -0700326 const std::shared_ptr<boost::container::flat_set<std::string>>&
327 activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700328{
Ed Tanous81ce6092020-12-17 16:54:55 +0000329 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700330 {
331 return;
332 }
333 if ((allSensors == nullptr) || (activeSensors == nullptr))
334 {
335 messages::resourceNotFound(
Ed Tanous81ce6092020-12-17 16:54:55 +0000336 sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
337 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200338 ? "Temperatures"
339 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700340
341 return;
342 }
343 if (allSensors->empty())
344 {
345 // Nothing to do, the activeSensors object is also empty
346 return;
347 }
348
Ed Tanous81ce6092020-12-17 16:54:55 +0000349 for (const char* type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700350 {
351 for (const std::string& sensor : *allSensors)
352 {
353 if (boost::starts_with(sensor, type))
354 {
355 activeSensors->emplace(sensor);
356 }
357 }
358 }
359}
360
361/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800362 * @brief Retrieves valid chassis path
363 * @param asyncResp Pointer to object holding response data
364 * @param callback Callback for next step to get valid chassis path
365 */
366template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700367void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800368 Callback&& callback)
369{
370 BMCWEB_LOG_DEBUG << "checkChassisId enter";
371 const std::array<const char*, 2> interfaces = {
372 "xyz.openbmc_project.Inventory.Item.Board",
373 "xyz.openbmc_project.Inventory.Item.Chassis"};
374
375 auto respHandler =
376 [callback{std::move(callback)},
377 asyncResp](const boost::system::error_code ec,
378 const std::vector<std::string>& chassisPaths) mutable {
379 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
380 if (ec)
381 {
382 BMCWEB_LOG_ERROR
383 << "getValidChassisPath respHandler DBUS error: " << ec;
384 messages::internalError(asyncResp->res);
385 return;
386 }
387
388 std::optional<std::string> chassisPath;
389 std::string chassisName;
390 for (const std::string& chassis : chassisPaths)
391 {
George Liu28aa8de2021-02-01 15:13:30 +0800392 sdbusplus::message::object_path path(chassis);
393 chassisName = path.filename();
394 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800395 {
396 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
397 continue;
398 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800399 if (chassisName == asyncResp->chassisId)
400 {
401 chassisPath = chassis;
402 break;
403 }
404 }
405 callback(chassisPath);
406 };
407
408 // Get the Chassis Collection
409 crow::connections::systemBus->async_method_call(
410 respHandler, "xyz.openbmc_project.ObjectMapper",
411 "/xyz/openbmc_project/object_mapper",
412 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
413 "/xyz/openbmc_project/inventory", 0, interfaces);
414 BMCWEB_LOG_DEBUG << "checkChassisId exit";
415}
416
417/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100418 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200419 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100420 * @param callback Callback for next step in gathered sensor processing
421 */
422template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700423void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700424 Callback&& callback)
425{
426 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500427 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700428 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500429 "xyz.openbmc_project.Inventory.Item.Chassis"};
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700430 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
431 const boost::system::error_code ec,
432 const std::vector<std::string>& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700433 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
434 if (ec)
435 {
436 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700437 messages::internalError(sensorsAsyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700438 return;
439 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100440
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700441 const std::string* chassisPath = nullptr;
442 std::string chassisName;
443 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700444 {
George Liu28aa8de2021-02-01 15:13:30 +0800445 sdbusplus::message::object_path path(chassis);
446 chassisName = path.filename();
447 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700449 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700450 continue;
451 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700452 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700453 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700454 chassisPath = &chassis;
455 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700456 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700457 }
458 if (chassisPath == nullptr)
459 {
460 messages::resourceNotFound(sensorsAsyncResp->res, "Chassis",
461 sensorsAsyncResp->chassisId);
462 return;
463 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700464
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700465 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200466 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700467 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700468 sensorsAsyncResp->res.jsonValue["@odata.type"] =
469 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700470 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200471 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700472 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700473 sensorsAsyncResp->res.jsonValue["@odata.type"] =
474 "#Thermal.v1_4_0.Thermal";
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800475 sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array();
476 sensorsAsyncResp->res.jsonValue["Temperatures"] =
477 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700478 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200479 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500480 {
481 sensorsAsyncResp->res.jsonValue["@odata.type"] =
482 "#SensorCollection.SensorCollection";
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500483 sensorsAsyncResp->res.jsonValue["Description"] =
484 "Collection of Sensors for this Chassis";
485 sensorsAsyncResp->res.jsonValue["Members"] =
486 nlohmann::json::array();
487 sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0;
488 }
489
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200490 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500491 {
492 sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500493 }
494
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700495 sensorsAsyncResp->res.jsonValue["@odata.id"] =
496 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
497 chassisSubNode;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700498 sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode;
499
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500500 // Get the list of all sensors for this Chassis element
501 std::string sensorPath = *chassisPath + "/all_sensors";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700502 crow::connections::systemBus->async_method_call(
503 [sensorsAsyncResp, callback{std::move(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700504 const boost::system::error_code& e,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700505 const std::variant<std::vector<std::string>>&
506 variantEndpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -0700507 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700508 {
Ed Tanous271584a2019-07-09 16:24:22 -0700509 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700510 {
511 messages::internalError(sensorsAsyncResp->res);
512 return;
513 }
514 }
515 const std::vector<std::string>* nodeSensorList =
516 std::get_if<std::vector<std::string>>(&(variantEndpoints));
517 if (nodeSensorList == nullptr)
518 {
519 messages::resourceNotFound(
520 sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200521 sensorsAsyncResp->chassisSubNode ==
522 sensors::node::thermal
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700523 ? "Temperatures"
Patrick Williams738c1e62021-02-22 17:14:25 -0600524 : sensorsAsyncResp->chassisSubNode ==
525 sensors::node::power
526 ? "Voltages"
527 : "Sensors");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700528 return;
529 }
530 const std::shared_ptr<boost::container::flat_set<std::string>>
531 culledSensorList = std::make_shared<
532 boost::container::flat_set<std::string>>();
533 reduceSensorList(sensorsAsyncResp, nodeSensorList,
534 culledSensorList);
535 callback(culledSensorList);
536 },
537 "xyz.openbmc_project.ObjectMapper", sensorPath,
538 "org.freedesktop.DBus.Properties", "Get",
539 "xyz.openbmc_project.Association", "endpoints");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100540 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100541
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700542 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700544 respHandler, "xyz.openbmc_project.ObjectMapper",
545 "/xyz/openbmc_project/object_mapper",
546 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700547 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100549}
550
551/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600552 * @brief Finds all DBus object paths that implement ObjectManager.
553 *
554 * Creates a mapping from the associated connection name to the object path.
555 *
556 * Finds the object paths asynchronously. Invokes callback when information has
557 * been obtained.
558 *
559 * The callback must have the following signature:
560 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500561 * callback(std::shared_ptr<boost::container::flat_map<std::string,
562 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600563 * @endcode
564 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700565 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600566 * @param callback Callback to invoke when object paths obtained.
567 */
568template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700569void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000570 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700571 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600572{
573 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
574 const std::array<std::string, 1> interfaces = {
575 "org.freedesktop.DBus.ObjectManager"};
576
577 // Response handler for GetSubTree DBus method
578 auto respHandler = [callback{std::move(callback)},
Ed Tanous81ce6092020-12-17 16:54:55 +0000579 sensorsAsyncResp](const boost::system::error_code ec,
Shawn McCarneyde629b62019-03-08 10:42:51 -0600580 const GetSubTreeType& subtree) {
581 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
582 if (ec)
583 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000584 messages::internalError(sensorsAsyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600585 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
586 << ec;
587 return;
588 }
589
590 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500591 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
592 objectMgrPaths = std::make_shared<
593 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600594 for (const std::pair<
595 std::string,
596 std::vector<std::pair<std::string, std::vector<std::string>>>>&
597 object : subtree)
598 {
599 // Loop over connections for current object path
600 const std::string& objectPath = object.first;
601 for (const std::pair<std::string, std::vector<std::string>>&
602 objData : object.second)
603 {
604 // Add mapping from connection to object path
605 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500606 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600607 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
608 << objectPath;
609 }
610 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500611 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600612 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
613 };
614
615 // Query mapper for all DBus object paths that implement ObjectManager
616 crow::connections::systemBus->async_method_call(
617 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
618 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700619 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600620 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
621}
622
623/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500624 * @brief Returns the Redfish State value for the specified inventory item.
625 * @param inventoryItem D-Bus inventory item associated with a sensor.
626 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700627 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000628inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500629{
630 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
631 {
632 return "Absent";
633 }
James Feist34dd1792019-05-17 14:10:54 -0700634
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500635 return "Enabled";
636}
637
638/**
639 * @brief Returns the Redfish Health value for the specified sensor.
640 * @param sensorJson Sensor JSON object.
641 * @param interfacesDict Map of all sensor interfaces.
642 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
643 * be nullptr if no associated inventory item was found.
644 * @return Health value for sensor.
645 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000646inline std::string getHealth(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500647 nlohmann::json& sensorJson,
James Feist34dd1792019-05-17 14:10:54 -0700648 const boost::container::flat_map<
649 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500650 interfacesDict,
651 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700652{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500653 // Get current health value (if any) in the sensor JSON object. Some JSON
654 // objects contain multiple sensors (such as PowerSupplies). We want to set
655 // the overall health to be the most severe of any of the sensors.
656 std::string currentHealth;
657 auto statusIt = sensorJson.find("Status");
658 if (statusIt != sensorJson.end())
659 {
660 auto healthIt = statusIt->find("Health");
661 if (healthIt != statusIt->end())
662 {
663 std::string* health = healthIt->get_ptr<std::string*>();
664 if (health != nullptr)
665 {
666 currentHealth = *health;
667 }
668 }
669 }
670
671 // If current health in JSON object is already Critical, return that. This
672 // should override the sensor health, which might be less severe.
673 if (currentHealth == "Critical")
674 {
675 return "Critical";
676 }
677
678 // Check if sensor has critical threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700679 auto criticalThresholdIt =
680 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
681 if (criticalThresholdIt != interfacesDict.end())
682 {
683 auto thresholdHighIt =
684 criticalThresholdIt->second.find("CriticalAlarmHigh");
685 auto thresholdLowIt =
686 criticalThresholdIt->second.find("CriticalAlarmLow");
687 if (thresholdHighIt != criticalThresholdIt->second.end())
688 {
689 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
690 if (asserted == nullptr)
691 {
692 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
693 }
694 else if (*asserted)
695 {
696 return "Critical";
697 }
698 }
699 if (thresholdLowIt != criticalThresholdIt->second.end())
700 {
701 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
702 if (asserted == nullptr)
703 {
704 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
705 }
706 else if (*asserted)
707 {
708 return "Critical";
709 }
710 }
711 }
712
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500713 // Check if associated inventory item is not functional
714 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
715 {
716 return "Critical";
717 }
718
719 // If current health in JSON object is already Warning, return that. This
720 // should override the sensor status, which might be less severe.
721 if (currentHealth == "Warning")
722 {
723 return "Warning";
724 }
725
726 // Check if sensor has warning threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700727 auto warningThresholdIt =
728 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
729 if (warningThresholdIt != interfacesDict.end())
730 {
731 auto thresholdHighIt =
732 warningThresholdIt->second.find("WarningAlarmHigh");
733 auto thresholdLowIt =
734 warningThresholdIt->second.find("WarningAlarmLow");
735 if (thresholdHighIt != warningThresholdIt->second.end())
736 {
737 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
738 if (asserted == nullptr)
739 {
740 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
741 }
742 else if (*asserted)
743 {
744 return "Warning";
745 }
746 }
747 if (thresholdLowIt != warningThresholdIt->second.end())
748 {
749 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
750 if (asserted == nullptr)
751 {
752 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
753 }
754 else if (*asserted)
755 {
756 return "Warning";
757 }
758 }
759 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500760
James Feist34dd1792019-05-17 14:10:54 -0700761 return "OK";
762}
763
Ed Tanous23a21a12020-07-25 04:45:05 +0000764inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500765 const InventoryItem* inventoryItem)
766{
767 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
768 {
769 switch (inventoryItem->ledState)
770 {
771 case LedState::OFF:
772 sensorJson["IndicatorLED"] = "Off";
773 break;
774 case LedState::ON:
775 sensorJson["IndicatorLED"] = "Lit";
776 break;
777 case LedState::BLINK:
778 sensorJson["IndicatorLED"] = "Blinking";
779 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000780 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500781 break;
782 }
783 }
784}
785
James Feist34dd1792019-05-17 14:10:54 -0700786/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100787 * @brief Builds a json sensor representation of a sensor.
788 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500789 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100790 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200791 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100792 * @param interfacesDict A dictionary of the interfaces and properties of said
793 * interfaces to be built from
794 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500795 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
796 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100797 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000798inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100799 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700800 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100801 const boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700802 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100803 interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000804 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700805{
806 // We need a value interface before we can do anything with it
807 auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
808 if (valueIt == interfacesDict.end())
809 {
810 BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
811 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100812 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100813
Ed Tanous1abe55e2018-09-05 08:30:59 -0700814 // Assume values exist as is (10^0 == 1) if no scale exists
815 int64_t scaleMultiplier = 0;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100816
Ed Tanous1abe55e2018-09-05 08:30:59 -0700817 auto scaleIt = valueIt->second.find("Scale");
818 // If a scale exists, pull value as int64, and use the scaling.
819 if (scaleIt != valueIt->second.end())
820 {
Ed Tanousabf2add2019-01-22 16:40:12 -0800821 const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700822 if (int64Value != nullptr)
823 {
824 scaleMultiplier = *int64Value;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100825 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100826 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700827
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200828 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500829 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500830 // For sensors in SensorCollection we set Id instead of MemberId,
831 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000832 sensorJson["Id"] = sensorName;
833 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500834 }
835 else if (sensorType != "power")
836 {
837 // Set MemberId and Name for non-power sensors. For PowerSupplies and
838 // PowerControl, those properties have more general values because
839 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000840 sensorJson["MemberId"] = sensorName;
841 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500842 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700843
Ed Tanous81ce6092020-12-17 16:54:55 +0000844 sensorJson["Status"]["State"] = getState(inventoryItem);
845 sensorJson["Status"]["Health"] =
846 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700847
848 // Parameter to set to override the type we get from dbus, and force it to
849 // int, regardless of what is available. This is used for schemas like fan,
850 // that require integers, not floats.
851 bool forceToInt = false;
852
Anthony Wilson3929aca2019-07-19 15:42:33 -0500853 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200854 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500855 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000856 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500857 if (sensorType == "power")
858 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000859 sensorJson["ReadingUnits"] = "Watts";
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500860 }
861 else if (sensorType == "current")
862 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000863 sensorJson["ReadingUnits"] = "Amperes";
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500864 }
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200865 else if (sensorType == "utilization")
866 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000867 sensorJson["ReadingUnits"] = "Percent";
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200868 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500869 }
870 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700871 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500872 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000873 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700874 // TODO(ed) Documentation says that path should be type fan_tach,
875 // implementation seems to implement fan
876 }
877 else if (sensorType == "fan" || sensorType == "fan_tach")
878 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500879 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000880 sensorJson["ReadingUnits"] = "RPM";
881 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
882 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700883 forceToInt = true;
884 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700885 else if (sensorType == "fan_pwm")
886 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500887 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000888 sensorJson["ReadingUnits"] = "Percent";
889 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
890 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700891 forceToInt = true;
892 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700893 else if (sensorType == "voltage")
894 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500895 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000896 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700897 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700898 else if (sensorType == "power")
899 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700900 std::string sensorNameLower =
901 boost::algorithm::to_lower_copy(sensorName);
902
Eddie James028f7eb2019-05-17 21:24:36 +0000903 if (!sensorName.compare("total_power"))
904 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000905 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500906 // Put multiple "sensors" into a single PowerControl, so have
907 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000908 sensorJson["MemberId"] = "0";
909 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500910 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000911 }
912 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700913 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500914 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700915 }
916 else
917 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500918 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700919 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700920 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700921 else
922 {
923 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
924 return;
925 }
926 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500927 std::vector<
928 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
929 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700930 properties.reserve(7);
931
932 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600933
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200934 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500935 {
936 properties.emplace_back(
937 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
938 "/Thresholds/UpperCaution/Reading"_json_pointer);
939 properties.emplace_back(
940 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
941 "/Thresholds/LowerCaution/Reading"_json_pointer);
942 properties.emplace_back(
943 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
944 "/Thresholds/UpperCritical/Reading"_json_pointer);
945 properties.emplace_back(
946 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
947 "/Thresholds/LowerCritical/Reading"_json_pointer);
948 }
949 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600950 {
951 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500952 "WarningHigh",
953 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600954 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500955 "WarningLow",
956 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600957 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500958 "CriticalHigh",
959 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600960 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500961 "CriticalLow",
962 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600963 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700964
Ed Tanous2474adf2018-09-05 16:31:16 -0700965 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
966
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200967 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500968 {
969 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500970 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500971 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500972 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500973 }
974 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700975 {
976 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500977 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700978 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500979 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500981 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700982 {
983 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500984 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700985 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500986 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 }
988
Anthony Wilson3929aca2019-07-19 15:42:33 -0500989 for (const std::tuple<const char*, const char*,
990 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 {
992 auto interfaceProperties = interfacesDict.find(std::get<0>(p));
993 if (interfaceProperties != interfacesDict.end())
994 {
Ed Tanous271584a2019-07-09 16:24:22 -0700995 auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
996 if (thisValueIt != interfaceProperties->second.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700997 {
Ed Tanous271584a2019-07-09 16:24:22 -0700998 const SensorVariant& valueVariant = thisValueIt->second;
Anthony Wilson3929aca2019-07-19 15:42:33 -0500999
1000 // The property we want to set may be nested json, so use
1001 // a json_pointer for easy indexing into the json structure.
1002 const nlohmann::json::json_pointer& key = std::get<2>(p);
1003
Ed Tanous1abe55e2018-09-05 08:30:59 -07001004 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001005 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001006
Ed Tanousabf2add2019-01-22 16:40:12 -08001007 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001008 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001009 double temp = 0.0;
1010 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 {
Ed Tanous271584a2019-07-09 16:24:22 -07001012 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001013 }
1014 else if (doubleValue != nullptr)
1015 {
1016 temp = *doubleValue;
1017 }
Eddie James028f7eb2019-05-17 21:24:36 +00001018 else if (uValue != nullptr)
1019 {
1020 temp = *uValue;
1021 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001022 else
1023 {
1024 BMCWEB_LOG_ERROR
1025 << "Got value interface that wasn't int or double";
1026 continue;
1027 }
1028 temp = temp * std::pow(10, scaleMultiplier);
1029 if (forceToInt)
1030 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001031 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001032 }
1033 else
1034 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001035 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001036 }
1037 }
1038 }
1039 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001040
Ed Tanous81ce6092020-12-17 16:54:55 +00001041 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001042 "/xyz/openbmc_project/sensors/" + sensorType +
1043 "/" + sensorName);
1044
Ed Tanous1abe55e2018-09-05 08:30:59 -07001045 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001046}
1047
Ed Tanousb5a76932020-09-29 16:16:58 -07001048inline void populateFanRedundancy(
1049 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001050{
1051 crow::connections::systemBus->async_method_call(
1052 [sensorsAsyncResp](const boost::system::error_code ec,
1053 const GetSubTreeType& resp) {
1054 if (ec)
1055 {
1056 return; // don't have to have this interface
1057 }
Ed Tanouse278c182019-03-13 16:23:37 -07001058 for (const std::pair<std::string,
1059 std::vector<std::pair<
1060 std::string, std::vector<std::string>>>>&
1061 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001062 {
Ed Tanouse278c182019-03-13 16:23:37 -07001063 const std::string& path = pathPair.first;
1064 const std::vector<
1065 std::pair<std::string, std::vector<std::string>>>& objDict =
1066 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001067 if (objDict.empty())
1068 {
1069 continue; // this should be impossible
1070 }
1071
1072 const std::string& owner = objDict.begin()->first;
1073 crow::connections::systemBus->async_method_call(
1074 [path, owner,
Ed Tanous271584a2019-07-09 16:24:22 -07001075 sensorsAsyncResp](const boost::system::error_code e,
James Feist8bd25cc2019-03-15 15:14:00 -07001076 std::variant<std::vector<std::string>>
1077 variantEndpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001078 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001079 {
1080 return; // if they don't have an association we
1081 // can't tell what chassis is
1082 }
1083 // verify part of the right chassis
1084 auto endpoints = std::get_if<std::vector<std::string>>(
1085 &variantEndpoints);
1086
1087 if (endpoints == nullptr)
1088 {
1089 BMCWEB_LOG_ERROR << "Invalid association interface";
1090 messages::internalError(sensorsAsyncResp->res);
1091 return;
1092 }
1093
1094 auto found = std::find_if(
1095 endpoints->begin(), endpoints->end(),
1096 [sensorsAsyncResp](const std::string& entry) {
1097 return entry.find(
1098 sensorsAsyncResp->chassisId) !=
1099 std::string::npos;
1100 });
1101
1102 if (found == endpoints->end())
1103 {
1104 return;
1105 }
1106 crow::connections::systemBus->async_method_call(
1107 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001108 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001109 const boost::container::flat_map<
1110 std::string,
1111 std::variant<uint8_t,
1112 std::vector<std::string>,
1113 std::string>>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001114 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001115 {
1116 return; // don't have to have this
1117 // interface
1118 }
1119 auto findFailures = ret.find("AllowedFailures");
1120 auto findCollection = ret.find("Collection");
1121 auto findStatus = ret.find("Status");
1122
1123 if (findFailures == ret.end() ||
1124 findCollection == ret.end() ||
1125 findStatus == ret.end())
1126 {
1127 BMCWEB_LOG_ERROR
1128 << "Invalid redundancy interface";
1129 messages::internalError(
1130 sensorsAsyncResp->res);
1131 return;
1132 }
1133
1134 auto allowedFailures = std::get_if<uint8_t>(
1135 &(findFailures->second));
1136 auto collection =
1137 std::get_if<std::vector<std::string>>(
1138 &(findCollection->second));
1139 auto status = std::get_if<std::string>(
1140 &(findStatus->second));
1141
1142 if (allowedFailures == nullptr ||
1143 collection == nullptr || status == nullptr)
1144 {
1145
1146 BMCWEB_LOG_ERROR
1147 << "Invalid redundancy interface "
1148 "types";
1149 messages::internalError(
1150 sensorsAsyncResp->res);
1151 return;
1152 }
George Liu28aa8de2021-02-01 15:13:30 +08001153 sdbusplus::message::object_path objectPath(
1154 path);
1155 std::string name = objectPath.filename();
1156 if (name.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001157 {
1158 // this should be impossible
1159 messages::internalError(
1160 sensorsAsyncResp->res);
1161 return;
1162 }
James Feist8bd25cc2019-03-15 15:14:00 -07001163 std::replace(name.begin(), name.end(), '_',
1164 ' ');
1165
1166 std::string health;
1167
1168 if (boost::ends_with(*status, "Full"))
1169 {
1170 health = "OK";
1171 }
1172 else if (boost::ends_with(*status, "Degraded"))
1173 {
1174 health = "Warning";
1175 }
1176 else
1177 {
1178 health = "Critical";
1179 }
1180 std::vector<nlohmann::json> redfishCollection;
1181 const auto& fanRedfish =
1182 sensorsAsyncResp->res.jsonValue["Fans"];
1183 for (const std::string& item : *collection)
1184 {
George Liu28aa8de2021-02-01 15:13:30 +08001185 sdbusplus::message::object_path path(item);
1186 std::string itemName = path.filename();
1187 if (itemName.empty())
1188 {
1189 continue;
1190 }
James Feist8bd25cc2019-03-15 15:14:00 -07001191 /*
1192 todo(ed): merge patch that fixes the names
1193 std::replace(itemName.begin(),
1194 itemName.end(), '_', ' ');*/
1195 auto schemaItem = std::find_if(
1196 fanRedfish.begin(), fanRedfish.end(),
1197 [itemName](const nlohmann::json& fan) {
1198 return fan["MemberId"] == itemName;
1199 });
1200 if (schemaItem != fanRedfish.end())
1201 {
1202 redfishCollection.push_back(
1203 {{"@odata.id",
1204 (*schemaItem)["@odata.id"]}});
1205 }
1206 else
1207 {
1208 BMCWEB_LOG_ERROR
1209 << "failed to find fan in schema";
1210 messages::internalError(
1211 sensorsAsyncResp->res);
1212 return;
1213 }
1214 }
1215
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001216 size_t minNumNeeded =
1217 collection->size() > 0
1218 ? collection->size() - *allowedFailures
1219 : 0;
Ed Tanous271584a2019-07-09 16:24:22 -07001220 nlohmann::json& jResp =
1221 sensorsAsyncResp->res
1222 .jsonValue["Redundancy"];
1223 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001224 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301225 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001226 sensorsAsyncResp->chassisId + "/" +
1227 sensorsAsyncResp->chassisSubNode +
1228 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001229 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001230 {"@odata.type",
1231 "#Redundancy.v1_3_2.Redundancy"},
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001232 {"MinNumNeeded", minNumNeeded},
James Feist8bd25cc2019-03-15 15:14:00 -07001233 {"MemberId", name},
1234 {"Mode", "N+m"},
1235 {"Name", name},
1236 {"RedundancySet", redfishCollection},
1237 {"Status",
1238 {{"Health", health},
1239 {"State", "Enabled"}}}});
1240 },
1241 owner, path, "org.freedesktop.DBus.Properties",
1242 "GetAll",
1243 "xyz.openbmc_project.Control.FanRedundancy");
1244 },
James Feist02e92e32019-06-26 12:07:05 -07001245 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
James Feist8bd25cc2019-03-15 15:14:00 -07001246 "org.freedesktop.DBus.Properties", "Get",
1247 "xyz.openbmc_project.Association", "endpoints");
1248 }
1249 },
1250 "xyz.openbmc_project.ObjectMapper",
1251 "/xyz/openbmc_project/object_mapper",
1252 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1253 "/xyz/openbmc_project/control", 2,
1254 std::array<const char*, 1>{
1255 "xyz.openbmc_project.Control.FanRedundancy"});
1256}
1257
Ed Tanousb5a76932020-09-29 16:16:58 -07001258inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001259 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001260{
Ed Tanous81ce6092020-12-17 16:54:55 +00001261 nlohmann::json& response = sensorsAsyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001262 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001263 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001264 {
1265 sensorHeaders = {"Voltages", "PowerSupplies"};
1266 }
1267 for (const std::string& sensorGroup : sensorHeaders)
1268 {
1269 nlohmann::json::iterator entry = response.find(sensorGroup);
1270 if (entry != response.end())
1271 {
1272 std::sort(entry->begin(), entry->end(),
1273 [](nlohmann::json& c1, nlohmann::json& c2) {
1274 return c1["Name"] < c2["Name"];
1275 });
1276
1277 // add the index counts to the end of each entry
1278 size_t count = 0;
1279 for (nlohmann::json& sensorJson : *entry)
1280 {
1281 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1282 if (odata == sensorJson.end())
1283 {
1284 continue;
1285 }
1286 std::string* value = odata->get_ptr<std::string*>();
1287 if (value != nullptr)
1288 {
1289 *value += std::to_string(count);
1290 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001291 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001292 }
1293 }
1294 }
1295 }
1296}
1297
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001298/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001299 * @brief Finds the inventory item with the specified object path.
1300 * @param inventoryItems D-Bus inventory items associated with sensors.
1301 * @param invItemObjPath D-Bus object path of inventory item.
1302 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001303 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001304inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001305 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001306 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001307{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001308 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001309 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001310 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001311 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001312 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001313 }
1314 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001315 return nullptr;
1316}
1317
1318/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001319 * @brief Finds the inventory item associated with the specified sensor.
1320 * @param inventoryItems D-Bus inventory items associated with sensors.
1321 * @param sensorObjPath D-Bus object path of sensor.
1322 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001323 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001324inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001325 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001326 const std::string& sensorObjPath)
1327{
1328 for (InventoryItem& inventoryItem : *inventoryItems)
1329 {
1330 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1331 {
1332 return &inventoryItem;
1333 }
1334 }
1335 return nullptr;
1336}
1337
1338/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001339 * @brief Finds the inventory item associated with the specified led path.
1340 * @param inventoryItems D-Bus inventory items associated with sensors.
1341 * @param ledObjPath D-Bus object path of led.
1342 * @return Inventory item within vector, or nullptr if no match found.
1343 */
1344inline InventoryItem*
1345 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1346 const std::string& ledObjPath)
1347{
1348 for (InventoryItem& inventoryItem : inventoryItems)
1349 {
1350 if (inventoryItem.ledObjectPath == ledObjPath)
1351 {
1352 return &inventoryItem;
1353 }
1354 }
1355 return nullptr;
1356}
1357
1358/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001359 * @brief Adds inventory item and associated sensor to specified vector.
1360 *
1361 * Adds a new InventoryItem to the vector if necessary. Searches for an
1362 * existing InventoryItem with the specified object path. If not found, one is
1363 * added to the vector.
1364 *
1365 * Next, the specified sensor is added to the set of sensors associated with the
1366 * InventoryItem.
1367 *
1368 * @param inventoryItems D-Bus inventory items associated with sensors.
1369 * @param invItemObjPath D-Bus object path of inventory item.
1370 * @param sensorObjPath D-Bus object path of sensor
1371 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001372inline void addInventoryItem(
1373 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1374 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001375{
1376 // Look for inventory item in vector
1377 InventoryItem* inventoryItem =
1378 findInventoryItem(inventoryItems, invItemObjPath);
1379
1380 // If inventory item doesn't exist in vector, add it
1381 if (inventoryItem == nullptr)
1382 {
1383 inventoryItems->emplace_back(invItemObjPath);
1384 inventoryItem = &(inventoryItems->back());
1385 }
1386
1387 // Add sensor to set of sensors associated with inventory item
1388 inventoryItem->sensors.emplace(sensorObjPath);
1389}
1390
1391/**
1392 * @brief Stores D-Bus data in the specified inventory item.
1393 *
1394 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1395 * specified InventoryItem.
1396 *
1397 * This data is later used to provide sensor property values in the JSON
1398 * response.
1399 *
1400 * @param inventoryItem Inventory item where data will be stored.
1401 * @param interfacesDict Map containing D-Bus interfaces and their properties
1402 * for the specified inventory item.
1403 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001404inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001405 InventoryItem& inventoryItem,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001406 const boost::container::flat_map<
1407 std::string, boost::container::flat_map<std::string, SensorVariant>>&
1408 interfacesDict)
1409{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001410 // Get properties from Inventory.Item interface
1411 auto interfaceIt =
1412 interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1413 if (interfaceIt != interfacesDict.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001414 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001415 auto propertyIt = interfaceIt->second.find("Present");
1416 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001417 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001418 const bool* value = std::get_if<bool>(&propertyIt->second);
1419 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001420 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421 inventoryItem.isPresent = *value;
1422 }
1423 }
1424 }
1425
1426 // Check if Inventory.Item.PowerSupply interface is present
1427 interfaceIt =
1428 interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1429 if (interfaceIt != interfacesDict.end())
1430 {
1431 inventoryItem.isPowerSupply = true;
1432 }
1433
1434 // Get properties from Inventory.Decorator.Asset interface
1435 interfaceIt =
1436 interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1437 if (interfaceIt != interfacesDict.end())
1438 {
1439 auto propertyIt = interfaceIt->second.find("Manufacturer");
1440 if (propertyIt != interfaceIt->second.end())
1441 {
1442 const std::string* value =
1443 std::get_if<std::string>(&propertyIt->second);
1444 if (value != nullptr)
1445 {
1446 inventoryItem.manufacturer = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001447 }
1448 }
1449
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001450 propertyIt = interfaceIt->second.find("Model");
1451 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001452 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001453 const std::string* value =
1454 std::get_if<std::string>(&propertyIt->second);
1455 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001456 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001457 inventoryItem.model = *value;
1458 }
1459 }
1460
1461 propertyIt = interfaceIt->second.find("PartNumber");
1462 if (propertyIt != interfaceIt->second.end())
1463 {
1464 const std::string* value =
1465 std::get_if<std::string>(&propertyIt->second);
1466 if (value != nullptr)
1467 {
1468 inventoryItem.partNumber = *value;
1469 }
1470 }
1471
1472 propertyIt = interfaceIt->second.find("SerialNumber");
1473 if (propertyIt != interfaceIt->second.end())
1474 {
1475 const std::string* value =
1476 std::get_if<std::string>(&propertyIt->second);
1477 if (value != nullptr)
1478 {
1479 inventoryItem.serialNumber = *value;
1480 }
1481 }
1482 }
1483
1484 // Get properties from State.Decorator.OperationalStatus interface
1485 interfaceIt = interfacesDict.find(
1486 "xyz.openbmc_project.State.Decorator.OperationalStatus");
1487 if (interfaceIt != interfacesDict.end())
1488 {
1489 auto propertyIt = interfaceIt->second.find("Functional");
1490 if (propertyIt != interfaceIt->second.end())
1491 {
1492 const bool* value = std::get_if<bool>(&propertyIt->second);
1493 if (value != nullptr)
1494 {
1495 inventoryItem.isFunctional = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001496 }
1497 }
1498 }
1499}
1500
1501/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001502 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001503 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001504 * Uses the specified connections (services) to obtain D-Bus data for inventory
1505 * items associated with sensors. Stores the resulting data in the
1506 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001507 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001508 * This data is later used to provide sensor property values in the JSON
1509 * response.
1510 *
1511 * Finds the inventory item data asynchronously. Invokes callback when data has
1512 * been obtained.
1513 *
1514 * The callback must have the following signature:
1515 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001516 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001517 * @endcode
1518 *
1519 * This function is called recursively, obtaining data asynchronously from one
1520 * connection in each call. This ensures the callback is not invoked until the
1521 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001522 *
1523 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001524 * @param inventoryItems D-Bus inventory items associated with sensors.
1525 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001526 * @param objectMgrPaths Mappings from connection name to DBus object path that
1527 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001528 * @param callback Callback to invoke when inventory data has been obtained.
1529 * @param invConnectionsIndex Current index in invConnections. Only specified
1530 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001531 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001532template <typename Callback>
1533static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001535 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001536 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1537 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001538 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001539 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001540{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001541 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001542
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001543 // If no more connections left, call callback
1544 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001545 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001546 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001547 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1548 return;
1549 }
1550
1551 // Get inventory item data from current connection
1552 auto it = invConnections->nth(invConnectionsIndex);
1553 if (it != invConnections->end())
1554 {
1555 const std::string& invConnection = *it;
1556
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001557 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001558 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1559 objectMgrPaths, callback{std::move(callback)},
1560 invConnectionsIndex](
1561 const boost::system::error_code ec,
1562 ManagedObjectsVectorType& resp) {
1563 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001564 if (ec)
1565 {
1566 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001567 << "getInventoryItemsData respHandler DBus error " << ec;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001568 messages::internalError(sensorsAsyncResp->res);
1569 return;
1570 }
1571
1572 // Loop through returned object paths
1573 for (const auto& objDictEntry : resp)
1574 {
1575 const std::string& objPath =
1576 static_cast<const std::string&>(objDictEntry.first);
1577
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001578 // If this object path is one of the specified inventory items
1579 InventoryItem* inventoryItem =
1580 findInventoryItem(inventoryItems, objPath);
1581 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001582 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001583 // Store inventory data in InventoryItem
1584 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001585 }
1586 }
1587
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001588 // Recurse to get inventory item data from next connection
1589 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1590 invConnections, objectMgrPaths,
1591 std::move(callback), invConnectionsIndex + 1);
1592
1593 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001594 };
1595
1596 // Find DBus object path that implements ObjectManager for the current
1597 // connection. If no mapping found, default to "/".
1598 auto iter = objectMgrPaths->find(invConnection);
1599 const std::string& objectMgrPath =
1600 (iter != objectMgrPaths->end()) ? iter->second : "/";
1601 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1602 << objectMgrPath;
1603
1604 // Get all object paths and their interfaces for current connection
1605 crow::connections::systemBus->async_method_call(
1606 std::move(respHandler), invConnection, objectMgrPath,
1607 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1608 }
1609
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001610 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611}
1612
1613/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001614 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001615 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001616 * Gets the D-Bus connections (services) that provide data for the inventory
1617 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618 *
1619 * Finds the connections asynchronously. Invokes callback when information has
1620 * been obtained.
1621 *
1622 * The callback must have the following signature:
1623 * @code
1624 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1625 * invConnections)
1626 * @endcode
1627 *
1628 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001629 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001630 * @param callback Callback to invoke when connections have been obtained.
1631 */
1632template <typename Callback>
1633static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001634 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1635 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001636 Callback&& callback)
1637{
1638 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1639
1640 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001641 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001643 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1644 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001645 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1646
1647 // Response handler for parsing output from GetSubTree
1648 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001649 inventoryItems](const boost::system::error_code ec,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001650 const GetSubTreeType& subtree) {
1651 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1652 if (ec)
1653 {
1654 messages::internalError(sensorsAsyncResp->res);
1655 BMCWEB_LOG_ERROR
1656 << "getInventoryItemsConnections respHandler DBus error " << ec;
1657 return;
1658 }
1659
1660 // Make unique list of connections for desired inventory items
1661 std::shared_ptr<boost::container::flat_set<std::string>>
1662 invConnections =
1663 std::make_shared<boost::container::flat_set<std::string>>();
1664 invConnections->reserve(8);
1665
1666 // Loop through objects from GetSubTree
1667 for (const std::pair<
1668 std::string,
1669 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1670 object : subtree)
1671 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001672 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001673 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001674 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001675 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001676 // Store all connections to inventory item
1677 for (const std::pair<std::string, std::vector<std::string>>&
1678 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001679 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001680 const std::string& invConnection = objData.first;
1681 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001682 }
1683 }
1684 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001685
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001686 callback(invConnections);
1687 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1688 };
1689
1690 // Make call to ObjectMapper to find all inventory items
1691 crow::connections::systemBus->async_method_call(
1692 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1693 "/xyz/openbmc_project/object_mapper",
1694 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1695 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1696}
1697
1698/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001699 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001700 *
1701 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001702 * inventory items. Then finds the associations from those inventory items to
1703 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001704 *
1705 * Finds the inventory items asynchronously. Invokes callback when information
1706 * has been obtained.
1707 *
1708 * The callback must have the following signature:
1709 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001710 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001711 * @endcode
1712 *
1713 * @param sensorsAsyncResp Pointer to object holding response data.
1714 * @param sensorNames All sensors within the current chassis.
1715 * @param objectMgrPaths Mappings from connection name to DBus object path that
1716 * implements ObjectManager.
1717 * @param callback Callback to invoke when inventory items have been obtained.
1718 */
1719template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001720static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001721 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1722 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1723 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001724 objectMgrPaths,
1725 Callback&& callback)
1726{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001727 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001728
1729 // Response handler for GetManagedObjects
1730 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1731 sensorNames](const boost::system::error_code ec,
1732 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001733 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001734 if (ec)
1735 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001736 BMCWEB_LOG_ERROR
1737 << "getInventoryItemAssociations respHandler DBus error " << ec;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001738 messages::internalError(sensorsAsyncResp->res);
1739 return;
1740 }
1741
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001742 // Create vector to hold list of inventory items
1743 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1744 std::make_shared<std::vector<InventoryItem>>();
1745
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001746 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001747 std::string sensorAssocPath;
1748 sensorAssocPath.reserve(128); // avoid memory allocations
1749 for (const auto& objDictEntry : resp)
1750 {
1751 const std::string& objPath =
1752 static_cast<const std::string&>(objDictEntry.first);
1753 const boost::container::flat_map<
1754 std::string, boost::container::flat_map<
1755 std::string, dbus::utility::DbusVariantType>>&
1756 interfacesDict = objDictEntry.second;
1757
1758 // If path is inventory association for one of the specified sensors
1759 for (const std::string& sensorName : *sensorNames)
1760 {
1761 sensorAssocPath = sensorName;
1762 sensorAssocPath += "/inventory";
1763 if (objPath == sensorAssocPath)
1764 {
1765 // Get Association interface for object path
1766 auto assocIt =
1767 interfacesDict.find("xyz.openbmc_project.Association");
1768 if (assocIt != interfacesDict.end())
1769 {
1770 // Get inventory item from end point
1771 auto endpointsIt = assocIt->second.find("endpoints");
1772 if (endpointsIt != assocIt->second.end())
1773 {
1774 const std::vector<std::string>* endpoints =
1775 std::get_if<std::vector<std::string>>(
1776 &endpointsIt->second);
1777 if ((endpoints != nullptr) && !endpoints->empty())
1778 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001779 // Add inventory item to vector
1780 const std::string& invItemPath =
1781 endpoints->front();
1782 addInventoryItem(inventoryItems, invItemPath,
1783 sensorName);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001784 }
1785 }
1786 }
1787 break;
1788 }
1789 }
1790 }
1791
Anthony Wilsond5005492019-07-31 16:34:17 -05001792 // Now loop through the returned object paths again, this time to
1793 // find the leds associated with the inventory items we just found
1794 std::string inventoryAssocPath;
1795 inventoryAssocPath.reserve(128); // avoid memory allocations
1796 for (const auto& objDictEntry : resp)
1797 {
1798 const std::string& objPath =
1799 static_cast<const std::string&>(objDictEntry.first);
1800 const boost::container::flat_map<
1801 std::string, boost::container::flat_map<
1802 std::string, dbus::utility::DbusVariantType>>&
1803 interfacesDict = objDictEntry.second;
1804
1805 for (InventoryItem& inventoryItem : *inventoryItems)
1806 {
1807 inventoryAssocPath = inventoryItem.objectPath;
1808 inventoryAssocPath += "/leds";
1809 if (objPath == inventoryAssocPath)
1810 {
1811 // Get Association interface for object path
1812 auto assocIt =
1813 interfacesDict.find("xyz.openbmc_project.Association");
1814 if (assocIt != interfacesDict.end())
1815 {
1816 // Get inventory item from end point
1817 auto endpointsIt = assocIt->second.find("endpoints");
1818 if (endpointsIt != assocIt->second.end())
1819 {
1820 const std::vector<std::string>* endpoints =
1821 std::get_if<std::vector<std::string>>(
1822 &endpointsIt->second);
1823 if ((endpoints != nullptr) && !endpoints->empty())
1824 {
1825 // Store LED path in inventory item
1826 const std::string& ledPath = endpoints->front();
1827 inventoryItem.ledObjectPath = ledPath;
1828 }
1829 }
1830 }
1831 break;
1832 }
1833 }
1834 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001835 callback(inventoryItems);
1836 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001837 };
1838
1839 // Find DBus object path that implements ObjectManager for ObjectMapper
1840 std::string connection = "xyz.openbmc_project.ObjectMapper";
1841 auto iter = objectMgrPaths->find(connection);
1842 const std::string& objectMgrPath =
1843 (iter != objectMgrPaths->end()) ? iter->second : "/";
1844 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1845 << objectMgrPath;
1846
1847 // Call GetManagedObjects on the ObjectMapper to get all associations
1848 crow::connections::systemBus->async_method_call(
1849 std::move(respHandler), connection, objectMgrPath,
1850 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1851
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001852 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001853}
1854
1855/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001856 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1857 *
1858 * Uses the specified connections (services) to obtain D-Bus data for inventory
1859 * item leds associated with sensors. Stores the resulting data in the
1860 * inventoryItems vector.
1861 *
1862 * This data is later used to provide sensor property values in the JSON
1863 * response.
1864 *
1865 * Finds the inventory item led data asynchronously. Invokes callback when data
1866 * has been obtained.
1867 *
1868 * The callback must have the following signature:
1869 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001870 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001871 * @endcode
1872 *
1873 * This function is called recursively, obtaining data asynchronously from one
1874 * connection in each call. This ensures the callback is not invoked until the
1875 * last asynchronous function has completed.
1876 *
1877 * @param sensorsAsyncResp Pointer to object holding response data.
1878 * @param inventoryItems D-Bus inventory items associated with sensors.
1879 * @param ledConnections Connections that provide data for the inventory leds.
1880 * @param callback Callback to invoke when inventory data has been obtained.
1881 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1882 * in recursive calls to this function.
1883 */
1884template <typename Callback>
1885void getInventoryLedData(
1886 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1887 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1888 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1889 ledConnections,
1890 Callback&& callback, size_t ledConnectionsIndex = 0)
1891{
1892 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1893
1894 // If no more connections left, call callback
1895 if (ledConnectionsIndex >= ledConnections->size())
1896 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001897 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001898 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1899 return;
1900 }
1901
1902 // Get inventory item data from current connection
1903 auto it = ledConnections->nth(ledConnectionsIndex);
1904 if (it != ledConnections->end())
1905 {
1906 const std::string& ledPath = (*it).first;
1907 const std::string& ledConnection = (*it).second;
1908 // Response handler for Get State property
1909 auto respHandler =
1910 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1911 callback{std::move(callback)},
1912 ledConnectionsIndex](const boost::system::error_code ec,
1913 const std::variant<std::string>& ledState) {
1914 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1915 if (ec)
1916 {
1917 BMCWEB_LOG_ERROR
1918 << "getInventoryLedData respHandler DBus error " << ec;
1919 messages::internalError(sensorsAsyncResp->res);
1920 return;
1921 }
1922
1923 const std::string* state = std::get_if<std::string>(&ledState);
1924 if (state != nullptr)
1925 {
1926 BMCWEB_LOG_DEBUG << "Led state: " << *state;
1927 // Find inventory item with this LED object path
1928 InventoryItem* inventoryItem =
1929 findInventoryItemForLed(*inventoryItems, ledPath);
1930 if (inventoryItem != nullptr)
1931 {
1932 // Store LED state in InventoryItem
1933 if (boost::ends_with(*state, "On"))
1934 {
1935 inventoryItem->ledState = LedState::ON;
1936 }
1937 else if (boost::ends_with(*state, "Blink"))
1938 {
1939 inventoryItem->ledState = LedState::BLINK;
1940 }
1941 else if (boost::ends_with(*state, "Off"))
1942 {
1943 inventoryItem->ledState = LedState::OFF;
1944 }
1945 else
1946 {
1947 inventoryItem->ledState = LedState::UNKNOWN;
1948 }
1949 }
1950 }
1951 else
1952 {
1953 BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
1954 << ledPath;
1955 }
1956
1957 // Recurse to get LED data from next connection
1958 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1959 ledConnections, std::move(callback),
1960 ledConnectionsIndex + 1);
1961
1962 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1963 };
1964
1965 // Get the State property for the current LED
1966 crow::connections::systemBus->async_method_call(
1967 std::move(respHandler), ledConnection, ledPath,
1968 "org.freedesktop.DBus.Properties", "Get",
1969 "xyz.openbmc_project.Led.Physical", "State");
1970 }
1971
1972 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1973}
1974
1975/**
1976 * @brief Gets LED data for LEDs associated with given inventory items.
1977 *
1978 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1979 * associated with the specified inventory items. Then gets the LED data from
1980 * each connection and stores it in the inventory item.
1981 *
1982 * This data is later used to provide sensor property values in the JSON
1983 * response.
1984 *
1985 * Finds the LED data asynchronously. Invokes callback when information has
1986 * been obtained.
1987 *
1988 * The callback must have the following signature:
1989 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001990 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001991 * @endcode
1992 *
1993 * @param sensorsAsyncResp Pointer to object holding response data.
1994 * @param inventoryItems D-Bus inventory items associated with sensors.
1995 * @param callback Callback to invoke when inventory items have been obtained.
1996 */
1997template <typename Callback>
1998void getInventoryLeds(
1999 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2000 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2001 Callback&& callback)
2002{
2003 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2004
2005 const std::string path = "/xyz/openbmc_project";
2006 const std::array<std::string, 1> interfaces = {
2007 "xyz.openbmc_project.Led.Physical"};
2008
2009 // Response handler for parsing output from GetSubTree
2010 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2011 inventoryItems](const boost::system::error_code ec,
2012 const GetSubTreeType& subtree) {
2013 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2014 if (ec)
2015 {
2016 messages::internalError(sensorsAsyncResp->res);
2017 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2018 << ec;
2019 return;
2020 }
2021
2022 // Build map of LED object paths to connections
2023 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2024 ledConnections = std::make_shared<
2025 boost::container::flat_map<std::string, std::string>>();
2026
2027 // Loop through objects from GetSubTree
2028 for (const std::pair<
2029 std::string,
2030 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2031 object : subtree)
2032 {
2033 // Check if object path is LED for one of the specified inventory
2034 // items
2035 const std::string& ledPath = object.first;
2036 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2037 {
2038 // Add mapping from ledPath to connection
2039 const std::string& connection = object.second.begin()->first;
2040 (*ledConnections)[ledPath] = connection;
2041 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2042 << connection;
2043 }
2044 }
2045
2046 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2047 std::move(callback));
2048 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2049 };
2050 // Make call to ObjectMapper to find all inventory items
2051 crow::connections::systemBus->async_method_call(
2052 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2053 "/xyz/openbmc_project/object_mapper",
2054 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2055 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2056}
2057
2058/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002059 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2060 *
2061 * Uses the specified connections (services) (currently assumes just one) to
2062 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2063 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2064 *
2065 * This data is later used to provide sensor property values in the JSON
2066 * response.
2067 *
2068 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2069 * when data has been obtained.
2070 *
2071 * The callback must have the following signature:
2072 * @code
2073 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2074 * @endcode
2075 *
2076 * @param sensorsAsyncResp Pointer to object holding response data.
2077 * @param inventoryItems D-Bus inventory items associated with sensors.
2078 * @param psAttributesConnections Connections that provide data for the Power
2079 * Supply Attributes
2080 * @param callback Callback to invoke when data has been obtained.
2081 */
2082template <typename Callback>
2083void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002084 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002085 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2086 const boost::container::flat_map<std::string, std::string>&
2087 psAttributesConnections,
2088 Callback&& callback)
2089{
2090 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2091
2092 if (psAttributesConnections.empty())
2093 {
2094 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2095 callback(inventoryItems);
2096 return;
2097 }
2098
2099 // Assuming just one connection (service) for now
2100 auto it = psAttributesConnections.nth(0);
2101
2102 const std::string& psAttributesPath = (*it).first;
2103 const std::string& psAttributesConnection = (*it).second;
2104
2105 // Response handler for Get DeratingFactor property
2106 auto respHandler = [sensorsAsyncResp, inventoryItems,
2107 callback{std::move(callback)}](
2108 const boost::system::error_code ec,
2109 const std::variant<uint32_t>& deratingFactor) {
2110 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2111 if (ec)
2112 {
2113 BMCWEB_LOG_ERROR
2114 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
2115 messages::internalError(sensorsAsyncResp->res);
2116 return;
2117 }
2118
2119 const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
2120 if (value != nullptr)
2121 {
2122 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
2123 // Store value in Power Supply Inventory Items
2124 for (InventoryItem& inventoryItem : *inventoryItems)
2125 {
2126 if (inventoryItem.isPowerSupply == true)
2127 {
2128 inventoryItem.powerSupplyEfficiencyPercent =
2129 static_cast<int>(*value);
2130 }
2131 }
2132 }
2133 else
2134 {
2135 BMCWEB_LOG_DEBUG
2136 << "Failed to find EfficiencyPercent value for PowerSupplies";
2137 }
2138
2139 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2140 callback(inventoryItems);
2141 };
2142
2143 // Get the DeratingFactor property for the PowerSupplyAttributes
2144 // Currently only property on the interface/only one we care about
2145 crow::connections::systemBus->async_method_call(
2146 std::move(respHandler), psAttributesConnection, psAttributesPath,
2147 "org.freedesktop.DBus.Properties", "Get",
2148 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
2149
2150 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2151}
2152
2153/**
2154 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2155 *
2156 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2157 * data. Then gets the Power Supply Attributes data from the connection
2158 * (currently just assumes 1 connection) and stores the data in the inventory
2159 * item.
2160 *
2161 * This data is later used to provide sensor property values in the JSON
2162 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2163 *
2164 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2165 * when information has been obtained.
2166 *
2167 * The callback must have the following signature:
2168 * @code
2169 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2170 * @endcode
2171 *
2172 * @param sensorsAsyncResp Pointer to object holding response data.
2173 * @param inventoryItems D-Bus inventory items associated with sensors.
2174 * @param callback Callback to invoke when data has been obtained.
2175 */
2176template <typename Callback>
2177void getPowerSupplyAttributes(
2178 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2179 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2180 Callback&& callback)
2181{
2182 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2183
2184 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002185 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002186 {
2187 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2188 callback(inventoryItems);
2189 return;
2190 }
2191
2192 const std::array<std::string, 1> interfaces = {
2193 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2194
2195 // Response handler for parsing output from GetSubTree
2196 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2197 inventoryItems](const boost::system::error_code ec,
2198 const GetSubTreeType& subtree) {
2199 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2200 if (ec)
2201 {
2202 messages::internalError(sensorsAsyncResp->res);
2203 BMCWEB_LOG_ERROR
2204 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2205 return;
2206 }
2207 if (subtree.size() == 0)
2208 {
2209 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2210 callback(inventoryItems);
2211 return;
2212 }
2213
2214 // Currently we only support 1 power supply attribute, use this for
2215 // all the power supplies. Build map of object path to connection.
2216 // Assume just 1 connection and 1 path for now.
2217 boost::container::flat_map<std::string, std::string>
2218 psAttributesConnections;
2219
2220 if (subtree[0].first.empty() || subtree[0].second.empty())
2221 {
2222 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2223 callback(inventoryItems);
2224 return;
2225 }
2226
2227 const std::string& psAttributesPath = subtree[0].first;
2228 const std::string& connection = subtree[0].second.begin()->first;
2229
2230 if (connection.empty())
2231 {
2232 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2233 callback(inventoryItems);
2234 return;
2235 }
2236
2237 psAttributesConnections[psAttributesPath] = connection;
2238 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2239 << connection;
2240
2241 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2242 psAttributesConnections,
2243 std::move(callback));
2244 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2245 };
2246 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2247 crow::connections::systemBus->async_method_call(
2248 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2249 "/xyz/openbmc_project/object_mapper",
2250 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2251 "/xyz/openbmc_project", 0, interfaces);
2252 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2253}
2254
2255/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002257 *
2258 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002259 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002260 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002261 * This data is later used to provide sensor property values in the JSON
2262 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002263 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002264 * Finds the inventory items asynchronously. Invokes callback when the
2265 * inventory items have been obtained.
2266 *
2267 * The callback must have the following signature:
2268 * @code
2269 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2270 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002271 *
2272 * @param sensorsAsyncResp Pointer to object holding response data.
2273 * @param sensorNames All sensors within the current chassis.
2274 * @param objectMgrPaths Mappings from connection name to DBus object path that
2275 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002276 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002277 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002278template <typename Callback>
2279static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002280 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2281 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2282 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002283 objectMgrPaths,
2284 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002285{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002286 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2287 auto getInventoryItemAssociationsCb =
2288 [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2289 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2290 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002291 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002292 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2293 callback{std::move(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002294 std::shared_ptr<boost::container::flat_set<std::string>>
2295 invConnections) {
2296 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002297 auto getInventoryItemsDataCb =
2298 [sensorsAsyncResp, inventoryItems,
2299 callback{std::move(callback)}]() {
2300 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002301
2302 auto getInventoryLedsCb = [sensorsAsyncResp,
2303 inventoryItems,
2304 callback{std::move(
2305 callback)}]() {
2306 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2307 // Find Power Supply Attributes and get the data
2308 getPowerSupplyAttributes(sensorsAsyncResp,
2309 inventoryItems,
2310 std::move(callback));
2311 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2312 };
2313
Anthony Wilsond5005492019-07-31 16:34:17 -05002314 // Find led connections and get the data
2315 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002316 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002317 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2318 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002319
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002320 // Get inventory item data from connections
2321 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2322 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002323 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002324 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2325 };
2326
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002327 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002328 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002329 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002330 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002331 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002332 };
2333
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002334 // Get associations from sensors to inventory items
2335 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2336 std::move(getInventoryItemAssociationsCb));
2337 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2338}
2339
2340/**
2341 * @brief Returns JSON PowerSupply object for the specified inventory item.
2342 *
2343 * Searches for a JSON PowerSupply object that matches the specified inventory
2344 * item. If one is not found, a new PowerSupply object is added to the JSON
2345 * array.
2346 *
2347 * Multiple sensors are often associated with one power supply inventory item.
2348 * As a result, multiple sensor values are stored in one JSON PowerSupply
2349 * object.
2350 *
2351 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2352 * @param inventoryItem Inventory item for the power supply.
2353 * @param chassisId Chassis that contains the power supply.
2354 * @return JSON PowerSupply object for the specified inventory item.
2355 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002356inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002357 const InventoryItem& inventoryItem,
2358 const std::string& chassisId)
2359{
2360 // Check if matching PowerSupply object already exists in JSON array
2361 for (nlohmann::json& powerSupply : powerSupplyArray)
2362 {
2363 if (powerSupply["MemberId"] == inventoryItem.name)
2364 {
2365 return powerSupply;
2366 }
2367 }
2368
2369 // Add new PowerSupply object to JSON array
2370 powerSupplyArray.push_back({});
2371 nlohmann::json& powerSupply = powerSupplyArray.back();
2372 powerSupply["@odata.id"] =
2373 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2374 powerSupply["MemberId"] = inventoryItem.name;
2375 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2376 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2377 powerSupply["Model"] = inventoryItem.model;
2378 powerSupply["PartNumber"] = inventoryItem.partNumber;
2379 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002380 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002381
Gunnar Mills42cbe532019-08-15 15:26:54 -05002382 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2383 {
2384 powerSupply["EfficiencyPercent"] =
2385 inventoryItem.powerSupplyEfficiencyPercent;
2386 }
2387
2388 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002389 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2390 powerSupply["Status"]["Health"] = health;
2391
2392 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002393}
2394
2395/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002396 * @brief Gets the values of the specified sensors.
2397 *
2398 * Stores the results as JSON in the SensorsAsyncResp.
2399 *
2400 * Gets the sensor values asynchronously. Stores the results later when the
2401 * information has been obtained.
2402 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002403 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002404 *
2405 * To minimize the number of DBus calls, the DBus method
2406 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2407 * values of all sensors provided by a connection (service).
2408 *
2409 * The connections set contains all the connections that provide sensor values.
2410 *
2411 * The objectMgrPaths map contains mappings from a connection name to the
2412 * corresponding DBus object path that implements ObjectManager.
2413 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002414 * The InventoryItem vector contains D-Bus inventory items associated with the
2415 * sensors. Inventory item data is needed for some Redfish sensor properties.
2416 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002417 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002418 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002419 * @param connections Connections that provide sensor values.
2420 * @param objectMgrPaths Mappings from connection name to DBus object path that
2421 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002422 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002423 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002424inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002425 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002426 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002427 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002428 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002429 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002430 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002431{
2432 BMCWEB_LOG_DEBUG << "getSensorData enter";
2433 // Get managed objects from all services exposing sensors
2434 for (const std::string& connection : connections)
2435 {
2436 // Response handler to process managed objects
Ed Tanous81ce6092020-12-17 16:54:55 +00002437 auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002438 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002439 const boost::system::error_code ec,
2440 ManagedObjectsVectorType& resp) {
2441 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2442 if (ec)
2443 {
2444 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
Ed Tanous81ce6092020-12-17 16:54:55 +00002445 messages::internalError(sensorsAsyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002446 return;
2447 }
2448 // Go through all objects and update response with sensor data
2449 for (const auto& objDictEntry : resp)
2450 {
2451 const std::string& objPath =
2452 static_cast<const std::string&>(objDictEntry.first);
2453 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2454 << objPath;
2455
Shawn McCarneyde629b62019-03-08 10:42:51 -06002456 std::vector<std::string> split;
2457 // Reserve space for
2458 // /xyz/openbmc_project/sensors/<name>/<subname>
2459 split.reserve(6);
2460 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2461 if (split.size() < 6)
2462 {
2463 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2464 << objPath;
2465 continue;
2466 }
2467 // These indexes aren't intuitive, as boost::split puts an empty
2468 // string at the beginning
2469 const std::string& sensorType = split[4];
2470 const std::string& sensorName = split[5];
2471 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2472 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002473 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002474 {
2475 BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2476 continue;
2477 }
2478
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002479 // Find inventory item (if any) associated with sensor
2480 InventoryItem* inventoryItem =
2481 findInventoryItemForSensor(inventoryItems, objPath);
2482
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002483 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002484 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002485
2486 nlohmann::json* sensorJson = nullptr;
2487
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002488 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002489 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002490 sensorsAsyncResp->res.jsonValue["@odata.id"] =
2491 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2492 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002493 sensorName;
Ed Tanous81ce6092020-12-17 16:54:55 +00002494 sensorJson = &(sensorsAsyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002495 }
2496 else
2497 {
Ed Tanous271584a2019-07-09 16:24:22 -07002498 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002499 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002500 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002501 fieldName = "Temperatures";
2502 }
2503 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2504 sensorType == "fan_pwm")
2505 {
2506 fieldName = "Fans";
2507 }
2508 else if (sensorType == "voltage")
2509 {
2510 fieldName = "Voltages";
2511 }
2512 else if (sensorType == "power")
2513 {
2514 if (!sensorName.compare("total_power"))
2515 {
2516 fieldName = "PowerControl";
2517 }
2518 else if ((inventoryItem != nullptr) &&
2519 (inventoryItem->isPowerSupply))
2520 {
2521 fieldName = "PowerSupplies";
2522 }
2523 else
2524 {
2525 // Other power sensors are in SensorCollection
2526 continue;
2527 }
2528 }
2529 else
2530 {
2531 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2532 << sensorType;
2533 continue;
2534 }
2535
2536 nlohmann::json& tempArray =
Ed Tanous81ce6092020-12-17 16:54:55 +00002537 sensorsAsyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002538 if (fieldName == "PowerControl")
2539 {
2540 if (tempArray.empty())
2541 {
2542 // Put multiple "sensors" into a single
2543 // PowerControl. Follows MemberId naming and
2544 // naming in power.hpp.
2545 tempArray.push_back(
2546 {{"@odata.id",
2547 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002548 sensorsAsyncResp->chassisId + "/" +
2549 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002550 fieldName + "/0"}});
2551 }
2552 sensorJson = &(tempArray.back());
2553 }
2554 else if (fieldName == "PowerSupplies")
2555 {
2556 if (inventoryItem != nullptr)
2557 {
2558 sensorJson =
2559 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002560 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002561 }
2562 }
2563 else
2564 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002565 tempArray.push_back(
2566 {{"@odata.id",
2567 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002568 sensorsAsyncResp->chassisId + "/" +
2569 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002570 fieldName + "/"}});
2571 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002572 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002573 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002574
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002575 if (sensorJson != nullptr)
2576 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002577 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002578 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002579 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002580 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002581 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002582 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002583 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002584 sortJSONResponse(sensorsAsyncResp);
2585 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002586 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002587 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002588 }
James Feist8bd25cc2019-03-15 15:14:00 -07002589 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002590 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2591 };
2592
2593 // Find DBus object path that implements ObjectManager for the current
2594 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002595 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002596 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002597 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002598 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2599 << objectMgrPath;
2600
2601 crow::connections::systemBus->async_method_call(
2602 getManagedObjectsCb, connection, objectMgrPath,
2603 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002604 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002605 BMCWEB_LOG_DEBUG << "getSensorData exit";
2606}
2607
Ed Tanous23a21a12020-07-25 04:45:05 +00002608inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002609 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002610 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002611{
2612 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002613 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002614 const boost::container::flat_set<std::string>& connections) {
2615 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2616 auto getObjectManagerPathsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002617 [sensorsAsyncResp, sensorNames,
Ed Tanousb5a76932020-09-29 16:16:58 -07002618 connections](const std::shared_ptr<boost::container::flat_map<
2619 std::string, std::string>>& objectMgrPaths) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002620 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2621 auto getInventoryItemsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002622 [sensorsAsyncResp, sensorNames, connections,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002623 objectMgrPaths](
Ed Tanousf23b7292020-10-15 09:41:17 -07002624 const std::shared_ptr<std::vector<InventoryItem>>&
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002625 inventoryItems) {
2626 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2627 // Get sensor data and store results in JSON
Ed Tanous81ce6092020-12-17 16:54:55 +00002628 getSensorData(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002629 connections, objectMgrPaths,
Ed Tanousf23b7292020-10-15 09:41:17 -07002630 inventoryItems);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002631 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2632 };
2633
2634 // Get inventory items associated with sensors
Ed Tanous81ce6092020-12-17 16:54:55 +00002635 getInventoryItems(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002636 objectMgrPaths,
2637 std::move(getInventoryItemsCb));
2638
2639 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2640 };
2641
2642 // Get mapping from connection names to the DBus object
2643 // paths that implement the ObjectManager interface
Ed Tanous81ce6092020-12-17 16:54:55 +00002644 getObjectManagerPaths(sensorsAsyncResp,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002645 std::move(getObjectManagerPathsCb));
2646 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2647 };
2648
2649 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002650 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002651}
2652
Shawn McCarneyde629b62019-03-08 10:42:51 -06002653/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002654 * @brief Entry point for retrieving sensors data related to requested
2655 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002656 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002657 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002658inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002659 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002660{
2661 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002662 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002663 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002664 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002665 sensorNames) {
2666 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Ed Tanous81ce6092020-12-17 16:54:55 +00002667 processSensorList(sensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002668 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2669 };
Ed Tanous81ce6092020-12-17 16:54:55 +00002670 sensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002671
Shawn McCarney26f03892019-05-03 13:20:24 -05002672 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002673 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002674 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002675}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002676
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302677/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002678 * @brief Find the requested sensorName in the list of all sensors supplied by
2679 * the chassis node
2680 *
2681 * @param sensorName The sensor name supplied in the PATCH request
2682 * @param sensorsList The list of sensors managed by the chassis node
2683 * @param sensorsModified The list of sensors that were found as a result of
2684 * repeated calls to this function
2685 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002686inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302687 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002688 boost::container::flat_set<std::string>& sensorsList,
2689 boost::container::flat_set<std::string>& sensorsModified)
2690{
George Liu28aa8de2021-02-01 15:13:30 +08002691 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002692 {
George Liu28aa8de2021-02-01 15:13:30 +08002693 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002694 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002695 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002696 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002697 continue;
2698 }
2699 if (thisSensorName == sensorName)
2700 {
2701 sensorsModified.emplace(chassisSensor);
2702 return true;
2703 }
2704 }
2705 return false;
2706}
2707
2708/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302709 * @brief Entry point for overriding sensor values of given sensor
2710 *
2711 * @param res response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002712 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002713 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302714 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002715inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002716 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002717 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002718 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302719{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002720 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002721 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302722
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302723 const char* propertyValueName;
2724 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302725 std::string memberId;
2726 double value;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302727 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302728 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302729 if (collectionItems.first == "Temperatures")
2730 {
2731 propertyValueName = "ReadingCelsius";
2732 }
2733 else if (collectionItems.first == "Fans")
2734 {
2735 propertyValueName = "Reading";
2736 }
2737 else
2738 {
2739 propertyValueName = "ReadingVolts";
2740 }
2741 for (auto& item : collectionItems.second)
2742 {
Carol Wang4bb3dc32019-10-17 18:15:02 +08002743 if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId",
2744 memberId, propertyValueName, value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302745 {
2746 return;
2747 }
2748 overrideMap.emplace(memberId,
2749 std::make_pair(value, collectionItems.first));
2750 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302751 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002752
Ed Tanousb5a76932020-09-29 16:16:58 -07002753 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2754 const std::shared_ptr<
2755 boost::container::flat_set<
2756 std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002757 // Match sensor names in the PATCH request to those managed by the
2758 // chassis node
2759 const std::shared_ptr<boost::container::flat_set<std::string>>
2760 sensorNames =
2761 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302762 for (const auto& item : overrideMap)
2763 {
2764 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002765 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2766 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302767 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302768 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302769 messages::resourceNotFound(sensorAsyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302770 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302771 return;
2772 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302773 }
2774 // Get the connection to which the memberId belongs
2775 auto getObjectsWithConnectionCb =
2776 [sensorAsyncResp, overrideMap](
Ed Tanouscb13a392020-07-25 19:02:03 +00002777 const boost::container::flat_set<std::string>& /*connections*/,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302778 const std::set<std::pair<std::string, std::string>>&
2779 objectsWithConnection) {
2780 if (objectsWithConnection.size() != overrideMap.size())
2781 {
2782 BMCWEB_LOG_INFO
2783 << "Unable to find all objects with proper connection "
2784 << objectsWithConnection.size() << " requested "
2785 << overrideMap.size() << "\n";
2786 messages::resourceNotFound(
2787 sensorAsyncResp->res,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002788 sensorAsyncResp->chassisSubNode ==
2789 sensors::node::thermal
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302790 ? "Temperatures"
2791 : "Voltages",
2792 "Count");
2793 return;
2794 }
2795 for (const auto& item : objectsWithConnection)
2796 {
George Liu28aa8de2021-02-01 15:13:30 +08002797 sdbusplus::message::object_path path(item.first);
2798 std::string sensorName = path.filename();
2799 if (sensorName.empty())
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302800 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302801 messages::internalError(sensorAsyncResp->res);
2802 return;
2803 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302804
2805 const auto& iterator = overrideMap.find(sensorName);
2806 if (iterator == overrideMap.end())
2807 {
2808 BMCWEB_LOG_INFO << "Unable to find sensor object"
2809 << item.first << "\n";
2810 messages::internalError(sensorAsyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302811 return;
2812 }
2813 crow::connections::systemBus->async_method_call(
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302814 [sensorAsyncResp](const boost::system::error_code ec) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302815 if (ec)
2816 {
2817 BMCWEB_LOG_DEBUG
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302818 << "setOverrideValueStatus DBUS error: "
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302819 << ec;
2820 messages::internalError(sensorAsyncResp->res);
2821 return;
2822 }
2823 },
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302824 item.second, item.first,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302825 "org.freedesktop.DBus.Properties", "Set",
2826 "xyz.openbmc_project.Sensor.Value", "Value",
Patrick Williams19bd78d2020-05-13 17:38:24 -05002827 std::variant<double>(iterator->second.first));
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302828 }
2829 };
2830 // Get object with connection for the given sensor name
2831 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2832 std::move(getObjectsWithConnectionCb));
2833 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302834 // get full sensor list for the given chassisId and cross verify the sensor.
2835 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2836}
2837
Ed Tanous23a21a12020-07-25 04:45:05 +00002838inline bool isOverridingAllowed(const std::string& manufacturingModeStatus)
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002839{
2840 if (manufacturingModeStatus ==
2841 "xyz.openbmc_project.Control.Security.SpecialMode.Modes.Manufacturing")
2842 {
2843 return true;
2844 }
2845
2846#ifdef BMCWEB_ENABLE_VALIDATION_UNSECURE_FEATURE
2847 if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
2848 "SpecialMode.Modes.ValidationUnsecure")
2849 {
2850 return true;
2851 }
2852
2853#endif
2854
2855 return false;
2856}
2857
2858/**
2859 * @brief Entry point for Checking the manufacturing mode before doing sensor
2860 * override values of given sensor
2861 *
2862 * @param res response object
2863 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002864 * @param chassisSubNode Chassis Node for which the query has to happen
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002865 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002866inline void checkAndDoSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002867 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002868 std::unordered_map<std::string, std::vector<nlohmann::json>>&
2869 allCollections)
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002870{
2871 BMCWEB_LOG_INFO << "checkAndDoSensorsOverride for subnode"
2872 << sensorAsyncResp->chassisSubNode << "\n";
2873
2874 const std::array<std::string, 1> interfaces = {
2875 "xyz.openbmc_project.Security.SpecialMode"};
2876
2877 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +00002878 [sensorAsyncResp, allCollections](const boost::system::error_code ec2,
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002879 const GetSubTreeType& resp) mutable {
Ed Tanous23a21a12020-07-25 04:45:05 +00002880 if (ec2)
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002881 {
2882 BMCWEB_LOG_DEBUG
2883 << "Error in querying GetSubTree with Object Mapper. "
Ed Tanous23a21a12020-07-25 04:45:05 +00002884 << ec2;
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002885 messages::internalError(sensorAsyncResp->res);
2886 return;
2887 }
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002888#ifdef BMCWEB_INSECURE_UNRESTRICTED_SENSOR_OVERRIDE
2889 // Proceed with sensor override
2890 setSensorsOverride(sensorAsyncResp, allCollections);
2891 return;
2892#endif
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002893
2894 if (resp.size() != 1)
2895 {
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002896 BMCWEB_LOG_WARNING
2897 << "Overriding sensor value is not allowed - Internal "
2898 "error in querying SpecialMode property.";
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002899 messages::internalError(sensorAsyncResp->res);
2900 return;
2901 }
2902 const std::string& path = resp[0].first;
2903 const std::string& serviceName = resp[0].second.begin()->first;
2904
2905 if (path.empty() || serviceName.empty())
2906 {
2907 BMCWEB_LOG_DEBUG
2908 << "Path or service name is returned as empty. ";
2909 messages::internalError(sensorAsyncResp->res);
2910 return;
2911 }
2912
2913 // Sensor override is allowed only in manufacturing mode or
2914 // validation unsecure mode .
2915 crow::connections::systemBus->async_method_call(
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002916 [sensorAsyncResp, allCollections,
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002917 path](const boost::system::error_code ec,
2918 std::variant<std::string>& getManufactMode) mutable {
2919 if (ec)
2920 {
2921 BMCWEB_LOG_DEBUG
2922 << "Error in querying Special mode property " << ec;
2923 messages::internalError(sensorAsyncResp->res);
2924 return;
2925 }
2926
2927 const std::string* manufacturingModeStatus =
2928 std::get_if<std::string>(&getManufactMode);
2929
2930 if (nullptr == manufacturingModeStatus)
2931 {
2932 BMCWEB_LOG_DEBUG << "Sensor override mode is not "
2933 "Enabled. Returning ... ";
2934 messages::internalError(sensorAsyncResp->res);
2935 return;
2936 }
2937
2938 if (isOverridingAllowed(*manufacturingModeStatus))
2939 {
2940 BMCWEB_LOG_INFO << "Manufacturing mode is Enabled. "
2941 "Proceeding further... ";
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002942 setSensorsOverride(sensorAsyncResp, allCollections);
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002943 }
2944 else
2945 {
2946 BMCWEB_LOG_WARNING
2947 << "Manufacturing mode is not Enabled...can't "
2948 "Override the sensor value. ";
2949
2950 messages::actionNotSupported(
2951 sensorAsyncResp->res,
2952 "Overriding of Sensor Value for non "
2953 "manufacturing mode");
2954 return;
2955 }
2956 },
2957 serviceName, path, "org.freedesktop.DBus.Properties", "Get",
2958 "xyz.openbmc_project.Security.SpecialMode", "SpecialMode");
2959 },
2960
2961 "xyz.openbmc_project.ObjectMapper",
2962 "/xyz/openbmc_project/object_mapper",
2963 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 5, interfaces);
2964}
2965
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002966/**
2967 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2968 * path of the sensor.
2969 *
2970 * Function builds valid Redfish response for sensor query of given chassis and
2971 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2972 * it to caller in a callback.
2973 *
2974 * @param chassis Chassis for which retrieval should be performed
2975 * @param node Node (group) of sensors. See sensors::node for supported values
2976 * @param mapComplete Callback to be called with retrieval result
2977 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002978inline void retrieveUriToDbusMap(const std::string& chassis,
2979 const std::string& node,
2980 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002981{
2982 auto typesIt = sensors::dbus::types.find(node);
2983 if (typesIt == sensors::dbus::types.end())
2984 {
2985 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2986 mapComplete(boost::beast::http::status::bad_request, {});
2987 return;
2988 }
2989
2990 auto respBuffer = std::make_shared<crow::Response>();
2991 auto callback =
2992 [respBuffer, mapCompleteCb{std::move(mapComplete)}](
2993 const boost::beast::http::status status,
2994 const boost::container::flat_map<std::string, std::string>&
2995 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2996
2997 auto resp = std::make_shared<SensorsAsyncResp>(
2998 *respBuffer, chassis, typesIt->second, node, std::move(callback));
2999 getChassisData(resp);
3000}
3001
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003002class SensorCollection : public Node
3003{
3004 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07003005 SensorCollection(App& app) :
Gunnar Millsf99c3792020-04-14 21:54:42 -05003006 Node(app, "/redfish/v1/Chassis/<str>/Sensors/", std::string())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003007 {
3008 entityPrivileges = {
3009 {boost::beast::http::verb::get, {{"Login"}}},
3010 {boost::beast::http::verb::head, {{"Login"}}},
3011 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3012 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3013 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3014 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3015 }
3016
3017 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00003018 void doGet(crow::Response& res, const crow::Request&,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003019 const std::vector<std::string>& params) override
3020 {
3021 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
3022 if (params.size() != 1)
3023 {
3024 BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1";
3025 messages::internalError(res);
3026 res.end();
3027 return;
3028 }
3029
3030 const std::string& chassisId = params[0];
3031 std::shared_ptr<SensorsAsyncResp> asyncResp =
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02003032 std::make_shared<SensorsAsyncResp>(
3033 res, chassisId, sensors::dbus::types.at(sensors::node::sensors),
3034 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003035
3036 auto getChassisCb =
Ed Tanousb5a76932020-09-29 16:16:58 -07003037 [asyncResp](
3038 const std::shared_ptr<boost::container::flat_set<std::string>>&
3039 sensorNames) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003040 BMCWEB_LOG_DEBUG << "getChassisCb enter";
3041
3042 nlohmann::json& entriesArray =
3043 asyncResp->res.jsonValue["Members"];
3044 for (auto& sensor : *sensorNames)
3045 {
3046 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
3047
George Liu28aa8de2021-02-01 15:13:30 +08003048 sdbusplus::message::object_path path(sensor);
3049 std::string sensorName = path.filename();
3050 if (sensorName.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003051 {
3052 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
3053 messages::internalError(asyncResp->res);
3054 return;
3055 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003056 entriesArray.push_back(
3057 {{"@odata.id",
3058 "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
3059 asyncResp->chassisSubNode + "/" + sensorName}});
3060 }
3061
3062 asyncResp->res.jsonValue["Members@odata.count"] =
3063 entriesArray.size();
3064 BMCWEB_LOG_DEBUG << "getChassisCb exit";
3065 };
3066
3067 // Get set of sensors in chassis
3068 getChassis(asyncResp, std::move(getChassisCb));
3069 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
3070 }
3071};
3072
3073class Sensor : public Node
3074{
3075 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07003076 Sensor(App& app) :
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003077 Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(),
3078 std::string())
3079 {
3080 entityPrivileges = {
3081 {boost::beast::http::verb::get, {{"Login"}}},
3082 {boost::beast::http::verb::head, {{"Login"}}},
3083 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3084 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3085 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3086 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3087 }
3088
3089 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00003090 void doGet(crow::Response& res, const crow::Request&,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003091 const std::vector<std::string>& params) override
3092 {
3093 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3094 if (params.size() != 2)
3095 {
3096 BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2";
3097 messages::internalError(res);
3098 res.end();
3099 return;
3100 }
3101 const std::string& chassisId = params[0];
3102 std::shared_ptr<SensorsAsyncResp> asyncResp =
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02003103 std::make_shared<SensorsAsyncResp>(res, chassisId,
3104 std::vector<const char*>(),
3105 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003106
3107 const std::string& sensorName = params[1];
3108 const std::array<const char*, 1> interfaces = {
3109 "xyz.openbmc_project.Sensor.Value"};
3110
3111 // Get a list of all of the sensors that implement Sensor.Value
3112 // and get the path and service name associated with the sensor
3113 crow::connections::systemBus->async_method_call(
3114 [asyncResp, sensorName](const boost::system::error_code ec,
3115 const GetSubTreeType& subtree) {
3116 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3117 if (ec)
3118 {
3119 messages::internalError(asyncResp->res);
3120 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3121 << "Dbus error " << ec;
3122 return;
3123 }
3124
3125 GetSubTreeType::const_iterator it = std::find_if(
3126 subtree.begin(), subtree.end(),
3127 [sensorName](
3128 const std::pair<
3129 std::string,
3130 std::vector<std::pair<std::string,
3131 std::vector<std::string>>>>&
3132 object) {
George Liu28aa8de2021-02-01 15:13:30 +08003133 sdbusplus::message::object_path path(object.first);
3134 std::string name = path.filename();
3135 if (name.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003136 {
3137 BMCWEB_LOG_ERROR << "Invalid sensor path: "
George Liu28aa8de2021-02-01 15:13:30 +08003138 << object.first;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003139 return false;
3140 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003141
3142 return name == sensorName;
3143 });
3144
3145 if (it == subtree.end())
3146 {
3147 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3148 << sensorName;
3149 messages::resourceNotFound(asyncResp->res, "Sensor",
3150 sensorName);
3151 return;
3152 }
3153 std::string_view sensorPath = (*it).first;
3154 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3155 << sensorName << "': " << sensorPath;
3156
3157 const std::shared_ptr<boost::container::flat_set<std::string>>
3158 sensorList = std::make_shared<
3159 boost::container::flat_set<std::string>>();
3160
3161 sensorList->emplace(sensorPath);
3162 processSensorList(asyncResp, sensorList);
3163 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3164 },
3165 "xyz.openbmc_project.ObjectMapper",
3166 "/xyz/openbmc_project/object_mapper",
3167 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3168 "/xyz/openbmc_project/sensors", 2, interfaces);
3169 }
3170};
3171
Ed Tanous1abe55e2018-09-05 08:30:59 -07003172} // namespace redfish