blob: c44ff71d8c6e0303800b7745fa1c35334843e425 [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
18#include <math.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -070019
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>
Ed Tanousabf2add2019-01-22 16:40:12 -080026#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace redfish
29{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010030
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010031using GetSubTreeType = std::vector<
32 std::pair<std::string,
33 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
34
Ed Tanousabf2add2019-01-22 16:40:12 -080035using SensorVariant = std::variant<int64_t, double>;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070036
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010037using ManagedObjectsVectorType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070038 sdbusplus::message::object_path,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010039 boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070040 std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010041
42/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020043 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010044 * Gathers data needed for response processing after async calls are done
45 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070046class SensorsAsyncResp
47{
48 public:
49 SensorsAsyncResp(crow::Response& response, const std::string& chassisId,
Ed Tanousb01bf292019-03-25 19:25:26 +000050 const std::initializer_list<const char*> types,
Ed Tanous2474adf2018-09-05 16:31:16 -070051 const std::string& subNode) :
Ed Tanous43b761d2019-02-13 20:10:56 -080052 res(response),
53 chassisId(chassisId), types(types), chassisSubNode(subNode)
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 {
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010055 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020056
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 ~SensorsAsyncResp()
58 {
59 if (res.result() == boost::beast::http::status::internal_server_error)
60 {
61 // Reset the json object to clear out any data that made it in
62 // before the error happened todo(ed) handle error condition with
63 // proper code
64 res.jsonValue = nlohmann::json::object();
65 }
66 res.end();
67 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010068
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 crow::Response& res;
70 std::string chassisId{};
71 const std::vector<const char*> types;
Ed Tanous2474adf2018-09-05 16:31:16 -070072 std::string chassisSubNode{};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010073};
74
75/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053076 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020077 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010078 * @param sensorNames Sensors retrieved from chassis
79 * @param callback Callback for processing gathered connections
80 */
81template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053082void getObjectsWithConnection(
83 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -070084 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053085 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -070086{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053087 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 const std::string path = "/xyz/openbmc_project/sensors";
89 const std::array<std::string, 1> interfaces = {
90 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010091
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 // Response handler for parsing objects subtree
93 auto respHandler = [callback{std::move(callback)}, SensorsAsyncResp,
94 sensorNames](const boost::system::error_code ec,
95 const GetSubTreeType& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053096 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 if (ec)
98 {
Ed Tanous5f7d88c2018-11-14 14:08:56 -080099 messages::internalError(SensorsAsyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530100 BMCWEB_LOG_ERROR
101 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100103 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100104
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
106
107 // Make unique list of connections only for requested sensor types and
108 // found in the chassis
109 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530110 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
112 // producers
113 connections.reserve(8);
114
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700115 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
116 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 {
118 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
119 }
120
121 for (const std::pair<
122 std::string,
123 std::vector<std::pair<std::string, std::vector<std::string>>>>&
124 object : subtree)
125 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700126 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700128 for (const std::pair<std::string, std::vector<std::string>>&
129 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700131 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
132 connections.insert(objData.first);
133 objectsWithConnection.insert(
134 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700135 }
136 }
137 }
138 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530139 callback(std::move(connections), std::move(objectsWithConnection));
140 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700141 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700142 // Make call to ObjectMapper to find all sensors objects
143 crow::connections::systemBus->async_method_call(
144 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
145 "/xyz/openbmc_project/object_mapper",
146 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530147 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
148}
149
150/**
151 * @brief Create connections necessary for sensors
152 * @param SensorsAsyncResp Pointer to object holding response data
153 * @param sensorNames Sensors retrieved from chassis
154 * @param callback Callback for processing gathered connections
155 */
156template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700157void getConnections(
158 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
159 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
160 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530161{
162 auto objectsWithConnectionCb =
163 [callback](const boost::container::flat_set<std::string>& connections,
164 const std::set<std::pair<std::string, std::string>>&
165 objectsWithConnection) {
166 callback(std::move(connections));
167 };
168 getObjectsWithConnection(SensorsAsyncResp, sensorNames,
169 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100170}
171
172/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700173 * @brief Shrinks the list of sensors for processing
174 * @param SensorsAysncResp The class holding the Redfish response
175 * @param allSensors A list of all the sensors associated to the
176 * chassis element (i.e. baseboard, front panel, etc...)
177 * @param activeSensors A list that is a reduction of the incoming
178 * allSensors list. Eliminate Thermal sensors when a Power request is
179 * made, and eliminate Power sensors when a Thermal request is made.
180 */
181void reduceSensorList(
182 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
183 const std::vector<std::string>* allSensors,
184 std::shared_ptr<boost::container::flat_set<std::string>> activeSensors)
185{
186 if (SensorsAsyncResp == nullptr)
187 {
188 return;
189 }
190 if ((allSensors == nullptr) || (activeSensors == nullptr))
191 {
192 messages::resourceNotFound(
193 SensorsAsyncResp->res, SensorsAsyncResp->chassisSubNode,
194 SensorsAsyncResp->chassisSubNode == "Thermal" ? "Temperatures"
195 : "Voltages");
196
197 return;
198 }
199 if (allSensors->empty())
200 {
201 // Nothing to do, the activeSensors object is also empty
202 return;
203 }
204
205 for (const char* type : SensorsAsyncResp->types)
206 {
207 for (const std::string& sensor : *allSensors)
208 {
209 if (boost::starts_with(sensor, type))
210 {
211 activeSensors->emplace(sensor);
212 }
213 }
214 }
215}
216
217/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100218 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
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 callback Callback for next step in gathered sensor processing
221 */
222template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700223void getChassis(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 Callback&& callback)
225{
226 BMCWEB_LOG_DEBUG << "getChassis enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700227 const std::array<const char*, 3> interfaces = {
228 "xyz.openbmc_project.Inventory.Item.Board",
229 "xyz.openbmc_project.Inventory.Item.Chassis",
230 "xyz.openbmc_project.Inventory.Item.PowerSupply"};
231 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
232 const boost::system::error_code ec,
233 const std::vector<std::string>& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
235 if (ec)
236 {
237 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700238 messages::internalError(sensorsAsyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 return;
240 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100241
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700242 const std::string* chassisPath = nullptr;
243 std::string chassisName;
244 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700246 std::size_t lastPos = chassis.rfind("/");
247 if (lastPos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700249 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700250 continue;
251 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700252 chassisName = chassis.substr(lastPos + 1);
253 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700255 chassisPath = &chassis;
256 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700257 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700258 }
259 if (chassisPath == nullptr)
260 {
261 messages::resourceNotFound(sensorsAsyncResp->res, "Chassis",
262 sensorsAsyncResp->chassisId);
263 return;
264 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700265
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700266 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
267 if (chassisSubNode == "Power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700269 sensorsAsyncResp->res.jsonValue["@odata.type"] =
270 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700271 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700272 else if (chassisSubNode == "Thermal")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700273 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700274 sensorsAsyncResp->res.jsonValue["@odata.type"] =
275 "#Thermal.v1_4_0.Thermal";
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800276 sensorsAsyncResp->res.jsonValue["Fans"] = nlohmann::json::array();
277 sensorsAsyncResp->res.jsonValue["Temperatures"] =
278 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700280 sensorsAsyncResp->res.jsonValue["@odata.id"] =
281 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
282 chassisSubNode;
283
284 sensorsAsyncResp->res.jsonValue["@odata.context"] =
285 "/redfish/v1/$metadata#" + chassisSubNode + "." + chassisSubNode;
286 sensorsAsyncResp->res.jsonValue["Id"] = chassisSubNode;
287 sensorsAsyncResp->res.jsonValue["Name"] = chassisSubNode;
288
289 // Get the list of sensors for this Chassis element
290 std::string sensorPath = *chassisPath + "/sensors";
291 crow::connections::systemBus->async_method_call(
292 [sensorsAsyncResp, callback{std::move(callback)}](
293 const boost::system::error_code ec,
294 const std::variant<std::vector<std::string>>&
295 variantEndpoints) {
296 if (ec)
297 {
298 if (ec.value() != EBADR)
299 {
300 messages::internalError(sensorsAsyncResp->res);
301 return;
302 }
303 }
304 const std::vector<std::string>* nodeSensorList =
305 std::get_if<std::vector<std::string>>(&(variantEndpoints));
306 if (nodeSensorList == nullptr)
307 {
308 messages::resourceNotFound(
309 sensorsAsyncResp->res, sensorsAsyncResp->chassisSubNode,
310 sensorsAsyncResp->chassisSubNode == "Thermal"
311 ? "Temperatures"
312 : "Voltages");
313 return;
314 }
315 const std::shared_ptr<boost::container::flat_set<std::string>>
316 culledSensorList = std::make_shared<
317 boost::container::flat_set<std::string>>();
318 reduceSensorList(sensorsAsyncResp, nodeSensorList,
319 culledSensorList);
320 callback(culledSensorList);
321 },
322 "xyz.openbmc_project.ObjectMapper", sensorPath,
323 "org.freedesktop.DBus.Properties", "Get",
324 "xyz.openbmc_project.Association", "endpoints");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100325 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100326
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700327 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700328 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700329 respHandler, "xyz.openbmc_project.ObjectMapper",
330 "/xyz/openbmc_project/object_mapper",
331 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
332 "/xyz/openbmc_project/inventory", int32_t(0), interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700333 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100334}
335
336/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600337 * @brief Finds all DBus object paths that implement ObjectManager.
338 *
339 * Creates a mapping from the associated connection name to the object path.
340 *
341 * Finds the object paths asynchronously. Invokes callback when information has
342 * been obtained.
343 *
344 * The callback must have the following signature:
345 * @code
346 * callback(const boost::container::flat_map<std::string,
347 * std::string>& objectMgrPaths)
348 * @endcode
349 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700350 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600351 * @param callback Callback to invoke when object paths obtained.
352 */
353template <typename Callback>
354void getObjectManagerPaths(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
355 Callback&& callback)
356{
357 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
358 const std::array<std::string, 1> interfaces = {
359 "org.freedesktop.DBus.ObjectManager"};
360
361 // Response handler for GetSubTree DBus method
362 auto respHandler = [callback{std::move(callback)},
363 SensorsAsyncResp](const boost::system::error_code ec,
364 const GetSubTreeType& subtree) {
365 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
366 if (ec)
367 {
368 messages::internalError(SensorsAsyncResp->res);
369 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
370 << ec;
371 return;
372 }
373
374 // Loop over returned object paths
375 boost::container::flat_map<std::string, std::string> objectMgrPaths;
376 for (const std::pair<
377 std::string,
378 std::vector<std::pair<std::string, std::vector<std::string>>>>&
379 object : subtree)
380 {
381 // Loop over connections for current object path
382 const std::string& objectPath = object.first;
383 for (const std::pair<std::string, std::vector<std::string>>&
384 objData : object.second)
385 {
386 // Add mapping from connection to object path
387 const std::string& connection = objData.first;
388 objectMgrPaths[connection] = objectPath;
389 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
390 << objectPath;
391 }
392 }
393 callback(std::move(objectMgrPaths));
394 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
395 };
396
397 // Query mapper for all DBus object paths that implement ObjectManager
398 crow::connections::systemBus->async_method_call(
399 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
400 "/xyz/openbmc_project/object_mapper",
401 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", int32_t(0),
402 interfaces);
403 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
404}
405
406/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100407 * @brief Builds a json sensor representation of a sensor.
408 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500409 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100410 * build
411 * @param interfacesDict A dictionary of the interfaces and properties of said
412 * interfaces to be built from
413 * @param sensor_json The json object to fill
414 */
415void objectInterfacesToJson(
416 const std::string& sensorName, const std::string& sensorType,
417 const boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700418 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100419 interfacesDict,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700420 nlohmann::json& sensor_json)
421{
422 // We need a value interface before we can do anything with it
423 auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
424 if (valueIt == interfacesDict.end())
425 {
426 BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
427 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100428 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100429
Ed Tanous1abe55e2018-09-05 08:30:59 -0700430 // Assume values exist as is (10^0 == 1) if no scale exists
431 int64_t scaleMultiplier = 0;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100432
Ed Tanous1abe55e2018-09-05 08:30:59 -0700433 auto scaleIt = valueIt->second.find("Scale");
434 // If a scale exists, pull value as int64, and use the scaling.
435 if (scaleIt != valueIt->second.end())
436 {
Ed Tanousabf2add2019-01-22 16:40:12 -0800437 const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700438 if (int64Value != nullptr)
439 {
440 scaleMultiplier = *int64Value;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100441 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100442 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700443
444 sensor_json["MemberId"] = sensorName;
Ed Tanouse742b6c2019-05-03 15:06:53 -0700445 sensor_json["Name"] = boost::replace_all_copy(sensorName, "_", " ");
446
Ed Tanous1abe55e2018-09-05 08:30:59 -0700447 sensor_json["Status"]["State"] = "Enabled";
448 sensor_json["Status"]["Health"] = "OK";
449
450 // Parameter to set to override the type we get from dbus, and force it to
451 // int, regardless of what is available. This is used for schemas like fan,
452 // that require integers, not floats.
453 bool forceToInt = false;
454
455 const char* unit = "Reading";
456 if (sensorType == "temperature")
457 {
458 unit = "ReadingCelsius";
459 sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature";
460 // TODO(ed) Documentation says that path should be type fan_tach,
461 // implementation seems to implement fan
462 }
463 else if (sensorType == "fan" || sensorType == "fan_tach")
464 {
465 unit = "Reading";
466 sensor_json["ReadingUnits"] = "RPM";
467 sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
468 forceToInt = true;
469 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700470 else if (sensorType == "fan_pwm")
471 {
472 unit = "Reading";
473 sensor_json["ReadingUnits"] = "Percent";
474 sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
475 forceToInt = true;
476 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700477 else if (sensorType == "voltage")
478 {
479 unit = "ReadingVolts";
480 sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage";
481 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700482 else if (sensorType == "power")
483 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700484 std::string sensorNameLower =
485 boost::algorithm::to_lower_copy(sensorName);
486
487 if (sensorNameLower.find("input") != std::string::npos)
488 {
489 unit = "PowerInputWatts";
490 }
491 else
492 {
493 unit = "PowerOutputWatts";
494 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700495 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700496 else
497 {
498 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
499 return;
500 }
501 // Map of dbus interface name, dbus property name and redfish property_name
502 std::vector<std::tuple<const char*, const char*, const char*>> properties;
503 properties.reserve(7);
504
505 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600506
507 // If sensor type doesn't map to Redfish PowerSupply, add threshold props
508 if ((sensorType != "current") && (sensorType != "power"))
509 {
510 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
511 "WarningHigh", "UpperThresholdNonCritical");
512 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
513 "WarningLow", "LowerThresholdNonCritical");
514 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
515 "CriticalHigh", "UpperThresholdCritical");
516 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
517 "CriticalLow", "LowerThresholdCritical");
518 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700519
Ed Tanous2474adf2018-09-05 16:31:16 -0700520 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
521
Ed Tanous1abe55e2018-09-05 08:30:59 -0700522 if (sensorType == "temperature")
523 {
524 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
525 "MinReadingRangeTemp");
526 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
527 "MaxReadingRangeTemp");
528 }
Shawn McCarneyde629b62019-03-08 10:42:51 -0600529 else if ((sensorType != "current") && (sensorType != "power"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530 {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600531 // Sensor type doesn't map to Redfish PowerSupply; add min/max props
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
533 "MinReadingRange");
534 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
535 "MaxReadingRange");
536 }
537
538 for (const std::tuple<const char*, const char*, const char*>& p :
539 properties)
540 {
541 auto interfaceProperties = interfacesDict.find(std::get<0>(p));
542 if (interfaceProperties != interfacesDict.end())
543 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000544 auto valueIt = interfaceProperties->second.find(std::get<1>(p));
545 if (valueIt != interfaceProperties->second.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000547 const SensorVariant& valueVariant = valueIt->second;
548 nlohmann::json& valueIt = sensor_json[std::get<2>(p)];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700549 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -0800550 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700551
Ed Tanousabf2add2019-01-22 16:40:12 -0800552 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700553 double temp = 0.0;
554 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700555 {
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700556 temp = *int64Value;
557 }
558 else if (doubleValue != nullptr)
559 {
560 temp = *doubleValue;
561 }
562 else
563 {
564 BMCWEB_LOG_ERROR
565 << "Got value interface that wasn't int or double";
566 continue;
567 }
568 temp = temp * std::pow(10, scaleMultiplier);
569 if (forceToInt)
570 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000571 valueIt = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700572 }
573 else
574 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000575 valueIt = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700576 }
577 }
578 }
579 }
580 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100581}
582
James Feist8bd25cc2019-03-15 15:14:00 -0700583static void
584 populateFanRedundancy(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp)
585{
586 crow::connections::systemBus->async_method_call(
587 [sensorsAsyncResp](const boost::system::error_code ec,
588 const GetSubTreeType& resp) {
589 if (ec)
590 {
591 return; // don't have to have this interface
592 }
Ed Tanouse278c182019-03-13 16:23:37 -0700593 for (const std::pair<std::string,
594 std::vector<std::pair<
595 std::string, std::vector<std::string>>>>&
596 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -0700597 {
Ed Tanouse278c182019-03-13 16:23:37 -0700598 const std::string& path = pathPair.first;
599 const std::vector<
600 std::pair<std::string, std::vector<std::string>>>& objDict =
601 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -0700602 if (objDict.empty())
603 {
604 continue; // this should be impossible
605 }
606
607 const std::string& owner = objDict.begin()->first;
608 crow::connections::systemBus->async_method_call(
609 [path, owner,
610 sensorsAsyncResp](const boost::system::error_code ec,
611 std::variant<std::vector<std::string>>
612 variantEndpoints) {
613 if (ec)
614 {
615 return; // if they don't have an association we
616 // can't tell what chassis is
617 }
618 // verify part of the right chassis
619 auto endpoints = std::get_if<std::vector<std::string>>(
620 &variantEndpoints);
621
622 if (endpoints == nullptr)
623 {
624 BMCWEB_LOG_ERROR << "Invalid association interface";
625 messages::internalError(sensorsAsyncResp->res);
626 return;
627 }
628
629 auto found = std::find_if(
630 endpoints->begin(), endpoints->end(),
631 [sensorsAsyncResp](const std::string& entry) {
632 return entry.find(
633 sensorsAsyncResp->chassisId) !=
634 std::string::npos;
635 });
636
637 if (found == endpoints->end())
638 {
639 return;
640 }
641 crow::connections::systemBus->async_method_call(
642 [path, sensorsAsyncResp](
643 const boost::system::error_code ec,
644 const boost::container::flat_map<
645 std::string,
646 std::variant<uint8_t,
647 std::vector<std::string>,
648 std::string>>& ret) {
649 if (ec)
650 {
651 return; // don't have to have this
652 // interface
653 }
654 auto findFailures = ret.find("AllowedFailures");
655 auto findCollection = ret.find("Collection");
656 auto findStatus = ret.find("Status");
657
658 if (findFailures == ret.end() ||
659 findCollection == ret.end() ||
660 findStatus == ret.end())
661 {
662 BMCWEB_LOG_ERROR
663 << "Invalid redundancy interface";
664 messages::internalError(
665 sensorsAsyncResp->res);
666 return;
667 }
668
669 auto allowedFailures = std::get_if<uint8_t>(
670 &(findFailures->second));
671 auto collection =
672 std::get_if<std::vector<std::string>>(
673 &(findCollection->second));
674 auto status = std::get_if<std::string>(
675 &(findStatus->second));
676
677 if (allowedFailures == nullptr ||
678 collection == nullptr || status == nullptr)
679 {
680
681 BMCWEB_LOG_ERROR
682 << "Invalid redundancy interface "
683 "types";
684 messages::internalError(
685 sensorsAsyncResp->res);
686 return;
687 }
688 size_t lastSlash = path.rfind("/");
689 if (lastSlash == std::string::npos)
690 {
691 // this should be impossible
692 messages::internalError(
693 sensorsAsyncResp->res);
694 return;
695 }
696 std::string name = path.substr(lastSlash + 1);
697 std::replace(name.begin(), name.end(), '_',
698 ' ');
699
700 std::string health;
701
702 if (boost::ends_with(*status, "Full"))
703 {
704 health = "OK";
705 }
706 else if (boost::ends_with(*status, "Degraded"))
707 {
708 health = "Warning";
709 }
710 else
711 {
712 health = "Critical";
713 }
714 std::vector<nlohmann::json> redfishCollection;
715 const auto& fanRedfish =
716 sensorsAsyncResp->res.jsonValue["Fans"];
717 for (const std::string& item : *collection)
718 {
719 lastSlash = item.rfind("/");
720 // make a copy as collection is const
721 std::string itemName =
722 item.substr(lastSlash + 1);
723 /*
724 todo(ed): merge patch that fixes the names
725 std::replace(itemName.begin(),
726 itemName.end(), '_', ' ');*/
727 auto schemaItem = std::find_if(
728 fanRedfish.begin(), fanRedfish.end(),
729 [itemName](const nlohmann::json& fan) {
730 return fan["MemberId"] == itemName;
731 });
732 if (schemaItem != fanRedfish.end())
733 {
734 redfishCollection.push_back(
735 {{"@odata.id",
736 (*schemaItem)["@odata.id"]}});
737 }
738 else
739 {
740 BMCWEB_LOG_ERROR
741 << "failed to find fan in schema";
742 messages::internalError(
743 sensorsAsyncResp->res);
744 return;
745 }
746 }
747
748 auto& resp = sensorsAsyncResp->res
749 .jsonValue["Redundancy"];
750 resp.push_back(
751 {{"@odata.id",
752 "/refish/v1/Chassis/" +
753 sensorsAsyncResp->chassisId + "/" +
754 sensorsAsyncResp->chassisSubNode +
755 "#/Redundancy/" +
756 std::to_string(resp.size())},
757 {"@odata.type",
758 "#Redundancy.v1_3_2.Redundancy"},
759 {"MinNumNeeded",
760 collection->size() - *allowedFailures},
761 {"MemberId", name},
762 {"Mode", "N+m"},
763 {"Name", name},
764 {"RedundancySet", redfishCollection},
765 {"Status",
766 {{"Health", health},
767 {"State", "Enabled"}}}});
768 },
769 owner, path, "org.freedesktop.DBus.Properties",
770 "GetAll",
771 "xyz.openbmc_project.Control.FanRedundancy");
772 },
773 "xyz.openbmc_project.ObjectMapper", path + "/inventory",
774 "org.freedesktop.DBus.Properties", "Get",
775 "xyz.openbmc_project.Association", "endpoints");
776 }
777 },
778 "xyz.openbmc_project.ObjectMapper",
779 "/xyz/openbmc_project/object_mapper",
780 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
781 "/xyz/openbmc_project/control", 2,
782 std::array<const char*, 1>{
783 "xyz.openbmc_project.Control.FanRedundancy"});
784}
785
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700786void sortJSONResponse(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
787{
788 nlohmann::json& response = SensorsAsyncResp->res.jsonValue;
789 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
790 if (SensorsAsyncResp->chassisSubNode == "Power")
791 {
792 sensorHeaders = {"Voltages", "PowerSupplies"};
793 }
794 for (const std::string& sensorGroup : sensorHeaders)
795 {
796 nlohmann::json::iterator entry = response.find(sensorGroup);
797 if (entry != response.end())
798 {
799 std::sort(entry->begin(), entry->end(),
800 [](nlohmann::json& c1, nlohmann::json& c2) {
801 return c1["Name"] < c2["Name"];
802 });
803
804 // add the index counts to the end of each entry
805 size_t count = 0;
806 for (nlohmann::json& sensorJson : *entry)
807 {
808 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
809 if (odata == sensorJson.end())
810 {
811 continue;
812 }
813 std::string* value = odata->get_ptr<std::string*>();
814 if (value != nullptr)
815 {
816 *value += std::to_string(count);
817 count++;
818 }
819 }
820 }
821 }
822}
823
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100824/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600825 * @brief Gets the values of the specified sensors.
826 *
827 * Stores the results as JSON in the SensorsAsyncResp.
828 *
829 * Gets the sensor values asynchronously. Stores the results later when the
830 * information has been obtained.
831 *
832 * The sensorNames set contains all sensors for the current chassis.
833 * SensorsAsyncResp contains the requested sensor types. Only sensors of a
834 * requested type are included in the JSON output.
835 *
836 * To minimize the number of DBus calls, the DBus method
837 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
838 * values of all sensors provided by a connection (service).
839 *
840 * The connections set contains all the connections that provide sensor values.
841 *
842 * The objectMgrPaths map contains mappings from a connection name to the
843 * corresponding DBus object path that implements ObjectManager.
844 *
845 * @param SensorsAsyncResp Pointer to object holding response data.
846 * @param sensorNames All sensors within the current chassis.
847 * @param connections Connections that provide sensor values.
848 * @param objectMgrPaths Mappings from connection name to DBus object path that
849 * implements ObjectManager.
850 */
851void getSensorData(
852 std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700853 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -0600854 const boost::container::flat_set<std::string>& connections,
855 const boost::container::flat_map<std::string, std::string>& objectMgrPaths)
856{
857 BMCWEB_LOG_DEBUG << "getSensorData enter";
858 // Get managed objects from all services exposing sensors
859 for (const std::string& connection : connections)
860 {
861 // Response handler to process managed objects
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700862 auto getManagedObjectsCb = [SensorsAsyncResp, sensorNames](
Shawn McCarneyde629b62019-03-08 10:42:51 -0600863 const boost::system::error_code ec,
864 ManagedObjectsVectorType& resp) {
865 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
866 if (ec)
867 {
868 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
869 messages::internalError(SensorsAsyncResp->res);
870 return;
871 }
872 // Go through all objects and update response with sensor data
873 for (const auto& objDictEntry : resp)
874 {
875 const std::string& objPath =
876 static_cast<const std::string&>(objDictEntry.first);
877 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
878 << objPath;
879
Shawn McCarneyde629b62019-03-08 10:42:51 -0600880 std::vector<std::string> split;
881 // Reserve space for
882 // /xyz/openbmc_project/sensors/<name>/<subname>
883 split.reserve(6);
884 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
885 if (split.size() < 6)
886 {
887 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
888 << objPath;
889 continue;
890 }
891 // These indexes aren't intuitive, as boost::split puts an empty
892 // string at the beginning
893 const std::string& sensorType = split[4];
894 const std::string& sensorName = split[5];
895 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
896 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700897 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -0600898 {
899 BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
900 continue;
901 }
902
903 const char* fieldName = nullptr;
904 if (sensorType == "temperature")
905 {
906 fieldName = "Temperatures";
907 }
908 else if (sensorType == "fan" || sensorType == "fan_tach" ||
909 sensorType == "fan_pwm")
910 {
911 fieldName = "Fans";
912 }
913 else if (sensorType == "voltage")
914 {
915 fieldName = "Voltages";
916 }
917 else if (sensorType == "current")
918 {
919 fieldName = "PowerSupplies";
920 }
921 else if (sensorType == "power")
922 {
923 fieldName = "PowerSupplies";
924 }
925 else
926 {
927 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
928 << sensorType;
929 continue;
930 }
931
932 nlohmann::json& tempArray =
933 SensorsAsyncResp->res.jsonValue[fieldName];
934
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700935 if (fieldName == "PowerSupplies" && !tempArray.empty())
936 {
937 // Power supplies put multiple "sensors" into a single power
938 // supply entry, so only create the first one
939 }
940 else
941 {
942 tempArray.push_back(
943 {{"@odata.id", "/redfish/v1/Chassis/" +
944 SensorsAsyncResp->chassisId + "/" +
945 SensorsAsyncResp->chassisSubNode +
946 "#/" + fieldName + "/"}});
947 }
Shawn McCarneyde629b62019-03-08 10:42:51 -0600948 nlohmann::json& sensorJson = tempArray.back();
949
950 objectInterfacesToJson(sensorName, sensorType,
951 objDictEntry.second, sensorJson);
952 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700953 if (SensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -0700954 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700955 sortJSONResponse(SensorsAsyncResp);
956 if (SensorsAsyncResp->chassisSubNode == "Thermal")
957 {
958 populateFanRedundancy(SensorsAsyncResp);
959 }
James Feist8bd25cc2019-03-15 15:14:00 -0700960 }
Shawn McCarneyde629b62019-03-08 10:42:51 -0600961 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
962 };
963
964 // Find DBus object path that implements ObjectManager for the current
965 // connection. If no mapping found, default to "/".
966 auto iter = objectMgrPaths.find(connection);
967 const std::string& objectMgrPath =
968 (iter != objectMgrPaths.end()) ? iter->second : "/";
969 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
970 << objectMgrPath;
971
972 crow::connections::systemBus->async_method_call(
973 getManagedObjectsCb, connection, objectMgrPath,
974 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
975 };
976 BMCWEB_LOG_DEBUG << "getSensorData exit";
977}
978
979/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100980 * @brief Entry point for retrieving sensors data related to requested
981 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200982 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100983 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700984void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
985{
986 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700987 auto getChassisCb =
988 [SensorsAsyncResp](
989 std::shared_ptr<boost::container::flat_set<std::string>>
990 sensorNames) {
991 BMCWEB_LOG_DEBUG << "getChassisCb enter";
992 auto getConnectionCb =
993 [SensorsAsyncResp,
994 sensorNames](const boost::container::flat_set<std::string>&
995 connections) {
996 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
997 auto getObjectManagerPathsCb =
998 [SensorsAsyncResp, sensorNames, connections](
999 const boost::container::flat_map<
1000 std::string, std::string>& objectMgrPaths) {
1001 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
1002 // Get sensor data and store results in JSON
1003 // response
1004 getSensorData(SensorsAsyncResp, sensorNames,
1005 connections, objectMgrPaths);
1006 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
1007 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001008
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001009 // Get mapping from connection names to the DBus object
1010 // paths that implement the ObjectManager interface
1011 getObjectManagerPaths(SensorsAsyncResp,
1012 std::move(getObjectManagerPathsCb));
1013 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
1014 };
Shawn McCarneyde629b62019-03-08 10:42:51 -06001015
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001016 // Get set of connections that provide sensor values
1017 getConnections(SensorsAsyncResp, sensorNames,
1018 std::move(getConnectionCb));
1019 BMCWEB_LOG_DEBUG << "getChassisCb exit";
1020 };
Jennifer Lee4f9a2132019-03-04 12:45:19 -08001021 SensorsAsyncResp->res.jsonValue["Redundancy"] = nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001022
Shawn McCarney26f03892019-05-03 13:20:24 -05001023 // Get set of sensors in chassis
Ed Tanous1abe55e2018-09-05 08:30:59 -07001024 getChassis(SensorsAsyncResp, std::move(getChassisCb));
1025 BMCWEB_LOG_DEBUG << "getChassisData exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001026};
1027
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301028/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001029 * @brief Find the requested sensorName in the list of all sensors supplied by
1030 * the chassis node
1031 *
1032 * @param sensorName The sensor name supplied in the PATCH request
1033 * @param sensorsList The list of sensors managed by the chassis node
1034 * @param sensorsModified The list of sensors that were found as a result of
1035 * repeated calls to this function
1036 */
1037bool findSensorNameUsingSensorPath(
1038 const std::string& sensorName,
1039 boost::container::flat_set<std::string>& sensorsList,
1040 boost::container::flat_set<std::string>& sensorsModified)
1041{
1042 for (const std::string& chassisSensor : sensorsList)
1043 {
1044 std::string thisSensorName;
1045 if (!dbus::utility::getNthStringFromPath(chassisSensor, 5,
1046 thisSensorName))
1047 {
1048 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
1049 << chassisSensor;
1050 continue;
1051 }
1052 if (thisSensorName == sensorName)
1053 {
1054 sensorsModified.emplace(chassisSensor);
1055 return true;
1056 }
1057 }
1058 return false;
1059}
1060
1061/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301062 * @brief Entry point for overriding sensor values of given sensor
1063 *
1064 * @param res response object
1065 * @param req request object
1066 * @param params parameter passed for CRUD
1067 * @param typeList TypeList of sensors for the resource queried
1068 * @param chassisSubNode Chassis Node for which the query has to happen
1069 */
1070void setSensorOverride(crow::Response& res, const crow::Request& req,
1071 const std::vector<std::string>& params,
Ed Tanousb01bf292019-03-25 19:25:26 +00001072 const std::initializer_list<const char*> typeList,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301073 const std::string& chassisSubNode)
1074{
1075
1076 // TODO: Need to figure out dynamic way to restrict patch (Set Sensor
1077 // override) based on another d-bus announcement to be more generic.
1078 if (params.size() != 1)
1079 {
1080 messages::internalError(res);
1081 res.end();
1082 return;
1083 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301084
1085 std::unordered_map<std::string, std::vector<nlohmann::json>> allCollections;
1086 std::optional<std::vector<nlohmann::json>> temperatureCollections;
1087 std::optional<std::vector<nlohmann::json>> fanCollections;
1088 std::vector<nlohmann::json> voltageCollections;
1089 BMCWEB_LOG_INFO << "setSensorOverride for subNode" << chassisSubNode
1090 << "\n";
1091
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301092 if (chassisSubNode == "Thermal")
1093 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301094 if (!json_util::readJson(req, res, "Temperatures",
1095 temperatureCollections, "Fans",
1096 fanCollections))
1097 {
1098 return;
1099 }
1100 if (!temperatureCollections && !fanCollections)
1101 {
1102 messages::resourceNotFound(res, "Thermal",
1103 "Temperatures / Voltages");
1104 res.end();
1105 return;
1106 }
1107 if (temperatureCollections)
1108 {
1109 allCollections.emplace("Temperatures",
1110 *std::move(temperatureCollections));
1111 }
1112 if (fanCollections)
1113 {
1114 allCollections.emplace("Fans", *std::move(fanCollections));
1115 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301116 }
1117 else if (chassisSubNode == "Power")
1118 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301119 if (!json_util::readJson(req, res, "Voltages", voltageCollections))
1120 {
1121 return;
1122 }
1123 allCollections.emplace("Voltages", std::move(voltageCollections));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301124 }
1125 else
1126 {
1127 res.result(boost::beast::http::status::not_found);
1128 res.end();
1129 return;
1130 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301131
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301132 const char* propertyValueName;
1133 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301134 std::string memberId;
1135 double value;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301136 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301137 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301138 if (collectionItems.first == "Temperatures")
1139 {
1140 propertyValueName = "ReadingCelsius";
1141 }
1142 else if (collectionItems.first == "Fans")
1143 {
1144 propertyValueName = "Reading";
1145 }
1146 else
1147 {
1148 propertyValueName = "ReadingVolts";
1149 }
1150 for (auto& item : collectionItems.second)
1151 {
1152 if (!json_util::readJson(item, res, "MemberId", memberId,
1153 propertyValueName, value))
1154 {
1155 return;
1156 }
1157 overrideMap.emplace(memberId,
1158 std::make_pair(value, collectionItems.first));
1159 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301160 }
1161 const std::string& chassisName = params[0];
1162 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
1163 res, chassisName, typeList, chassisSubNode);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001164 auto getChassisSensorListCb = [sensorAsyncResp,
1165 overrideMap](const std::shared_ptr<
1166 boost::container::flat_set<
1167 std::string>>
1168 sensorsList) {
1169 // Match sensor names in the PATCH request to those managed by the
1170 // chassis node
1171 const std::shared_ptr<boost::container::flat_set<std::string>>
1172 sensorNames =
1173 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301174 for (const auto& item : overrideMap)
1175 {
1176 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001177 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
1178 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301179 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301180 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301181 messages::resourceNotFound(sensorAsyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301182 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301183 return;
1184 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301185 }
1186 // Get the connection to which the memberId belongs
1187 auto getObjectsWithConnectionCb =
1188 [sensorAsyncResp, overrideMap](
1189 const boost::container::flat_set<std::string>& connections,
1190 const std::set<std::pair<std::string, std::string>>&
1191 objectsWithConnection) {
1192 if (objectsWithConnection.size() != overrideMap.size())
1193 {
1194 BMCWEB_LOG_INFO
1195 << "Unable to find all objects with proper connection "
1196 << objectsWithConnection.size() << " requested "
1197 << overrideMap.size() << "\n";
1198 messages::resourceNotFound(
1199 sensorAsyncResp->res,
1200 sensorAsyncResp->chassisSubNode == "Thermal"
1201 ? "Temperatures"
1202 : "Voltages",
1203 "Count");
1204 return;
1205 }
1206 for (const auto& item : objectsWithConnection)
1207 {
1208
1209 auto lastPos = item.first.rfind('/');
1210 if (lastPos == std::string::npos)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301211 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301212 messages::internalError(sensorAsyncResp->res);
1213 return;
1214 }
1215 std::string sensorName = item.first.substr(lastPos + 1);
1216
1217 const auto& iterator = overrideMap.find(sensorName);
1218 if (iterator == overrideMap.end())
1219 {
1220 BMCWEB_LOG_INFO << "Unable to find sensor object"
1221 << item.first << "\n";
1222 messages::internalError(sensorAsyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301223 return;
1224 }
1225 crow::connections::systemBus->async_method_call(
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301226 [sensorAsyncResp](const boost::system::error_code ec) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301227 if (ec)
1228 {
1229 BMCWEB_LOG_DEBUG
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301230 << "setOverrideValueStatus DBUS error: "
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301231 << ec;
1232 messages::internalError(sensorAsyncResp->res);
1233 return;
1234 }
1235 },
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301236 item.second, item.first,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301237 "org.freedesktop.DBus.Properties", "Set",
1238 "xyz.openbmc_project.Sensor.Value", "Value",
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05301239 sdbusplus::message::variant<double>(
1240 iterator->second.first));
1241 }
1242 };
1243 // Get object with connection for the given sensor name
1244 getObjectsWithConnection(sensorAsyncResp, sensorNames,
1245 std::move(getObjectsWithConnectionCb));
1246 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05301247 // get full sensor list for the given chassisId and cross verify the sensor.
1248 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
1249}
1250
Ed Tanous1abe55e2018-09-05 08:30:59 -07001251} // namespace redfish