| #include "common/instance_id.hpp" |
| #include "common/types.hpp" |
| #include "mock_sensor_manager.hpp" |
| #include "platform-mc/terminus_manager.hpp" |
| #include "test/test_instance_id.hpp" |
| |
| #include <sdeventplus/event.hpp> |
| |
| #include <gtest/gtest.h> |
| |
| using ::testing::_; |
| using ::testing::Between; |
| using ::testing::Return; |
| |
| class SensorManagerTest : public testing::Test |
| { |
| protected: |
| SensorManagerTest() : |
| bus(pldm::utils::DBusHandler::getBus()), |
| event(sdeventplus::Event::get_default()), instanceIdDb(), |
| reqHandler(pldmTransport, event, instanceIdDb, false), |
| terminusManager(event, reqHandler, instanceIdDb, termini, nullptr), |
| sensorManager(event, terminusManager, termini) |
| {} |
| |
| void runEventLoopForSeconds(uint64_t sec) |
| { |
| uint64_t t0 = 0; |
| uint64_t t1 = 0; |
| uint64_t usec = sec * 1000000; |
| uint64_t elapsed = 0; |
| sd_event_now(event.get(), CLOCK_MONOTONIC, &t0); |
| do |
| { |
| if (!sd_event_run(event.get(), usec - elapsed)) |
| { |
| break; |
| } |
| sd_event_now(event.get(), CLOCK_MONOTONIC, &t1); |
| elapsed = t1 - t0; |
| } while (elapsed < usec); |
| } |
| |
| PldmTransport* pldmTransport = nullptr; |
| sdbusplus::bus::bus& bus; |
| sdeventplus::Event event; |
| TestInstanceIdDb instanceIdDb; |
| pldm::requester::Handler<pldm::requester::Request> reqHandler; |
| pldm::platform_mc::TerminusManager terminusManager; |
| pldm::platform_mc::MockSensorManager sensorManager; |
| std::map<pldm_tid_t, std::shared_ptr<pldm::platform_mc::Terminus>> termini; |
| |
| std::vector<uint8_t> pdr1{ |
| 0x1, |
| 0x0, |
| 0x0, |
| 0x0, // record handle |
| 0x1, // PDRHeaderVersion |
| PLDM_NUMERIC_SENSOR_PDR, // PDRType |
| 0x0, |
| 0x0, // recordChangeNumber |
| PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH + |
| PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH + |
| PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH, |
| 0, // dataLength |
| 0, |
| 0, // PLDMTerminusHandle |
| 0x1, |
| 0x0, // sensorID=1 |
| PLDM_ENTITY_POWER_SUPPLY, |
| 0, // entityType=Power Supply(120) |
| 1, |
| 0, // entityInstanceNumber |
| 0x1, |
| 0x0, // containerID=1 |
| PLDM_NO_INIT, // sensorInit |
| false, // sensorAuxiliaryNamesPDR |
| PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C |
| 1, // unitModifier = 1 |
| 0, // rateUnit |
| 0, // baseOEMUnitHandle |
| 0, // auxUnit |
| 0, // auxUnitModifier |
| 0, // auxRateUnit |
| 0, // rel |
| 0, // auxOEMUnitHandle |
| true, // isLinear |
| PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize |
| 0, |
| 0, |
| 0xc0, |
| 0x3f, // resolution=1.5 |
| 0, |
| 0, |
| 0x80, |
| 0x3f, // offset=1.0 |
| 0, |
| 0, // accuracy |
| 0, // plusTolerance |
| 0, // minusTolerance |
| 2, // hysteresis |
| 0, // supportedThresholds |
| 0, // thresholdAndHysteresisVolatility |
| 0, |
| 0, |
| 0x80, |
| 0x3f, // stateTransistionInterval=1.0 |
| 0, |
| 0, |
| 0x80, |
| 0x3f, // updateInverval=1.0 |
| 255, // maxReadable |
| 0, // minReadable |
| PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat |
| 0, // rangeFieldsupport |
| 0, // nominalValue |
| 0, // normalMax |
| 0, // normalMin |
| 0, // warningHigh |
| 0, // warningLow |
| 0, // criticalHigh |
| 0, // criticalLow |
| 0, // fatalHigh |
| 0 // fatalLow |
| }; |
| |
| std::vector<uint8_t> pdr2{ |
| 0x1, 0x0, 0x0, |
| 0x0, // record handle |
| 0x1, // PDRHeaderVersion |
| PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType |
| 0x1, |
| 0x0, // recordChangeNumber |
| 0x11, |
| 0, // dataLength |
| /* Entity Auxiliary Names PDR Data*/ |
| 3, |
| 0x80, // entityType system software |
| 0x1, |
| 0x0, // Entity instance number =1 |
| 0, |
| 0, // Overal system |
| 0, // shared Name Count one name only |
| 01, // nameStringCount |
| 0x65, 0x6e, 0x00, |
| 0x00, // Language Tag "en" |
| 0x53, 0x00, 0x30, 0x00, |
| 0x00 // Entity Name "S0" |
| }; |
| }; |
| |
| TEST_F(SensorManagerTest, sensorPollingTest) |
| { |
| uint64_t seconds = 10; |
| uint64_t expectedTimes = (seconds * 1000) / SENSOR_POLLING_TIME; |
| |
| pldm_tid_t tid = 1; |
| termini[tid] = std::make_shared<pldm::platform_mc::Terminus>(tid, 0); |
| termini[tid]->pdrs.push_back(pdr1); |
| termini[tid]->pdrs.push_back(pdr2); |
| termini[tid]->parseTerminusPDRs(); |
| |
| EXPECT_CALL(sensorManager, doSensorPolling(tid)) |
| .Times(Between(expectedTimes - 3, expectedTimes + 3)) |
| .WillRepeatedly(Return()); |
| |
| sensorManager.startPolling(tid); |
| |
| runEventLoopForSeconds(seconds); |
| |
| sensorManager.stopPolling(tid); |
| } |