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