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