blob: 212a4a4125d8d36632a6fd0b178a23771bb02391 [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 Tanousabf2add2019-01-22 16:40:12 -080028#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030namespace redfish
31{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010032
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010033using GetSubTreeType = std::vector<
34 std::pair<std::string,
35 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
36
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -050037using SensorVariant =
38 std::variant<int64_t, double, uint32_t, bool, std::string>;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070039
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010040using ManagedObjectsVectorType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 sdbusplus::message::object_path,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010042 boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070043 std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010044
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020045namespace sensors
46{
47namespace node
48{
49static constexpr std::string_view power = "Power";
50static constexpr std::string_view sensors = "Sensors";
51static constexpr std::string_view thermal = "Thermal";
52} // namespace node
53
54namespace dbus
55{
56static const boost::container::flat_map<std::string_view,
57 std::vector<const char*>>
58 types = {{node::power,
59 {"/xyz/openbmc_project/sensors/voltage",
60 "/xyz/openbmc_project/sensors/power"}},
61 {node::sensors,
62 {"/xyz/openbmc_project/sensors/power",
63 "/xyz/openbmc_project/sensors/current",
64 "/xyz/openbmc_project/sensors/utilization"}},
65 {node::thermal,
66 {"/xyz/openbmc_project/sensors/fan_tach",
67 "/xyz/openbmc_project/sensors/temperature",
68 "/xyz/openbmc_project/sensors/fan_pwm"}}};
69}
70} // namespace sensors
71
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010072/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020073 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010074 * Gathers data needed for response processing after async calls are done
75 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070076class SensorsAsyncResp
77{
78 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020079 using DataCompleteCb = std::function<void(
80 const boost::beast::http::status status,
81 const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
82
83 struct SensorData
84 {
85 const std::string name;
86 std::string uri;
87 const std::string valueKey;
88 const std::string dbusPath;
89 };
90
Ed Tanous271584a2019-07-09 16:24:22 -070091 SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
92 const std::vector<const char*> typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020093 const std::string_view& subNode) :
Ed Tanous43b761d2019-02-13 20:10:56 -080094 res(response),
Ed Tanous271584a2019-07-09 16:24:22 -070095 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050096 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020097
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020098 // Store extra data about sensor mapping and return it in callback
99 SensorsAsyncResp(crow::Response& response, const std::string& chassisIdIn,
100 const std::vector<const char*> typesIn,
101 const std::string_view& subNode,
102 DataCompleteCb&& creationComplete) :
103 res(response),
104 chassisId(chassisIdIn), types(typesIn),
105 chassisSubNode(subNode), metadata{std::vector<SensorData>()},
106 dataComplete{std::move(creationComplete)}
107 {}
108
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 ~SensorsAsyncResp()
110 {
111 if (res.result() == boost::beast::http::status::internal_server_error)
112 {
113 // Reset the json object to clear out any data that made it in
114 // before the error happened todo(ed) handle error condition with
115 // proper code
116 res.jsonValue = nlohmann::json::object();
117 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200118
119 if (dataComplete && metadata)
120 {
121 boost::container::flat_map<std::string, std::string> map;
122 if (res.result() == boost::beast::http::status::ok)
123 {
124 for (auto& sensor : *metadata)
125 {
126 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
127 sensor.dbusPath));
128 }
129 }
130 dataComplete(res.result(), map);
131 }
132
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133 res.end();
134 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100135
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200136 void addMetadata(const nlohmann::json& sensorObject,
137 const std::string& valueKey, const std::string& dbusPath)
138 {
139 if (metadata)
140 {
141 metadata->emplace_back(SensorData{sensorObject["Name"],
142 sensorObject["@odata.id"],
143 valueKey, dbusPath});
144 }
145 }
146
147 void updateUri(const std::string& name, const std::string& uri)
148 {
149 if (metadata)
150 {
151 for (auto& sensor : *metadata)
152 {
153 if (sensor.name == name)
154 {
155 sensor.uri = uri;
156 }
157 }
158 }
159 }
160
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161 crow::Response& res;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200162 const std::string chassisId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700163 const std::vector<const char*> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200164 const std::string chassisSubNode;
165
166 private:
167 std::optional<std::vector<SensorData>> metadata;
168 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100169};
170
171/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500172 * Possible states for physical inventory leds
173 */
174enum class LedState
175{
176 OFF,
177 ON,
178 BLINK,
179 UNKNOWN
180};
181
182/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500183 * D-Bus inventory item associated with one or more sensors.
184 */
185class InventoryItem
186{
187 public:
188 InventoryItem(const std::string& objPath) :
189 objectPath(objPath), name(), isPresent(true), isFunctional(true),
Gunnar Mills42cbe532019-08-15 15:26:54 -0500190 isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
191 model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""),
Anthony Wilsond5005492019-07-31 16:34:17 -0500192 ledState(LedState::UNKNOWN)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500193 {
194 // Set inventory item name to last node of object path
195 auto pos = objectPath.rfind('/');
196 if ((pos != std::string::npos) && ((pos + 1) < objectPath.size()))
197 {
198 name = objectPath.substr(pos + 1);
199 }
200 }
201
202 std::string objectPath;
203 std::string name;
204 bool isPresent;
205 bool isFunctional;
206 bool isPowerSupply;
Gunnar Mills42cbe532019-08-15 15:26:54 -0500207 int powerSupplyEfficiencyPercent;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500208 std::string manufacturer;
209 std::string model;
210 std::string partNumber;
211 std::string serialNumber;
212 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500213 std::string ledObjectPath;
214 LedState ledState;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500215};
216
217/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530218 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200219 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100220 * @param sensorNames Sensors retrieved from chassis
221 * @param callback Callback for processing gathered connections
222 */
223template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530224void getObjectsWithConnection(
225 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700226 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530227 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530229 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 const std::string path = "/xyz/openbmc_project/sensors";
231 const std::array<std::string, 1> interfaces = {
232 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100233
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 // Response handler for parsing objects subtree
235 auto respHandler = [callback{std::move(callback)}, SensorsAsyncResp,
236 sensorNames](const boost::system::error_code ec,
237 const GetSubTreeType& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530238 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 if (ec)
240 {
Ed Tanous5f7d88c2018-11-14 14:08:56 -0800241 messages::internalError(SensorsAsyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530242 BMCWEB_LOG_ERROR
243 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700244 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100245 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100246
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
248
249 // Make unique list of connections only for requested sensor types and
250 // found in the chassis
251 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530252 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
254 // producers
255 connections.reserve(8);
256
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700257 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
258 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259 {
260 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
261 }
262
263 for (const std::pair<
264 std::string,
265 std::vector<std::pair<std::string, std::vector<std::string>>>>&
266 object : subtree)
267 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700268 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700269 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700270 for (const std::pair<std::string, std::vector<std::string>>&
271 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700272 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700273 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
274 connections.insert(objData.first);
275 objectsWithConnection.insert(
276 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700277 }
278 }
279 }
280 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530281 callback(std::move(connections), std::move(objectsWithConnection));
282 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700283 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700284 // Make call to ObjectMapper to find all sensors objects
285 crow::connections::systemBus->async_method_call(
286 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
287 "/xyz/openbmc_project/object_mapper",
288 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530289 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
290}
291
292/**
293 * @brief Create connections necessary for sensors
294 * @param SensorsAsyncResp Pointer to object holding response data
295 * @param sensorNames Sensors retrieved from chassis
296 * @param callback Callback for processing gathered connections
297 */
298template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700299void getConnections(
300 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
301 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
302 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530303{
304 auto objectsWithConnectionCb =
305 [callback](const boost::container::flat_set<std::string>& connections,
306 const std::set<std::pair<std::string, std::string>>&
307 objectsWithConnection) {
308 callback(std::move(connections));
309 };
310 getObjectsWithConnection(SensorsAsyncResp, sensorNames,
311 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 */
323void reduceSensorList(
324 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
325 const std::vector<std::string>* allSensors,
326 std::shared_ptr<boost::container::flat_set<std::string>> activeSensors)
327{
328 if (SensorsAsyncResp == nullptr)
329 {
330 return;
331 }
332 if ((allSensors == nullptr) || (activeSensors == nullptr))
333 {
334 messages::resourceNotFound(
335 SensorsAsyncResp->res, SensorsAsyncResp->chassisSubNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200336 SensorsAsyncResp->chassisSubNode == sensors::node::thermal
337 ? "Temperatures"
338 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700339
340 return;
341 }
342 if (allSensors->empty())
343 {
344 // Nothing to do, the activeSensors object is also empty
345 return;
346 }
347
348 for (const char* type : SensorsAsyncResp->types)
349 {
350 for (const std::string& sensor : *allSensors)
351 {
352 if (boost::starts_with(sensor, type))
353 {
354 activeSensors->emplace(sensor);
355 }
356 }
357 }
358}
359
360/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800361 * @brief Retrieves valid chassis path
362 * @param asyncResp Pointer to object holding response data
363 * @param callback Callback for next step to get valid chassis path
364 */
365template <typename Callback>
366void getValidChassisPath(std::shared_ptr<SensorsAsyncResp> asyncResp,
367 Callback&& callback)
368{
369 BMCWEB_LOG_DEBUG << "checkChassisId enter";
370 const std::array<const char*, 2> interfaces = {
371 "xyz.openbmc_project.Inventory.Item.Board",
372 "xyz.openbmc_project.Inventory.Item.Chassis"};
373
374 auto respHandler =
375 [callback{std::move(callback)},
376 asyncResp](const boost::system::error_code ec,
377 const std::vector<std::string>& chassisPaths) mutable {
378 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
379 if (ec)
380 {
381 BMCWEB_LOG_ERROR
382 << "getValidChassisPath respHandler DBUS error: " << ec;
383 messages::internalError(asyncResp->res);
384 return;
385 }
386
387 std::optional<std::string> chassisPath;
388 std::string chassisName;
389 for (const std::string& chassis : chassisPaths)
390 {
391 std::size_t lastPos = chassis.rfind("/");
392 if (lastPos == std::string::npos)
393 {
394 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
395 continue;
396 }
397 chassisName = chassis.substr(lastPos + 1);
398 if (chassisName == asyncResp->chassisId)
399 {
400 chassisPath = chassis;
401 break;
402 }
403 }
404 callback(chassisPath);
405 };
406
407 // Get the Chassis Collection
408 crow::connections::systemBus->async_method_call(
409 respHandler, "xyz.openbmc_project.ObjectMapper",
410 "/xyz/openbmc_project/object_mapper",
411 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
412 "/xyz/openbmc_project/inventory", 0, interfaces);
413 BMCWEB_LOG_DEBUG << "checkChassisId exit";
414}
415
416/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100417 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200418 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100419 * @param callback Callback for next step in gathered sensor processing
420 */
421template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700422void getChassis(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700423 Callback&& callback)
424{
425 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500426 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700427 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500428 "xyz.openbmc_project.Inventory.Item.Chassis"};
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700429 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
430 const boost::system::error_code ec,
431 const std::vector<std::string>& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700432 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
433 if (ec)
434 {
435 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700436 messages::internalError(sensorsAsyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700437 return;
438 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100439
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700440 const std::string* chassisPath = nullptr;
441 std::string chassisName;
442 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700443 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700444 std::size_t lastPos = chassis.rfind("/");
445 if (lastPos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700446 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 continue;
449 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700450 chassisName = chassis.substr(lastPos + 1);
451 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700452 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700453 chassisPath = &chassis;
454 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700455 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700456 }
457 if (chassisPath == nullptr)
458 {
459 messages::resourceNotFound(sensorsAsyncResp->res, "Chassis",
460 sensorsAsyncResp->chassisId);
461 return;
462 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700463
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700464 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200465 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700466 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700467 sensorsAsyncResp->res.jsonValue["@odata.type"] =
468 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700469 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200470 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700471 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700472 sensorsAsyncResp->res.jsonValue["@odata.type"] =
473 "#Thermal.v1_4_0.Thermal";
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800474 sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array();
475 sensorsAsyncResp->res.jsonValue["Temperatures"] =
476 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700477 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200478 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500479 {
480 sensorsAsyncResp->res.jsonValue["@odata.type"] =
481 "#SensorCollection.SensorCollection";
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500482 sensorsAsyncResp->res.jsonValue["Description"] =
483 "Collection of Sensors for this Chassis";
484 sensorsAsyncResp->res.jsonValue["Members"] =
485 nlohmann::json::array();
486 sensorsAsyncResp->res.jsonValue["Members@odata.count"] = 0;
487 }
488
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200489 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500490 {
491 sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500492 }
493
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700494 sensorsAsyncResp->res.jsonValue["@odata.id"] =
495 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
496 chassisSubNode;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700497 sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode;
498
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500499 // Get the list of all sensors for this Chassis element
500 std::string sensorPath = *chassisPath + "/all_sensors";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700501 crow::connections::systemBus->async_method_call(
502 [sensorsAsyncResp, callback{std::move(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700503 const boost::system::error_code& e,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700504 const std::variant<std::vector<std::string>>&
505 variantEndpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -0700506 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700507 {
Ed Tanous271584a2019-07-09 16:24:22 -0700508 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700509 {
510 messages::internalError(sensorsAsyncResp->res);
511 return;
512 }
513 }
514 const std::vector<std::string>* nodeSensorList =
515 std::get_if<std::vector<std::string>>(&(variantEndpoints));
516 if (nodeSensorList == nullptr)
517 {
518 messages::resourceNotFound(
519 sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200520 sensorsAsyncResp->chassisSubNode ==
521 sensors::node::thermal
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700522 ? "Temperatures"
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200523 : sensorsAsyncResp->chassisSubNode ==
524 sensors::node::power
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500525 ? "Voltages"
526 : "Sensors");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700527 return;
528 }
529 const std::shared_ptr<boost::container::flat_set<std::string>>
530 culledSensorList = std::make_shared<
531 boost::container::flat_set<std::string>>();
532 reduceSensorList(sensorsAsyncResp, nodeSensorList,
533 culledSensorList);
534 callback(culledSensorList);
535 },
536 "xyz.openbmc_project.ObjectMapper", sensorPath,
537 "org.freedesktop.DBus.Properties", "Get",
538 "xyz.openbmc_project.Association", "endpoints");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100539 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100540
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700543 respHandler, "xyz.openbmc_project.ObjectMapper",
544 "/xyz/openbmc_project/object_mapper",
545 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700546 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100548}
549
550/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600551 * @brief Finds all DBus object paths that implement ObjectManager.
552 *
553 * Creates a mapping from the associated connection name to the object path.
554 *
555 * Finds the object paths asynchronously. Invokes callback when information has
556 * been obtained.
557 *
558 * The callback must have the following signature:
559 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500560 * callback(std::shared_ptr<boost::container::flat_map<std::string,
561 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600562 * @endcode
563 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700564 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600565 * @param callback Callback to invoke when object paths obtained.
566 */
567template <typename Callback>
568void getObjectManagerPaths(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
569 Callback&& callback)
570{
571 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
572 const std::array<std::string, 1> interfaces = {
573 "org.freedesktop.DBus.ObjectManager"};
574
575 // Response handler for GetSubTree DBus method
576 auto respHandler = [callback{std::move(callback)},
577 SensorsAsyncResp](const boost::system::error_code ec,
578 const GetSubTreeType& subtree) {
579 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
580 if (ec)
581 {
582 messages::internalError(SensorsAsyncResp->res);
583 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
584 << ec;
585 return;
586 }
587
588 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500589 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
590 objectMgrPaths = std::make_shared<
591 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600592 for (const std::pair<
593 std::string,
594 std::vector<std::pair<std::string, std::vector<std::string>>>>&
595 object : subtree)
596 {
597 // Loop over connections for current object path
598 const std::string& objectPath = object.first;
599 for (const std::pair<std::string, std::vector<std::string>>&
600 objData : object.second)
601 {
602 // Add mapping from connection to object path
603 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500604 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600605 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
606 << objectPath;
607 }
608 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500609 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600610 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
611 };
612
613 // Query mapper for all DBus object paths that implement ObjectManager
614 crow::connections::systemBus->async_method_call(
615 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
616 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700617 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600618 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
619}
620
621/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500622 * @brief Returns the Redfish State value for the specified inventory item.
623 * @param inventoryItem D-Bus inventory item associated with a sensor.
624 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700625 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500626static std::string getState(const InventoryItem* inventoryItem)
627{
628 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
629 {
630 return "Absent";
631 }
James Feist34dd1792019-05-17 14:10:54 -0700632
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500633 return "Enabled";
634}
635
636/**
637 * @brief Returns the Redfish Health value for the specified sensor.
638 * @param sensorJson Sensor JSON object.
639 * @param interfacesDict Map of all sensor interfaces.
640 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
641 * be nullptr if no associated inventory item was found.
642 * @return Health value for sensor.
643 */
James Feist34dd1792019-05-17 14:10:54 -0700644static std::string getHealth(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500645 nlohmann::json& sensorJson,
James Feist34dd1792019-05-17 14:10:54 -0700646 const boost::container::flat_map<
647 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500648 interfacesDict,
649 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700650{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500651 // Get current health value (if any) in the sensor JSON object. Some JSON
652 // objects contain multiple sensors (such as PowerSupplies). We want to set
653 // the overall health to be the most severe of any of the sensors.
654 std::string currentHealth;
655 auto statusIt = sensorJson.find("Status");
656 if (statusIt != sensorJson.end())
657 {
658 auto healthIt = statusIt->find("Health");
659 if (healthIt != statusIt->end())
660 {
661 std::string* health = healthIt->get_ptr<std::string*>();
662 if (health != nullptr)
663 {
664 currentHealth = *health;
665 }
666 }
667 }
668
669 // If current health in JSON object is already Critical, return that. This
670 // should override the sensor health, which might be less severe.
671 if (currentHealth == "Critical")
672 {
673 return "Critical";
674 }
675
676 // Check if sensor has critical threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700677 auto criticalThresholdIt =
678 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
679 if (criticalThresholdIt != interfacesDict.end())
680 {
681 auto thresholdHighIt =
682 criticalThresholdIt->second.find("CriticalAlarmHigh");
683 auto thresholdLowIt =
684 criticalThresholdIt->second.find("CriticalAlarmLow");
685 if (thresholdHighIt != criticalThresholdIt->second.end())
686 {
687 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
688 if (asserted == nullptr)
689 {
690 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
691 }
692 else if (*asserted)
693 {
694 return "Critical";
695 }
696 }
697 if (thresholdLowIt != criticalThresholdIt->second.end())
698 {
699 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
700 if (asserted == nullptr)
701 {
702 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
703 }
704 else if (*asserted)
705 {
706 return "Critical";
707 }
708 }
709 }
710
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500711 // Check if associated inventory item is not functional
712 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
713 {
714 return "Critical";
715 }
716
717 // If current health in JSON object is already Warning, return that. This
718 // should override the sensor status, which might be less severe.
719 if (currentHealth == "Warning")
720 {
721 return "Warning";
722 }
723
724 // Check if sensor has warning threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700725 auto warningThresholdIt =
726 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
727 if (warningThresholdIt != interfacesDict.end())
728 {
729 auto thresholdHighIt =
730 warningThresholdIt->second.find("WarningAlarmHigh");
731 auto thresholdLowIt =
732 warningThresholdIt->second.find("WarningAlarmLow");
733 if (thresholdHighIt != warningThresholdIt->second.end())
734 {
735 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
736 if (asserted == nullptr)
737 {
738 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
739 }
740 else if (*asserted)
741 {
742 return "Warning";
743 }
744 }
745 if (thresholdLowIt != warningThresholdIt->second.end())
746 {
747 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
748 if (asserted == nullptr)
749 {
750 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
751 }
752 else if (*asserted)
753 {
754 return "Warning";
755 }
756 }
757 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500758
James Feist34dd1792019-05-17 14:10:54 -0700759 return "OK";
760}
761
Anthony Wilsond5005492019-07-31 16:34:17 -0500762static void setLedState(nlohmann::json& sensorJson,
763 const InventoryItem* inventoryItem)
764{
765 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
766 {
767 switch (inventoryItem->ledState)
768 {
769 case LedState::OFF:
770 sensorJson["IndicatorLED"] = "Off";
771 break;
772 case LedState::ON:
773 sensorJson["IndicatorLED"] = "Lit";
774 break;
775 case LedState::BLINK:
776 sensorJson["IndicatorLED"] = "Blinking";
777 break;
778 default:
779 break;
780 }
781 }
782}
783
James Feist34dd1792019-05-17 14:10:54 -0700784/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100785 * @brief Builds a json sensor representation of a sensor.
786 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500787 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100788 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200789 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100790 * @param interfacesDict A dictionary of the interfaces and properties of said
791 * interfaces to be built from
792 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500793 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
794 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100795 */
796void objectInterfacesToJson(
797 const std::string& sensorName, const std::string& sensorType,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200798 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100799 const boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700800 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100801 interfacesDict,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500802 nlohmann::json& sensor_json, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700803{
804 // We need a value interface before we can do anything with it
805 auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
806 if (valueIt == interfacesDict.end())
807 {
808 BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
809 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100810 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100811
Ed Tanous1abe55e2018-09-05 08:30:59 -0700812 // Assume values exist as is (10^0 == 1) if no scale exists
813 int64_t scaleMultiplier = 0;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100814
Ed Tanous1abe55e2018-09-05 08:30:59 -0700815 auto scaleIt = valueIt->second.find("Scale");
816 // If a scale exists, pull value as int64, and use the scaling.
817 if (scaleIt != valueIt->second.end())
818 {
Ed Tanousabf2add2019-01-22 16:40:12 -0800819 const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700820 if (int64Value != nullptr)
821 {
822 scaleMultiplier = *int64Value;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100823 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100824 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200826 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500827 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500828 // For sensors in SensorCollection we set Id instead of MemberId,
829 // including power sensors.
830 sensor_json["Id"] = sensorName;
831 sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
832 }
833 else if (sensorType != "power")
834 {
835 // Set MemberId and Name for non-power sensors. For PowerSupplies and
836 // PowerControl, those properties have more general values because
837 // multiple sensors can be stored in the same JSON object.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500838 sensor_json["MemberId"] = sensorName;
839 sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
840 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700841
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500842 sensor_json["Status"]["State"] = getState(inventoryItem);
843 sensor_json["Status"]["Health"] =
844 getHealth(sensor_json, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845
846 // Parameter to set to override the type we get from dbus, and force it to
847 // int, regardless of what is available. This is used for schemas like fan,
848 // that require integers, not floats.
849 bool forceToInt = false;
850
Anthony Wilson3929aca2019-07-19 15:42:33 -0500851 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200852 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500853 {
854 sensor_json["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500855 if (sensorType == "power")
856 {
857 sensor_json["ReadingUnits"] = "Watts";
858 }
859 else if (sensorType == "current")
860 {
861 sensor_json["ReadingUnits"] = "Amperes";
862 }
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200863 else if (sensorType == "utilization")
864 {
865 sensor_json["ReadingUnits"] = "Percent";
866 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500867 }
868 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700869 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500870 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700871 sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature";
872 // TODO(ed) Documentation says that path should be type fan_tach,
873 // implementation seems to implement fan
874 }
875 else if (sensorType == "fan" || sensorType == "fan_tach")
876 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500877 unit = "/Reading"_json_pointer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700878 sensor_json["ReadingUnits"] = "RPM";
879 sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
Anthony Wilsond5005492019-07-31 16:34:17 -0500880 setLedState(sensor_json, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 forceToInt = true;
882 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700883 else if (sensorType == "fan_pwm")
884 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500885 unit = "/Reading"_json_pointer;
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700886 sensor_json["ReadingUnits"] = "Percent";
887 sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
Anthony Wilsond5005492019-07-31 16:34:17 -0500888 setLedState(sensor_json, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700889 forceToInt = true;
890 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700891 else if (sensorType == "voltage")
892 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500893 unit = "/ReadingVolts"_json_pointer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700894 sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage";
895 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700896 else if (sensorType == "power")
897 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700898 std::string sensorNameLower =
899 boost::algorithm::to_lower_copy(sensorName);
900
Eddie James028f7eb2019-05-17 21:24:36 +0000901 if (!sensorName.compare("total_power"))
902 {
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500903 sensor_json["@odata.type"] = "#Power.v1_0_0.PowerControl";
904 // Put multiple "sensors" into a single PowerControl, so have
905 // generic names for MemberId and Name. Follows Redfish mockup.
906 sensor_json["MemberId"] = "0";
907 sensor_json["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500908 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000909 }
910 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700911 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700913 }
914 else
915 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500916 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700917 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700918 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 else
920 {
921 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
922 return;
923 }
924 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500925 std::vector<
926 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
927 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700928 properties.reserve(7);
929
930 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600931
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200932 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500933 {
934 properties.emplace_back(
935 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
936 "/Thresholds/UpperCaution/Reading"_json_pointer);
937 properties.emplace_back(
938 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
939 "/Thresholds/LowerCaution/Reading"_json_pointer);
940 properties.emplace_back(
941 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
942 "/Thresholds/UpperCritical/Reading"_json_pointer);
943 properties.emplace_back(
944 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
945 "/Thresholds/LowerCritical/Reading"_json_pointer);
946 }
947 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600948 {
949 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500950 "WarningHigh",
951 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600952 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500953 "WarningLow",
954 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600955 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500956 "CriticalHigh",
957 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600958 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500959 "CriticalLow",
960 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600961 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700962
Ed Tanous2474adf2018-09-05 16:31:16 -0700963 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
964
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200965 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500966 {
967 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500968 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500969 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500970 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500971 }
972 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973 {
974 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500975 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700976 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500977 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700978 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500979 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 {
981 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500982 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500984 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700985 }
986
Anthony Wilson3929aca2019-07-19 15:42:33 -0500987 for (const std::tuple<const char*, const char*,
988 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 {
990 auto interfaceProperties = interfacesDict.find(std::get<0>(p));
991 if (interfaceProperties != interfacesDict.end())
992 {
Ed Tanous271584a2019-07-09 16:24:22 -0700993 auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
994 if (thisValueIt != interfaceProperties->second.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700995 {
Ed Tanous271584a2019-07-09 16:24:22 -0700996 const SensorVariant& valueVariant = thisValueIt->second;
Anthony Wilson3929aca2019-07-19 15:42:33 -0500997
998 // The property we want to set may be nested json, so use
999 // a json_pointer for easy indexing into the json structure.
1000 const nlohmann::json::json_pointer& key = std::get<2>(p);
1001
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001003 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001004
Ed Tanousabf2add2019-01-22 16:40:12 -08001005 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001006 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001007 double temp = 0.0;
1008 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001009 {
Ed Tanous271584a2019-07-09 16:24:22 -07001010 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001011 }
1012 else if (doubleValue != nullptr)
1013 {
1014 temp = *doubleValue;
1015 }
Eddie James028f7eb2019-05-17 21:24:36 +00001016 else if (uValue != nullptr)
1017 {
1018 temp = *uValue;
1019 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001020 else
1021 {
1022 BMCWEB_LOG_ERROR
1023 << "Got value interface that wasn't int or double";
1024 continue;
1025 }
1026 temp = temp * std::pow(10, scaleMultiplier);
1027 if (forceToInt)
1028 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001029 sensor_json[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001030 }
1031 else
1032 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001033 sensor_json[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034 }
1035 }
1036 }
1037 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001038
1039 sensorsAsyncResp->addMetadata(sensor_json, unit.to_string(),
1040 "/xyz/openbmc_project/sensors/" + sensorType +
1041 "/" + sensorName);
1042
Ed Tanous1abe55e2018-09-05 08:30:59 -07001043 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001044}
1045
James Feist8bd25cc2019-03-15 15:14:00 -07001046static void
1047 populateFanRedundancy(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp)
1048{
1049 crow::connections::systemBus->async_method_call(
1050 [sensorsAsyncResp](const boost::system::error_code ec,
1051 const GetSubTreeType& resp) {
1052 if (ec)
1053 {
1054 return; // don't have to have this interface
1055 }
Ed Tanouse278c182019-03-13 16:23:37 -07001056 for (const std::pair<std::string,
1057 std::vector<std::pair<
1058 std::string, std::vector<std::string>>>>&
1059 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001060 {
Ed Tanouse278c182019-03-13 16:23:37 -07001061 const std::string& path = pathPair.first;
1062 const std::vector<
1063 std::pair<std::string, std::vector<std::string>>>& objDict =
1064 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001065 if (objDict.empty())
1066 {
1067 continue; // this should be impossible
1068 }
1069
1070 const std::string& owner = objDict.begin()->first;
1071 crow::connections::systemBus->async_method_call(
1072 [path, owner,
Ed Tanous271584a2019-07-09 16:24:22 -07001073 sensorsAsyncResp](const boost::system::error_code e,
James Feist8bd25cc2019-03-15 15:14:00 -07001074 std::variant<std::vector<std::string>>
1075 variantEndpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001076 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001077 {
1078 return; // if they don't have an association we
1079 // can't tell what chassis is
1080 }
1081 // verify part of the right chassis
1082 auto endpoints = std::get_if<std::vector<std::string>>(
1083 &variantEndpoints);
1084
1085 if (endpoints == nullptr)
1086 {
1087 BMCWEB_LOG_ERROR << "Invalid association interface";
1088 messages::internalError(sensorsAsyncResp->res);
1089 return;
1090 }
1091
1092 auto found = std::find_if(
1093 endpoints->begin(), endpoints->end(),
1094 [sensorsAsyncResp](const std::string& entry) {
1095 return entry.find(
1096 sensorsAsyncResp->chassisId) !=
1097 std::string::npos;
1098 });
1099
1100 if (found == endpoints->end())
1101 {
1102 return;
1103 }
1104 crow::connections::systemBus->async_method_call(
1105 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001106 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001107 const boost::container::flat_map<
1108 std::string,
1109 std::variant<uint8_t,
1110 std::vector<std::string>,
1111 std::string>>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001112 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001113 {
1114 return; // don't have to have this
1115 // interface
1116 }
1117 auto findFailures = ret.find("AllowedFailures");
1118 auto findCollection = ret.find("Collection");
1119 auto findStatus = ret.find("Status");
1120
1121 if (findFailures == ret.end() ||
1122 findCollection == ret.end() ||
1123 findStatus == ret.end())
1124 {
1125 BMCWEB_LOG_ERROR
1126 << "Invalid redundancy interface";
1127 messages::internalError(
1128 sensorsAsyncResp->res);
1129 return;
1130 }
1131
1132 auto allowedFailures = std::get_if<uint8_t>(
1133 &(findFailures->second));
1134 auto collection =
1135 std::get_if<std::vector<std::string>>(
1136 &(findCollection->second));
1137 auto status = std::get_if<std::string>(
1138 &(findStatus->second));
1139
1140 if (allowedFailures == nullptr ||
1141 collection == nullptr || status == nullptr)
1142 {
1143
1144 BMCWEB_LOG_ERROR
1145 << "Invalid redundancy interface "
1146 "types";
1147 messages::internalError(
1148 sensorsAsyncResp->res);
1149 return;
1150 }
1151 size_t lastSlash = path.rfind("/");
1152 if (lastSlash == std::string::npos)
1153 {
1154 // this should be impossible
1155 messages::internalError(
1156 sensorsAsyncResp->res);
1157 return;
1158 }
1159 std::string name = path.substr(lastSlash + 1);
1160 std::replace(name.begin(), name.end(), '_',
1161 ' ');
1162
1163 std::string health;
1164
1165 if (boost::ends_with(*status, "Full"))
1166 {
1167 health = "OK";
1168 }
1169 else if (boost::ends_with(*status, "Degraded"))
1170 {
1171 health = "Warning";
1172 }
1173 else
1174 {
1175 health = "Critical";
1176 }
1177 std::vector<nlohmann::json> redfishCollection;
1178 const auto& fanRedfish =
1179 sensorsAsyncResp->res.jsonValue["Fans"];
1180 for (const std::string& item : *collection)
1181 {
1182 lastSlash = item.rfind("/");
1183 // make a copy as collection is const
1184 std::string itemName =
1185 item.substr(lastSlash + 1);
1186 /*
1187 todo(ed): merge patch that fixes the names
1188 std::replace(itemName.begin(),
1189 itemName.end(), '_', ' ');*/
1190 auto schemaItem = std::find_if(
1191 fanRedfish.begin(), fanRedfish.end(),
1192 [itemName](const nlohmann::json& fan) {
1193 return fan["MemberId"] == itemName;
1194 });
1195 if (schemaItem != fanRedfish.end())
1196 {
1197 redfishCollection.push_back(
1198 {{"@odata.id",
1199 (*schemaItem)["@odata.id"]}});
1200 }
1201 else
1202 {
1203 BMCWEB_LOG_ERROR
1204 << "failed to find fan in schema";
1205 messages::internalError(
1206 sensorsAsyncResp->res);
1207 return;
1208 }
1209 }
1210
Ed Tanous271584a2019-07-09 16:24:22 -07001211 nlohmann::json& jResp =
1212 sensorsAsyncResp->res
1213 .jsonValue["Redundancy"];
1214 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001215 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301216 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001217 sensorsAsyncResp->chassisId + "/" +
1218 sensorsAsyncResp->chassisSubNode +
1219 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001220 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001221 {"@odata.type",
1222 "#Redundancy.v1_3_2.Redundancy"},
1223 {"MinNumNeeded",
1224 collection->size() - *allowedFailures},
1225 {"MemberId", name},
1226 {"Mode", "N+m"},
1227 {"Name", name},
1228 {"RedundancySet", redfishCollection},
1229 {"Status",
1230 {{"Health", health},
1231 {"State", "Enabled"}}}});
1232 },
1233 owner, path, "org.freedesktop.DBus.Properties",
1234 "GetAll",
1235 "xyz.openbmc_project.Control.FanRedundancy");
1236 },
James Feist02e92e32019-06-26 12:07:05 -07001237 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
James Feist8bd25cc2019-03-15 15:14:00 -07001238 "org.freedesktop.DBus.Properties", "Get",
1239 "xyz.openbmc_project.Association", "endpoints");
1240 }
1241 },
1242 "xyz.openbmc_project.ObjectMapper",
1243 "/xyz/openbmc_project/object_mapper",
1244 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1245 "/xyz/openbmc_project/control", 2,
1246 std::array<const char*, 1>{
1247 "xyz.openbmc_project.Control.FanRedundancy"});
1248}
1249
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001250void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
1251{
1252 nlohmann::json& response = SensorsAsyncResp->res.jsonValue;
1253 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001254 if (SensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001255 {
1256 sensorHeaders = {"Voltages", "PowerSupplies"};
1257 }
1258 for (const std::string& sensorGroup : sensorHeaders)
1259 {
1260 nlohmann::json::iterator entry = response.find(sensorGroup);
1261 if (entry != response.end())
1262 {
1263 std::sort(entry->begin(), entry->end(),
1264 [](nlohmann::json& c1, nlohmann::json& c2) {
1265 return c1["Name"] < c2["Name"];
1266 });
1267
1268 // add the index counts to the end of each entry
1269 size_t count = 0;
1270 for (nlohmann::json& sensorJson : *entry)
1271 {
1272 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1273 if (odata == sensorJson.end())
1274 {
1275 continue;
1276 }
1277 std::string* value = odata->get_ptr<std::string*>();
1278 if (value != nullptr)
1279 {
1280 *value += std::to_string(count);
1281 count++;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001282 SensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001283 }
1284 }
1285 }
1286 }
1287}
1288
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001289/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001290 * @brief Finds the inventory item with the specified object path.
1291 * @param inventoryItems D-Bus inventory items associated with sensors.
1292 * @param invItemObjPath D-Bus object path of inventory item.
1293 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001294 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001295static InventoryItem* findInventoryItem(
1296 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1297 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001298{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001299 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001300 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001301 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001302 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001303 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001304 }
1305 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001306 return nullptr;
1307}
1308
1309/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001310 * @brief Finds the inventory item associated with the specified sensor.
1311 * @param inventoryItems D-Bus inventory items associated with sensors.
1312 * @param sensorObjPath D-Bus object path of sensor.
1313 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001314 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001315static InventoryItem* findInventoryItemForSensor(
1316 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1317 const std::string& sensorObjPath)
1318{
1319 for (InventoryItem& inventoryItem : *inventoryItems)
1320 {
1321 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1322 {
1323 return &inventoryItem;
1324 }
1325 }
1326 return nullptr;
1327}
1328
1329/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001330 * @brief Finds the inventory item associated with the specified led path.
1331 * @param inventoryItems D-Bus inventory items associated with sensors.
1332 * @param ledObjPath D-Bus object path of led.
1333 * @return Inventory item within vector, or nullptr if no match found.
1334 */
1335inline InventoryItem*
1336 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1337 const std::string& ledObjPath)
1338{
1339 for (InventoryItem& inventoryItem : inventoryItems)
1340 {
1341 if (inventoryItem.ledObjectPath == ledObjPath)
1342 {
1343 return &inventoryItem;
1344 }
1345 }
1346 return nullptr;
1347}
1348
1349/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001350 * @brief Adds inventory item and associated sensor to specified vector.
1351 *
1352 * Adds a new InventoryItem to the vector if necessary. Searches for an
1353 * existing InventoryItem with the specified object path. If not found, one is
1354 * added to the vector.
1355 *
1356 * Next, the specified sensor is added to the set of sensors associated with the
1357 * InventoryItem.
1358 *
1359 * @param inventoryItems D-Bus inventory items associated with sensors.
1360 * @param invItemObjPath D-Bus object path of inventory item.
1361 * @param sensorObjPath D-Bus object path of sensor
1362 */
1363static void
1364 addInventoryItem(std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1365 const std::string& invItemObjPath,
1366 const std::string& sensorObjPath)
1367{
1368 // Look for inventory item in vector
1369 InventoryItem* inventoryItem =
1370 findInventoryItem(inventoryItems, invItemObjPath);
1371
1372 // If inventory item doesn't exist in vector, add it
1373 if (inventoryItem == nullptr)
1374 {
1375 inventoryItems->emplace_back(invItemObjPath);
1376 inventoryItem = &(inventoryItems->back());
1377 }
1378
1379 // Add sensor to set of sensors associated with inventory item
1380 inventoryItem->sensors.emplace(sensorObjPath);
1381}
1382
1383/**
1384 * @brief Stores D-Bus data in the specified inventory item.
1385 *
1386 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1387 * specified InventoryItem.
1388 *
1389 * This data is later used to provide sensor property values in the JSON
1390 * response.
1391 *
1392 * @param inventoryItem Inventory item where data will be stored.
1393 * @param interfacesDict Map containing D-Bus interfaces and their properties
1394 * for the specified inventory item.
1395 */
1396static void storeInventoryItemData(
1397 InventoryItem& inventoryItem,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001398 const boost::container::flat_map<
1399 std::string, boost::container::flat_map<std::string, SensorVariant>>&
1400 interfacesDict)
1401{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001402 // Get properties from Inventory.Item interface
1403 auto interfaceIt =
1404 interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1405 if (interfaceIt != interfacesDict.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001406 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001407 auto propertyIt = interfaceIt->second.find("Present");
1408 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001409 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001410 const bool* value = std::get_if<bool>(&propertyIt->second);
1411 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001412 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001413 inventoryItem.isPresent = *value;
1414 }
1415 }
1416 }
1417
1418 // Check if Inventory.Item.PowerSupply interface is present
1419 interfaceIt =
1420 interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1421 if (interfaceIt != interfacesDict.end())
1422 {
1423 inventoryItem.isPowerSupply = true;
1424 }
1425
1426 // Get properties from Inventory.Decorator.Asset interface
1427 interfaceIt =
1428 interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1429 if (interfaceIt != interfacesDict.end())
1430 {
1431 auto propertyIt = interfaceIt->second.find("Manufacturer");
1432 if (propertyIt != interfaceIt->second.end())
1433 {
1434 const std::string* value =
1435 std::get_if<std::string>(&propertyIt->second);
1436 if (value != nullptr)
1437 {
1438 inventoryItem.manufacturer = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001439 }
1440 }
1441
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001442 propertyIt = interfaceIt->second.find("Model");
1443 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001444 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001445 const std::string* value =
1446 std::get_if<std::string>(&propertyIt->second);
1447 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001448 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001449 inventoryItem.model = *value;
1450 }
1451 }
1452
1453 propertyIt = interfaceIt->second.find("PartNumber");
1454 if (propertyIt != interfaceIt->second.end())
1455 {
1456 const std::string* value =
1457 std::get_if<std::string>(&propertyIt->second);
1458 if (value != nullptr)
1459 {
1460 inventoryItem.partNumber = *value;
1461 }
1462 }
1463
1464 propertyIt = interfaceIt->second.find("SerialNumber");
1465 if (propertyIt != interfaceIt->second.end())
1466 {
1467 const std::string* value =
1468 std::get_if<std::string>(&propertyIt->second);
1469 if (value != nullptr)
1470 {
1471 inventoryItem.serialNumber = *value;
1472 }
1473 }
1474 }
1475
1476 // Get properties from State.Decorator.OperationalStatus interface
1477 interfaceIt = interfacesDict.find(
1478 "xyz.openbmc_project.State.Decorator.OperationalStatus");
1479 if (interfaceIt != interfacesDict.end())
1480 {
1481 auto propertyIt = interfaceIt->second.find("Functional");
1482 if (propertyIt != interfaceIt->second.end())
1483 {
1484 const bool* value = std::get_if<bool>(&propertyIt->second);
1485 if (value != nullptr)
1486 {
1487 inventoryItem.isFunctional = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001488 }
1489 }
1490 }
1491}
1492
1493/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001494 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001495 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 * Uses the specified connections (services) to obtain D-Bus data for inventory
1497 * items associated with sensors. Stores the resulting data in the
1498 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001499 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001500 * This data is later used to provide sensor property values in the JSON
1501 * response.
1502 *
1503 * Finds the inventory item data asynchronously. Invokes callback when data has
1504 * been obtained.
1505 *
1506 * The callback must have the following signature:
1507 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001508 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001509 * @endcode
1510 *
1511 * This function is called recursively, obtaining data asynchronously from one
1512 * connection in each call. This ensures the callback is not invoked until the
1513 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001514 *
1515 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001516 * @param inventoryItems D-Bus inventory items associated with sensors.
1517 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001518 * @param objectMgrPaths Mappings from connection name to DBus object path that
1519 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001520 * @param callback Callback to invoke when inventory data has been obtained.
1521 * @param invConnectionsIndex Current index in invConnections. Only specified
1522 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001523 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001524template <typename Callback>
1525static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001526 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001527 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001528 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1529 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001530 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001531 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001532{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001533 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001535 // If no more connections left, call callback
1536 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001537 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001538 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001539 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1540 return;
1541 }
1542
1543 // Get inventory item data from current connection
1544 auto it = invConnections->nth(invConnectionsIndex);
1545 if (it != invConnections->end())
1546 {
1547 const std::string& invConnection = *it;
1548
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001549 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001550 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1551 objectMgrPaths, callback{std::move(callback)},
1552 invConnectionsIndex](
1553 const boost::system::error_code ec,
1554 ManagedObjectsVectorType& resp) {
1555 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001556 if (ec)
1557 {
1558 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001559 << "getInventoryItemsData respHandler DBus error " << ec;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 messages::internalError(sensorsAsyncResp->res);
1561 return;
1562 }
1563
1564 // Loop through returned object paths
1565 for (const auto& objDictEntry : resp)
1566 {
1567 const std::string& objPath =
1568 static_cast<const std::string&>(objDictEntry.first);
1569
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001570 // If this object path is one of the specified inventory items
1571 InventoryItem* inventoryItem =
1572 findInventoryItem(inventoryItems, objPath);
1573 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001574 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001575 // Store inventory data in InventoryItem
1576 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001577 }
1578 }
1579
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001580 // Recurse to get inventory item data from next connection
1581 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1582 invConnections, objectMgrPaths,
1583 std::move(callback), invConnectionsIndex + 1);
1584
1585 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001586 };
1587
1588 // Find DBus object path that implements ObjectManager for the current
1589 // connection. If no mapping found, default to "/".
1590 auto iter = objectMgrPaths->find(invConnection);
1591 const std::string& objectMgrPath =
1592 (iter != objectMgrPaths->end()) ? iter->second : "/";
1593 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1594 << objectMgrPath;
1595
1596 // Get all object paths and their interfaces for current connection
1597 crow::connections::systemBus->async_method_call(
1598 std::move(respHandler), invConnection, objectMgrPath,
1599 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1600 }
1601
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001602 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001603}
1604
1605/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001607 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001608 * Gets the D-Bus connections (services) that provide data for the inventory
1609 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001610 *
1611 * Finds the connections asynchronously. Invokes callback when information has
1612 * been obtained.
1613 *
1614 * The callback must have the following signature:
1615 * @code
1616 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1617 * invConnections)
1618 * @endcode
1619 *
1620 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001621 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001622 * @param callback Callback to invoke when connections have been obtained.
1623 */
1624template <typename Callback>
1625static void getInventoryItemsConnections(
1626 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001627 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001628 Callback&& callback)
1629{
1630 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1631
1632 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001633 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001634 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001635 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1636 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001637 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1638
1639 // Response handler for parsing output from GetSubTree
1640 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001641 inventoryItems](const boost::system::error_code ec,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 const GetSubTreeType& subtree) {
1643 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1644 if (ec)
1645 {
1646 messages::internalError(sensorsAsyncResp->res);
1647 BMCWEB_LOG_ERROR
1648 << "getInventoryItemsConnections respHandler DBus error " << ec;
1649 return;
1650 }
1651
1652 // Make unique list of connections for desired inventory items
1653 std::shared_ptr<boost::container::flat_set<std::string>>
1654 invConnections =
1655 std::make_shared<boost::container::flat_set<std::string>>();
1656 invConnections->reserve(8);
1657
1658 // Loop through objects from GetSubTree
1659 for (const std::pair<
1660 std::string,
1661 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1662 object : subtree)
1663 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001664 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001665 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001666 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001667 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001668 // Store all connections to inventory item
1669 for (const std::pair<std::string, std::vector<std::string>>&
1670 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001671 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001672 const std::string& invConnection = objData.first;
1673 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001674 }
1675 }
1676 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001677
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001678 callback(invConnections);
1679 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1680 };
1681
1682 // Make call to ObjectMapper to find all inventory items
1683 crow::connections::systemBus->async_method_call(
1684 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1685 "/xyz/openbmc_project/object_mapper",
1686 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1687 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1688}
1689
1690/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001691 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001692 *
1693 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001694 * inventory items. Then finds the associations from those inventory items to
1695 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001696 *
1697 * Finds the inventory items asynchronously. Invokes callback when information
1698 * has been obtained.
1699 *
1700 * The callback must have the following signature:
1701 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001702 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001703 * @endcode
1704 *
1705 * @param sensorsAsyncResp Pointer to object holding response data.
1706 * @param sensorNames All sensors within the current chassis.
1707 * @param objectMgrPaths Mappings from connection name to DBus object path that
1708 * implements ObjectManager.
1709 * @param callback Callback to invoke when inventory items have been obtained.
1710 */
1711template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001712static void getInventoryItemAssociations(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001713 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1714 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
1715 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1716 objectMgrPaths,
1717 Callback&& callback)
1718{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001719 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001720
1721 // Response handler for GetManagedObjects
1722 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1723 sensorNames](const boost::system::error_code ec,
1724 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001725 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001726 if (ec)
1727 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001728 BMCWEB_LOG_ERROR
1729 << "getInventoryItemAssociations respHandler DBus error " << ec;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001730 messages::internalError(sensorsAsyncResp->res);
1731 return;
1732 }
1733
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001734 // Create vector to hold list of inventory items
1735 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1736 std::make_shared<std::vector<InventoryItem>>();
1737
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001738 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001739 std::string sensorAssocPath;
1740 sensorAssocPath.reserve(128); // avoid memory allocations
1741 for (const auto& objDictEntry : resp)
1742 {
1743 const std::string& objPath =
1744 static_cast<const std::string&>(objDictEntry.first);
1745 const boost::container::flat_map<
1746 std::string, boost::container::flat_map<
1747 std::string, dbus::utility::DbusVariantType>>&
1748 interfacesDict = objDictEntry.second;
1749
1750 // If path is inventory association for one of the specified sensors
1751 for (const std::string& sensorName : *sensorNames)
1752 {
1753 sensorAssocPath = sensorName;
1754 sensorAssocPath += "/inventory";
1755 if (objPath == sensorAssocPath)
1756 {
1757 // Get Association interface for object path
1758 auto assocIt =
1759 interfacesDict.find("xyz.openbmc_project.Association");
1760 if (assocIt != interfacesDict.end())
1761 {
1762 // Get inventory item from end point
1763 auto endpointsIt = assocIt->second.find("endpoints");
1764 if (endpointsIt != assocIt->second.end())
1765 {
1766 const std::vector<std::string>* endpoints =
1767 std::get_if<std::vector<std::string>>(
1768 &endpointsIt->second);
1769 if ((endpoints != nullptr) && !endpoints->empty())
1770 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001771 // Add inventory item to vector
1772 const std::string& invItemPath =
1773 endpoints->front();
1774 addInventoryItem(inventoryItems, invItemPath,
1775 sensorName);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001776 }
1777 }
1778 }
1779 break;
1780 }
1781 }
1782 }
1783
Anthony Wilsond5005492019-07-31 16:34:17 -05001784 // Now loop through the returned object paths again, this time to
1785 // find the leds associated with the inventory items we just found
1786 std::string inventoryAssocPath;
1787 inventoryAssocPath.reserve(128); // avoid memory allocations
1788 for (const auto& objDictEntry : resp)
1789 {
1790 const std::string& objPath =
1791 static_cast<const std::string&>(objDictEntry.first);
1792 const boost::container::flat_map<
1793 std::string, boost::container::flat_map<
1794 std::string, dbus::utility::DbusVariantType>>&
1795 interfacesDict = objDictEntry.second;
1796
1797 for (InventoryItem& inventoryItem : *inventoryItems)
1798 {
1799 inventoryAssocPath = inventoryItem.objectPath;
1800 inventoryAssocPath += "/leds";
1801 if (objPath == inventoryAssocPath)
1802 {
1803 // Get Association interface for object path
1804 auto assocIt =
1805 interfacesDict.find("xyz.openbmc_project.Association");
1806 if (assocIt != interfacesDict.end())
1807 {
1808 // Get inventory item from end point
1809 auto endpointsIt = assocIt->second.find("endpoints");
1810 if (endpointsIt != assocIt->second.end())
1811 {
1812 const std::vector<std::string>* endpoints =
1813 std::get_if<std::vector<std::string>>(
1814 &endpointsIt->second);
1815 if ((endpoints != nullptr) && !endpoints->empty())
1816 {
1817 // Store LED path in inventory item
1818 const std::string& ledPath = endpoints->front();
1819 inventoryItem.ledObjectPath = ledPath;
1820 }
1821 }
1822 }
1823 break;
1824 }
1825 }
1826 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001827 callback(inventoryItems);
1828 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001829 };
1830
1831 // Find DBus object path that implements ObjectManager for ObjectMapper
1832 std::string connection = "xyz.openbmc_project.ObjectMapper";
1833 auto iter = objectMgrPaths->find(connection);
1834 const std::string& objectMgrPath =
1835 (iter != objectMgrPaths->end()) ? iter->second : "/";
1836 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1837 << objectMgrPath;
1838
1839 // Call GetManagedObjects on the ObjectMapper to get all associations
1840 crow::connections::systemBus->async_method_call(
1841 std::move(respHandler), connection, objectMgrPath,
1842 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1843
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001844 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001845}
1846
1847/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001848 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1849 *
1850 * Uses the specified connections (services) to obtain D-Bus data for inventory
1851 * item leds associated with sensors. Stores the resulting data in the
1852 * inventoryItems vector.
1853 *
1854 * This data is later used to provide sensor property values in the JSON
1855 * response.
1856 *
1857 * Finds the inventory item led data asynchronously. Invokes callback when data
1858 * has been obtained.
1859 *
1860 * The callback must have the following signature:
1861 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001862 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001863 * @endcode
1864 *
1865 * This function is called recursively, obtaining data asynchronously from one
1866 * connection in each call. This ensures the callback is not invoked until the
1867 * last asynchronous function has completed.
1868 *
1869 * @param sensorsAsyncResp Pointer to object holding response data.
1870 * @param inventoryItems D-Bus inventory items associated with sensors.
1871 * @param ledConnections Connections that provide data for the inventory leds.
1872 * @param callback Callback to invoke when inventory data has been obtained.
1873 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1874 * in recursive calls to this function.
1875 */
1876template <typename Callback>
1877void getInventoryLedData(
1878 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1879 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1880 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1881 ledConnections,
1882 Callback&& callback, size_t ledConnectionsIndex = 0)
1883{
1884 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1885
1886 // If no more connections left, call callback
1887 if (ledConnectionsIndex >= ledConnections->size())
1888 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001889 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001890 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1891 return;
1892 }
1893
1894 // Get inventory item data from current connection
1895 auto it = ledConnections->nth(ledConnectionsIndex);
1896 if (it != ledConnections->end())
1897 {
1898 const std::string& ledPath = (*it).first;
1899 const std::string& ledConnection = (*it).second;
1900 // Response handler for Get State property
1901 auto respHandler =
1902 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1903 callback{std::move(callback)},
1904 ledConnectionsIndex](const boost::system::error_code ec,
1905 const std::variant<std::string>& ledState) {
1906 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1907 if (ec)
1908 {
1909 BMCWEB_LOG_ERROR
1910 << "getInventoryLedData respHandler DBus error " << ec;
1911 messages::internalError(sensorsAsyncResp->res);
1912 return;
1913 }
1914
1915 const std::string* state = std::get_if<std::string>(&ledState);
1916 if (state != nullptr)
1917 {
1918 BMCWEB_LOG_DEBUG << "Led state: " << *state;
1919 // Find inventory item with this LED object path
1920 InventoryItem* inventoryItem =
1921 findInventoryItemForLed(*inventoryItems, ledPath);
1922 if (inventoryItem != nullptr)
1923 {
1924 // Store LED state in InventoryItem
1925 if (boost::ends_with(*state, "On"))
1926 {
1927 inventoryItem->ledState = LedState::ON;
1928 }
1929 else if (boost::ends_with(*state, "Blink"))
1930 {
1931 inventoryItem->ledState = LedState::BLINK;
1932 }
1933 else if (boost::ends_with(*state, "Off"))
1934 {
1935 inventoryItem->ledState = LedState::OFF;
1936 }
1937 else
1938 {
1939 inventoryItem->ledState = LedState::UNKNOWN;
1940 }
1941 }
1942 }
1943 else
1944 {
1945 BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
1946 << ledPath;
1947 }
1948
1949 // Recurse to get LED data from next connection
1950 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1951 ledConnections, std::move(callback),
1952 ledConnectionsIndex + 1);
1953
1954 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1955 };
1956
1957 // Get the State property for the current LED
1958 crow::connections::systemBus->async_method_call(
1959 std::move(respHandler), ledConnection, ledPath,
1960 "org.freedesktop.DBus.Properties", "Get",
1961 "xyz.openbmc_project.Led.Physical", "State");
1962 }
1963
1964 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1965}
1966
1967/**
1968 * @brief Gets LED data for LEDs associated with given inventory items.
1969 *
1970 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1971 * associated with the specified inventory items. Then gets the LED data from
1972 * each connection and stores it in the inventory item.
1973 *
1974 * This data is later used to provide sensor property values in the JSON
1975 * response.
1976 *
1977 * Finds the LED data asynchronously. Invokes callback when information has
1978 * been obtained.
1979 *
1980 * The callback must have the following signature:
1981 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001982 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001983 * @endcode
1984 *
1985 * @param sensorsAsyncResp Pointer to object holding response data.
1986 * @param inventoryItems D-Bus inventory items associated with sensors.
1987 * @param callback Callback to invoke when inventory items have been obtained.
1988 */
1989template <typename Callback>
1990void getInventoryLeds(
1991 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1992 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1993 Callback&& callback)
1994{
1995 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1996
1997 const std::string path = "/xyz/openbmc_project";
1998 const std::array<std::string, 1> interfaces = {
1999 "xyz.openbmc_project.Led.Physical"};
2000
2001 // Response handler for parsing output from GetSubTree
2002 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2003 inventoryItems](const boost::system::error_code ec,
2004 const GetSubTreeType& subtree) {
2005 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2006 if (ec)
2007 {
2008 messages::internalError(sensorsAsyncResp->res);
2009 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2010 << ec;
2011 return;
2012 }
2013
2014 // Build map of LED object paths to connections
2015 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2016 ledConnections = std::make_shared<
2017 boost::container::flat_map<std::string, std::string>>();
2018
2019 // Loop through objects from GetSubTree
2020 for (const std::pair<
2021 std::string,
2022 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2023 object : subtree)
2024 {
2025 // Check if object path is LED for one of the specified inventory
2026 // items
2027 const std::string& ledPath = object.first;
2028 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2029 {
2030 // Add mapping from ledPath to connection
2031 const std::string& connection = object.second.begin()->first;
2032 (*ledConnections)[ledPath] = connection;
2033 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2034 << connection;
2035 }
2036 }
2037
2038 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2039 std::move(callback));
2040 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2041 };
2042 // Make call to ObjectMapper to find all inventory items
2043 crow::connections::systemBus->async_method_call(
2044 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2045 "/xyz/openbmc_project/object_mapper",
2046 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2047 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2048}
2049
2050/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002051 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2052 *
2053 * Uses the specified connections (services) (currently assumes just one) to
2054 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2055 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2056 *
2057 * This data is later used to provide sensor property values in the JSON
2058 * response.
2059 *
2060 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2061 * when data has been obtained.
2062 *
2063 * The callback must have the following signature:
2064 * @code
2065 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2066 * @endcode
2067 *
2068 * @param sensorsAsyncResp Pointer to object holding response data.
2069 * @param inventoryItems D-Bus inventory items associated with sensors.
2070 * @param psAttributesConnections Connections that provide data for the Power
2071 * Supply Attributes
2072 * @param callback Callback to invoke when data has been obtained.
2073 */
2074template <typename Callback>
2075void getPowerSupplyAttributesData(
2076 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2077 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2078 const boost::container::flat_map<std::string, std::string>&
2079 psAttributesConnections,
2080 Callback&& callback)
2081{
2082 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2083
2084 if (psAttributesConnections.empty())
2085 {
2086 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2087 callback(inventoryItems);
2088 return;
2089 }
2090
2091 // Assuming just one connection (service) for now
2092 auto it = psAttributesConnections.nth(0);
2093
2094 const std::string& psAttributesPath = (*it).first;
2095 const std::string& psAttributesConnection = (*it).second;
2096
2097 // Response handler for Get DeratingFactor property
2098 auto respHandler = [sensorsAsyncResp, inventoryItems,
2099 callback{std::move(callback)}](
2100 const boost::system::error_code ec,
2101 const std::variant<uint32_t>& deratingFactor) {
2102 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2103 if (ec)
2104 {
2105 BMCWEB_LOG_ERROR
2106 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
2107 messages::internalError(sensorsAsyncResp->res);
2108 return;
2109 }
2110
2111 const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
2112 if (value != nullptr)
2113 {
2114 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
2115 // Store value in Power Supply Inventory Items
2116 for (InventoryItem& inventoryItem : *inventoryItems)
2117 {
2118 if (inventoryItem.isPowerSupply == true)
2119 {
2120 inventoryItem.powerSupplyEfficiencyPercent =
2121 static_cast<int>(*value);
2122 }
2123 }
2124 }
2125 else
2126 {
2127 BMCWEB_LOG_DEBUG
2128 << "Failed to find EfficiencyPercent value for PowerSupplies";
2129 }
2130
2131 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2132 callback(inventoryItems);
2133 };
2134
2135 // Get the DeratingFactor property for the PowerSupplyAttributes
2136 // Currently only property on the interface/only one we care about
2137 crow::connections::systemBus->async_method_call(
2138 std::move(respHandler), psAttributesConnection, psAttributesPath,
2139 "org.freedesktop.DBus.Properties", "Get",
2140 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
2141
2142 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2143}
2144
2145/**
2146 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2147 *
2148 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2149 * data. Then gets the Power Supply Attributes data from the connection
2150 * (currently just assumes 1 connection) and stores the data in the inventory
2151 * item.
2152 *
2153 * This data is later used to provide sensor property values in the JSON
2154 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2155 *
2156 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2157 * when information has been obtained.
2158 *
2159 * The callback must have the following signature:
2160 * @code
2161 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2162 * @endcode
2163 *
2164 * @param sensorsAsyncResp Pointer to object holding response data.
2165 * @param inventoryItems D-Bus inventory items associated with sensors.
2166 * @param callback Callback to invoke when data has been obtained.
2167 */
2168template <typename Callback>
2169void getPowerSupplyAttributes(
2170 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2171 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2172 Callback&& callback)
2173{
2174 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2175
2176 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002177 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002178 {
2179 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2180 callback(inventoryItems);
2181 return;
2182 }
2183
2184 const std::array<std::string, 1> interfaces = {
2185 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2186
2187 // Response handler for parsing output from GetSubTree
2188 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2189 inventoryItems](const boost::system::error_code ec,
2190 const GetSubTreeType& subtree) {
2191 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2192 if (ec)
2193 {
2194 messages::internalError(sensorsAsyncResp->res);
2195 BMCWEB_LOG_ERROR
2196 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2197 return;
2198 }
2199 if (subtree.size() == 0)
2200 {
2201 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2202 callback(inventoryItems);
2203 return;
2204 }
2205
2206 // Currently we only support 1 power supply attribute, use this for
2207 // all the power supplies. Build map of object path to connection.
2208 // Assume just 1 connection and 1 path for now.
2209 boost::container::flat_map<std::string, std::string>
2210 psAttributesConnections;
2211
2212 if (subtree[0].first.empty() || subtree[0].second.empty())
2213 {
2214 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2215 callback(inventoryItems);
2216 return;
2217 }
2218
2219 const std::string& psAttributesPath = subtree[0].first;
2220 const std::string& connection = subtree[0].second.begin()->first;
2221
2222 if (connection.empty())
2223 {
2224 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2225 callback(inventoryItems);
2226 return;
2227 }
2228
2229 psAttributesConnections[psAttributesPath] = connection;
2230 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2231 << connection;
2232
2233 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2234 psAttributesConnections,
2235 std::move(callback));
2236 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2237 };
2238 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2239 crow::connections::systemBus->async_method_call(
2240 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2241 "/xyz/openbmc_project/object_mapper",
2242 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2243 "/xyz/openbmc_project", 0, interfaces);
2244 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2245}
2246
2247/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002248 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002249 *
2250 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002251 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002252 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002253 * This data is later used to provide sensor property values in the JSON
2254 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002255 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 * Finds the inventory items asynchronously. Invokes callback when the
2257 * inventory items have been obtained.
2258 *
2259 * The callback must have the following signature:
2260 * @code
2261 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2262 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002263 *
2264 * @param sensorsAsyncResp Pointer to object holding response data.
2265 * @param sensorNames All sensors within the current chassis.
2266 * @param objectMgrPaths Mappings from connection name to DBus object path that
2267 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002268 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002269 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002270template <typename Callback>
2271static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002272 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2273 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2274 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002275 objectMgrPaths,
2276 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002277{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002278 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2279 auto getInventoryItemAssociationsCb =
2280 [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2281 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2282 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002283 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002284 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2285 callback{std::move(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002286 std::shared_ptr<boost::container::flat_set<std::string>>
2287 invConnections) {
2288 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002289 auto getInventoryItemsDataCb =
2290 [sensorsAsyncResp, inventoryItems,
2291 callback{std::move(callback)}]() {
2292 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002293
2294 auto getInventoryLedsCb = [sensorsAsyncResp,
2295 inventoryItems,
2296 callback{std::move(
2297 callback)}]() {
2298 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2299 // Find Power Supply Attributes and get the data
2300 getPowerSupplyAttributes(sensorsAsyncResp,
2301 inventoryItems,
2302 std::move(callback));
2303 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2304 };
2305
Anthony Wilsond5005492019-07-31 16:34:17 -05002306 // Find led connections and get the data
2307 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002308 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002309 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2310 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002311
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002312 // Get inventory item data from connections
2313 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2314 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002315 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002316 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2317 };
2318
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002319 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002320 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002321 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002322 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002323 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002324 };
2325
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002326 // Get associations from sensors to inventory items
2327 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2328 std::move(getInventoryItemAssociationsCb));
2329 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2330}
2331
2332/**
2333 * @brief Returns JSON PowerSupply object for the specified inventory item.
2334 *
2335 * Searches for a JSON PowerSupply object that matches the specified inventory
2336 * item. If one is not found, a new PowerSupply object is added to the JSON
2337 * array.
2338 *
2339 * Multiple sensors are often associated with one power supply inventory item.
2340 * As a result, multiple sensor values are stored in one JSON PowerSupply
2341 * object.
2342 *
2343 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2344 * @param inventoryItem Inventory item for the power supply.
2345 * @param chassisId Chassis that contains the power supply.
2346 * @return JSON PowerSupply object for the specified inventory item.
2347 */
2348static nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2349 const InventoryItem& inventoryItem,
2350 const std::string& chassisId)
2351{
2352 // Check if matching PowerSupply object already exists in JSON array
2353 for (nlohmann::json& powerSupply : powerSupplyArray)
2354 {
2355 if (powerSupply["MemberId"] == inventoryItem.name)
2356 {
2357 return powerSupply;
2358 }
2359 }
2360
2361 // Add new PowerSupply object to JSON array
2362 powerSupplyArray.push_back({});
2363 nlohmann::json& powerSupply = powerSupplyArray.back();
2364 powerSupply["@odata.id"] =
2365 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2366 powerSupply["MemberId"] = inventoryItem.name;
2367 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2368 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2369 powerSupply["Model"] = inventoryItem.model;
2370 powerSupply["PartNumber"] = inventoryItem.partNumber;
2371 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002372 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002373
Gunnar Mills42cbe532019-08-15 15:26:54 -05002374 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2375 {
2376 powerSupply["EfficiencyPercent"] =
2377 inventoryItem.powerSupplyEfficiencyPercent;
2378 }
2379
2380 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002381 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2382 powerSupply["Status"]["Health"] = health;
2383
2384 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002385}
2386
2387/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002388 * @brief Gets the values of the specified sensors.
2389 *
2390 * Stores the results as JSON in the SensorsAsyncResp.
2391 *
2392 * Gets the sensor values asynchronously. Stores the results later when the
2393 * information has been obtained.
2394 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002395 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002396 *
2397 * To minimize the number of DBus calls, the DBus method
2398 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2399 * values of all sensors provided by a connection (service).
2400 *
2401 * The connections set contains all the connections that provide sensor values.
2402 *
2403 * The objectMgrPaths map contains mappings from a connection name to the
2404 * corresponding DBus object path that implements ObjectManager.
2405 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002406 * The InventoryItem vector contains D-Bus inventory items associated with the
2407 * sensors. Inventory item data is needed for some Redfish sensor properties.
2408 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002409 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002410 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002411 * @param connections Connections that provide sensor values.
2412 * @param objectMgrPaths Mappings from connection name to DBus object path that
2413 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002414 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002415 */
2416void getSensorData(
2417 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002418 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002419 const boost::container::flat_set<std::string>& connections,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002420 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002421 objectMgrPaths,
2422 std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002423{
2424 BMCWEB_LOG_DEBUG << "getSensorData enter";
2425 // Get managed objects from all services exposing sensors
2426 for (const std::string& connection : connections)
2427 {
2428 // Response handler to process managed objects
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002429 auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002430 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002431 const boost::system::error_code ec,
2432 ManagedObjectsVectorType& resp) {
2433 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2434 if (ec)
2435 {
2436 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
2437 messages::internalError(SensorsAsyncResp->res);
2438 return;
2439 }
2440 // Go through all objects and update response with sensor data
2441 for (const auto& objDictEntry : resp)
2442 {
2443 const std::string& objPath =
2444 static_cast<const std::string&>(objDictEntry.first);
2445 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2446 << objPath;
2447
Shawn McCarneyde629b62019-03-08 10:42:51 -06002448 std::vector<std::string> split;
2449 // Reserve space for
2450 // /xyz/openbmc_project/sensors/<name>/<subname>
2451 split.reserve(6);
2452 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2453 if (split.size() < 6)
2454 {
2455 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2456 << objPath;
2457 continue;
2458 }
2459 // These indexes aren't intuitive, as boost::split puts an empty
2460 // string at the beginning
2461 const std::string& sensorType = split[4];
2462 const std::string& sensorName = split[5];
2463 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2464 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002465 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002466 {
2467 BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2468 continue;
2469 }
2470
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002471 // Find inventory item (if any) associated with sensor
2472 InventoryItem* inventoryItem =
2473 findInventoryItemForSensor(inventoryItems, objPath);
2474
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002475 const std::string& sensorSchema =
2476 SensorsAsyncResp->chassisSubNode;
2477
2478 nlohmann::json* sensorJson = nullptr;
2479
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002480 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002481 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002482 SensorsAsyncResp->res.jsonValue["@odata.id"] =
2483 "/redfish/v1/Chassis/" + SensorsAsyncResp->chassisId +
2484 "/" + SensorsAsyncResp->chassisSubNode + "/" +
2485 sensorName;
2486 sensorJson = &(SensorsAsyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002487 }
2488 else
2489 {
Ed Tanous271584a2019-07-09 16:24:22 -07002490 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002491 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002492 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002493 fieldName = "Temperatures";
2494 }
2495 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2496 sensorType == "fan_pwm")
2497 {
2498 fieldName = "Fans";
2499 }
2500 else if (sensorType == "voltage")
2501 {
2502 fieldName = "Voltages";
2503 }
2504 else if (sensorType == "power")
2505 {
2506 if (!sensorName.compare("total_power"))
2507 {
2508 fieldName = "PowerControl";
2509 }
2510 else if ((inventoryItem != nullptr) &&
2511 (inventoryItem->isPowerSupply))
2512 {
2513 fieldName = "PowerSupplies";
2514 }
2515 else
2516 {
2517 // Other power sensors are in SensorCollection
2518 continue;
2519 }
2520 }
2521 else
2522 {
2523 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2524 << sensorType;
2525 continue;
2526 }
2527
2528 nlohmann::json& tempArray =
2529 SensorsAsyncResp->res.jsonValue[fieldName];
2530 if (fieldName == "PowerControl")
2531 {
2532 if (tempArray.empty())
2533 {
2534 // Put multiple "sensors" into a single
2535 // PowerControl. Follows MemberId naming and
2536 // naming in power.hpp.
2537 tempArray.push_back(
2538 {{"@odata.id",
2539 "/redfish/v1/Chassis/" +
2540 SensorsAsyncResp->chassisId + "/" +
2541 SensorsAsyncResp->chassisSubNode + "#/" +
2542 fieldName + "/0"}});
2543 }
2544 sensorJson = &(tempArray.back());
2545 }
2546 else if (fieldName == "PowerSupplies")
2547 {
2548 if (inventoryItem != nullptr)
2549 {
2550 sensorJson =
2551 &(getPowerSupply(tempArray, *inventoryItem,
2552 SensorsAsyncResp->chassisId));
2553 }
2554 }
2555 else
2556 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002557 tempArray.push_back(
2558 {{"@odata.id",
2559 "/redfish/v1/Chassis/" +
2560 SensorsAsyncResp->chassisId + "/" +
2561 SensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002562 fieldName + "/"}});
2563 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002564 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002565 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002566
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002567 if (sensorJson != nullptr)
2568 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002569 objectInterfacesToJson(
2570 sensorName, sensorType, SensorsAsyncResp,
2571 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002572 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002573 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002574 if (SensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002575 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002576 sortJSONResponse(SensorsAsyncResp);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002577 if (SensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002578 {
2579 populateFanRedundancy(SensorsAsyncResp);
2580 }
James Feist8bd25cc2019-03-15 15:14:00 -07002581 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002582 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2583 };
2584
2585 // Find DBus object path that implements ObjectManager for the current
2586 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002587 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002588 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002589 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002590 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2591 << objectMgrPath;
2592
2593 crow::connections::systemBus->async_method_call(
2594 getManagedObjectsCb, connection, objectMgrPath,
2595 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
2596 };
2597 BMCWEB_LOG_DEBUG << "getSensorData exit";
2598}
2599
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002600void processSensorList(
2601 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
2602 std::shared_ptr<boost::container::flat_set<std::string>> sensorNames)
2603{
2604 auto getConnectionCb =
2605 [SensorsAsyncResp, sensorNames](
2606 const boost::container::flat_set<std::string>& connections) {
2607 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2608 auto getObjectManagerPathsCb =
2609 [SensorsAsyncResp, sensorNames, connections](
2610 std::shared_ptr<
2611 boost::container::flat_map<std::string, std::string>>
2612 objectMgrPaths) {
2613 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2614 auto getInventoryItemsCb =
2615 [SensorsAsyncResp, sensorNames, connections,
2616 objectMgrPaths](
2617 std::shared_ptr<std::vector<InventoryItem>>
2618 inventoryItems) {
2619 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2620 // Get sensor data and store results in JSON
2621 getSensorData(SensorsAsyncResp, sensorNames,
2622 connections, objectMgrPaths,
2623 inventoryItems);
2624 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2625 };
2626
2627 // Get inventory items associated with sensors
2628 getInventoryItems(SensorsAsyncResp, sensorNames,
2629 objectMgrPaths,
2630 std::move(getInventoryItemsCb));
2631
2632 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2633 };
2634
2635 // Get mapping from connection names to the DBus object
2636 // paths that implement the ObjectManager interface
2637 getObjectManagerPaths(SensorsAsyncResp,
2638 std::move(getObjectManagerPathsCb));
2639 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2640 };
2641
2642 // Get set of connections that provide sensor values
2643 getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb));
2644}
2645
Shawn McCarneyde629b62019-03-08 10:42:51 -06002646/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002647 * @brief Entry point for retrieving sensors data related to requested
2648 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002649 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002650 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002651void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
2652{
2653 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002654 auto getChassisCb =
2655 [SensorsAsyncResp](
2656 std::shared_ptr<boost::container::flat_set<std::string>>
2657 sensorNames) {
2658 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002659 processSensorList(SensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002660 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2661 };
Jennifer Lee4f9a2132019-03-04 12:45:19 -08002662 SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002663
Shawn McCarney26f03892019-05-03 13:20:24 -05002664 // Get set of sensors in chassis
Ed Tanous1abe55e2018-09-05 08:30:59 -07002665 getChassis(SensorsAsyncResp, std::move(getChassisCb));
2666 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002667}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002668
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302669/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002670 * @brief Find the requested sensorName in the list of all sensors supplied by
2671 * the chassis node
2672 *
2673 * @param sensorName The sensor name supplied in the PATCH request
2674 * @param sensorsList The list of sensors managed by the chassis node
2675 * @param sensorsModified The list of sensors that were found as a result of
2676 * repeated calls to this function
2677 */
2678bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302679 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002680 boost::container::flat_set<std::string>& sensorsList,
2681 boost::container::flat_set<std::string>& sensorsModified)
2682{
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302683 for (std::string_view chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002684 {
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302685 std::size_t pos = chassisSensor.rfind("/");
2686 if (pos >= (chassisSensor.size() - 1))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002687 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002688 continue;
2689 }
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302690 std::string_view thisSensorName = chassisSensor.substr(pos + 1);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002691 if (thisSensorName == sensorName)
2692 {
2693 sensorsModified.emplace(chassisSensor);
2694 return true;
2695 }
2696 }
2697 return false;
2698}
2699
2700/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302701 * @brief Entry point for overriding sensor values of given sensor
2702 *
2703 * @param res response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002704 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002705 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302706 */
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002707void setSensorsOverride(
Carol Wang4bb3dc32019-10-17 18:15:02 +08002708 std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
2709 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002710 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302711{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002712 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002713 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302714
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302715 const char* propertyValueName;
2716 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302717 std::string memberId;
2718 double value;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302719 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302720 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302721 if (collectionItems.first == "Temperatures")
2722 {
2723 propertyValueName = "ReadingCelsius";
2724 }
2725 else if (collectionItems.first == "Fans")
2726 {
2727 propertyValueName = "Reading";
2728 }
2729 else
2730 {
2731 propertyValueName = "ReadingVolts";
2732 }
2733 for (auto& item : collectionItems.second)
2734 {
Carol Wang4bb3dc32019-10-17 18:15:02 +08002735 if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId",
2736 memberId, propertyValueName, value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302737 {
2738 return;
2739 }
2740 overrideMap.emplace(memberId,
2741 std::make_pair(value, collectionItems.first));
2742 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302743 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002744
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002745 auto getChassisSensorListCb = [sensorAsyncResp,
2746 overrideMap](const std::shared_ptr<
2747 boost::container::flat_set<
2748 std::string>>
2749 sensorsList) {
2750 // Match sensor names in the PATCH request to those managed by the
2751 // chassis node
2752 const std::shared_ptr<boost::container::flat_set<std::string>>
2753 sensorNames =
2754 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302755 for (const auto& item : overrideMap)
2756 {
2757 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002758 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2759 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302760 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302761 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302762 messages::resourceNotFound(sensorAsyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302763 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302764 return;
2765 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302766 }
2767 // Get the connection to which the memberId belongs
2768 auto getObjectsWithConnectionCb =
2769 [sensorAsyncResp, overrideMap](
2770 const boost::container::flat_set<std::string>& connections,
2771 const std::set<std::pair<std::string, std::string>>&
2772 objectsWithConnection) {
2773 if (objectsWithConnection.size() != overrideMap.size())
2774 {
2775 BMCWEB_LOG_INFO
2776 << "Unable to find all objects with proper connection "
2777 << objectsWithConnection.size() << " requested "
2778 << overrideMap.size() << "\n";
2779 messages::resourceNotFound(
2780 sensorAsyncResp->res,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002781 sensorAsyncResp->chassisSubNode ==
2782 sensors::node::thermal
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302783 ? "Temperatures"
2784 : "Voltages",
2785 "Count");
2786 return;
2787 }
2788 for (const auto& item : objectsWithConnection)
2789 {
2790
2791 auto lastPos = item.first.rfind('/');
2792 if (lastPos == std::string::npos)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302793 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302794 messages::internalError(sensorAsyncResp->res);
2795 return;
2796 }
2797 std::string sensorName = item.first.substr(lastPos + 1);
2798
2799 const auto& iterator = overrideMap.find(sensorName);
2800 if (iterator == overrideMap.end())
2801 {
2802 BMCWEB_LOG_INFO << "Unable to find sensor object"
2803 << item.first << "\n";
2804 messages::internalError(sensorAsyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302805 return;
2806 }
2807 crow::connections::systemBus->async_method_call(
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302808 [sensorAsyncResp](const boost::system::error_code ec) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302809 if (ec)
2810 {
2811 BMCWEB_LOG_DEBUG
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302812 << "setOverrideValueStatus DBUS error: "
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302813 << ec;
2814 messages::internalError(sensorAsyncResp->res);
2815 return;
2816 }
2817 },
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302818 item.second, item.first,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302819 "org.freedesktop.DBus.Properties", "Set",
2820 "xyz.openbmc_project.Sensor.Value", "Value",
Patrick Williams19bd78d2020-05-13 17:38:24 -05002821 std::variant<double>(iterator->second.first));
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302822 }
2823 };
2824 // Get object with connection for the given sensor name
2825 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2826 std::move(getObjectsWithConnectionCb));
2827 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302828 // get full sensor list for the given chassisId and cross verify the sensor.
2829 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2830}
2831
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002832bool isOverridingAllowed(const std::string& manufacturingModeStatus)
2833{
2834 if (manufacturingModeStatus ==
2835 "xyz.openbmc_project.Control.Security.SpecialMode.Modes.Manufacturing")
2836 {
2837 return true;
2838 }
2839
2840#ifdef BMCWEB_ENABLE_VALIDATION_UNSECURE_FEATURE
2841 if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
2842 "SpecialMode.Modes.ValidationUnsecure")
2843 {
2844 return true;
2845 }
2846
2847#endif
2848
2849 return false;
2850}
2851
2852/**
2853 * @brief Entry point for Checking the manufacturing mode before doing sensor
2854 * override values of given sensor
2855 *
2856 * @param res response object
2857 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002858 * @param chassisSubNode Chassis Node for which the query has to happen
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002859 */
2860void checkAndDoSensorsOverride(
2861 std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002862 std::unordered_map<std::string, std::vector<nlohmann::json>>&
2863 allCollections)
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002864{
2865 BMCWEB_LOG_INFO << "checkAndDoSensorsOverride for subnode"
2866 << sensorAsyncResp->chassisSubNode << "\n";
2867
2868 const std::array<std::string, 1> interfaces = {
2869 "xyz.openbmc_project.Security.SpecialMode"};
2870
2871 crow::connections::systemBus->async_method_call(
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002872 [sensorAsyncResp, allCollections](const boost::system::error_code ec,
2873 const GetSubTreeType& resp) mutable {
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002874 if (ec)
2875 {
2876 BMCWEB_LOG_DEBUG
2877 << "Error in querying GetSubTree with Object Mapper. "
2878 << ec;
2879 messages::internalError(sensorAsyncResp->res);
2880 return;
2881 }
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002882#ifdef BMCWEB_INSECURE_UNRESTRICTED_SENSOR_OVERRIDE
2883 // Proceed with sensor override
2884 setSensorsOverride(sensorAsyncResp, allCollections);
2885 return;
2886#endif
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002887
2888 if (resp.size() != 1)
2889 {
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002890 BMCWEB_LOG_WARNING
2891 << "Overriding sensor value is not allowed - Internal "
2892 "error in querying SpecialMode property.";
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002893 messages::internalError(sensorAsyncResp->res);
2894 return;
2895 }
2896 const std::string& path = resp[0].first;
2897 const std::string& serviceName = resp[0].second.begin()->first;
2898
2899 if (path.empty() || serviceName.empty())
2900 {
2901 BMCWEB_LOG_DEBUG
2902 << "Path or service name is returned as empty. ";
2903 messages::internalError(sensorAsyncResp->res);
2904 return;
2905 }
2906
2907 // Sensor override is allowed only in manufacturing mode or
2908 // validation unsecure mode .
2909 crow::connections::systemBus->async_method_call(
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002910 [sensorAsyncResp, allCollections,
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002911 path](const boost::system::error_code ec,
2912 std::variant<std::string>& getManufactMode) mutable {
2913 if (ec)
2914 {
2915 BMCWEB_LOG_DEBUG
2916 << "Error in querying Special mode property " << ec;
2917 messages::internalError(sensorAsyncResp->res);
2918 return;
2919 }
2920
2921 const std::string* manufacturingModeStatus =
2922 std::get_if<std::string>(&getManufactMode);
2923
2924 if (nullptr == manufacturingModeStatus)
2925 {
2926 BMCWEB_LOG_DEBUG << "Sensor override mode is not "
2927 "Enabled. Returning ... ";
2928 messages::internalError(sensorAsyncResp->res);
2929 return;
2930 }
2931
2932 if (isOverridingAllowed(*manufacturingModeStatus))
2933 {
2934 BMCWEB_LOG_INFO << "Manufacturing mode is Enabled. "
2935 "Proceeding further... ";
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002936 setSensorsOverride(sensorAsyncResp, allCollections);
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002937 }
2938 else
2939 {
2940 BMCWEB_LOG_WARNING
2941 << "Manufacturing mode is not Enabled...can't "
2942 "Override the sensor value. ";
2943
2944 messages::actionNotSupported(
2945 sensorAsyncResp->res,
2946 "Overriding of Sensor Value for non "
2947 "manufacturing mode");
2948 return;
2949 }
2950 },
2951 serviceName, path, "org.freedesktop.DBus.Properties", "Get",
2952 "xyz.openbmc_project.Security.SpecialMode", "SpecialMode");
2953 },
2954
2955 "xyz.openbmc_project.ObjectMapper",
2956 "/xyz/openbmc_project/object_mapper",
2957 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 5, interfaces);
2958}
2959
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002960/**
2961 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2962 * path of the sensor.
2963 *
2964 * Function builds valid Redfish response for sensor query of given chassis and
2965 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2966 * it to caller in a callback.
2967 *
2968 * @param chassis Chassis for which retrieval should be performed
2969 * @param node Node (group) of sensors. See sensors::node for supported values
2970 * @param mapComplete Callback to be called with retrieval result
2971 */
2972void retrieveUriToDbusMap(const std::string& chassis, const std::string& node,
2973 SensorsAsyncResp::DataCompleteCb&& mapComplete)
2974{
2975 auto typesIt = sensors::dbus::types.find(node);
2976 if (typesIt == sensors::dbus::types.end())
2977 {
2978 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2979 mapComplete(boost::beast::http::status::bad_request, {});
2980 return;
2981 }
2982
2983 auto respBuffer = std::make_shared<crow::Response>();
2984 auto callback =
2985 [respBuffer, mapCompleteCb{std::move(mapComplete)}](
2986 const boost::beast::http::status status,
2987 const boost::container::flat_map<std::string, std::string>&
2988 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2989
2990 auto resp = std::make_shared<SensorsAsyncResp>(
2991 *respBuffer, chassis, typesIt->second, node, std::move(callback));
2992 getChassisData(resp);
2993}
2994
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002995class SensorCollection : public Node
2996{
2997 public:
2998 SensorCollection(CrowApp& app) :
Gunnar Millsf99c3792020-04-14 21:54:42 -05002999 Node(app, "/redfish/v1/Chassis/<str>/Sensors/", std::string())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003000 {
3001 entityPrivileges = {
3002 {boost::beast::http::verb::get, {{"Login"}}},
3003 {boost::beast::http::verb::head, {{"Login"}}},
3004 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3005 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3006 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3007 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3008 }
3009
3010 private:
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003011 void doGet(crow::Response& res, const crow::Request& req,
3012 const std::vector<std::string>& params) override
3013 {
3014 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
3015 if (params.size() != 1)
3016 {
3017 BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1";
3018 messages::internalError(res);
3019 res.end();
3020 return;
3021 }
3022
3023 const std::string& chassisId = params[0];
3024 std::shared_ptr<SensorsAsyncResp> asyncResp =
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02003025 std::make_shared<SensorsAsyncResp>(
3026 res, chassisId, sensors::dbus::types.at(sensors::node::sensors),
3027 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003028
3029 auto getChassisCb =
3030 [asyncResp](std::shared_ptr<boost::container::flat_set<std::string>>
3031 sensorNames) {
3032 BMCWEB_LOG_DEBUG << "getChassisCb enter";
3033
3034 nlohmann::json& entriesArray =
3035 asyncResp->res.jsonValue["Members"];
3036 for (auto& sensor : *sensorNames)
3037 {
3038 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
3039
3040 std::size_t lastPos = sensor.rfind("/");
3041 if (lastPos == std::string::npos ||
3042 lastPos + 1 >= sensor.size())
3043 {
3044 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
3045 messages::internalError(asyncResp->res);
3046 return;
3047 }
3048 std::string sensorName = sensor.substr(lastPos + 1);
3049 entriesArray.push_back(
3050 {{"@odata.id",
3051 "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
3052 asyncResp->chassisSubNode + "/" + sensorName}});
3053 }
3054
3055 asyncResp->res.jsonValue["Members@odata.count"] =
3056 entriesArray.size();
3057 BMCWEB_LOG_DEBUG << "getChassisCb exit";
3058 };
3059
3060 // Get set of sensors in chassis
3061 getChassis(asyncResp, std::move(getChassisCb));
3062 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
3063 }
3064};
3065
3066class Sensor : public Node
3067{
3068 public:
3069 Sensor(CrowApp& app) :
3070 Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(),
3071 std::string())
3072 {
3073 entityPrivileges = {
3074 {boost::beast::http::verb::get, {{"Login"}}},
3075 {boost::beast::http::verb::head, {{"Login"}}},
3076 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3077 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3078 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3079 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3080 }
3081
3082 private:
3083 void doGet(crow::Response& res, const crow::Request& req,
3084 const std::vector<std::string>& params) override
3085 {
3086 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3087 if (params.size() != 2)
3088 {
3089 BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2";
3090 messages::internalError(res);
3091 res.end();
3092 return;
3093 }
3094 const std::string& chassisId = params[0];
3095 std::shared_ptr<SensorsAsyncResp> asyncResp =
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02003096 std::make_shared<SensorsAsyncResp>(res, chassisId,
3097 std::vector<const char*>(),
3098 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003099
3100 const std::string& sensorName = params[1];
3101 const std::array<const char*, 1> interfaces = {
3102 "xyz.openbmc_project.Sensor.Value"};
3103
3104 // Get a list of all of the sensors that implement Sensor.Value
3105 // and get the path and service name associated with the sensor
3106 crow::connections::systemBus->async_method_call(
3107 [asyncResp, sensorName](const boost::system::error_code ec,
3108 const GetSubTreeType& subtree) {
3109 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3110 if (ec)
3111 {
3112 messages::internalError(asyncResp->res);
3113 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3114 << "Dbus error " << ec;
3115 return;
3116 }
3117
3118 GetSubTreeType::const_iterator it = std::find_if(
3119 subtree.begin(), subtree.end(),
3120 [sensorName](
3121 const std::pair<
3122 std::string,
3123 std::vector<std::pair<std::string,
3124 std::vector<std::string>>>>&
3125 object) {
3126 std::string_view sensor = object.first;
3127 std::size_t lastPos = sensor.rfind("/");
3128 if (lastPos == std::string::npos ||
3129 lastPos + 1 >= sensor.size())
3130 {
3131 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3132 << sensor;
3133 return false;
3134 }
3135 std::string_view name = sensor.substr(lastPos + 1);
3136
3137 return name == sensorName;
3138 });
3139
3140 if (it == subtree.end())
3141 {
3142 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3143 << sensorName;
3144 messages::resourceNotFound(asyncResp->res, "Sensor",
3145 sensorName);
3146 return;
3147 }
3148 std::string_view sensorPath = (*it).first;
3149 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3150 << sensorName << "': " << sensorPath;
3151
3152 const std::shared_ptr<boost::container::flat_set<std::string>>
3153 sensorList = std::make_shared<
3154 boost::container::flat_set<std::string>>();
3155
3156 sensorList->emplace(sensorPath);
3157 processSensorList(asyncResp, sensorList);
3158 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3159 },
3160 "xyz.openbmc_project.ObjectMapper",
3161 "/xyz/openbmc_project/object_mapper",
3162 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3163 "/xyz/openbmc_project/sensors", 2, interfaces);
3164 }
3165};
3166
Ed Tanous1abe55e2018-09-05 08:30:59 -07003167} // namespace redfish