blob: 99a03c9bf677881c849981b7bd8c52ee503f6b49 [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
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001211 size_t minNumNeeded =
1212 collection->size() > 0
1213 ? collection->size() - *allowedFailures
1214 : 0;
Ed Tanous271584a2019-07-09 16:24:22 -07001215 nlohmann::json& jResp =
1216 sensorsAsyncResp->res
1217 .jsonValue["Redundancy"];
1218 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001219 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301220 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001221 sensorsAsyncResp->chassisId + "/" +
1222 sensorsAsyncResp->chassisSubNode +
1223 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001224 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001225 {"@odata.type",
1226 "#Redundancy.v1_3_2.Redundancy"},
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001227 {"MinNumNeeded", minNumNeeded},
James Feist8bd25cc2019-03-15 15:14:00 -07001228 {"MemberId", name},
1229 {"Mode", "N+m"},
1230 {"Name", name},
1231 {"RedundancySet", redfishCollection},
1232 {"Status",
1233 {{"Health", health},
1234 {"State", "Enabled"}}}});
1235 },
1236 owner, path, "org.freedesktop.DBus.Properties",
1237 "GetAll",
1238 "xyz.openbmc_project.Control.FanRedundancy");
1239 },
James Feist02e92e32019-06-26 12:07:05 -07001240 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
James Feist8bd25cc2019-03-15 15:14:00 -07001241 "org.freedesktop.DBus.Properties", "Get",
1242 "xyz.openbmc_project.Association", "endpoints");
1243 }
1244 },
1245 "xyz.openbmc_project.ObjectMapper",
1246 "/xyz/openbmc_project/object_mapper",
1247 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1248 "/xyz/openbmc_project/control", 2,
1249 std::array<const char*, 1>{
1250 "xyz.openbmc_project.Control.FanRedundancy"});
1251}
1252
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001253void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
1254{
1255 nlohmann::json& response = SensorsAsyncResp->res.jsonValue;
1256 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001257 if (SensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001258 {
1259 sensorHeaders = {"Voltages", "PowerSupplies"};
1260 }
1261 for (const std::string& sensorGroup : sensorHeaders)
1262 {
1263 nlohmann::json::iterator entry = response.find(sensorGroup);
1264 if (entry != response.end())
1265 {
1266 std::sort(entry->begin(), entry->end(),
1267 [](nlohmann::json& c1, nlohmann::json& c2) {
1268 return c1["Name"] < c2["Name"];
1269 });
1270
1271 // add the index counts to the end of each entry
1272 size_t count = 0;
1273 for (nlohmann::json& sensorJson : *entry)
1274 {
1275 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1276 if (odata == sensorJson.end())
1277 {
1278 continue;
1279 }
1280 std::string* value = odata->get_ptr<std::string*>();
1281 if (value != nullptr)
1282 {
1283 *value += std::to_string(count);
1284 count++;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001285 SensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001286 }
1287 }
1288 }
1289 }
1290}
1291
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001292/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001293 * @brief Finds the inventory item with the specified object path.
1294 * @param inventoryItems D-Bus inventory items associated with sensors.
1295 * @param invItemObjPath D-Bus object path of inventory item.
1296 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001297 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001298static InventoryItem* findInventoryItem(
1299 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1300 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001301{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001302 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001303 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001304 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001305 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001306 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001307 }
1308 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001309 return nullptr;
1310}
1311
1312/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001313 * @brief Finds the inventory item associated with the specified sensor.
1314 * @param inventoryItems D-Bus inventory items associated with sensors.
1315 * @param sensorObjPath D-Bus object path of sensor.
1316 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001317 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001318static InventoryItem* findInventoryItemForSensor(
1319 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1320 const std::string& sensorObjPath)
1321{
1322 for (InventoryItem& inventoryItem : *inventoryItems)
1323 {
1324 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1325 {
1326 return &inventoryItem;
1327 }
1328 }
1329 return nullptr;
1330}
1331
1332/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001333 * @brief Finds the inventory item associated with the specified led path.
1334 * @param inventoryItems D-Bus inventory items associated with sensors.
1335 * @param ledObjPath D-Bus object path of led.
1336 * @return Inventory item within vector, or nullptr if no match found.
1337 */
1338inline InventoryItem*
1339 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1340 const std::string& ledObjPath)
1341{
1342 for (InventoryItem& inventoryItem : inventoryItems)
1343 {
1344 if (inventoryItem.ledObjectPath == ledObjPath)
1345 {
1346 return &inventoryItem;
1347 }
1348 }
1349 return nullptr;
1350}
1351
1352/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001353 * @brief Adds inventory item and associated sensor to specified vector.
1354 *
1355 * Adds a new InventoryItem to the vector if necessary. Searches for an
1356 * existing InventoryItem with the specified object path. If not found, one is
1357 * added to the vector.
1358 *
1359 * Next, the specified sensor is added to the set of sensors associated with the
1360 * InventoryItem.
1361 *
1362 * @param inventoryItems D-Bus inventory items associated with sensors.
1363 * @param invItemObjPath D-Bus object path of inventory item.
1364 * @param sensorObjPath D-Bus object path of sensor
1365 */
1366static void
1367 addInventoryItem(std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1368 const std::string& invItemObjPath,
1369 const std::string& sensorObjPath)
1370{
1371 // Look for inventory item in vector
1372 InventoryItem* inventoryItem =
1373 findInventoryItem(inventoryItems, invItemObjPath);
1374
1375 // If inventory item doesn't exist in vector, add it
1376 if (inventoryItem == nullptr)
1377 {
1378 inventoryItems->emplace_back(invItemObjPath);
1379 inventoryItem = &(inventoryItems->back());
1380 }
1381
1382 // Add sensor to set of sensors associated with inventory item
1383 inventoryItem->sensors.emplace(sensorObjPath);
1384}
1385
1386/**
1387 * @brief Stores D-Bus data in the specified inventory item.
1388 *
1389 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1390 * specified InventoryItem.
1391 *
1392 * This data is later used to provide sensor property values in the JSON
1393 * response.
1394 *
1395 * @param inventoryItem Inventory item where data will be stored.
1396 * @param interfacesDict Map containing D-Bus interfaces and their properties
1397 * for the specified inventory item.
1398 */
1399static void storeInventoryItemData(
1400 InventoryItem& inventoryItem,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001401 const boost::container::flat_map<
1402 std::string, boost::container::flat_map<std::string, SensorVariant>>&
1403 interfacesDict)
1404{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001405 // Get properties from Inventory.Item interface
1406 auto interfaceIt =
1407 interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1408 if (interfaceIt != interfacesDict.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001409 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001410 auto propertyIt = interfaceIt->second.find("Present");
1411 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001412 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001413 const bool* value = std::get_if<bool>(&propertyIt->second);
1414 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001415 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001416 inventoryItem.isPresent = *value;
1417 }
1418 }
1419 }
1420
1421 // Check if Inventory.Item.PowerSupply interface is present
1422 interfaceIt =
1423 interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1424 if (interfaceIt != interfacesDict.end())
1425 {
1426 inventoryItem.isPowerSupply = true;
1427 }
1428
1429 // Get properties from Inventory.Decorator.Asset interface
1430 interfaceIt =
1431 interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1432 if (interfaceIt != interfacesDict.end())
1433 {
1434 auto propertyIt = interfaceIt->second.find("Manufacturer");
1435 if (propertyIt != interfaceIt->second.end())
1436 {
1437 const std::string* value =
1438 std::get_if<std::string>(&propertyIt->second);
1439 if (value != nullptr)
1440 {
1441 inventoryItem.manufacturer = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001442 }
1443 }
1444
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001445 propertyIt = interfaceIt->second.find("Model");
1446 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001447 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001448 const std::string* value =
1449 std::get_if<std::string>(&propertyIt->second);
1450 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001451 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001452 inventoryItem.model = *value;
1453 }
1454 }
1455
1456 propertyIt = interfaceIt->second.find("PartNumber");
1457 if (propertyIt != interfaceIt->second.end())
1458 {
1459 const std::string* value =
1460 std::get_if<std::string>(&propertyIt->second);
1461 if (value != nullptr)
1462 {
1463 inventoryItem.partNumber = *value;
1464 }
1465 }
1466
1467 propertyIt = interfaceIt->second.find("SerialNumber");
1468 if (propertyIt != interfaceIt->second.end())
1469 {
1470 const std::string* value =
1471 std::get_if<std::string>(&propertyIt->second);
1472 if (value != nullptr)
1473 {
1474 inventoryItem.serialNumber = *value;
1475 }
1476 }
1477 }
1478
1479 // Get properties from State.Decorator.OperationalStatus interface
1480 interfaceIt = interfacesDict.find(
1481 "xyz.openbmc_project.State.Decorator.OperationalStatus");
1482 if (interfaceIt != interfacesDict.end())
1483 {
1484 auto propertyIt = interfaceIt->second.find("Functional");
1485 if (propertyIt != interfaceIt->second.end())
1486 {
1487 const bool* value = std::get_if<bool>(&propertyIt->second);
1488 if (value != nullptr)
1489 {
1490 inventoryItem.isFunctional = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001491 }
1492 }
1493 }
1494}
1495
1496/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001497 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001499 * Uses the specified connections (services) to obtain D-Bus data for inventory
1500 * items associated with sensors. Stores the resulting data in the
1501 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001502 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001503 * This data is later used to provide sensor property values in the JSON
1504 * response.
1505 *
1506 * Finds the inventory item data asynchronously. Invokes callback when data has
1507 * been obtained.
1508 *
1509 * The callback must have the following signature:
1510 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001511 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001512 * @endcode
1513 *
1514 * This function is called recursively, obtaining data asynchronously from one
1515 * connection in each call. This ensures the callback is not invoked until the
1516 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001517 *
1518 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001519 * @param inventoryItems D-Bus inventory items associated with sensors.
1520 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001521 * @param objectMgrPaths Mappings from connection name to DBus object path that
1522 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001523 * @param callback Callback to invoke when inventory data has been obtained.
1524 * @param invConnectionsIndex Current index in invConnections. Only specified
1525 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001526 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001527template <typename Callback>
1528static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001529 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001530 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001531 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1532 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001533 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001534 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001535{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001536 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001537
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001538 // If no more connections left, call callback
1539 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001540 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001541 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001542 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1543 return;
1544 }
1545
1546 // Get inventory item data from current connection
1547 auto it = invConnections->nth(invConnectionsIndex);
1548 if (it != invConnections->end())
1549 {
1550 const std::string& invConnection = *it;
1551
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001552 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001553 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1554 objectMgrPaths, callback{std::move(callback)},
1555 invConnectionsIndex](
1556 const boost::system::error_code ec,
1557 ManagedObjectsVectorType& resp) {
1558 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 if (ec)
1560 {
1561 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001562 << "getInventoryItemsData respHandler DBus error " << ec;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001563 messages::internalError(sensorsAsyncResp->res);
1564 return;
1565 }
1566
1567 // Loop through returned object paths
1568 for (const auto& objDictEntry : resp)
1569 {
1570 const std::string& objPath =
1571 static_cast<const std::string&>(objDictEntry.first);
1572
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001573 // If this object path is one of the specified inventory items
1574 InventoryItem* inventoryItem =
1575 findInventoryItem(inventoryItems, objPath);
1576 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001577 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001578 // Store inventory data in InventoryItem
1579 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001580 }
1581 }
1582
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001583 // Recurse to get inventory item data from next connection
1584 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1585 invConnections, objectMgrPaths,
1586 std::move(callback), invConnectionsIndex + 1);
1587
1588 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001589 };
1590
1591 // Find DBus object path that implements ObjectManager for the current
1592 // connection. If no mapping found, default to "/".
1593 auto iter = objectMgrPaths->find(invConnection);
1594 const std::string& objectMgrPath =
1595 (iter != objectMgrPaths->end()) ? iter->second : "/";
1596 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1597 << objectMgrPath;
1598
1599 // Get all object paths and their interfaces for current connection
1600 crow::connections::systemBus->async_method_call(
1601 std::move(respHandler), invConnection, objectMgrPath,
1602 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1603 }
1604
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001605 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001606}
1607
1608/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001609 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001610 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001611 * Gets the D-Bus connections (services) that provide data for the inventory
1612 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 *
1614 * Finds the connections asynchronously. Invokes callback when information has
1615 * been obtained.
1616 *
1617 * The callback must have the following signature:
1618 * @code
1619 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1620 * invConnections)
1621 * @endcode
1622 *
1623 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001624 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 * @param callback Callback to invoke when connections have been obtained.
1626 */
1627template <typename Callback>
1628static void getInventoryItemsConnections(
1629 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001630 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631 Callback&& callback)
1632{
1633 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1634
1635 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001636 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001637 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001638 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1639 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001640 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1641
1642 // Response handler for parsing output from GetSubTree
1643 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001644 inventoryItems](const boost::system::error_code ec,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001645 const GetSubTreeType& subtree) {
1646 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1647 if (ec)
1648 {
1649 messages::internalError(sensorsAsyncResp->res);
1650 BMCWEB_LOG_ERROR
1651 << "getInventoryItemsConnections respHandler DBus error " << ec;
1652 return;
1653 }
1654
1655 // Make unique list of connections for desired inventory items
1656 std::shared_ptr<boost::container::flat_set<std::string>>
1657 invConnections =
1658 std::make_shared<boost::container::flat_set<std::string>>();
1659 invConnections->reserve(8);
1660
1661 // Loop through objects from GetSubTree
1662 for (const std::pair<
1663 std::string,
1664 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1665 object : subtree)
1666 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001667 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001668 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001669 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001670 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001671 // Store all connections to inventory item
1672 for (const std::pair<std::string, std::vector<std::string>>&
1673 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001674 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001675 const std::string& invConnection = objData.first;
1676 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001677 }
1678 }
1679 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001680
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001681 callback(invConnections);
1682 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1683 };
1684
1685 // Make call to ObjectMapper to find all inventory items
1686 crow::connections::systemBus->async_method_call(
1687 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1688 "/xyz/openbmc_project/object_mapper",
1689 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1690 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1691}
1692
1693/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001694 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001695 *
1696 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001697 * inventory items. Then finds the associations from those inventory items to
1698 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001699 *
1700 * Finds the inventory items asynchronously. Invokes callback when information
1701 * has been obtained.
1702 *
1703 * The callback must have the following signature:
1704 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001705 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001706 * @endcode
1707 *
1708 * @param sensorsAsyncResp Pointer to object holding response data.
1709 * @param sensorNames All sensors within the current chassis.
1710 * @param objectMgrPaths Mappings from connection name to DBus object path that
1711 * implements ObjectManager.
1712 * @param callback Callback to invoke when inventory items have been obtained.
1713 */
1714template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001715static void getInventoryItemAssociations(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001716 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1717 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
1718 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1719 objectMgrPaths,
1720 Callback&& callback)
1721{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001722 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001723
1724 // Response handler for GetManagedObjects
1725 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1726 sensorNames](const boost::system::error_code ec,
1727 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001728 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001729 if (ec)
1730 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001731 BMCWEB_LOG_ERROR
1732 << "getInventoryItemAssociations respHandler DBus error " << ec;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001733 messages::internalError(sensorsAsyncResp->res);
1734 return;
1735 }
1736
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001737 // Create vector to hold list of inventory items
1738 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1739 std::make_shared<std::vector<InventoryItem>>();
1740
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001741 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001742 std::string sensorAssocPath;
1743 sensorAssocPath.reserve(128); // avoid memory allocations
1744 for (const auto& objDictEntry : resp)
1745 {
1746 const std::string& objPath =
1747 static_cast<const std::string&>(objDictEntry.first);
1748 const boost::container::flat_map<
1749 std::string, boost::container::flat_map<
1750 std::string, dbus::utility::DbusVariantType>>&
1751 interfacesDict = objDictEntry.second;
1752
1753 // If path is inventory association for one of the specified sensors
1754 for (const std::string& sensorName : *sensorNames)
1755 {
1756 sensorAssocPath = sensorName;
1757 sensorAssocPath += "/inventory";
1758 if (objPath == sensorAssocPath)
1759 {
1760 // Get Association interface for object path
1761 auto assocIt =
1762 interfacesDict.find("xyz.openbmc_project.Association");
1763 if (assocIt != interfacesDict.end())
1764 {
1765 // Get inventory item from end point
1766 auto endpointsIt = assocIt->second.find("endpoints");
1767 if (endpointsIt != assocIt->second.end())
1768 {
1769 const std::vector<std::string>* endpoints =
1770 std::get_if<std::vector<std::string>>(
1771 &endpointsIt->second);
1772 if ((endpoints != nullptr) && !endpoints->empty())
1773 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001774 // Add inventory item to vector
1775 const std::string& invItemPath =
1776 endpoints->front();
1777 addInventoryItem(inventoryItems, invItemPath,
1778 sensorName);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001779 }
1780 }
1781 }
1782 break;
1783 }
1784 }
1785 }
1786
Anthony Wilsond5005492019-07-31 16:34:17 -05001787 // Now loop through the returned object paths again, this time to
1788 // find the leds associated with the inventory items we just found
1789 std::string inventoryAssocPath;
1790 inventoryAssocPath.reserve(128); // avoid memory allocations
1791 for (const auto& objDictEntry : resp)
1792 {
1793 const std::string& objPath =
1794 static_cast<const std::string&>(objDictEntry.first);
1795 const boost::container::flat_map<
1796 std::string, boost::container::flat_map<
1797 std::string, dbus::utility::DbusVariantType>>&
1798 interfacesDict = objDictEntry.second;
1799
1800 for (InventoryItem& inventoryItem : *inventoryItems)
1801 {
1802 inventoryAssocPath = inventoryItem.objectPath;
1803 inventoryAssocPath += "/leds";
1804 if (objPath == inventoryAssocPath)
1805 {
1806 // Get Association interface for object path
1807 auto assocIt =
1808 interfacesDict.find("xyz.openbmc_project.Association");
1809 if (assocIt != interfacesDict.end())
1810 {
1811 // Get inventory item from end point
1812 auto endpointsIt = assocIt->second.find("endpoints");
1813 if (endpointsIt != assocIt->second.end())
1814 {
1815 const std::vector<std::string>* endpoints =
1816 std::get_if<std::vector<std::string>>(
1817 &endpointsIt->second);
1818 if ((endpoints != nullptr) && !endpoints->empty())
1819 {
1820 // Store LED path in inventory item
1821 const std::string& ledPath = endpoints->front();
1822 inventoryItem.ledObjectPath = ledPath;
1823 }
1824 }
1825 }
1826 break;
1827 }
1828 }
1829 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001830 callback(inventoryItems);
1831 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001832 };
1833
1834 // Find DBus object path that implements ObjectManager for ObjectMapper
1835 std::string connection = "xyz.openbmc_project.ObjectMapper";
1836 auto iter = objectMgrPaths->find(connection);
1837 const std::string& objectMgrPath =
1838 (iter != objectMgrPaths->end()) ? iter->second : "/";
1839 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1840 << objectMgrPath;
1841
1842 // Call GetManagedObjects on the ObjectMapper to get all associations
1843 crow::connections::systemBus->async_method_call(
1844 std::move(respHandler), connection, objectMgrPath,
1845 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1846
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001847 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001848}
1849
1850/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001851 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1852 *
1853 * Uses the specified connections (services) to obtain D-Bus data for inventory
1854 * item leds associated with sensors. Stores the resulting data in the
1855 * inventoryItems vector.
1856 *
1857 * This data is later used to provide sensor property values in the JSON
1858 * response.
1859 *
1860 * Finds the inventory item led data asynchronously. Invokes callback when data
1861 * has been obtained.
1862 *
1863 * The callback must have the following signature:
1864 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001865 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001866 * @endcode
1867 *
1868 * This function is called recursively, obtaining data asynchronously from one
1869 * connection in each call. This ensures the callback is not invoked until the
1870 * last asynchronous function has completed.
1871 *
1872 * @param sensorsAsyncResp Pointer to object holding response data.
1873 * @param inventoryItems D-Bus inventory items associated with sensors.
1874 * @param ledConnections Connections that provide data for the inventory leds.
1875 * @param callback Callback to invoke when inventory data has been obtained.
1876 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1877 * in recursive calls to this function.
1878 */
1879template <typename Callback>
1880void getInventoryLedData(
1881 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1882 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1883 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1884 ledConnections,
1885 Callback&& callback, size_t ledConnectionsIndex = 0)
1886{
1887 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1888
1889 // If no more connections left, call callback
1890 if (ledConnectionsIndex >= ledConnections->size())
1891 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001892 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001893 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1894 return;
1895 }
1896
1897 // Get inventory item data from current connection
1898 auto it = ledConnections->nth(ledConnectionsIndex);
1899 if (it != ledConnections->end())
1900 {
1901 const std::string& ledPath = (*it).first;
1902 const std::string& ledConnection = (*it).second;
1903 // Response handler for Get State property
1904 auto respHandler =
1905 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1906 callback{std::move(callback)},
1907 ledConnectionsIndex](const boost::system::error_code ec,
1908 const std::variant<std::string>& ledState) {
1909 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1910 if (ec)
1911 {
1912 BMCWEB_LOG_ERROR
1913 << "getInventoryLedData respHandler DBus error " << ec;
1914 messages::internalError(sensorsAsyncResp->res);
1915 return;
1916 }
1917
1918 const std::string* state = std::get_if<std::string>(&ledState);
1919 if (state != nullptr)
1920 {
1921 BMCWEB_LOG_DEBUG << "Led state: " << *state;
1922 // Find inventory item with this LED object path
1923 InventoryItem* inventoryItem =
1924 findInventoryItemForLed(*inventoryItems, ledPath);
1925 if (inventoryItem != nullptr)
1926 {
1927 // Store LED state in InventoryItem
1928 if (boost::ends_with(*state, "On"))
1929 {
1930 inventoryItem->ledState = LedState::ON;
1931 }
1932 else if (boost::ends_with(*state, "Blink"))
1933 {
1934 inventoryItem->ledState = LedState::BLINK;
1935 }
1936 else if (boost::ends_with(*state, "Off"))
1937 {
1938 inventoryItem->ledState = LedState::OFF;
1939 }
1940 else
1941 {
1942 inventoryItem->ledState = LedState::UNKNOWN;
1943 }
1944 }
1945 }
1946 else
1947 {
1948 BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
1949 << ledPath;
1950 }
1951
1952 // Recurse to get LED data from next connection
1953 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1954 ledConnections, std::move(callback),
1955 ledConnectionsIndex + 1);
1956
1957 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1958 };
1959
1960 // Get the State property for the current LED
1961 crow::connections::systemBus->async_method_call(
1962 std::move(respHandler), ledConnection, ledPath,
1963 "org.freedesktop.DBus.Properties", "Get",
1964 "xyz.openbmc_project.Led.Physical", "State");
1965 }
1966
1967 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1968}
1969
1970/**
1971 * @brief Gets LED data for LEDs associated with given inventory items.
1972 *
1973 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1974 * associated with the specified inventory items. Then gets the LED data from
1975 * each connection and stores it in the inventory item.
1976 *
1977 * This data is later used to provide sensor property values in the JSON
1978 * response.
1979 *
1980 * Finds the LED data asynchronously. Invokes callback when information has
1981 * been obtained.
1982 *
1983 * The callback must have the following signature:
1984 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001985 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001986 * @endcode
1987 *
1988 * @param sensorsAsyncResp Pointer to object holding response data.
1989 * @param inventoryItems D-Bus inventory items associated with sensors.
1990 * @param callback Callback to invoke when inventory items have been obtained.
1991 */
1992template <typename Callback>
1993void getInventoryLeds(
1994 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1995 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1996 Callback&& callback)
1997{
1998 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1999
2000 const std::string path = "/xyz/openbmc_project";
2001 const std::array<std::string, 1> interfaces = {
2002 "xyz.openbmc_project.Led.Physical"};
2003
2004 // Response handler for parsing output from GetSubTree
2005 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2006 inventoryItems](const boost::system::error_code ec,
2007 const GetSubTreeType& subtree) {
2008 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2009 if (ec)
2010 {
2011 messages::internalError(sensorsAsyncResp->res);
2012 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2013 << ec;
2014 return;
2015 }
2016
2017 // Build map of LED object paths to connections
2018 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2019 ledConnections = std::make_shared<
2020 boost::container::flat_map<std::string, std::string>>();
2021
2022 // Loop through objects from GetSubTree
2023 for (const std::pair<
2024 std::string,
2025 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2026 object : subtree)
2027 {
2028 // Check if object path is LED for one of the specified inventory
2029 // items
2030 const std::string& ledPath = object.first;
2031 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2032 {
2033 // Add mapping from ledPath to connection
2034 const std::string& connection = object.second.begin()->first;
2035 (*ledConnections)[ledPath] = connection;
2036 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2037 << connection;
2038 }
2039 }
2040
2041 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2042 std::move(callback));
2043 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2044 };
2045 // Make call to ObjectMapper to find all inventory items
2046 crow::connections::systemBus->async_method_call(
2047 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2048 "/xyz/openbmc_project/object_mapper",
2049 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2050 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2051}
2052
2053/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002054 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2055 *
2056 * Uses the specified connections (services) (currently assumes just one) to
2057 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2058 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2059 *
2060 * This data is later used to provide sensor property values in the JSON
2061 * response.
2062 *
2063 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2064 * when data has been obtained.
2065 *
2066 * The callback must have the following signature:
2067 * @code
2068 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2069 * @endcode
2070 *
2071 * @param sensorsAsyncResp Pointer to object holding response data.
2072 * @param inventoryItems D-Bus inventory items associated with sensors.
2073 * @param psAttributesConnections Connections that provide data for the Power
2074 * Supply Attributes
2075 * @param callback Callback to invoke when data has been obtained.
2076 */
2077template <typename Callback>
2078void getPowerSupplyAttributesData(
2079 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2080 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2081 const boost::container::flat_map<std::string, std::string>&
2082 psAttributesConnections,
2083 Callback&& callback)
2084{
2085 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2086
2087 if (psAttributesConnections.empty())
2088 {
2089 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2090 callback(inventoryItems);
2091 return;
2092 }
2093
2094 // Assuming just one connection (service) for now
2095 auto it = psAttributesConnections.nth(0);
2096
2097 const std::string& psAttributesPath = (*it).first;
2098 const std::string& psAttributesConnection = (*it).second;
2099
2100 // Response handler for Get DeratingFactor property
2101 auto respHandler = [sensorsAsyncResp, inventoryItems,
2102 callback{std::move(callback)}](
2103 const boost::system::error_code ec,
2104 const std::variant<uint32_t>& deratingFactor) {
2105 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2106 if (ec)
2107 {
2108 BMCWEB_LOG_ERROR
2109 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
2110 messages::internalError(sensorsAsyncResp->res);
2111 return;
2112 }
2113
2114 const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
2115 if (value != nullptr)
2116 {
2117 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
2118 // Store value in Power Supply Inventory Items
2119 for (InventoryItem& inventoryItem : *inventoryItems)
2120 {
2121 if (inventoryItem.isPowerSupply == true)
2122 {
2123 inventoryItem.powerSupplyEfficiencyPercent =
2124 static_cast<int>(*value);
2125 }
2126 }
2127 }
2128 else
2129 {
2130 BMCWEB_LOG_DEBUG
2131 << "Failed to find EfficiencyPercent value for PowerSupplies";
2132 }
2133
2134 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2135 callback(inventoryItems);
2136 };
2137
2138 // Get the DeratingFactor property for the PowerSupplyAttributes
2139 // Currently only property on the interface/only one we care about
2140 crow::connections::systemBus->async_method_call(
2141 std::move(respHandler), psAttributesConnection, psAttributesPath,
2142 "org.freedesktop.DBus.Properties", "Get",
2143 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
2144
2145 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2146}
2147
2148/**
2149 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2150 *
2151 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2152 * data. Then gets the Power Supply Attributes data from the connection
2153 * (currently just assumes 1 connection) and stores the data in the inventory
2154 * item.
2155 *
2156 * This data is later used to provide sensor property values in the JSON
2157 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2158 *
2159 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2160 * when information has been obtained.
2161 *
2162 * The callback must have the following signature:
2163 * @code
2164 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2165 * @endcode
2166 *
2167 * @param sensorsAsyncResp Pointer to object holding response data.
2168 * @param inventoryItems D-Bus inventory items associated with sensors.
2169 * @param callback Callback to invoke when data has been obtained.
2170 */
2171template <typename Callback>
2172void getPowerSupplyAttributes(
2173 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2174 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2175 Callback&& callback)
2176{
2177 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2178
2179 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002180 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002181 {
2182 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2183 callback(inventoryItems);
2184 return;
2185 }
2186
2187 const std::array<std::string, 1> interfaces = {
2188 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2189
2190 // Response handler for parsing output from GetSubTree
2191 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2192 inventoryItems](const boost::system::error_code ec,
2193 const GetSubTreeType& subtree) {
2194 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2195 if (ec)
2196 {
2197 messages::internalError(sensorsAsyncResp->res);
2198 BMCWEB_LOG_ERROR
2199 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2200 return;
2201 }
2202 if (subtree.size() == 0)
2203 {
2204 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2205 callback(inventoryItems);
2206 return;
2207 }
2208
2209 // Currently we only support 1 power supply attribute, use this for
2210 // all the power supplies. Build map of object path to connection.
2211 // Assume just 1 connection and 1 path for now.
2212 boost::container::flat_map<std::string, std::string>
2213 psAttributesConnections;
2214
2215 if (subtree[0].first.empty() || subtree[0].second.empty())
2216 {
2217 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2218 callback(inventoryItems);
2219 return;
2220 }
2221
2222 const std::string& psAttributesPath = subtree[0].first;
2223 const std::string& connection = subtree[0].second.begin()->first;
2224
2225 if (connection.empty())
2226 {
2227 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2228 callback(inventoryItems);
2229 return;
2230 }
2231
2232 psAttributesConnections[psAttributesPath] = connection;
2233 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2234 << connection;
2235
2236 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2237 psAttributesConnections,
2238 std::move(callback));
2239 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2240 };
2241 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2242 crow::connections::systemBus->async_method_call(
2243 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2244 "/xyz/openbmc_project/object_mapper",
2245 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2246 "/xyz/openbmc_project", 0, interfaces);
2247 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2248}
2249
2250/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002251 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002252 *
2253 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002254 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002255 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 * This data is later used to provide sensor property values in the JSON
2257 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002258 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002259 * Finds the inventory items asynchronously. Invokes callback when the
2260 * inventory items have been obtained.
2261 *
2262 * The callback must have the following signature:
2263 * @code
2264 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2265 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002266 *
2267 * @param sensorsAsyncResp Pointer to object holding response data.
2268 * @param sensorNames All sensors within the current chassis.
2269 * @param objectMgrPaths Mappings from connection name to DBus object path that
2270 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002271 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002272 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002273template <typename Callback>
2274static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002275 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2276 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2277 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002278 objectMgrPaths,
2279 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002280{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002281 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2282 auto getInventoryItemAssociationsCb =
2283 [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2284 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2285 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002286 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002287 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2288 callback{std::move(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002289 std::shared_ptr<boost::container::flat_set<std::string>>
2290 invConnections) {
2291 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002292 auto getInventoryItemsDataCb =
2293 [sensorsAsyncResp, inventoryItems,
2294 callback{std::move(callback)}]() {
2295 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002296
2297 auto getInventoryLedsCb = [sensorsAsyncResp,
2298 inventoryItems,
2299 callback{std::move(
2300 callback)}]() {
2301 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2302 // Find Power Supply Attributes and get the data
2303 getPowerSupplyAttributes(sensorsAsyncResp,
2304 inventoryItems,
2305 std::move(callback));
2306 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2307 };
2308
Anthony Wilsond5005492019-07-31 16:34:17 -05002309 // Find led connections and get the data
2310 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002311 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002312 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2313 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002314
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002315 // Get inventory item data from connections
2316 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2317 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002318 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002319 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2320 };
2321
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002322 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002323 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002324 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002325 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002326 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002327 };
2328
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002329 // Get associations from sensors to inventory items
2330 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2331 std::move(getInventoryItemAssociationsCb));
2332 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2333}
2334
2335/**
2336 * @brief Returns JSON PowerSupply object for the specified inventory item.
2337 *
2338 * Searches for a JSON PowerSupply object that matches the specified inventory
2339 * item. If one is not found, a new PowerSupply object is added to the JSON
2340 * array.
2341 *
2342 * Multiple sensors are often associated with one power supply inventory item.
2343 * As a result, multiple sensor values are stored in one JSON PowerSupply
2344 * object.
2345 *
2346 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2347 * @param inventoryItem Inventory item for the power supply.
2348 * @param chassisId Chassis that contains the power supply.
2349 * @return JSON PowerSupply object for the specified inventory item.
2350 */
2351static nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2352 const InventoryItem& inventoryItem,
2353 const std::string& chassisId)
2354{
2355 // Check if matching PowerSupply object already exists in JSON array
2356 for (nlohmann::json& powerSupply : powerSupplyArray)
2357 {
2358 if (powerSupply["MemberId"] == inventoryItem.name)
2359 {
2360 return powerSupply;
2361 }
2362 }
2363
2364 // Add new PowerSupply object to JSON array
2365 powerSupplyArray.push_back({});
2366 nlohmann::json& powerSupply = powerSupplyArray.back();
2367 powerSupply["@odata.id"] =
2368 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2369 powerSupply["MemberId"] = inventoryItem.name;
2370 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2371 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2372 powerSupply["Model"] = inventoryItem.model;
2373 powerSupply["PartNumber"] = inventoryItem.partNumber;
2374 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002375 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002376
Gunnar Mills42cbe532019-08-15 15:26:54 -05002377 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2378 {
2379 powerSupply["EfficiencyPercent"] =
2380 inventoryItem.powerSupplyEfficiencyPercent;
2381 }
2382
2383 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002384 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2385 powerSupply["Status"]["Health"] = health;
2386
2387 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002388}
2389
2390/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002391 * @brief Gets the values of the specified sensors.
2392 *
2393 * Stores the results as JSON in the SensorsAsyncResp.
2394 *
2395 * Gets the sensor values asynchronously. Stores the results later when the
2396 * information has been obtained.
2397 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002398 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002399 *
2400 * To minimize the number of DBus calls, the DBus method
2401 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2402 * values of all sensors provided by a connection (service).
2403 *
2404 * The connections set contains all the connections that provide sensor values.
2405 *
2406 * The objectMgrPaths map contains mappings from a connection name to the
2407 * corresponding DBus object path that implements ObjectManager.
2408 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002409 * The InventoryItem vector contains D-Bus inventory items associated with the
2410 * sensors. Inventory item data is needed for some Redfish sensor properties.
2411 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002412 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002413 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002414 * @param connections Connections that provide sensor values.
2415 * @param objectMgrPaths Mappings from connection name to DBus object path that
2416 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002417 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002418 */
2419void getSensorData(
2420 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002421 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002422 const boost::container::flat_set<std::string>& connections,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002423 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002424 objectMgrPaths,
2425 std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002426{
2427 BMCWEB_LOG_DEBUG << "getSensorData enter";
2428 // Get managed objects from all services exposing sensors
2429 for (const std::string& connection : connections)
2430 {
2431 // Response handler to process managed objects
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002432 auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002433 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002434 const boost::system::error_code ec,
2435 ManagedObjectsVectorType& resp) {
2436 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2437 if (ec)
2438 {
2439 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
2440 messages::internalError(SensorsAsyncResp->res);
2441 return;
2442 }
2443 // Go through all objects and update response with sensor data
2444 for (const auto& objDictEntry : resp)
2445 {
2446 const std::string& objPath =
2447 static_cast<const std::string&>(objDictEntry.first);
2448 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2449 << objPath;
2450
Shawn McCarneyde629b62019-03-08 10:42:51 -06002451 std::vector<std::string> split;
2452 // Reserve space for
2453 // /xyz/openbmc_project/sensors/<name>/<subname>
2454 split.reserve(6);
2455 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2456 if (split.size() < 6)
2457 {
2458 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2459 << objPath;
2460 continue;
2461 }
2462 // These indexes aren't intuitive, as boost::split puts an empty
2463 // string at the beginning
2464 const std::string& sensorType = split[4];
2465 const std::string& sensorName = split[5];
2466 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2467 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002468 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002469 {
2470 BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2471 continue;
2472 }
2473
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002474 // Find inventory item (if any) associated with sensor
2475 InventoryItem* inventoryItem =
2476 findInventoryItemForSensor(inventoryItems, objPath);
2477
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002478 const std::string& sensorSchema =
2479 SensorsAsyncResp->chassisSubNode;
2480
2481 nlohmann::json* sensorJson = nullptr;
2482
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002483 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002484 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002485 SensorsAsyncResp->res.jsonValue["@odata.id"] =
2486 "/redfish/v1/Chassis/" + SensorsAsyncResp->chassisId +
2487 "/" + SensorsAsyncResp->chassisSubNode + "/" +
2488 sensorName;
2489 sensorJson = &(SensorsAsyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002490 }
2491 else
2492 {
Ed Tanous271584a2019-07-09 16:24:22 -07002493 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002494 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002495 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002496 fieldName = "Temperatures";
2497 }
2498 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2499 sensorType == "fan_pwm")
2500 {
2501 fieldName = "Fans";
2502 }
2503 else if (sensorType == "voltage")
2504 {
2505 fieldName = "Voltages";
2506 }
2507 else if (sensorType == "power")
2508 {
2509 if (!sensorName.compare("total_power"))
2510 {
2511 fieldName = "PowerControl";
2512 }
2513 else if ((inventoryItem != nullptr) &&
2514 (inventoryItem->isPowerSupply))
2515 {
2516 fieldName = "PowerSupplies";
2517 }
2518 else
2519 {
2520 // Other power sensors are in SensorCollection
2521 continue;
2522 }
2523 }
2524 else
2525 {
2526 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2527 << sensorType;
2528 continue;
2529 }
2530
2531 nlohmann::json& tempArray =
2532 SensorsAsyncResp->res.jsonValue[fieldName];
2533 if (fieldName == "PowerControl")
2534 {
2535 if (tempArray.empty())
2536 {
2537 // Put multiple "sensors" into a single
2538 // PowerControl. Follows MemberId naming and
2539 // naming in power.hpp.
2540 tempArray.push_back(
2541 {{"@odata.id",
2542 "/redfish/v1/Chassis/" +
2543 SensorsAsyncResp->chassisId + "/" +
2544 SensorsAsyncResp->chassisSubNode + "#/" +
2545 fieldName + "/0"}});
2546 }
2547 sensorJson = &(tempArray.back());
2548 }
2549 else if (fieldName == "PowerSupplies")
2550 {
2551 if (inventoryItem != nullptr)
2552 {
2553 sensorJson =
2554 &(getPowerSupply(tempArray, *inventoryItem,
2555 SensorsAsyncResp->chassisId));
2556 }
2557 }
2558 else
2559 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002560 tempArray.push_back(
2561 {{"@odata.id",
2562 "/redfish/v1/Chassis/" +
2563 SensorsAsyncResp->chassisId + "/" +
2564 SensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002565 fieldName + "/"}});
2566 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002567 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002568 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002569
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002570 if (sensorJson != nullptr)
2571 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002572 objectInterfacesToJson(
2573 sensorName, sensorType, SensorsAsyncResp,
2574 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002575 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002576 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002577 if (SensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002578 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002579 sortJSONResponse(SensorsAsyncResp);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002580 if (SensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002581 {
2582 populateFanRedundancy(SensorsAsyncResp);
2583 }
James Feist8bd25cc2019-03-15 15:14:00 -07002584 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002585 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2586 };
2587
2588 // Find DBus object path that implements ObjectManager for the current
2589 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002590 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002591 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002592 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002593 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2594 << objectMgrPath;
2595
2596 crow::connections::systemBus->async_method_call(
2597 getManagedObjectsCb, connection, objectMgrPath,
2598 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
2599 };
2600 BMCWEB_LOG_DEBUG << "getSensorData exit";
2601}
2602
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002603void processSensorList(
2604 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
2605 std::shared_ptr<boost::container::flat_set<std::string>> sensorNames)
2606{
2607 auto getConnectionCb =
2608 [SensorsAsyncResp, sensorNames](
2609 const boost::container::flat_set<std::string>& connections) {
2610 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2611 auto getObjectManagerPathsCb =
2612 [SensorsAsyncResp, sensorNames, connections](
2613 std::shared_ptr<
2614 boost::container::flat_map<std::string, std::string>>
2615 objectMgrPaths) {
2616 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2617 auto getInventoryItemsCb =
2618 [SensorsAsyncResp, sensorNames, connections,
2619 objectMgrPaths](
2620 std::shared_ptr<std::vector<InventoryItem>>
2621 inventoryItems) {
2622 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2623 // Get sensor data and store results in JSON
2624 getSensorData(SensorsAsyncResp, sensorNames,
2625 connections, objectMgrPaths,
2626 inventoryItems);
2627 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2628 };
2629
2630 // Get inventory items associated with sensors
2631 getInventoryItems(SensorsAsyncResp, sensorNames,
2632 objectMgrPaths,
2633 std::move(getInventoryItemsCb));
2634
2635 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2636 };
2637
2638 // Get mapping from connection names to the DBus object
2639 // paths that implement the ObjectManager interface
2640 getObjectManagerPaths(SensorsAsyncResp,
2641 std::move(getObjectManagerPathsCb));
2642 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2643 };
2644
2645 // Get set of connections that provide sensor values
2646 getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb));
2647}
2648
Shawn McCarneyde629b62019-03-08 10:42:51 -06002649/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002650 * @brief Entry point for retrieving sensors data related to requested
2651 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002652 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002653 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002654void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
2655{
2656 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002657 auto getChassisCb =
2658 [SensorsAsyncResp](
2659 std::shared_ptr<boost::container::flat_set<std::string>>
2660 sensorNames) {
2661 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002662 processSensorList(SensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002663 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2664 };
Jennifer Lee4f9a2132019-03-04 12:45:19 -08002665 SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002666
Shawn McCarney26f03892019-05-03 13:20:24 -05002667 // Get set of sensors in chassis
Ed Tanous1abe55e2018-09-05 08:30:59 -07002668 getChassis(SensorsAsyncResp, std::move(getChassisCb));
2669 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002670}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002671
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302672/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002673 * @brief Find the requested sensorName in the list of all sensors supplied by
2674 * the chassis node
2675 *
2676 * @param sensorName The sensor name supplied in the PATCH request
2677 * @param sensorsList The list of sensors managed by the chassis node
2678 * @param sensorsModified The list of sensors that were found as a result of
2679 * repeated calls to this function
2680 */
2681bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302682 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002683 boost::container::flat_set<std::string>& sensorsList,
2684 boost::container::flat_set<std::string>& sensorsModified)
2685{
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302686 for (std::string_view chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002687 {
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302688 std::size_t pos = chassisSensor.rfind("/");
2689 if (pos >= (chassisSensor.size() - 1))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002690 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002691 continue;
2692 }
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302693 std::string_view thisSensorName = chassisSensor.substr(pos + 1);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002694 if (thisSensorName == sensorName)
2695 {
2696 sensorsModified.emplace(chassisSensor);
2697 return true;
2698 }
2699 }
2700 return false;
2701}
2702
2703/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302704 * @brief Entry point for overriding sensor values of given sensor
2705 *
2706 * @param res response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002707 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002708 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302709 */
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002710void setSensorsOverride(
Carol Wang4bb3dc32019-10-17 18:15:02 +08002711 std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
2712 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002713 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302714{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002715 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002716 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302717
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302718 const char* propertyValueName;
2719 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302720 std::string memberId;
2721 double value;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302722 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302723 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302724 if (collectionItems.first == "Temperatures")
2725 {
2726 propertyValueName = "ReadingCelsius";
2727 }
2728 else if (collectionItems.first == "Fans")
2729 {
2730 propertyValueName = "Reading";
2731 }
2732 else
2733 {
2734 propertyValueName = "ReadingVolts";
2735 }
2736 for (auto& item : collectionItems.second)
2737 {
Carol Wang4bb3dc32019-10-17 18:15:02 +08002738 if (!json_util::readJson(item, sensorAsyncResp->res, "MemberId",
2739 memberId, propertyValueName, value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302740 {
2741 return;
2742 }
2743 overrideMap.emplace(memberId,
2744 std::make_pair(value, collectionItems.first));
2745 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302746 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002747
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002748 auto getChassisSensorListCb = [sensorAsyncResp,
2749 overrideMap](const std::shared_ptr<
2750 boost::container::flat_set<
2751 std::string>>
2752 sensorsList) {
2753 // Match sensor names in the PATCH request to those managed by the
2754 // chassis node
2755 const std::shared_ptr<boost::container::flat_set<std::string>>
2756 sensorNames =
2757 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302758 for (const auto& item : overrideMap)
2759 {
2760 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002761 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2762 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302763 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302764 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302765 messages::resourceNotFound(sensorAsyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302766 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302767 return;
2768 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302769 }
2770 // Get the connection to which the memberId belongs
2771 auto getObjectsWithConnectionCb =
2772 [sensorAsyncResp, overrideMap](
2773 const boost::container::flat_set<std::string>& connections,
2774 const std::set<std::pair<std::string, std::string>>&
2775 objectsWithConnection) {
2776 if (objectsWithConnection.size() != overrideMap.size())
2777 {
2778 BMCWEB_LOG_INFO
2779 << "Unable to find all objects with proper connection "
2780 << objectsWithConnection.size() << " requested "
2781 << overrideMap.size() << "\n";
2782 messages::resourceNotFound(
2783 sensorAsyncResp->res,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002784 sensorAsyncResp->chassisSubNode ==
2785 sensors::node::thermal
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302786 ? "Temperatures"
2787 : "Voltages",
2788 "Count");
2789 return;
2790 }
2791 for (const auto& item : objectsWithConnection)
2792 {
2793
2794 auto lastPos = item.first.rfind('/');
2795 if (lastPos == std::string::npos)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302796 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302797 messages::internalError(sensorAsyncResp->res);
2798 return;
2799 }
2800 std::string sensorName = item.first.substr(lastPos + 1);
2801
2802 const auto& iterator = overrideMap.find(sensorName);
2803 if (iterator == overrideMap.end())
2804 {
2805 BMCWEB_LOG_INFO << "Unable to find sensor object"
2806 << item.first << "\n";
2807 messages::internalError(sensorAsyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302808 return;
2809 }
2810 crow::connections::systemBus->async_method_call(
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302811 [sensorAsyncResp](const boost::system::error_code ec) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302812 if (ec)
2813 {
2814 BMCWEB_LOG_DEBUG
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302815 << "setOverrideValueStatus DBUS error: "
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302816 << ec;
2817 messages::internalError(sensorAsyncResp->res);
2818 return;
2819 }
2820 },
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302821 item.second, item.first,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302822 "org.freedesktop.DBus.Properties", "Set",
2823 "xyz.openbmc_project.Sensor.Value", "Value",
Patrick Williams19bd78d2020-05-13 17:38:24 -05002824 std::variant<double>(iterator->second.first));
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302825 }
2826 };
2827 // Get object with connection for the given sensor name
2828 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2829 std::move(getObjectsWithConnectionCb));
2830 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302831 // get full sensor list for the given chassisId and cross verify the sensor.
2832 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2833}
2834
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002835bool isOverridingAllowed(const std::string& manufacturingModeStatus)
2836{
2837 if (manufacturingModeStatus ==
2838 "xyz.openbmc_project.Control.Security.SpecialMode.Modes.Manufacturing")
2839 {
2840 return true;
2841 }
2842
2843#ifdef BMCWEB_ENABLE_VALIDATION_UNSECURE_FEATURE
2844 if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
2845 "SpecialMode.Modes.ValidationUnsecure")
2846 {
2847 return true;
2848 }
2849
2850#endif
2851
2852 return false;
2853}
2854
2855/**
2856 * @brief Entry point for Checking the manufacturing mode before doing sensor
2857 * override values of given sensor
2858 *
2859 * @param res response object
2860 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002861 * @param chassisSubNode Chassis Node for which the query has to happen
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002862 */
2863void checkAndDoSensorsOverride(
2864 std::shared_ptr<SensorsAsyncResp> sensorAsyncResp,
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002865 std::unordered_map<std::string, std::vector<nlohmann::json>>&
2866 allCollections)
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002867{
2868 BMCWEB_LOG_INFO << "checkAndDoSensorsOverride for subnode"
2869 << sensorAsyncResp->chassisSubNode << "\n";
2870
2871 const std::array<std::string, 1> interfaces = {
2872 "xyz.openbmc_project.Security.SpecialMode"};
2873
2874 crow::connections::systemBus->async_method_call(
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002875 [sensorAsyncResp, allCollections](const boost::system::error_code ec,
2876 const GetSubTreeType& resp) mutable {
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002877 if (ec)
2878 {
2879 BMCWEB_LOG_DEBUG
2880 << "Error in querying GetSubTree with Object Mapper. "
2881 << ec;
2882 messages::internalError(sensorAsyncResp->res);
2883 return;
2884 }
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002885#ifdef BMCWEB_INSECURE_UNRESTRICTED_SENSOR_OVERRIDE
2886 // Proceed with sensor override
2887 setSensorsOverride(sensorAsyncResp, allCollections);
2888 return;
2889#endif
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002890
2891 if (resp.size() != 1)
2892 {
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002893 BMCWEB_LOG_WARNING
2894 << "Overriding sensor value is not allowed - Internal "
2895 "error in querying SpecialMode property.";
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002896 messages::internalError(sensorAsyncResp->res);
2897 return;
2898 }
2899 const std::string& path = resp[0].first;
2900 const std::string& serviceName = resp[0].second.begin()->first;
2901
2902 if (path.empty() || serviceName.empty())
2903 {
2904 BMCWEB_LOG_DEBUG
2905 << "Path or service name is returned as empty. ";
2906 messages::internalError(sensorAsyncResp->res);
2907 return;
2908 }
2909
2910 // Sensor override is allowed only in manufacturing mode or
2911 // validation unsecure mode .
2912 crow::connections::systemBus->async_method_call(
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002913 [sensorAsyncResp, allCollections,
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002914 path](const boost::system::error_code ec,
2915 std::variant<std::string>& getManufactMode) mutable {
2916 if (ec)
2917 {
2918 BMCWEB_LOG_DEBUG
2919 << "Error in querying Special mode property " << ec;
2920 messages::internalError(sensorAsyncResp->res);
2921 return;
2922 }
2923
2924 const std::string* manufacturingModeStatus =
2925 std::get_if<std::string>(&getManufactMode);
2926
2927 if (nullptr == manufacturingModeStatus)
2928 {
2929 BMCWEB_LOG_DEBUG << "Sensor override mode is not "
2930 "Enabled. Returning ... ";
2931 messages::internalError(sensorAsyncResp->res);
2932 return;
2933 }
2934
2935 if (isOverridingAllowed(*manufacturingModeStatus))
2936 {
2937 BMCWEB_LOG_INFO << "Manufacturing mode is Enabled. "
2938 "Proceeding further... ";
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002939 setSensorsOverride(sensorAsyncResp, allCollections);
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002940 }
2941 else
2942 {
2943 BMCWEB_LOG_WARNING
2944 << "Manufacturing mode is not Enabled...can't "
2945 "Override the sensor value. ";
2946
2947 messages::actionNotSupported(
2948 sensorAsyncResp->res,
2949 "Overriding of Sensor Value for non "
2950 "manufacturing mode");
2951 return;
2952 }
2953 },
2954 serviceName, path, "org.freedesktop.DBus.Properties", "Get",
2955 "xyz.openbmc_project.Security.SpecialMode", "SpecialMode");
2956 },
2957
2958 "xyz.openbmc_project.ObjectMapper",
2959 "/xyz/openbmc_project/object_mapper",
2960 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 5, interfaces);
2961}
2962
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002963/**
2964 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2965 * path of the sensor.
2966 *
2967 * Function builds valid Redfish response for sensor query of given chassis and
2968 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2969 * it to caller in a callback.
2970 *
2971 * @param chassis Chassis for which retrieval should be performed
2972 * @param node Node (group) of sensors. See sensors::node for supported values
2973 * @param mapComplete Callback to be called with retrieval result
2974 */
2975void retrieveUriToDbusMap(const std::string& chassis, const std::string& node,
2976 SensorsAsyncResp::DataCompleteCb&& mapComplete)
2977{
2978 auto typesIt = sensors::dbus::types.find(node);
2979 if (typesIt == sensors::dbus::types.end())
2980 {
2981 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2982 mapComplete(boost::beast::http::status::bad_request, {});
2983 return;
2984 }
2985
2986 auto respBuffer = std::make_shared<crow::Response>();
2987 auto callback =
2988 [respBuffer, mapCompleteCb{std::move(mapComplete)}](
2989 const boost::beast::http::status status,
2990 const boost::container::flat_map<std::string, std::string>&
2991 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2992
2993 auto resp = std::make_shared<SensorsAsyncResp>(
2994 *respBuffer, chassis, typesIt->second, node, std::move(callback));
2995 getChassisData(resp);
2996}
2997
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002998class SensorCollection : public Node
2999{
3000 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07003001 SensorCollection(App& app) :
Gunnar Millsf99c3792020-04-14 21:54:42 -05003002 Node(app, "/redfish/v1/Chassis/<str>/Sensors/", std::string())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003003 {
3004 entityPrivileges = {
3005 {boost::beast::http::verb::get, {{"Login"}}},
3006 {boost::beast::http::verb::head, {{"Login"}}},
3007 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3008 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3009 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3010 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3011 }
3012
3013 private:
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003014 void doGet(crow::Response& res, const crow::Request& req,
3015 const std::vector<std::string>& params) override
3016 {
3017 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
3018 if (params.size() != 1)
3019 {
3020 BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1";
3021 messages::internalError(res);
3022 res.end();
3023 return;
3024 }
3025
3026 const std::string& chassisId = params[0];
3027 std::shared_ptr<SensorsAsyncResp> asyncResp =
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02003028 std::make_shared<SensorsAsyncResp>(
3029 res, chassisId, sensors::dbus::types.at(sensors::node::sensors),
3030 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003031
3032 auto getChassisCb =
3033 [asyncResp](std::shared_ptr<boost::container::flat_set<std::string>>
3034 sensorNames) {
3035 BMCWEB_LOG_DEBUG << "getChassisCb enter";
3036
3037 nlohmann::json& entriesArray =
3038 asyncResp->res.jsonValue["Members"];
3039 for (auto& sensor : *sensorNames)
3040 {
3041 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
3042
3043 std::size_t lastPos = sensor.rfind("/");
3044 if (lastPos == std::string::npos ||
3045 lastPos + 1 >= sensor.size())
3046 {
3047 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
3048 messages::internalError(asyncResp->res);
3049 return;
3050 }
3051 std::string sensorName = sensor.substr(lastPos + 1);
3052 entriesArray.push_back(
3053 {{"@odata.id",
3054 "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
3055 asyncResp->chassisSubNode + "/" + sensorName}});
3056 }
3057
3058 asyncResp->res.jsonValue["Members@odata.count"] =
3059 entriesArray.size();
3060 BMCWEB_LOG_DEBUG << "getChassisCb exit";
3061 };
3062
3063 // Get set of sensors in chassis
3064 getChassis(asyncResp, std::move(getChassisCb));
3065 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
3066 }
3067};
3068
3069class Sensor : public Node
3070{
3071 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07003072 Sensor(App& app) :
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003073 Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(),
3074 std::string())
3075 {
3076 entityPrivileges = {
3077 {boost::beast::http::verb::get, {{"Login"}}},
3078 {boost::beast::http::verb::head, {{"Login"}}},
3079 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3080 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3081 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3082 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3083 }
3084
3085 private:
3086 void doGet(crow::Response& res, const crow::Request& req,
3087 const std::vector<std::string>& params) override
3088 {
3089 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3090 if (params.size() != 2)
3091 {
3092 BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2";
3093 messages::internalError(res);
3094 res.end();
3095 return;
3096 }
3097 const std::string& chassisId = params[0];
3098 std::shared_ptr<SensorsAsyncResp> asyncResp =
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02003099 std::make_shared<SensorsAsyncResp>(res, chassisId,
3100 std::vector<const char*>(),
3101 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003102
3103 const std::string& sensorName = params[1];
3104 const std::array<const char*, 1> interfaces = {
3105 "xyz.openbmc_project.Sensor.Value"};
3106
3107 // Get a list of all of the sensors that implement Sensor.Value
3108 // and get the path and service name associated with the sensor
3109 crow::connections::systemBus->async_method_call(
3110 [asyncResp, sensorName](const boost::system::error_code ec,
3111 const GetSubTreeType& subtree) {
3112 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3113 if (ec)
3114 {
3115 messages::internalError(asyncResp->res);
3116 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3117 << "Dbus error " << ec;
3118 return;
3119 }
3120
3121 GetSubTreeType::const_iterator it = std::find_if(
3122 subtree.begin(), subtree.end(),
3123 [sensorName](
3124 const std::pair<
3125 std::string,
3126 std::vector<std::pair<std::string,
3127 std::vector<std::string>>>>&
3128 object) {
3129 std::string_view sensor = object.first;
3130 std::size_t lastPos = sensor.rfind("/");
3131 if (lastPos == std::string::npos ||
3132 lastPos + 1 >= sensor.size())
3133 {
3134 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3135 << sensor;
3136 return false;
3137 }
3138 std::string_view name = sensor.substr(lastPos + 1);
3139
3140 return name == sensorName;
3141 });
3142
3143 if (it == subtree.end())
3144 {
3145 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3146 << sensorName;
3147 messages::resourceNotFound(asyncResp->res, "Sensor",
3148 sensorName);
3149 return;
3150 }
3151 std::string_view sensorPath = (*it).first;
3152 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3153 << sensorName << "': " << sensorPath;
3154
3155 const std::shared_ptr<boost::container::flat_set<std::string>>
3156 sensorList = std::make_shared<
3157 boost::container::flat_set<std::string>>();
3158
3159 sensorList->emplace(sensorPath);
3160 processSensorList(asyncResp, sensorList);
3161 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3162 },
3163 "xyz.openbmc_project.ObjectMapper",
3164 "/xyz/openbmc_project/object_mapper",
3165 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3166 "/xyz/openbmc_project/sensors", 2, interfaces);
3167 }
3168};
3169
Ed Tanous1abe55e2018-09-05 08:30:59 -07003170} // namespace redfish