103a25f1bSShawn McCarney /**
203a25f1bSShawn McCarney * Copyright © 2021 IBM Corporation
303a25f1bSShawn McCarney *
403a25f1bSShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License");
503a25f1bSShawn McCarney * you may not use this file except in compliance with the License.
603a25f1bSShawn McCarney * You may obtain a copy of the License at
703a25f1bSShawn McCarney *
803a25f1bSShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0
903a25f1bSShawn McCarney *
1003a25f1bSShawn McCarney * Unless required by applicable law or agreed to in writing, software
1103a25f1bSShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS,
1203a25f1bSShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1303a25f1bSShawn McCarney * See the License for the specific language governing permissions and
1403a25f1bSShawn McCarney * limitations under the License.
1503a25f1bSShawn McCarney */
1603a25f1bSShawn McCarney
1703a25f1bSShawn McCarney #include "dbus_sensors.hpp"
1803a25f1bSShawn McCarney
1903a25f1bSShawn McCarney #include <utility>
2003a25f1bSShawn McCarney
2103a25f1bSShawn McCarney namespace phosphor::power::regulators
2203a25f1bSShawn McCarney {
2303a25f1bSShawn McCarney
enable()24c9c69518SShawn McCarney void DBusSensors::enable()
2503a25f1bSShawn McCarney {
2603a25f1bSShawn McCarney // Currently nothing to do here. The next monitoring cycle will set the
2703a25f1bSShawn McCarney // values of all sensors, and that will set them to the enabled state.
2803a25f1bSShawn McCarney }
2903a25f1bSShawn McCarney
endCycle()30c9c69518SShawn McCarney void DBusSensors::endCycle()
3103a25f1bSShawn McCarney {
3203a25f1bSShawn McCarney // Delete any sensors that were not updated during this monitoring cycle.
3303a25f1bSShawn McCarney // This can happen if the hardware device producing the sensors was removed
3403a25f1bSShawn McCarney // or replaced with a different version.
3503a25f1bSShawn McCarney auto it = sensors.begin();
3603a25f1bSShawn McCarney while (it != sensors.end())
3703a25f1bSShawn McCarney {
3803a25f1bSShawn McCarney // Increment iterator before potentially deleting the sensor.
3903a25f1bSShawn McCarney // map::erase() invalidates iterators/references to the erased element.
4003a25f1bSShawn McCarney auto& [sensorName, sensor] = *it;
4103a25f1bSShawn McCarney ++it;
4203a25f1bSShawn McCarney
4303a25f1bSShawn McCarney // Check if last update time for sensor is before cycle start time
4403a25f1bSShawn McCarney if (sensor->getLastUpdateTime() < cycleStartTime)
4503a25f1bSShawn McCarney {
4603a25f1bSShawn McCarney sensors.erase(sensorName);
4703a25f1bSShawn McCarney }
4803a25f1bSShawn McCarney }
4903a25f1bSShawn McCarney }
5003a25f1bSShawn McCarney
endRail(bool errorOccurred)51c9c69518SShawn McCarney void DBusSensors::endRail(bool errorOccurred)
5203a25f1bSShawn McCarney {
5303a25f1bSShawn McCarney // If an error occurred, set all sensors for current rail to the error state
5403a25f1bSShawn McCarney if (errorOccurred)
5503a25f1bSShawn McCarney {
5603a25f1bSShawn McCarney for (auto& [sensorName, sensor] : sensors)
5703a25f1bSShawn McCarney {
5803a25f1bSShawn McCarney if (sensor->getRail() == rail)
5903a25f1bSShawn McCarney {
6003a25f1bSShawn McCarney sensor->setToErrorState();
6103a25f1bSShawn McCarney }
6203a25f1bSShawn McCarney }
6303a25f1bSShawn McCarney }
6403a25f1bSShawn McCarney
6503a25f1bSShawn McCarney // Clear current rail information
6603a25f1bSShawn McCarney rail.clear();
6703a25f1bSShawn McCarney deviceInventoryPath.clear();
6803a25f1bSShawn McCarney chassisInventoryPath.clear();
6903a25f1bSShawn McCarney }
7003a25f1bSShawn McCarney
disable()71c9c69518SShawn McCarney void DBusSensors::disable()
7203a25f1bSShawn McCarney {
7303a25f1bSShawn McCarney // Disable all sensors
7403a25f1bSShawn McCarney for (auto& [sensorName, sensor] : sensors)
7503a25f1bSShawn McCarney {
7603a25f1bSShawn McCarney sensor->disable();
7703a25f1bSShawn McCarney }
7803a25f1bSShawn McCarney }
7903a25f1bSShawn McCarney
setValue(SensorType type,double value)80c9c69518SShawn McCarney void DBusSensors::setValue(SensorType type, double value)
8103a25f1bSShawn McCarney {
8203a25f1bSShawn McCarney // Build unique sensor name based on rail and sensor type
8303a25f1bSShawn McCarney std::string sensorName{rail + '_' + sensors::toString(type)};
8403a25f1bSShawn McCarney
8503a25f1bSShawn McCarney // Check to see if the sensor already exists
8603a25f1bSShawn McCarney auto it = sensors.find(sensorName);
8703a25f1bSShawn McCarney if (it != sensors.end())
8803a25f1bSShawn McCarney {
8903a25f1bSShawn McCarney // Sensor exists; update value
9003a25f1bSShawn McCarney it->second->setValue(value);
9103a25f1bSShawn McCarney }
9203a25f1bSShawn McCarney else
9303a25f1bSShawn McCarney {
9403a25f1bSShawn McCarney // Sensor doesn't exist; create it and add it to the map
95*f5402197SPatrick Williams auto sensor = std::make_unique<DBusSensor>(
96*f5402197SPatrick Williams bus, sensorName, type, value, rail, deviceInventoryPath,
9703a25f1bSShawn McCarney chassisInventoryPath);
9803a25f1bSShawn McCarney sensors.emplace(sensorName, std::move(sensor));
9903a25f1bSShawn McCarney }
10003a25f1bSShawn McCarney }
10103a25f1bSShawn McCarney
startCycle()102c9c69518SShawn McCarney void DBusSensors::startCycle()
10303a25f1bSShawn McCarney {
10403a25f1bSShawn McCarney // Store the time when this monitoring cycle started. This is used to
10503a25f1bSShawn McCarney // detect sensors that were not updated during this cycle.
10603a25f1bSShawn McCarney cycleStartTime = std::chrono::system_clock::now();
10703a25f1bSShawn McCarney }
10803a25f1bSShawn McCarney
startRail(const std::string & rail,const std::string & deviceInventoryPath,const std::string & chassisInventoryPath)10903a25f1bSShawn McCarney void DBusSensors::startRail(const std::string& rail,
11003a25f1bSShawn McCarney const std::string& deviceInventoryPath,
111c9c69518SShawn McCarney const std::string& chassisInventoryPath)
11203a25f1bSShawn McCarney {
11303a25f1bSShawn McCarney // Store current rail information; used later by setValue() and endRail()
11403a25f1bSShawn McCarney this->rail = rail;
11503a25f1bSShawn McCarney this->deviceInventoryPath = deviceInventoryPath;
11603a25f1bSShawn McCarney this->chassisInventoryPath = chassisInventoryPath;
11703a25f1bSShawn McCarney }
11803a25f1bSShawn McCarney
11903a25f1bSShawn McCarney } // namespace phosphor::power::regulators
120