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