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