1eac61a4bSGilbert Chen #include "common/instance_id.hpp"
2eac61a4bSGilbert Chen #include "common/types.hpp"
3eac61a4bSGilbert Chen #include "mock_sensor_manager.hpp"
4eac61a4bSGilbert Chen #include "platform-mc/terminus_manager.hpp"
5eac61a4bSGilbert Chen #include "test/test_instance_id.hpp"
6eac61a4bSGilbert Chen 
7eac61a4bSGilbert Chen #include <sdeventplus/event.hpp>
8eac61a4bSGilbert Chen 
9eac61a4bSGilbert Chen #include <gtest/gtest.h>
10eac61a4bSGilbert Chen 
1157632095SThu Nguyen using namespace ::testing;
12eac61a4bSGilbert Chen 
13eac61a4bSGilbert Chen class SensorManagerTest : public testing::Test
14eac61a4bSGilbert Chen {
15eac61a4bSGilbert Chen   protected:
16eac61a4bSGilbert Chen     SensorManagerTest() :
17eac61a4bSGilbert Chen         bus(pldm::utils::DBusHandler::getBus()),
18eac61a4bSGilbert Chen         event(sdeventplus::Event::get_default()), instanceIdDb(),
19eac61a4bSGilbert Chen         reqHandler(pldmTransport, event, instanceIdDb, false),
2051d66b59SThu Nguyen         terminusManager(event, reqHandler, instanceIdDb, termini, nullptr,
2151d66b59SThu Nguyen                         pldm::BmcMctpEid),
22eac61a4bSGilbert Chen         sensorManager(event, terminusManager, termini)
23eac61a4bSGilbert Chen     {}
24eac61a4bSGilbert Chen 
25eac61a4bSGilbert Chen     void runEventLoopForSeconds(uint64_t sec)
26eac61a4bSGilbert Chen     {
27eac61a4bSGilbert Chen         uint64_t t0 = 0;
28eac61a4bSGilbert Chen         uint64_t t1 = 0;
29eac61a4bSGilbert Chen         uint64_t usec = sec * 1000000;
30eac61a4bSGilbert Chen         uint64_t elapsed = 0;
31eac61a4bSGilbert Chen         sd_event_now(event.get(), CLOCK_MONOTONIC, &t0);
32eac61a4bSGilbert Chen         do
33eac61a4bSGilbert Chen         {
34eac61a4bSGilbert Chen             if (!sd_event_run(event.get(), usec - elapsed))
35eac61a4bSGilbert Chen             {
36eac61a4bSGilbert Chen                 break;
37eac61a4bSGilbert Chen             }
38eac61a4bSGilbert Chen             sd_event_now(event.get(), CLOCK_MONOTONIC, &t1);
39eac61a4bSGilbert Chen             elapsed = t1 - t0;
40eac61a4bSGilbert Chen         } while (elapsed < usec);
41eac61a4bSGilbert Chen     }
42eac61a4bSGilbert Chen 
43eac61a4bSGilbert Chen     PldmTransport* pldmTransport = nullptr;
44e2b83747SPatrick Williams     sdbusplus::bus_t& bus;
45eac61a4bSGilbert Chen     sdeventplus::Event event;
46eac61a4bSGilbert Chen     TestInstanceIdDb instanceIdDb;
47eac61a4bSGilbert Chen     pldm::requester::Handler<pldm::requester::Request> reqHandler;
48eac61a4bSGilbert Chen     pldm::platform_mc::TerminusManager terminusManager;
49eac61a4bSGilbert Chen     pldm::platform_mc::MockSensorManager sensorManager;
50eac61a4bSGilbert Chen     std::map<pldm_tid_t, std::shared_ptr<pldm::platform_mc::Terminus>> termini;
51eac61a4bSGilbert Chen 
52eac61a4bSGilbert Chen     std::vector<uint8_t> pdr1{
53eac61a4bSGilbert Chen         0x1,
54eac61a4bSGilbert Chen         0x0,
55eac61a4bSGilbert Chen         0x0,
56eac61a4bSGilbert Chen         0x0,                     // record handle
57eac61a4bSGilbert Chen         0x1,                     // PDRHeaderVersion
58eac61a4bSGilbert Chen         PLDM_NUMERIC_SENSOR_PDR, // PDRType
59eac61a4bSGilbert Chen         0x0,
60eac61a4bSGilbert Chen         0x0,                     // recordChangeNumber
61eac61a4bSGilbert Chen         PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
62eac61a4bSGilbert Chen             PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
63eac61a4bSGilbert Chen             PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
64eac61a4bSGilbert Chen         0,                             // dataLength
65eac61a4bSGilbert Chen         0,
66eac61a4bSGilbert Chen         0,                             // PLDMTerminusHandle
67eac61a4bSGilbert Chen         0x1,
68eac61a4bSGilbert Chen         0x0,                           // sensorID=1
69eac61a4bSGilbert Chen         PLDM_ENTITY_POWER_SUPPLY,
70eac61a4bSGilbert Chen         0,                             // entityType=Power Supply(120)
71eac61a4bSGilbert Chen         1,
72eac61a4bSGilbert Chen         0,                             // entityInstanceNumber
73eac61a4bSGilbert Chen         0x1,
74eac61a4bSGilbert Chen         0x0,                           // containerID=1
75eac61a4bSGilbert Chen         PLDM_NO_INIT,                  // sensorInit
76eac61a4bSGilbert Chen         false,                         // sensorAuxiliaryNamesPDR
77eac61a4bSGilbert Chen         PLDM_SENSOR_UNIT_DEGRESS_C,    // baseUint(2)=degrees C
78eac61a4bSGilbert Chen         1,                             // unitModifier = 1
79eac61a4bSGilbert Chen         0,                             // rateUnit
80eac61a4bSGilbert Chen         0,                             // baseOEMUnitHandle
81eac61a4bSGilbert Chen         0,                             // auxUnit
82eac61a4bSGilbert Chen         0,                             // auxUnitModifier
83eac61a4bSGilbert Chen         0,                             // auxRateUnit
84eac61a4bSGilbert Chen         0,                             // rel
85eac61a4bSGilbert Chen         0,                             // auxOEMUnitHandle
86eac61a4bSGilbert Chen         true,                          // isLinear
87eac61a4bSGilbert Chen         PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize
88eac61a4bSGilbert Chen         0,
89eac61a4bSGilbert Chen         0,
90eac61a4bSGilbert Chen         0xc0,
91eac61a4bSGilbert Chen         0x3f, // resolution=1.5
92eac61a4bSGilbert Chen         0,
93eac61a4bSGilbert Chen         0,
94eac61a4bSGilbert Chen         0x80,
95eac61a4bSGilbert Chen         0x3f, // offset=1.0
96eac61a4bSGilbert Chen         0,
97eac61a4bSGilbert Chen         0,    // accuracy
98eac61a4bSGilbert Chen         0,    // plusTolerance
99eac61a4bSGilbert Chen         0,    // minusTolerance
100eac61a4bSGilbert Chen         2,    // hysteresis
101eac61a4bSGilbert Chen         0,    // supportedThresholds
102eac61a4bSGilbert Chen         0,    // thresholdAndHysteresisVolatility
103eac61a4bSGilbert Chen         0,
104eac61a4bSGilbert Chen         0,
105eac61a4bSGilbert Chen         0x80,
106eac61a4bSGilbert Chen         0x3f, // stateTransistionInterval=1.0
107eac61a4bSGilbert Chen         0,
108eac61a4bSGilbert Chen         0,
109eac61a4bSGilbert Chen         0x80,
110eac61a4bSGilbert Chen         0x3f,                          // updateInverval=1.0
111eac61a4bSGilbert Chen         255,                           // maxReadable
112eac61a4bSGilbert Chen         0,                             // minReadable
113eac61a4bSGilbert Chen         PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
114eac61a4bSGilbert Chen         0,                             // rangeFieldsupport
115eac61a4bSGilbert Chen         0,                             // nominalValue
116eac61a4bSGilbert Chen         0,                             // normalMax
117eac61a4bSGilbert Chen         0,                             // normalMin
118eac61a4bSGilbert Chen         0,                             // warningHigh
119eac61a4bSGilbert Chen         0,                             // warningLow
120eac61a4bSGilbert Chen         0,                             // criticalHigh
121eac61a4bSGilbert Chen         0,                             // criticalLow
122eac61a4bSGilbert Chen         0,                             // fatalHigh
123eac61a4bSGilbert Chen         0                              // fatalLow
124eac61a4bSGilbert Chen     };
125eac61a4bSGilbert Chen 
126eac61a4bSGilbert Chen     std::vector<uint8_t> pdr2{
127eac61a4bSGilbert Chen         0x1, 0x0, 0x0,
128eac61a4bSGilbert Chen         0x0,                             // record handle
129eac61a4bSGilbert Chen         0x1,                             // PDRHeaderVersion
130eac61a4bSGilbert Chen         PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
131eac61a4bSGilbert Chen         0x1,
132eac61a4bSGilbert Chen         0x0,                             // recordChangeNumber
133eac61a4bSGilbert Chen         0x11,
134eac61a4bSGilbert Chen         0,                               // dataLength
135eac61a4bSGilbert Chen         /* Entity Auxiliary Names PDR Data*/
136eac61a4bSGilbert Chen         3,
137eac61a4bSGilbert Chen         0x80, // entityType system software
138eac61a4bSGilbert Chen         0x1,
139eac61a4bSGilbert Chen         0x0,  // Entity instance number =1
140eac61a4bSGilbert Chen         0,
141*04ac9971SManojkiran Eda         0,    // Overall system
142eac61a4bSGilbert Chen         0,    // shared Name Count one name only
143eac61a4bSGilbert Chen         01,   // nameStringCount
144eac61a4bSGilbert Chen         0x65, 0x6e, 0x00,
145eac61a4bSGilbert Chen         0x00, // Language Tag "en"
146eac61a4bSGilbert Chen         0x53, 0x00, 0x30, 0x00,
147eac61a4bSGilbert Chen         0x00  // Entity Name "S0"
148eac61a4bSGilbert Chen     };
149eac61a4bSGilbert Chen };
150eac61a4bSGilbert Chen 
151eac61a4bSGilbert Chen TEST_F(SensorManagerTest, sensorPollingTest)
152eac61a4bSGilbert Chen {
153eac61a4bSGilbert Chen     uint64_t seconds = 10;
154eac61a4bSGilbert Chen 
155eac61a4bSGilbert Chen     pldm_tid_t tid = 1;
156eac61a4bSGilbert Chen     termini[tid] = std::make_shared<pldm::platform_mc::Terminus>(tid, 0);
157eac61a4bSGilbert Chen     termini[tid]->pdrs.push_back(pdr1);
158eac61a4bSGilbert Chen     termini[tid]->pdrs.push_back(pdr2);
159eac61a4bSGilbert Chen     termini[tid]->parseTerminusPDRs();
160eac61a4bSGilbert Chen 
16157632095SThu Nguyen     uint64_t t0, t1;
16257632095SThu Nguyen     ASSERT_TRUE(sd_event_now(event.get(), CLOCK_MONOTONIC, &t0) >= 0);
16357632095SThu Nguyen     ON_CALL(sensorManager, doSensorPolling(tid))
16457632095SThu Nguyen         .WillByDefault([this, &t0, &t1](unsigned char) {
16557632095SThu Nguyen             ASSERT_TRUE(sd_event_now(event.get(), CLOCK_MONOTONIC, &t1) >= 0);
16657632095SThu Nguyen             EXPECT_GE(t1 - t0, SENSOR_POLLING_TIME * 1000);
16757632095SThu Nguyen             t0 = t1;
16857632095SThu Nguyen         });
169eac61a4bSGilbert Chen     EXPECT_CALL(sensorManager, doSensorPolling(tid))
17057632095SThu Nguyen         .Times(AtLeast(2))
171eac61a4bSGilbert Chen         .WillRepeatedly(Return());
172eac61a4bSGilbert Chen 
173eac61a4bSGilbert Chen     sensorManager.startPolling(tid);
174eac61a4bSGilbert Chen 
175eac61a4bSGilbert Chen     runEventLoopForSeconds(seconds);
176eac61a4bSGilbert Chen 
177eac61a4bSGilbert Chen     sensorManager.stopPolling(tid);
178eac61a4bSGilbert Chen }
179