blob: 0c77ef711b4ccfc4b8917f0f88178804fa07edc6 [file] [log] [blame]
Willy Tude54f482021-01-26 15:59:09 -08001/*
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
17#include "dbus-sdr/sdrutils.hpp"
18
George Liuc1c7eac2024-02-04 17:24:19 +080019#include <ipmid/utils.hpp>
Johnathan Mantey04b0b072023-10-10 12:30:35 -070020#include <nlohmann/json.hpp>
George Liude6694e2024-07-17 15:22:25 +080021#include <phosphor-logging/lg2.hpp>
Johnathan Mantey04b0b072023-10-10 12:30:35 -070022
23#include <fstream>
Willy Tu4eca2512022-06-20 21:14:51 -070024#include <optional>
25#include <unordered_set>
26
Scron Chang2703b022021-07-06 15:47:45 +080027#ifdef FEATURE_HYBRID_SENSORS
28
29#include <ipmid/utils.hpp>
30namespace ipmi
31{
32namespace sensor
33{
34extern const IdInfoMap sensors;
35} // namespace sensor
36} // namespace ipmi
37
38#endif
39
Johnathan Mantey34d19732024-07-17 07:59:54 -070040boost::container::flat_map<
41 const char*, std::pair<SensorTypeCodes, SensorEventTypeCodes>, CmpStr>
42 sensorTypes{
43 {{"temperature", std::make_pair(SensorTypeCodes::temperature,
44 SensorEventTypeCodes::threshold)},
45 {"voltage", std::make_pair(SensorTypeCodes::voltage,
46 SensorEventTypeCodes::threshold)},
47 {"current", std::make_pair(SensorTypeCodes::current,
48 SensorEventTypeCodes::threshold)},
49 {"fan_tach", std::make_pair(SensorTypeCodes::fan,
50 SensorEventTypeCodes::threshold)},
51 {"fan_pwm", std::make_pair(SensorTypeCodes::fan,
52 SensorEventTypeCodes::threshold)},
53 {"intrusion", std::make_pair(SensorTypeCodes::physical_security,
54 SensorEventTypeCodes::sensorSpecified)},
55 {"processor", std::make_pair(SensorTypeCodes::processor,
56 SensorEventTypeCodes::sensorSpecified)},
57 {"power", std::make_pair(SensorTypeCodes::other,
58 SensorEventTypeCodes::threshold)},
59 {"memory", std::make_pair(SensorTypeCodes::memory,
60 SensorEventTypeCodes::sensorSpecified)},
61 {"state", std::make_pair(SensorTypeCodes::power_unit,
62 SensorEventTypeCodes::sensorSpecified)},
63 {"buttons", std::make_pair(SensorTypeCodes::buttons,
64 SensorEventTypeCodes::sensorSpecified)},
65 {"watchdog", std::make_pair(SensorTypeCodes::watchdog2,
66 SensorEventTypeCodes::sensorSpecified)},
67 {"entity", std::make_pair(SensorTypeCodes::entity,
68 SensorEventTypeCodes::sensorSpecified)},
69 {"energy", std::make_pair(SensorTypeCodes::other,
70 SensorEventTypeCodes::threshold)}}};
71
Willy Tude54f482021-01-26 15:59:09 -080072namespace details
73{
Johnathan Mantey04b0b072023-10-10 12:30:35 -070074
75// IPMI supports a smaller number of sensors than are available via Redfish.
76// Trim the list of sensors, via a configuration file.
77// Read the IPMI Sensor Filtering section in docs/configuration.md for
78// a more detailed description.
79static void filterSensors(SensorSubTree& subtree)
80{
81 constexpr const char* filterFilename =
82 "/usr/share/ipmi-providers/sensor_filter.json";
83 std::ifstream filterFile(filterFilename);
84 if (!filterFile.good())
85 {
86 return;
87 }
Patrick Williams1318a5e2024-08-16 15:19:54 -040088 nlohmann::json sensorFilterJSON =
89 nlohmann::json::parse(filterFile, nullptr, false);
Johnathan Mantey04b0b072023-10-10 12:30:35 -070090 nlohmann::json::iterator svcFilterit =
91 sensorFilterJSON.find("ServiceFilter");
92 if (svcFilterit == sensorFilterJSON.end())
93 {
94 return;
95 }
96
97 subtree.erase(std::remove_if(subtree.begin(), subtree.end(),
98 [svcFilterit](SensorSubTree::value_type& kv) {
Patrick Williams1318a5e2024-08-16 15:19:54 -040099 auto& [_, serviceToIfaces] = kv;
Johnathan Mantey04b0b072023-10-10 12:30:35 -0700100
Patrick Williams1318a5e2024-08-16 15:19:54 -0400101 for (auto service = svcFilterit->begin();
102 service != svcFilterit->end();
103 ++service)
104 {
105 serviceToIfaces.erase(*service);
106 }
107 return serviceToIfaces.empty();
108 }),
Johnathan Mantey04b0b072023-10-10 12:30:35 -0700109 subtree.end());
110}
111
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800112uint16_t getSensorSubtree(std::shared_ptr<SensorSubTree>& subtree)
Willy Tude54f482021-01-26 15:59:09 -0800113{
114 static std::shared_ptr<SensorSubTree> sensorTreePtr;
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800115 static uint16_t sensorUpdatedIndex = 0;
Willy Tude54f482021-01-26 15:59:09 -0800116 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Patrick Williams5d82f472022-07-22 19:26:53 -0500117 static sdbusplus::bus::match_t sensorAdded(
Willy Tude54f482021-01-26 15:59:09 -0800118 *dbus,
119 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
120 "sensors/'",
Patrick Williams5d82f472022-07-22 19:26:53 -0500121 [](sdbusplus::message_t&) { sensorTreePtr.reset(); });
Willy Tude54f482021-01-26 15:59:09 -0800122
Patrick Williams5d82f472022-07-22 19:26:53 -0500123 static sdbusplus::bus::match_t sensorRemoved(
Willy Tude54f482021-01-26 15:59:09 -0800124 *dbus,
125 "type='signal',member='InterfacesRemoved',arg0path='/xyz/"
126 "openbmc_project/sensors/'",
Patrick Williams5d82f472022-07-22 19:26:53 -0500127 [](sdbusplus::message_t&) { sensorTreePtr.reset(); });
Willy Tude54f482021-01-26 15:59:09 -0800128
Willy Tude54f482021-01-26 15:59:09 -0800129 if (sensorTreePtr)
130 {
131 subtree = sensorTreePtr;
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800132 return sensorUpdatedIndex;
Willy Tude54f482021-01-26 15:59:09 -0800133 }
134
135 sensorTreePtr = std::make_shared<SensorSubTree>();
136
Willy Tude54f482021-01-26 15:59:09 -0800137 static constexpr const int32_t depth = 2;
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800138
Patrick Williams369824e2023-10-20 11:18:23 -0500139 auto lbdUpdateSensorTree = [&dbus](const char* path,
140 const auto& interfaces) {
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800141 auto mapperCall = dbus->new_method_call(
142 "xyz.openbmc_project.ObjectMapper",
143 "/xyz/openbmc_project/object_mapper",
144 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
145 SensorSubTree sensorTreePartial;
146
147 mapperCall.append(path, depth, interfaces);
148
149 try
150 {
151 auto mapperReply = dbus->call(mapperCall);
152 mapperReply.read(sensorTreePartial);
153 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500154 catch (const sdbusplus::exception_t& e)
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800155 {
George Liude6694e2024-07-17 15:22:25 +0800156 lg2::error("Failed to update subtree, path: {PATH}, error: {ERROR}",
157 "PATH", path, "ERROR", e);
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800158 return false;
159 }
160 if constexpr (debug)
161 {
162 std::fprintf(stderr, "IPMI updated: %zu sensors under %s\n",
163 sensorTreePartial.size(), path);
164 }
165 sensorTreePtr->merge(std::move(sensorTreePartial));
166 return true;
167 };
168
169 // Add sensors to SensorTree
170 static constexpr const std::array sensorInterfaces = {
Willy Tude54f482021-01-26 15:59:09 -0800171 "xyz.openbmc_project.Sensor.Value",
Jie Yangf0a89942021-07-29 15:30:25 -0700172 "xyz.openbmc_project.Sensor.ValueMutability",
Willy Tude54f482021-01-26 15:59:09 -0800173 "xyz.openbmc_project.Sensor.Threshold.Warning",
174 "xyz.openbmc_project.Sensor.Threshold.Critical"};
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800175 static constexpr const std::array vrInterfaces = {
176 "xyz.openbmc_project.Control.VoltageRegulatorMode"};
Willy Tude54f482021-01-26 15:59:09 -0800177
Patrick Williams1318a5e2024-08-16 15:19:54 -0400178 bool sensorRez =
179 lbdUpdateSensorTree("/xyz/openbmc_project/sensors", sensorInterfaces);
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800180
Scron Chang2703b022021-07-06 15:47:45 +0800181#ifdef FEATURE_HYBRID_SENSORS
182
183 if (!ipmi::sensor::sensors.empty())
184 {
185 for (const auto& sensor : ipmi::sensor::sensors)
186 {
187 // Threshold sensors should not be emplaced in here.
188 if (boost::starts_with(sensor.second.sensorPath,
189 "/xyz/openbmc_project/sensors/"))
190 {
191 continue;
192 }
193
194 // The bus service name is not listed in ipmi::sensor::Info. Give it
195 // an empty string. For those function using non-threshold sensors,
196 // the bus service name will be retrieved in an alternative way.
197 boost::container::flat_map<std::string, std::vector<std::string>>
198 connectionMap{
199 {"", {sensor.second.propertyInterfaces.begin()->first}}};
200 sensorTreePtr->emplace(sensor.second.sensorPath, connectionMap);
201 }
202 }
203
204#endif
205
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800206 // Error if searching for sensors failed.
207 if (!sensorRez)
Willy Tude54f482021-01-26 15:59:09 -0800208 {
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800209 return sensorUpdatedIndex;
Willy Tude54f482021-01-26 15:59:09 -0800210 }
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800211
Johnathan Mantey04b0b072023-10-10 12:30:35 -0700212 filterSensors(*sensorTreePtr);
Hao Jiang9a5b51e2021-01-06 10:32:22 -0800213 // Add VR control as optional search path.
214 (void)lbdUpdateSensorTree("/xyz/openbmc_project/vr", vrInterfaces);
215
Willy Tude54f482021-01-26 15:59:09 -0800216 subtree = sensorTreePtr;
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800217 sensorUpdatedIndex++;
Josh Lehana55c9532020-10-28 21:59:06 -0700218 // The SDR is being regenerated, wipe the old stats
219 sdrStatsTable.wipeTable();
Jie Yangf0a89942021-07-29 15:30:25 -0700220 sdrWriteTable.wipeTable();
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800221 return sensorUpdatedIndex;
Willy Tude54f482021-01-26 15:59:09 -0800222}
223
224bool getSensorNumMap(std::shared_ptr<SensorNumMap>& sensorNumMap)
225{
226 static std::shared_ptr<SensorNumMap> sensorNumMapPtr;
227 bool sensorNumMapUpated = false;
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800228 static uint16_t prevSensorUpdatedIndex = 0;
Willy Tude54f482021-01-26 15:59:09 -0800229 std::shared_ptr<SensorSubTree> sensorTree;
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800230 uint16_t curSensorUpdatedIndex = details::getSensorSubtree(sensorTree);
Willy Tude54f482021-01-26 15:59:09 -0800231 if (!sensorTree)
232 {
233 return sensorNumMapUpated;
234 }
235
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800236 if ((curSensorUpdatedIndex == prevSensorUpdatedIndex) && sensorNumMapPtr)
Willy Tude54f482021-01-26 15:59:09 -0800237 {
238 sensorNumMap = sensorNumMapPtr;
239 return sensorNumMapUpated;
240 }
Kuiying Wanga8b5b262021-02-06 23:38:22 +0800241 prevSensorUpdatedIndex = curSensorUpdatedIndex;
Willy Tude54f482021-01-26 15:59:09 -0800242
243 sensorNumMapPtr = std::make_shared<SensorNumMap>();
244
245 uint16_t sensorNum = 0;
246 uint16_t sensorIndex = 0;
247 for (const auto& sensor : *sensorTree)
248 {
249 sensorNumMapPtr->insert(
250 SensorNumMap::value_type(sensorNum, sensor.first));
251 sensorIndex++;
252 if (sensorIndex == maxSensorsPerLUN)
253 {
254 sensorIndex = lun1Sensor0;
255 }
256 else if (sensorIndex == (lun1Sensor0 | maxSensorsPerLUN))
257 {
258 // Skip assigning LUN 0x2 any sensors
259 sensorIndex = lun3Sensor0;
260 }
261 else if (sensorIndex == (lun3Sensor0 | maxSensorsPerLUN))
262 {
263 // this is an error, too many IPMI sensors
264 throw std::out_of_range("Maximum number of IPMI sensors exceeded.");
265 }
266 sensorNum = sensorIndex;
267 }
268 sensorNumMap = sensorNumMapPtr;
269 sensorNumMapUpated = true;
270 return sensorNumMapUpated;
271}
272} // namespace details
273
274bool getSensorSubtree(SensorSubTree& subtree)
275{
276 std::shared_ptr<SensorSubTree> sensorTree;
277 details::getSensorSubtree(sensorTree);
278 if (!sensorTree)
279 {
280 return false;
281 }
282
283 subtree = *sensorTree;
284 return true;
285}
286
Scron Chang2703b022021-07-06 15:47:45 +0800287#ifdef FEATURE_HYBRID_SENSORS
288// Static sensors are listed in sensor-gen.cpp.
289ipmi::sensor::IdInfoMap::const_iterator
290 findStaticSensor(const std::string& path)
291{
292 return std::find_if(
293 ipmi::sensor::sensors.begin(), ipmi::sensor::sensors.end(),
294 [&path](const ipmi::sensor::IdInfoMap::value_type& findSensor) {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400295 return findSensor.second.sensorPath == path;
296 });
Scron Chang2703b022021-07-06 15:47:45 +0800297}
298#endif
299
Willy Tude54f482021-01-26 15:59:09 -0800300std::string getSensorTypeStringFromPath(const std::string& path)
301{
302 // get sensor type string from path, path is defined as
303 // /xyz/openbmc_project/sensors/<type>/label
304 size_t typeEnd = path.rfind("/");
305 if (typeEnd == std::string::npos)
306 {
307 return path;
308 }
309 size_t typeStart = path.rfind("/", typeEnd - 1);
310 if (typeStart == std::string::npos)
311 {
312 return path;
313 }
314 // Start at the character after the '/'
315 typeStart++;
316 return path.substr(typeStart, typeEnd - typeStart);
317}
318
319uint8_t getSensorTypeFromPath(const std::string& path)
320{
321 uint8_t sensorType = 0;
322 std::string type = getSensorTypeStringFromPath(path);
323 auto findSensor = sensorTypes.find(type.c_str());
324 if (findSensor != sensorTypes.end())
325 {
Scron Chang2b42d7e2021-07-06 15:45:47 +0800326 sensorType =
327 static_cast<uint8_t>(std::get<sensorTypeCodes>(findSensor->second));
Willy Tude54f482021-01-26 15:59:09 -0800328 } // else default 0x0 RESERVED
329
330 return sensorType;
331}
332
333uint16_t getSensorNumberFromPath(const std::string& path)
334{
335 std::shared_ptr<SensorNumMap> sensorNumMapPtr;
336 details::getSensorNumMap(sensorNumMapPtr);
337 if (!sensorNumMapPtr)
338 {
339 return invalidSensorNumber;
340 }
341
342 try
343 {
344 return sensorNumMapPtr->right.at(path);
345 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500346 catch (const std::out_of_range& e)
Willy Tude54f482021-01-26 15:59:09 -0800347 {
Willy Tude54f482021-01-26 15:59:09 -0800348 return invalidSensorNumber;
349 }
350}
351
352uint8_t getSensorEventTypeFromPath(const std::string& path)
353{
Scron Chang2b42d7e2021-07-06 15:45:47 +0800354 uint8_t sensorEventType = 0;
355 std::string type = getSensorTypeStringFromPath(path);
356 auto findSensor = sensorTypes.find(type.c_str());
357 if (findSensor != sensorTypes.end())
358 {
359 sensorEventType = static_cast<uint8_t>(
360 std::get<sensorEventTypeCodes>(findSensor->second));
361 }
362
363 return sensorEventType;
Willy Tude54f482021-01-26 15:59:09 -0800364}
365
366std::string getPathFromSensorNumber(uint16_t sensorNum)
367{
368 std::shared_ptr<SensorNumMap> sensorNumMapPtr;
369 details::getSensorNumMap(sensorNumMapPtr);
370 if (!sensorNumMapPtr)
371 {
372 return std::string();
373 }
374
375 try
376 {
377 return sensorNumMapPtr->left.at(sensorNum);
378 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500379 catch (const std::out_of_range& e)
Willy Tude54f482021-01-26 15:59:09 -0800380 {
Willy Tude54f482021-01-26 15:59:09 -0800381 return std::string();
382 }
383}
384
385namespace ipmi
386{
387
Alexander Hansen8fb5b892023-11-02 17:29:34 +0100388std::optional<std::map<std::string, std::vector<std::string>>>
Willy Tude54f482021-01-26 15:59:09 -0800389 getObjectInterfaces(const char* path)
390{
391 std::map<std::string, std::vector<std::string>> interfacesResponse;
392 std::vector<std::string> interfaces;
393 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
394
Patrick Williams5d82f472022-07-22 19:26:53 -0500395 sdbusplus::message_t getObjectMessage =
Willy Tude54f482021-01-26 15:59:09 -0800396 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
397 "/xyz/openbmc_project/object_mapper",
398 "xyz.openbmc_project.ObjectMapper", "GetObject");
399 getObjectMessage.append(path, interfaces);
400
401 try
402 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500403 sdbusplus::message_t response = dbus->call(getObjectMessage);
Willy Tude54f482021-01-26 15:59:09 -0800404 response.read(interfacesResponse);
405 }
406 catch (const std::exception& e)
407 {
Alexander Hansen8fb5b892023-11-02 17:29:34 +0100408 return std::nullopt;
Willy Tude54f482021-01-26 15:59:09 -0800409 }
410
411 return interfacesResponse;
412}
413
Patrick Williams1318a5e2024-08-16 15:19:54 -0400414std::map<std::string, Value>
415 getEntityManagerProperties(const char* path, const char* interface)
Willy Tude54f482021-01-26 15:59:09 -0800416{
417 std::map<std::string, Value> properties;
418 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
419
Patrick Williams5d82f472022-07-22 19:26:53 -0500420 sdbusplus::message_t getProperties =
Willy Tude54f482021-01-26 15:59:09 -0800421 dbus->new_method_call("xyz.openbmc_project.EntityManager", path,
422 "org.freedesktop.DBus.Properties", "GetAll");
423 getProperties.append(interface);
424
425 try
426 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500427 sdbusplus::message_t response = dbus->call(getProperties);
Willy Tude54f482021-01-26 15:59:09 -0800428 response.read(properties);
429 }
430 catch (const std::exception& e)
431 {
George Liude6694e2024-07-17 15:22:25 +0800432 lg2::error("Failed to GetAll, path: {PATH}, interface: {INTERFACE}, "
433 "error: {ERROR}",
434 "PATH", path, "INTERFACE", interface, "ERROR", e);
Willy Tude54f482021-01-26 15:59:09 -0800435 }
436
437 return properties;
438}
439
Willy Tu4eca2512022-06-20 21:14:51 -0700440// Fetch the ipmiDecoratorPaths to get the list of dbus objects that
441// have ipmi decorator to prevent unnessary dbus call to fetch the info
442std::optional<std::unordered_set<std::string>>&
443 getIpmiDecoratorPaths(const std::optional<ipmi::Context::ptr>& ctx)
444{
445 static std::optional<std::unordered_set<std::string>> ipmiDecoratorPaths;
446
447 if (!ctx.has_value() || ipmiDecoratorPaths != std::nullopt)
448 {
449 return ipmiDecoratorPaths;
450 }
451
452 boost::system::error_code ec;
453 std::vector<std::string> paths =
454 (*ctx)->bus->yield_method_call<std::vector<std::string>>(
455 (*ctx)->yield, ec, "xyz.openbmc_project.ObjectMapper",
456 "/xyz/openbmc_project/object_mapper",
457 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
458 int32_t(0),
459 std::array<const char*, 1>{
460 "xyz.openbmc_project.Inventory.Decorator.Ipmi"});
461 if (ec)
462 {
463 return ipmiDecoratorPaths;
464 }
465
Patrick Williams1318a5e2024-08-16 15:19:54 -0400466 ipmiDecoratorPaths =
467 std::unordered_set<std::string>(paths.begin(), paths.end());
Willy Tu4eca2512022-06-20 21:14:51 -0700468 return ipmiDecoratorPaths;
469}
470
Willy Tude54f482021-01-26 15:59:09 -0800471const std::string* getSensorConfigurationInterface(
472 const std::map<std::string, std::vector<std::string>>&
473 sensorInterfacesResponse)
474{
475 auto entityManagerService =
476 sensorInterfacesResponse.find("xyz.openbmc_project.EntityManager");
477 if (entityManagerService == sensorInterfacesResponse.end())
478 {
479 return nullptr;
480 }
481
482 // Find the fan configuration first (fans can have multiple configuration
483 // interfaces).
484 for (const auto& entry : entityManagerService->second)
485 {
486 if (entry == "xyz.openbmc_project.Configuration.AspeedFan" ||
487 entry == "xyz.openbmc_project.Configuration.I2CFan" ||
488 entry == "xyz.openbmc_project.Configuration.NuvotonFan")
489 {
490 return &entry;
491 }
492 }
493
494 for (const auto& entry : entityManagerService->second)
495 {
496 if (boost::algorithm::starts_with(entry,
497 "xyz.openbmc_project.Configuration."))
498 {
499 return &entry;
500 }
501 }
502
503 return nullptr;
504}
505
506// Follow Association properties for Sensor back to the Board dbus object to
507// check for an EntityId and EntityInstance property.
Willy Tu4eca2512022-06-20 21:14:51 -0700508void updateIpmiFromAssociation(
509 const std::string& path,
510 const std::unordered_set<std::string>& ipmiDecoratorPaths,
511 const DbusInterfaceMap& sensorMap, uint8_t& entityId,
512 uint8_t& entityInstance)
Willy Tude54f482021-01-26 15:59:09 -0800513{
514 namespace fs = std::filesystem;
515
516 auto sensorAssociationObject =
517 sensorMap.find("xyz.openbmc_project.Association.Definitions");
518 if (sensorAssociationObject == sensorMap.end())
519 {
520 if constexpr (debug)
521 {
522 std::fprintf(stderr, "path=%s, no association interface found\n",
523 path.c_str());
524 }
525
526 return;
527 }
528
529 auto associationObject =
530 sensorAssociationObject->second.find("Associations");
531 if (associationObject == sensorAssociationObject->second.end())
532 {
533 if constexpr (debug)
534 {
535 std::fprintf(stderr, "path=%s, no association records found\n",
536 path.c_str());
537 }
538
539 return;
540 }
541
542 std::vector<Association> associationValues =
543 std::get<std::vector<Association>>(associationObject->second);
544
545 // loop through the Associations looking for the right one:
546 for (const auto& entry : associationValues)
547 {
548 // forward, reverse, endpoint
549 const std::string& forward = std::get<0>(entry);
550 const std::string& reverse = std::get<1>(entry);
551 const std::string& endpoint = std::get<2>(entry);
552
553 // We only currently concern ourselves with chassis+all_sensors.
554 if (!(forward == "chassis" && reverse == "all_sensors"))
555 {
556 continue;
557 }
558
559 // the endpoint is the board entry provided by
560 // Entity-Manager. so let's grab its properties if it has
561 // the right interface.
562
563 // just try grabbing the properties first.
Willy Tu4eca2512022-06-20 21:14:51 -0700564 ipmi::PropertyMap::iterator entityIdProp;
565 ipmi::PropertyMap::iterator entityInstanceProp;
566 if (ipmiDecoratorPaths.contains(endpoint))
567 {
568 std::map<std::string, Value> ipmiProperties =
569 getEntityManagerProperties(
570 endpoint.c_str(),
571 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
Willy Tude54f482021-01-26 15:59:09 -0800572
Willy Tu4eca2512022-06-20 21:14:51 -0700573 entityIdProp = ipmiProperties.find("EntityId");
574 entityInstanceProp = ipmiProperties.find("EntityInstance");
575 if (entityIdProp != ipmiProperties.end())
576 {
577 entityId = static_cast<uint8_t>(
578 std::get<uint64_t>(entityIdProp->second));
579 }
580 if (entityInstanceProp != ipmiProperties.end())
581 {
582 entityInstance = static_cast<uint8_t>(
583 std::get<uint64_t>(entityInstanceProp->second));
584 }
Willy Tude54f482021-01-26 15:59:09 -0800585 }
586
587 // Now check the entity-manager entry for this sensor to see
588 // if it has its own value and use that instead.
589 //
590 // In theory, checking this first saves us from checking
591 // both, except in most use-cases identified, there won't be
592 // a per sensor override, so we need to always check both.
593 std::string sensorNameFromPath = fs::path(path).filename();
594
595 std::string sensorConfigPath = endpoint + "/" + sensorNameFromPath;
596
597 // Download the interfaces for the sensor from
598 // Entity-Manager to find the name of the configuration
599 // interface.
Alexander Hansen8fb5b892023-11-02 17:29:34 +0100600 std::optional<std::map<std::string, std::vector<std::string>>>
601 sensorInterfacesResponseOpt =
Willy Tude54f482021-01-26 15:59:09 -0800602 getObjectInterfaces(sensorConfigPath.c_str());
603
Alexander Hansen8fb5b892023-11-02 17:29:34 +0100604 if (!sensorInterfacesResponseOpt.has_value())
605 {
George Liude6694e2024-07-17 15:22:25 +0800606 lg2::debug("Failed to GetObject, path: {PATH}", "PATH",
607 sensorConfigPath);
Alexander Hansen8fb5b892023-11-02 17:29:34 +0100608 continue;
609 }
610
Willy Tude54f482021-01-26 15:59:09 -0800611 const std::string* configurationInterface =
Alexander Hansen8fb5b892023-11-02 17:29:34 +0100612 getSensorConfigurationInterface(
613 sensorInterfacesResponseOpt.value());
Willy Tude54f482021-01-26 15:59:09 -0800614
Harvey Wu7bb84db2023-06-26 10:02:08 +0800615 // If there are multi association path settings and only one path exist,
616 // we need to continue if cannot find configuration interface for this
617 // sensor.
Willy Tude54f482021-01-26 15:59:09 -0800618 if (!configurationInterface)
619 {
Harvey Wu7bb84db2023-06-26 10:02:08 +0800620 continue;
Willy Tude54f482021-01-26 15:59:09 -0800621 }
622
623 // We found a configuration interface.
624 std::map<std::string, Value> configurationProperties =
625 getEntityManagerProperties(sensorConfigPath.c_str(),
626 configurationInterface->c_str());
627
628 entityIdProp = configurationProperties.find("EntityId");
629 entityInstanceProp = configurationProperties.find("EntityInstance");
630 if (entityIdProp != configurationProperties.end())
631 {
632 entityId =
633 static_cast<uint8_t>(std::get<uint64_t>(entityIdProp->second));
634 }
635 if (entityInstanceProp != configurationProperties.end())
636 {
637 entityInstance = static_cast<uint8_t>(
638 std::get<uint64_t>(entityInstanceProp->second));
639 }
640
641 // stop searching Association records.
642 break;
643 } // end for Association vectors.
644
645 if constexpr (debug)
646 {
647 std::fprintf(stderr, "path=%s, entityId=%d, entityInstance=%d\n",
648 path.c_str(), entityId, entityInstance);
649 }
650}
651
652} // namespace ipmi