xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 5f7d88c43ca389d0f053e0ce1aa3f3fc70f5f712)
1 /*
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>
19 
20 #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>
24 #include <dbus_singleton.hpp>
25 
26 namespace redfish
27 {
28 
29 constexpr const char* dbusSensorPrefix = "/xyz/openbmc_project/sensors/";
30 
31 using GetSubTreeType = std::vector<
32     std::pair<std::string,
33               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
34 
35 using SensorVariant = sdbusplus::message::variant<int64_t, double>;
36 
37 using ManagedObjectsVectorType = std::vector<std::pair<
38     sdbusplus::message::object_path,
39     boost::container::flat_map<
40         std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
41 
42 /**
43  * SensorsAsyncResp
44  * Gathers data needed for response processing after async calls are done
45  */
46 class SensorsAsyncResp
47 {
48   public:
49     SensorsAsyncResp(crow::Response& response, const std::string& chassisId,
50                      const std::initializer_list<const char*> types,
51                      const std::string& subNode) :
52         chassisId(chassisId),
53         res(response), types(types), chassisSubNode(subNode)
54     {
55         res.jsonValue["@odata.id"] =
56             "/redfish/v1/Chassis/" + chassisId + "/Thermal";
57     }
58 
59     ~SensorsAsyncResp()
60     {
61         if (res.result() == boost::beast::http::status::internal_server_error)
62         {
63             // Reset the json object to clear out any data that made it in
64             // before the error happened todo(ed) handle error condition with
65             // proper code
66             res.jsonValue = nlohmann::json::object();
67         }
68         res.end();
69     }
70 
71     crow::Response& res;
72     std::string chassisId{};
73     const std::vector<const char*> types;
74     std::string chassisSubNode{};
75 };
76 
77 /**
78  * @brief Creates connections necessary for chassis sensors
79  * @param SensorsAsyncResp Pointer to object holding response data
80  * @param sensorNames Sensors retrieved from chassis
81  * @param callback Callback for processing gathered connections
82  */
83 template <typename Callback>
84 void getConnections(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
85                     const boost::container::flat_set<std::string>& sensorNames,
86                     Callback&& callback)
87 {
88     BMCWEB_LOG_DEBUG << "getConnections enter";
89     const std::string path = "/xyz/openbmc_project/sensors";
90     const std::array<std::string, 1> interfaces = {
91         "xyz.openbmc_project.Sensor.Value"};
92 
93     // Response handler for parsing objects subtree
94     auto respHandler = [callback{std::move(callback)}, SensorsAsyncResp,
95                         sensorNames](const boost::system::error_code ec,
96                                      const GetSubTreeType& subtree) {
97         BMCWEB_LOG_DEBUG << "getConnections resp_handler enter";
98         if (ec)
99         {
100             messages::internalError(SensorsAsyncResp->res);
101             BMCWEB_LOG_ERROR << "getConnections resp_handler: Dbus error "
102                              << ec;
103             return;
104         }
105 
106         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
107 
108         // Make unique list of connections only for requested sensor types and
109         // found in the chassis
110         boost::container::flat_set<std::string> connections;
111         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
112         // producers
113         connections.reserve(8);
114 
115         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames.size();
116         for (const std::string& tsensor : sensorNames)
117         {
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         {
126             for (const char* type : SensorsAsyncResp->types)
127             {
128                 if (boost::starts_with(object.first, type))
129                 {
130                     auto lastPos = object.first.rfind('/');
131                     if (lastPos != std::string::npos)
132                     {
133                         std::string sensorName =
134                             object.first.substr(lastPos + 1);
135 
136                         if (sensorNames.find(sensorName) != sensorNames.end())
137                         {
138                             // For each Connection name
139                             for (const std::pair<std::string,
140                                                  std::vector<std::string>>&
141                                      objData : object.second)
142                             {
143                                 BMCWEB_LOG_DEBUG << "Adding connection: "
144                                                  << objData.first;
145                                 connections.insert(objData.first);
146                             }
147                         }
148                     }
149                     break;
150                 }
151             }
152         }
153         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
154         callback(std::move(connections));
155         BMCWEB_LOG_DEBUG << "getConnections resp_handler exit";
156     };
157 
158     // Make call to ObjectMapper to find all sensors objects
159     crow::connections::systemBus->async_method_call(
160         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
161         "/xyz/openbmc_project/object_mapper",
162         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
163     BMCWEB_LOG_DEBUG << "getConnections exit";
164 }
165 
166 /**
167  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
168  * @param SensorsAsyncResp   Pointer to object holding response data
169  * @param callback  Callback for next step in gathered sensor processing
170  */
171 template <typename Callback>
172 void getChassis(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp,
173                 Callback&& callback)
174 {
175     BMCWEB_LOG_DEBUG << "getChassis enter";
176     // Process response from EntityManager and extract chassis data
177     auto respHandler = [callback{std::move(callback)},
178                         SensorsAsyncResp](const boost::system::error_code ec,
179                                           ManagedObjectsVectorType& resp) {
180         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
181         if (ec)
182         {
183             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
184             messages::internalError(SensorsAsyncResp->res);
185             return;
186         }
187         boost::container::flat_set<std::string> sensorNames;
188 
189         //   SensorsAsyncResp->chassisId
190         bool foundChassis = false;
191         std::vector<std::string> split;
192         // Reserve space for
193         // /xyz/openbmc_project/inventory/<name>/<subname> + 3 subnames
194         split.reserve(8);
195 
196         for (const auto& objDictEntry : resp)
197         {
198             const std::string& objectPath =
199                 static_cast<const std::string&>(objDictEntry.first);
200             boost::algorithm::split(split, objectPath, boost::is_any_of("/"));
201             if (split.size() < 2)
202             {
203                 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
204                                  << objectPath;
205                 split.clear();
206                 continue;
207             }
208             const std::string& sensorName = split.end()[-1];
209             const std::string& chassisName = split.end()[-2];
210 
211             if (chassisName != SensorsAsyncResp->chassisId)
212             {
213                 split.clear();
214                 continue;
215             }
216             BMCWEB_LOG_DEBUG << "New sensor: " << sensorName;
217             foundChassis = true;
218             sensorNames.emplace(sensorName);
219             split.clear();
220         };
221         BMCWEB_LOG_DEBUG << "Found " << sensorNames.size() << " Sensor names";
222 
223         if (!foundChassis)
224         {
225             BMCWEB_LOG_INFO << "Unable to find chassis named "
226                             << SensorsAsyncResp->chassisId;
227             messages::resourceNotFound(SensorsAsyncResp->res, "Chassis",
228                                        SensorsAsyncResp->chassisId);
229         }
230         else
231         {
232             callback(sensorNames);
233         }
234         BMCWEB_LOG_DEBUG << "getChassis respHandler exit";
235     };
236 
237     // Make call to EntityManager to find all chassis objects
238     crow::connections::systemBus->async_method_call(
239         respHandler, "xyz.openbmc_project.EntityManager", "/",
240         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
241     BMCWEB_LOG_DEBUG << "getChassis exit";
242 }
243 
244 /**
245  * @brief Builds a json sensor representation of a sensor.
246  * @param sensorName  The name of the sensor to be built
247  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
248  * build
249  * @param interfacesDict  A dictionary of the interfaces and properties of said
250  * interfaces to be built from
251  * @param sensor_json  The json object to fill
252  */
253 void objectInterfacesToJson(
254     const std::string& sensorName, const std::string& sensorType,
255     const boost::container::flat_map<
256         std::string, boost::container::flat_map<std::string, SensorVariant>>&
257         interfacesDict,
258     nlohmann::json& sensor_json)
259 {
260     // We need a value interface before we can do anything with it
261     auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
262     if (valueIt == interfacesDict.end())
263     {
264         BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
265         return;
266     }
267 
268     // Assume values exist as is (10^0 == 1) if no scale exists
269     int64_t scaleMultiplier = 0;
270 
271     auto scaleIt = valueIt->second.find("Scale");
272     // If a scale exists, pull value as int64, and use the scaling.
273     if (scaleIt != valueIt->second.end())
274     {
275         const int64_t* int64Value =
276             mapbox::getPtr<const int64_t>(scaleIt->second);
277         if (int64Value != nullptr)
278         {
279             scaleMultiplier = *int64Value;
280         }
281     }
282 
283     sensor_json["MemberId"] = sensorName;
284     sensor_json["Name"] = sensorName;
285     sensor_json["Status"]["State"] = "Enabled";
286     sensor_json["Status"]["Health"] = "OK";
287 
288     // Parameter to set to override the type we get from dbus, and force it to
289     // int, regardless of what is available.  This is used for schemas like fan,
290     // that require integers, not floats.
291     bool forceToInt = false;
292 
293     const char* unit = "Reading";
294     if (sensorType == "temperature")
295     {
296         unit = "ReadingCelsius";
297         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature";
298         // TODO(ed) Documentation says that path should be type fan_tach,
299         // implementation seems to implement fan
300     }
301     else if (sensorType == "fan" || sensorType == "fan_tach")
302     {
303         unit = "Reading";
304         sensor_json["ReadingUnits"] = "RPM";
305         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
306         forceToInt = true;
307     }
308     else if (sensorType == "fan_pwm")
309     {
310         unit = "Reading";
311         sensor_json["ReadingUnits"] = "Percent";
312         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
313         forceToInt = true;
314     }
315     else if (sensorType == "voltage")
316     {
317         unit = "ReadingVolts";
318         sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage";
319     }
320     else if (sensorType == "power")
321     {
322         unit = "LastPowerOutputWatts";
323     }
324     else
325     {
326         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
327         return;
328     }
329     // Map of dbus interface name, dbus property name and redfish property_name
330     std::vector<std::tuple<const char*, const char*, const char*>> properties;
331     properties.reserve(7);
332 
333     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
334     properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
335                             "WarningHigh", "UpperThresholdNonCritical");
336     properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
337                             "WarningLow", "LowerThresholdNonCritical");
338     properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
339                             "CriticalHigh", "UpperThresholdCritical");
340     properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
341                             "CriticalLow", "LowerThresholdCritical");
342 
343     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
344 
345     if (sensorType == "temperature")
346     {
347         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
348                                 "MinReadingRangeTemp");
349         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
350                                 "MaxReadingRangeTemp");
351     }
352     else
353     {
354         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
355                                 "MinReadingRange");
356         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
357                                 "MaxReadingRange");
358     }
359 
360     for (const std::tuple<const char*, const char*, const char*>& p :
361          properties)
362     {
363         auto interfaceProperties = interfacesDict.find(std::get<0>(p));
364         if (interfaceProperties != interfacesDict.end())
365         {
366             auto valueIt = interfaceProperties->second.find(std::get<1>(p));
367             if (valueIt != interfaceProperties->second.end())
368             {
369                 const SensorVariant& valueVariant = valueIt->second;
370                 nlohmann::json& valueIt = sensor_json[std::get<2>(p)];
371                 // Attempt to pull the int64 directly
372                 const int64_t* int64Value =
373                     mapbox::getPtr<const int64_t>(valueVariant);
374 
375                 const double* doubleValue =
376                     mapbox::getPtr<const double>(valueVariant);
377                 double temp = 0.0;
378                 if (int64Value != nullptr)
379                 {
380                     temp = *int64Value;
381                 }
382                 else if (doubleValue != nullptr)
383                 {
384                     temp = *doubleValue;
385                 }
386                 else
387                 {
388                     BMCWEB_LOG_ERROR
389                         << "Got value interface that wasn't int or double";
390                     continue;
391                 }
392                 temp = temp * std::pow(10, scaleMultiplier);
393                 if (forceToInt)
394                 {
395                     valueIt = static_cast<int64_t>(temp);
396                 }
397                 else
398                 {
399                     valueIt = temp;
400                 }
401             }
402         }
403     }
404     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
405 }
406 
407 /**
408  * @brief Entry point for retrieving sensors data related to requested
409  *        chassis.
410  * @param SensorsAsyncResp   Pointer to object holding response data
411  */
412 void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp)
413 {
414     BMCWEB_LOG_DEBUG << "getChassisData enter";
415     auto getChassisCb = [&, SensorsAsyncResp](
416                             boost::container::flat_set<std::string>&
417                                 sensorNames) {
418         BMCWEB_LOG_DEBUG << "getChassisCb enter";
419         auto getConnectionCb =
420             [&, SensorsAsyncResp, sensorNames](
421                 const boost::container::flat_set<std::string>& connections) {
422                 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
423                 // Get managed objects from all services exposing sensors
424                 for (const std::string& connection : connections)
425                 {
426                     // Response handler to process managed objects
427                     auto getManagedObjectsCb =
428                         [&, SensorsAsyncResp,
429                          sensorNames](const boost::system::error_code ec,
430                                       ManagedObjectsVectorType& resp) {
431                             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
432                             if (ec)
433                             {
434                                 BMCWEB_LOG_ERROR
435                                     << "getManagedObjectsCb DBUS error: " << ec;
436                                 messages::internalError(SensorsAsyncResp->res);
437                                 return;
438                             }
439                             // Go through all objects and update response with
440                             // sensor data
441                             for (const auto& objDictEntry : resp)
442                             {
443                                 const std::string& objPath =
444                                     static_cast<const std::string&>(
445                                         objDictEntry.first);
446                                 BMCWEB_LOG_DEBUG
447                                     << "getManagedObjectsCb parsing object "
448                                     << objPath;
449 
450                                 std::vector<std::string> split;
451                                 // Reserve space for
452                                 // /xyz/openbmc_project/sensors/<name>/<subname>
453                                 split.reserve(6);
454                                 boost::algorithm::split(split, objPath,
455                                                         boost::is_any_of("/"));
456                                 if (split.size() < 6)
457                                 {
458                                     BMCWEB_LOG_ERROR
459                                         << "Got path that isn't long enough "
460                                         << objPath;
461                                     continue;
462                                 }
463                                 // These indexes aren't intuitive, as
464                                 // boost::split puts an empty string at the
465                                 // beggining
466                                 const std::string& sensorType = split[4];
467                                 const std::string& sensorName = split[5];
468                                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
469                                                  << " sensorType "
470                                                  << sensorType;
471                                 if (sensorNames.find(sensorName) ==
472                                     sensorNames.end())
473                                 {
474                                     BMCWEB_LOG_ERROR << sensorName
475                                                      << " not in sensor list ";
476                                     continue;
477                                 }
478 
479                                 const char* fieldName = nullptr;
480                                 if (sensorType == "temperature")
481                                 {
482                                     fieldName = "Temperatures";
483                                 }
484                                 else if (sensorType == "fan" ||
485                                          sensorType == "fan_tach" ||
486                                          sensorType == "fan_pwm")
487                                 {
488                                     fieldName = "Fans";
489                                 }
490                                 else if (sensorType == "voltage")
491                                 {
492                                     fieldName = "Voltages";
493                                 }
494                                 else if (sensorType == "current")
495                                 {
496                                     fieldName = "PowerSupply";
497                                 }
498                                 else if (sensorType == "power")
499                                 {
500                                     fieldName = "PowerSupply";
501                                 }
502                                 else
503                                 {
504                                     BMCWEB_LOG_ERROR
505                                         << "Unsure how to handle sensorType "
506                                         << sensorType;
507                                     continue;
508                                 }
509 
510                                 nlohmann::json& tempArray =
511                                     SensorsAsyncResp->res.jsonValue[fieldName];
512 
513                                 tempArray.push_back(
514                                     {{"@odata.id",
515                                       "/redfish/v1/Chassis/" +
516                                           SensorsAsyncResp->chassisId + "/" +
517                                           SensorsAsyncResp->chassisSubNode +
518                                           "#/" + fieldName + "/" +
519                                           std::to_string(tempArray.size())}});
520                                 nlohmann::json& sensorJson = tempArray.back();
521 
522                                 objectInterfacesToJson(sensorName, sensorType,
523                                                        objDictEntry.second,
524                                                        sensorJson);
525                             }
526                             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
527                         };
528                     crow::connections::systemBus->async_method_call(
529                         getManagedObjectsCb, connection, "/",
530                         "org.freedesktop.DBus.ObjectManager",
531                         "GetManagedObjects");
532                 };
533                 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
534             };
535         // get connections and then pass it to get sensors
536         getConnections(SensorsAsyncResp, sensorNames,
537                        std::move(getConnectionCb));
538         BMCWEB_LOG_DEBUG << "getChassisCb exit";
539     };
540 
541     // get chassis information related to sensors
542     getChassis(SensorsAsyncResp, std::move(getChassisCb));
543     BMCWEB_LOG_DEBUG << "getChassisData exit";
544 };
545 
546 } // namespace redfish
547