blob: eee7bc2f4b0880f4b9f09b0e1d9fb57cac1bf9a8 [file] [log] [blame]
Shawn McCarney03a25f12021-04-24 17:02:04 -05001/**
2 * Copyright © 2021 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
17#include "dbus_sensors.hpp"
18
19#include <utility>
20
21namespace phosphor::power::regulators
22{
23
24void DBusSensors::enable(Services& /*services*/)
25{
26 // Currently nothing to do here. The next monitoring cycle will set the
27 // values of all sensors, and that will set them to the enabled state.
28}
29
30void DBusSensors::endCycle(Services& services)
31{
32 // Delete any sensors that were not updated during this monitoring cycle.
33 // This can happen if the hardware device producing the sensors was removed
34 // or replaced with a different version.
35 auto it = sensors.begin();
36 while (it != sensors.end())
37 {
38 // Increment iterator before potentially deleting the sensor.
39 // map::erase() invalidates iterators/references to the erased element.
40 auto& [sensorName, sensor] = *it;
41 ++it;
42
43 // Check if last update time for sensor is before cycle start time
44 if (sensor->getLastUpdateTime() < cycleStartTime)
45 {
46 services.getJournal().logDebug("Deleted sensor " + sensorName);
47 sensors.erase(sensorName);
48 }
49 }
50}
51
52void DBusSensors::endRail(bool errorOccurred, Services& /*services*/)
53{
54 // If an error occurred, set all sensors for current rail to the error state
55 if (errorOccurred)
56 {
57 for (auto& [sensorName, sensor] : sensors)
58 {
59 if (sensor->getRail() == rail)
60 {
61 sensor->setToErrorState();
62 }
63 }
64 }
65
66 // Clear current rail information
67 rail.clear();
68 deviceInventoryPath.clear();
69 chassisInventoryPath.clear();
70}
71
72void DBusSensors::disable(Services& /*services*/)
73{
74 // Disable all sensors
75 for (auto& [sensorName, sensor] : sensors)
76 {
77 sensor->disable();
78 }
79}
80
81void DBusSensors::setValue(SensorType type, double value, Services& services)
82{
83 // Build unique sensor name based on rail and sensor type
84 std::string sensorName{rail + '_' + sensors::toString(type)};
85
86 // Check to see if the sensor already exists
87 auto it = sensors.find(sensorName);
88 if (it != sensors.end())
89 {
90 // Sensor exists; update value
91 it->second->setValue(value);
92 }
93 else
94 {
95 // Sensor doesn't exist; create it and add it to the map
96 auto sensor = std::make_unique<DBusSensor>(bus, sensorName, type, value,
97 rail, deviceInventoryPath,
98 chassisInventoryPath);
99 sensors.emplace(sensorName, std::move(sensor));
100 services.getJournal().logDebug("Created sensor " + sensorName);
101 }
102}
103
104void DBusSensors::startCycle(Services& /*services*/)
105{
106 // Store the time when this monitoring cycle started. This is used to
107 // detect sensors that were not updated during this cycle.
108 cycleStartTime = std::chrono::system_clock::now();
109}
110
111void DBusSensors::startRail(const std::string& rail,
112 const std::string& deviceInventoryPath,
113 const std::string& chassisInventoryPath,
114 Services& /*services*/)
115{
116 // Store current rail information; used later by setValue() and endRail()
117 this->rail = rail;
118 this->deviceInventoryPath = deviceInventoryPath;
119 this->chassisInventoryPath = chassisInventoryPath;
120}
121
122} // namespace phosphor::power::regulators