xref: /openbmc/phosphor-hwmon/mainloop.cpp (revision 64129937)
1 /**
2  * Copyright © 2016 IBM 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 #include "config.h"
17 
18 #include "mainloop.hpp"
19 
20 #include "env.hpp"
21 #include "fan_pwm.hpp"
22 #include "fan_speed.hpp"
23 #include "hwmon.hpp"
24 #include "hwmonio.hpp"
25 #include "sensor.hpp"
26 #include "sensorset.hpp"
27 #include "sysfs.hpp"
28 #include "targets.hpp"
29 #include "thresholds.hpp"
30 #include "util.hpp"
31 
32 #include <phosphor-logging/elog-errors.hpp>
33 #include <xyz/openbmc_project/Sensor/Device/error.hpp>
34 
35 #include <cassert>
36 #include <cstdlib>
37 #include <format>
38 #include <functional>
39 #include <future>
40 #include <iostream>
41 #include <memory>
42 #include <sstream>
43 #include <string>
44 #include <unordered_set>
45 
46 using namespace phosphor::logging;
47 
48 // Initialization for Warning Objects
49 decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
50     &WarningObject::warningLow;
51 decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
52     &WarningObject::warningHigh;
53 decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
54     &WarningObject::warningLow;
55 decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
56     &WarningObject::warningHigh;
57 decltype(Thresholds<WarningObject>::alarmLo)
58     Thresholds<WarningObject>::alarmLo = &WarningObject::warningAlarmLow;
59 decltype(Thresholds<WarningObject>::alarmHi)
60     Thresholds<WarningObject>::alarmHi = &WarningObject::warningAlarmHigh;
61 decltype(Thresholds<WarningObject>::getAlarmLow)
62     Thresholds<WarningObject>::getAlarmLow = &WarningObject::warningAlarmLow;
63 decltype(Thresholds<WarningObject>::getAlarmHigh)
64     Thresholds<WarningObject>::getAlarmHigh = &WarningObject::warningAlarmHigh;
65 decltype(Thresholds<WarningObject>::assertLowSignal)
66     Thresholds<WarningObject>::assertLowSignal =
67         &WarningObject::warningLowAlarmAsserted;
68 decltype(Thresholds<WarningObject>::assertHighSignal)
69     Thresholds<WarningObject>::assertHighSignal =
70         &WarningObject::warningHighAlarmAsserted;
71 decltype(Thresholds<WarningObject>::deassertLowSignal)
72     Thresholds<WarningObject>::deassertLowSignal =
73         &WarningObject::warningLowAlarmDeasserted;
74 decltype(Thresholds<WarningObject>::deassertHighSignal)
75     Thresholds<WarningObject>::deassertHighSignal =
76         &WarningObject::warningHighAlarmDeasserted;
77 
78 // Initialization for Critical Objects
79 decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
80     &CriticalObject::criticalLow;
81 decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
82     &CriticalObject::criticalHigh;
83 decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
84     &CriticalObject::criticalLow;
85 decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
86     &CriticalObject::criticalHigh;
87 decltype(Thresholds<CriticalObject>::alarmLo)
88     Thresholds<CriticalObject>::alarmLo = &CriticalObject::criticalAlarmLow;
89 decltype(Thresholds<CriticalObject>::alarmHi)
90     Thresholds<CriticalObject>::alarmHi = &CriticalObject::criticalAlarmHigh;
91 decltype(Thresholds<CriticalObject>::getAlarmLow)
92     Thresholds<CriticalObject>::getAlarmLow = &CriticalObject::criticalAlarmLow;
93 decltype(Thresholds<CriticalObject>::getAlarmHigh)
94     Thresholds<CriticalObject>::getAlarmHigh =
95         &CriticalObject::criticalAlarmHigh;
96 decltype(Thresholds<CriticalObject>::assertLowSignal)
97     Thresholds<CriticalObject>::assertLowSignal =
98         &CriticalObject::criticalLowAlarmAsserted;
99 decltype(Thresholds<CriticalObject>::assertHighSignal)
100     Thresholds<CriticalObject>::assertHighSignal =
101         &CriticalObject::criticalHighAlarmAsserted;
102 decltype(Thresholds<CriticalObject>::deassertLowSignal)
103     Thresholds<CriticalObject>::deassertLowSignal =
104         &CriticalObject::criticalLowAlarmDeasserted;
105 decltype(Thresholds<CriticalObject>::deassertHighSignal)
106     Thresholds<CriticalObject>::deassertHighSignal =
107         &CriticalObject::criticalHighAlarmDeasserted;
108 
updateSensorInterfaces(InterfaceMap & ifaces,SensorValueType value)109 void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
110 {
111     for (auto& iface : ifaces)
112     {
113         switch (iface.first)
114         {
115                 // clang-format off
116             case InterfaceType::VALUE:
117             {
118                 auto& valueIface =
119                     std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
120                 valueIface->value(value);
121             }
122             break;
123             // clang-format on
124             case InterfaceType::WARN:
125                 checkThresholds<WarningObject>(iface.second, value);
126                 break;
127             case InterfaceType::CRIT:
128                 checkThresholds<CriticalObject>(iface.second, value);
129                 break;
130             default:
131                 break;
132         }
133     }
134 }
135 
getID(SensorSet::container_t::const_reference sensor)136 std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
137 {
138     std::string id;
139 
140     /*
141      * Check if the value of the MODE_<item><X> env variable for the sensor
142      * is set. If it is, then read the from the <item><X>_<mode>
143      * file. The name of the DBUS object would be the value of the env
144      * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
145      * doesn't exist, then the name of DBUS object is the value of the env
146      * variable LABEL_<item><X>.
147      *
148      * For example, if MODE_temp1 = "label", then code reads the temp1_label
149      * file.  If it has a 5 in it, then it will use the following entry to
150      * name the object: LABEL_temp5 = "My DBus object name".
151      *
152      */
153     auto mode = env::getEnv("MODE", sensor.first);
154     if (!mode.empty())
155     {
156         id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
157                                 sensor.first);
158 
159         if (id.empty())
160         {
161             return id;
162         }
163     }
164 
165     // Use the ID we looked up above if there was one,
166     // otherwise use the standard one.
167     id = (id.empty()) ? sensor.first.second : id;
168 
169     return id;
170 }
171 
172 SensorIdentifiers
getIdentifiers(SensorSet::container_t::const_reference sensor)173     MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
174 {
175     std::string id = getID(sensor);
176     std::string label;
177     std::string accuracy;
178     std::string priority;
179 
180     if (!id.empty())
181     {
182         // Ignore inputs without a label.
183         label = env::getEnv("LABEL", sensor.first.first, id);
184         accuracy = env::getEnv("ACCURACY", sensor.first.first, id);
185         priority = env::getEnv("PRIORITY", sensor.first.first, id);
186     }
187 
188     return std::make_tuple(std::move(id), std::move(label), std::move(accuracy),
189                            std::move(priority));
190 }
191 
192 /**
193  * Reads the environment parameters of a sensor and creates an object with
194  * atleast the `Value` interface, otherwise returns without creating the object.
195  * If the `Value` interface is successfully created, by reading the sensor's
196  * corresponding sysfs file's value, the additional interfaces for the sensor
197  * are created and the InterfacesAdded signal is emitted. The object's state
198  * data is then returned for sensor state monitoring within the main loop.
199  */
200 std::optional<ObjectStateData>
getObject(SensorSet::container_t::const_reference sensor)201     MainLoop::getObject(SensorSet::container_t::const_reference sensor)
202 {
203     auto properties = getIdentifiers(sensor);
204     if (std::get<sensorID>(properties).empty() ||
205         std::get<sensorLabel>(properties).empty())
206     {
207         return {};
208     }
209 
210     hwmon::Attributes attrs;
211     if (!hwmon::getAttributes(sensor.first.first, attrs))
212     {
213         return {};
214     }
215 
216     const auto& [sensorSetKey, sensorAttrs] = sensor;
217     const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
218 
219     /* Note: The sensor objects all share the same ioAccess object. */
220     auto sensorObj = std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess,
221                                                       _devPath);
222 
223     // Get list of return codes for removing sensors on device
224     auto devRmRCs = env::getEnv("REMOVERCS");
225     // Add sensor removal return codes defined at the device level
226     sensorObj->addRemoveRCs(devRmRCs);
227 
228     std::string objectPath{_root};
229     objectPath.append(1, '/');
230     objectPath.append(hwmon::getNamespace(attrs));
231     objectPath.append(1, '/');
232     objectPath.append(std::get<sensorLabel>(properties));
233 
234     ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
235     RetryIO retryIO(hwmonio::retries, hwmonio::delay);
236     if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
237     {
238         // When adding a sensor that was purposely removed,
239         // don't retry on errors when reading its value
240         std::get<size_t>(retryIO) = 0;
241     }
242     auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
243     try
244     {
245         // Add accuracy interface
246         auto accuracyStr = std::get<sensorAccuracy>(properties);
247         try
248         {
249             if (!accuracyStr.empty())
250             {
251                 auto accuracy = stod(accuracyStr);
252                 sensorObj->addAccuracy(info, accuracy);
253             }
254         }
255         catch (const std::invalid_argument&)
256         {}
257 
258         // Add priority interface
259         auto priorityStr = std::get<sensorPriority>(properties);
260         try
261         {
262             if (!priorityStr.empty())
263             {
264                 auto priority = std::stoul(priorityStr);
265                 sensorObj->addPriority(info, priority);
266             }
267         }
268         catch (const std::invalid_argument&)
269         {}
270 
271         // Add status interface based on _fault file being present
272         sensorObj->addStatus(info);
273         valueInterface = sensorObj->addValue(retryIO, info, _timedoutMap);
274     }
275     catch (const std::system_error& e)
276     {
277         auto file = sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
278                                            sensorSysfsNum,
279                                            hwmon::entry::cinput);
280 
281         // Check sensorAdjusts for sensor removal RCs
282         auto& sAdjusts = sensorObj->getAdjusts();
283         if (sAdjusts.rmRCs.count(e.code().value()) > 0)
284         {
285             // Return code found in sensor return code removal list
286             if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
287             {
288                 // Trace for sensor not already removed from dbus
289                 log<level::INFO>("Sensor not added to dbus for read fail",
290                                  entry("FILE=%s", file.c_str()),
291                                  entry("RC=%d", e.code().value()));
292                 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
293             }
294             return {};
295         }
296 
297         using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
298         report<ReadFailure>(
299             xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
300                 e.code().value()),
301             xyz::openbmc_project::Sensor::Device::ReadFailure::
302                 CALLOUT_DEVICE_PATH(_devPath.c_str()));
303 
304         log<level::INFO>(std::format("Failing sysfs file: {} errno: {}", file,
305                                      e.code().value())
306                              .c_str());
307         exit(EXIT_FAILURE);
308     }
309     auto sensorValue = valueInterface->value();
310     int64_t scale = sensorObj->getScale();
311 
312     addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
313                                 sensorValue, info, scale);
314     addThreshold<CriticalObject>(sensorSysfsType,
315                                  std::get<sensorID>(properties), sensorValue,
316                                  info, scale);
317 
318     auto target = addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath,
319                                              info);
320     if (target)
321     {
322         target->enable();
323     }
324     addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
325 
326     // All the interfaces have been created.  Go ahead
327     // and emit InterfacesAdded.
328     valueInterface->emit_object_added();
329 
330     // Save sensor object specifications
331     _sensorObjects[sensorSetKey] = std::move(sensorObj);
332 
333     return std::make_pair(std::move(std::get<sensorLabel>(properties)),
334                           std::move(info));
335 }
336 
MainLoop(sdbusplus::bus_t && bus,const std::string & param,const std::string & path,const std::string & devPath,const char * prefix,const char * root,const std::string & instanceId,const hwmonio::HwmonIOInterface * ioIntf)337 MainLoop::MainLoop(sdbusplus::bus_t&& bus, const std::string& param,
338                    const std::string& path, const std::string& devPath,
339                    const char* prefix, const char* root,
340                    const std::string& instanceId,
341                    const hwmonio::HwmonIOInterface* ioIntf) :
342     _bus(std::move(bus)),
343     _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
344     _devPath(devPath), _prefix(prefix), _root(root), _state(),
345     _instanceId(instanceId), _ioAccess(ioIntf),
346     _event(sdeventplus::Event::get_default()),
347     _timer(_event, std::bind(&MainLoop::read, this))
348 {
349     // Strip off any trailing slashes.
350     std::string p = path;
351     while (!p.empty() && p.back() == '/')
352     {
353         p.pop_back();
354     }
355 
356     // Given the furthest right /, set instance to
357     // the basename, and hwmonRoot to the leading path.
358     auto n = p.rfind('/');
359     if (n != std::string::npos)
360     {
361         _instance.assign(p.substr(n + 1));
362         _hwmonRoot.assign(p.substr(0, n));
363     }
364 
365     assert(!_instance.empty());
366     assert(!_hwmonRoot.empty());
367 }
368 
shutdown()369 void MainLoop::shutdown() noexcept
370 {
371     _event.exit(0);
372 }
373 
run()374 void MainLoop::run()
375 {
376     init();
377 
378     std::function<void()> callback(std::bind(&MainLoop::read, this));
379     try
380     {
381         _timer.restart(std::chrono::microseconds(_interval));
382 
383         // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
384 
385         // TODO: Issue#7 - Should probably periodically check the SensorSet
386         //       for new entries.
387 
388         _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
389         _event.loop();
390     }
391     catch (const std::exception& e)
392     {
393         log<level::ERR>("Error in sysfs polling loop",
394                         entry("ERROR=%s", e.what()));
395         throw;
396     }
397 }
398 
init()399 void MainLoop::init()
400 {
401     // Check sysfs for available sensors.
402     auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
403 
404     for (const auto& i : *sensors)
405     {
406         auto object = getObject(i);
407         if (object)
408         {
409             // Construct the SensorSet value
410             // std::tuple<SensorSet::mapped_type,
411             //            std::string(Sensor Label),
412             //            ObjectInfo>
413             auto value = std::make_tuple(std::move(i.second),
414                                          std::move((*object).first),
415                                          std::move((*object).second));
416 
417             _state[std::move(i.first)] = std::move(value);
418         }
419 
420         // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
421         if ((i.first.first == hwmon::type::power) &&
422             (phosphor::utility::isAverageEnvSet(i.first)))
423         {
424             _average.setAverageValue(i.first, std::make_pair(0, 0));
425         }
426     }
427 
428     /* If there are no sensors specified by labels, exit. */
429     if (0 == _state.size())
430     {
431         exit(0);
432     }
433 
434     {
435         std::stringstream ss;
436         std::string id = _instanceId;
437         if (id.empty())
438         {
439             id =
440                 std::to_string(std::hash<std::string>{}(_devPath + _pathParam));
441         }
442         ss << _prefix << "-" << id << ".Hwmon1";
443 
444         _bus.request_name(ss.str().c_str());
445     }
446 
447     {
448         auto interval = env::getEnv("INTERVAL");
449         if (!interval.empty())
450         {
451             _interval = std::strtoull(interval.c_str(), NULL, 10);
452         }
453     }
454 }
455 
read()456 void MainLoop::read()
457 {
458     // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
459     //       ensure the objects all exist?
460 
461     // Iterate through all the sensors.
462     for (auto& [sensorSetKey, sensorStateTuple] : _state)
463     {
464         const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
465         auto& [attrs, unused, objInfo] = sensorStateTuple;
466 
467         if (attrs.find(hwmon::entry::input) == attrs.end())
468         {
469             continue;
470         }
471 
472         // Read value from sensor.
473         std::string input = hwmon::entry::input;
474         if (sensorSysfsType == hwmon::type::pwm)
475         {
476             input = "";
477         }
478         // If type is power and AVERAGE_power* is true in env, use average
479         // instead of input
480         else if ((sensorSysfsType == hwmon::type::power) &&
481                  (phosphor::utility::isAverageEnvSet(sensorSetKey)))
482         {
483             input = hwmon::entry::average;
484         }
485 
486         SensorValueType value;
487         auto& obj = std::get<InterfaceMap>(objInfo);
488         std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
489 
490         auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
491             obj[InterfaceType::STATUS]);
492         // As long as addStatus is called before addValue, statusIface
493         // should never be nullptr.
494         assert(statusIface);
495 
496         try
497         {
498             if (sensor->hasFaultFile())
499             {
500                 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
501                                              hwmon::entry::fault,
502                                              hwmonio::retries, hwmonio::delay);
503                 // Skip reading from a sensor with a valid fault file
504                 // and set the functional property accordingly
505                 if (!statusIface->functional((fault == 0) ? true : false))
506                 {
507                     continue;
508                 }
509             }
510 
511             {
512                 // RAII object for GPIO unlock / lock
513                 auto locker = sensor::gpioUnlock(sensor->getGpio());
514 
515                 // For sensors with attribute ASYNC_READ_TIMEOUT,
516                 // spawn a thread with timeout
517                 auto asyncReadTimeout = env::getEnv("ASYNC_READ_TIMEOUT",
518                                                     sensorSetKey);
519                 if (!asyncReadTimeout.empty())
520                 {
521                     std::chrono::milliseconds asyncTimeout{
522                         std::stoi(asyncReadTimeout)};
523                     value = sensor::asyncRead(
524                         sensorSetKey, _ioAccess, asyncTimeout, _timedoutMap,
525                         sensorSysfsType, sensorSysfsNum, input,
526                         hwmonio::retries, hwmonio::delay);
527                 }
528                 else
529                 {
530                     // Retry for up to a second if device is busy
531                     // or has a transient error.
532                     value = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
533                                             input, hwmonio::retries,
534                                             hwmonio::delay);
535                 }
536 
537                 // Set functional property to true if we could read sensor
538                 statusIface->functional(true);
539 
540                 value = sensor->adjustValue(value);
541 
542                 if (input == hwmon::entry::average)
543                 {
544                     // Calculate the values of averageMap based on current
545                     // average value, current average_interval value, previous
546                     // average value, previous average_interval value
547                     int64_t interval =
548                         _ioAccess->read(sensorSysfsType, sensorSysfsNum,
549                                         hwmon::entry::caverage_interval,
550                                         hwmonio::retries, hwmonio::delay);
551                     auto ret = _average.getAverageValue(sensorSetKey);
552                     assert(ret);
553 
554                     const auto& [preAverage, preInterval] = *ret;
555 
556                     auto calValue = Average::calcAverage(
557                         preAverage, preInterval, value, interval);
558                     if (calValue)
559                     {
560                         // Update previous values in averageMap before the
561                         // variable value is changed next
562                         _average.setAverageValue(
563                             sensorSetKey, std::make_pair(value, interval));
564                         // Update value to be calculated average
565                         value = calValue.value();
566                     }
567                     else
568                     {
569                         // the value of
570                         // power*_average_interval is not changed yet, use the
571                         // previous calculated average instead. So skip dbus
572                         // update.
573                         continue;
574                     }
575                 }
576             }
577 
578             updateSensorInterfaces(obj, value);
579         }
580         catch (const std::system_error& e)
581         {
582 #if UPDATE_FUNCTIONAL_ON_FAIL
583             // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
584             // thrown, set the functional property to false.
585             // We cannot set this with the 'continue' in the lower block
586             // as the code may exit before reaching it.
587             statusIface->functional(false);
588 #endif
589             auto file = sysfs::make_sysfs_path(
590                 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
591 
592             // Check sensorAdjusts for sensor removal RCs
593             auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
594             if (sAdjusts.rmRCs.count(e.code().value()) > 0)
595             {
596                 // Return code found in sensor return code removal list
597                 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
598                 {
599                     // Trace for sensor not already removed from dbus
600                     log<level::INFO>("Remove sensor from dbus for read fail",
601                                      entry("FILE=%s", file.c_str()),
602                                      entry("RC=%d", e.code().value()));
603                     // Mark this sensor to be removed from dbus
604                     _rmSensors[sensorSetKey] = attrs;
605                 }
606                 continue;
607             }
608 #if UPDATE_FUNCTIONAL_ON_FAIL
609             // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
610             continue;
611 #endif
612             using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
613                 Error;
614             report<ReadFailure>(
615                 xyz::openbmc_project::Sensor::Device::ReadFailure::
616                     CALLOUT_ERRNO(e.code().value()),
617                 xyz::openbmc_project::Sensor::Device::ReadFailure::
618                     CALLOUT_DEVICE_PATH(_devPath.c_str()));
619 
620             log<level::INFO>(std::format("Failing sysfs file: {} errno: {}",
621                                          file, e.code().value())
622                                  .c_str());
623 
624             exit(EXIT_FAILURE);
625         }
626     }
627 
628     removeSensors();
629 
630     addDroppedSensors();
631 }
632 
removeSensors()633 void MainLoop::removeSensors()
634 {
635     // Remove any sensors marked for removal
636     for (const auto& i : _rmSensors)
637     {
638         // Remove sensor object from dbus using emit_object_removed()
639         auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
640         auto& objPath = std::get<std::string>(objInfo);
641 
642         _bus.emit_object_removed(objPath.c_str());
643 
644         // Erase sensor object info
645         _state.erase(i.first);
646     }
647 }
648 
addDroppedSensors()649 void MainLoop::addDroppedSensors()
650 {
651     // Attempt to add any sensors that were removed
652     auto it = _rmSensors.begin();
653     while (it != _rmSensors.end())
654     {
655         if (_state.find(it->first) == _state.end())
656         {
657             SensorSet::container_t::value_type ssValueType =
658                 std::make_pair(it->first, it->second);
659 
660             auto object = getObject(ssValueType);
661             if (object)
662             {
663                 // Construct the SensorSet value
664                 // std::tuple<SensorSet::mapped_type,
665                 //            std::string(Sensor Label),
666                 //            ObjectInfo>
667                 auto value = std::make_tuple(std::move(ssValueType.second),
668                                              std::move((*object).first),
669                                              std::move((*object).second));
670 
671                 _state[std::move(ssValueType.first)] = std::move(value);
672 
673                 std::string input = hwmon::entry::input;
674                 // If type is power and AVERAGE_power* is true in env, use
675                 // average instead of input
676                 if ((it->first.first == hwmon::type::power) &&
677                     (phosphor::utility::isAverageEnvSet(it->first)))
678                 {
679                     input = hwmon::entry::average;
680                 }
681                 // Sensor object added, erase entry from removal list
682                 auto file = sysfs::make_sysfs_path(_ioAccess->path(),
683                                                    it->first.first,
684                                                    it->first.second, input);
685 
686                 log<level::INFO>("Added sensor to dbus after successful read",
687                                  entry("FILE=%s", file.c_str()));
688 
689                 it = _rmSensors.erase(it);
690             }
691             else
692             {
693                 ++it;
694             }
695         }
696         else
697         {
698             // Sanity check to remove sensors that were re-added
699             it = _rmSensors.erase(it);
700         }
701     }
702 }
703 
704 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
705