blob: 9723da3aa575497025a7cbc912a6ae9a6d79632f [file]
#include "state_sensor.hpp"
#include <format>
#include <iostream>
namespace pldm
{
namespace platform_mc
{
StateSensor::StateSensor(const pldm_tid_t tid,
std::shared_ptr<pldm_state_sensor_pdr> pdr,
std::string& sensorName) :
tid(tid), sensorId(pdr->sensor_id), sensorName(sensorName),
compositeSensorCount(pdr->composite_sensor_count)
{}
void StateSensor::addStateSet(uint16_t stateSetId)
{
compositeSensors.push_back(PldmStateSetReading{stateSetId, {0, 0, 0, 0}});
}
int StateSensor::getCompositeSensorCount() const
{
return compositeSensors.size();
}
void StateSensor::setFunctionalStatus(bool functional)
{
if (operationalStatusIntf)
{
operationalStatusIntf->functional(functional);
}
}
void StateSensor::updateStateReading(uint8_t cnt,
get_sensor_state_field fields[])
{
if (cnt != getCompositeSensorCount())
{
std::cerr
<< std::format(
"Error : {} got invalid CompositeSensorCount, expect:{}, got: {}",
sensorName, unsigned(getCompositeSensorCount()),
unsigned(cnt))
<< std::endl;
setFunctionalStatus(false);
return;
}
std::vector<PldmStateReadingTuple> newReading;
for (int i = 0; i < cnt; i++)
{
compositeSensors[i].reading = fields[i];
newReading.push_back(PldmStateReadingTuple{
compositeSensors[i].stateSetId, fields[i].sensor_op_state,
fields[i].present_state, fields[i].previous_state,
fields[i].event_state});
}
if (stateReadingIntf)
{
stateReadingIntf->values(newReading);
}
setFunctionalStatus(true);
}
void StateSensor::invalidateStateSensor()
{
std::vector<PldmStateReadingTuple> newReading;
get_sensor_state_field field{PLDM_SENSOR_STATUSUNKOWN, 0, 0, 0};
for (PldmStateSetReading& reading : compositeSensors)
{
reading.reading = field;
newReading.push_back(PldmStateReadingTuple{
reading.stateSetId, field.sensor_op_state, field.present_state,
field.previous_state, field.event_state});
}
if (stateReadingIntf)
{
stateReadingIntf->values(newReading);
}
setFunctionalStatus(false);
}
void StateSensor::createStateSensor(sdbusplus::bus::bus& bus)
{
sensorPath = "/xyz/openbmc_project/sensors/PLDMState/" + sensorName;
try
{
operationalStatusIntf = std::make_unique<OperationalStatusIntf>(
bus, sensorPath.c_str(), OperationalStatusIntf::action::defer_emit);
operationalStatusIntf->functional(false);
stateReadingIntf =
std::make_unique<StateReadingIntf>(bus, sensorPath.c_str());
}
catch (const std::exception& e)
{
std::cerr << "Failed to create StateSensor interfaces for "
<< sensorName << ": " << e.what() << std::endl;
}
}
} // namespace platform_mc
} // namespace pldm