1eac61a4bSGilbert Chen
2eac61a4bSGilbert Chen #include "platform-mc/numeric_sensor.hpp"
3eac61a4bSGilbert Chen #include "platform-mc/terminus.hpp"
4eac61a4bSGilbert Chen
5*fe252795SManojkiran Eda #include <libpldm/entity.h>
6*fe252795SManojkiran Eda #include <libpldm/platform.h>
7*fe252795SManojkiran Eda
8eac61a4bSGilbert Chen #include <gtest/gtest.h>
9eac61a4bSGilbert Chen
TEST(NumericSensor,conversionFormula)10eac61a4bSGilbert Chen TEST(NumericSensor, conversionFormula)
11eac61a4bSGilbert Chen {
12eac61a4bSGilbert Chen std::vector<uint8_t> pdr1{
13eac61a4bSGilbert Chen 0x1,
14eac61a4bSGilbert Chen 0x0,
15eac61a4bSGilbert Chen 0x0,
16eac61a4bSGilbert Chen 0x0, // record handle
17eac61a4bSGilbert Chen 0x1, // PDRHeaderVersion
18eac61a4bSGilbert Chen PLDM_NUMERIC_SENSOR_PDR, // PDRType
19eac61a4bSGilbert Chen 0x0,
20eac61a4bSGilbert Chen 0x0, // recordChangeNumber
21eac61a4bSGilbert Chen PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
22eac61a4bSGilbert Chen PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
23eac61a4bSGilbert Chen PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
24eac61a4bSGilbert Chen 0, // dataLength
25eac61a4bSGilbert Chen 0,
26eac61a4bSGilbert Chen 0, // PLDMTerminusHandle
27eac61a4bSGilbert Chen 0x1,
28eac61a4bSGilbert Chen 0x0, // sensorID=1
29eac61a4bSGilbert Chen PLDM_ENTITY_POWER_SUPPLY,
30eac61a4bSGilbert Chen 0, // entityType=Power Supply(120)
31eac61a4bSGilbert Chen 1,
32eac61a4bSGilbert Chen 0, // entityInstanceNumber
33eac61a4bSGilbert Chen 0x1,
34eac61a4bSGilbert Chen 0x0, // containerID=1
35eac61a4bSGilbert Chen PLDM_NO_INIT, // sensorInit
36eac61a4bSGilbert Chen false, // sensorAuxiliaryNamesPDR
37eac61a4bSGilbert Chen PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
38eac61a4bSGilbert Chen 1, // unitModifier = 1
39eac61a4bSGilbert Chen 0, // rateUnit
40eac61a4bSGilbert Chen 0, // baseOEMUnitHandle
41eac61a4bSGilbert Chen 0, // auxUnit
42eac61a4bSGilbert Chen 0, // auxUnitModifier
43eac61a4bSGilbert Chen 0, // auxRateUnit
44eac61a4bSGilbert Chen 0, // rel
45eac61a4bSGilbert Chen 0, // auxOEMUnitHandle
46eac61a4bSGilbert Chen true, // isLinear
47eac61a4bSGilbert Chen PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize
48eac61a4bSGilbert Chen 0,
49eac61a4bSGilbert Chen 0,
50eac61a4bSGilbert Chen 0xc0,
51eac61a4bSGilbert Chen 0x3f, // resolution=1.5
52eac61a4bSGilbert Chen 0,
53eac61a4bSGilbert Chen 0,
54eac61a4bSGilbert Chen 0x80,
55eac61a4bSGilbert Chen 0x3f, // offset=1.0
56eac61a4bSGilbert Chen 0,
57eac61a4bSGilbert Chen 0, // accuracy
58eac61a4bSGilbert Chen 0, // plusTolerance
59eac61a4bSGilbert Chen 0, // minusTolerance
60eac61a4bSGilbert Chen 2, // hysteresis
61eac61a4bSGilbert Chen 0, // supportedThresholds
62eac61a4bSGilbert Chen 0, // thresholdAndHysteresisVolatility
63eac61a4bSGilbert Chen 0,
64eac61a4bSGilbert Chen 0,
65eac61a4bSGilbert Chen 0x80,
66eac61a4bSGilbert Chen 0x3f, // stateTransistionInterval=1.0
67eac61a4bSGilbert Chen 0,
68eac61a4bSGilbert Chen 0,
69eac61a4bSGilbert Chen 0x80,
70eac61a4bSGilbert Chen 0x3f, // updateInverval=1.0
71eac61a4bSGilbert Chen 255, // maxReadable
72eac61a4bSGilbert Chen 0, // minReadable
73eac61a4bSGilbert Chen PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
74eac61a4bSGilbert Chen 0, // rangeFieldsupport
75eac61a4bSGilbert Chen 0, // nominalValue
76eac61a4bSGilbert Chen 0, // normalMax
77eac61a4bSGilbert Chen 0, // normalMin
78eac61a4bSGilbert Chen 0, // warningHigh
79eac61a4bSGilbert Chen 0, // warningLow
80eac61a4bSGilbert Chen 0, // criticalHigh
81eac61a4bSGilbert Chen 0, // criticalLow
82eac61a4bSGilbert Chen 0, // fatalHigh
83eac61a4bSGilbert Chen 0 // fatalLow
84eac61a4bSGilbert Chen };
85eac61a4bSGilbert Chen
86eac61a4bSGilbert Chen auto numericSensorPdr = std::make_shared<pldm_numeric_sensor_value_pdr>();
87eac61a4bSGilbert Chen std::printf("pdr size=%ld\n", pdr1.size());
88eac61a4bSGilbert Chen auto rc = decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(),
89eac61a4bSGilbert Chen numericSensorPdr.get());
90eac61a4bSGilbert Chen EXPECT_EQ(rc, PLDM_SUCCESS);
91eac61a4bSGilbert Chen
92eac61a4bSGilbert Chen std::string sensorName{"test1"};
93eac61a4bSGilbert Chen std::string inventoryPath{
94eac61a4bSGilbert Chen "/xyz/openbmc_project/inventroy/Item/Board/PLDM_device_1"};
95eac61a4bSGilbert Chen pldm::platform_mc::NumericSensor sensor(0x01, true, numericSensorPdr,
96eac61a4bSGilbert Chen sensorName, inventoryPath);
97eac61a4bSGilbert Chen double reading = 40.0;
98eac61a4bSGilbert Chen double convertedValue = 0;
99eac61a4bSGilbert Chen convertedValue = sensor.conversionFormula(reading);
100eac61a4bSGilbert Chen convertedValue = sensor.unitModifier(convertedValue);
101eac61a4bSGilbert Chen
102eac61a4bSGilbert Chen // (40*1.5 + 1.0 ) * 10^1 = 610
103eac61a4bSGilbert Chen EXPECT_EQ(610, convertedValue);
104eac61a4bSGilbert Chen }
105eac61a4bSGilbert Chen
TEST(NumericSensor,checkThreshold)106eac61a4bSGilbert Chen TEST(NumericSensor, checkThreshold)
107eac61a4bSGilbert Chen {
108eac61a4bSGilbert Chen std::vector<uint8_t> pdr1{
109eac61a4bSGilbert Chen 0x1,
110eac61a4bSGilbert Chen 0x0,
111eac61a4bSGilbert Chen 0x0,
112eac61a4bSGilbert Chen 0x0, // record handle
113eac61a4bSGilbert Chen 0x1, // PDRHeaderVersion
114eac61a4bSGilbert Chen PLDM_NUMERIC_SENSOR_PDR, // PDRType
115eac61a4bSGilbert Chen 0x0,
116eac61a4bSGilbert Chen 0x0, // recordChangeNumber
117eac61a4bSGilbert Chen PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
118eac61a4bSGilbert Chen PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
119eac61a4bSGilbert Chen PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
120eac61a4bSGilbert Chen 0, // dataLength
121eac61a4bSGilbert Chen 0,
122eac61a4bSGilbert Chen 0, // PLDMTerminusHandle
123eac61a4bSGilbert Chen 0x1,
124eac61a4bSGilbert Chen 0x0, // sensorID=1
125eac61a4bSGilbert Chen PLDM_ENTITY_POWER_SUPPLY,
126eac61a4bSGilbert Chen 0, // entityType=Power Supply(120)
127eac61a4bSGilbert Chen 1,
128eac61a4bSGilbert Chen 0, // entityInstanceNumber
129eac61a4bSGilbert Chen 0x1,
130eac61a4bSGilbert Chen 0x0, // containerID=1
131eac61a4bSGilbert Chen PLDM_NO_INIT, // sensorInit
132eac61a4bSGilbert Chen false, // sensorAuxiliaryNamesPDR
133eac61a4bSGilbert Chen PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
134eac61a4bSGilbert Chen 1, // unitModifier = 1
135eac61a4bSGilbert Chen 0, // rateUnit
136eac61a4bSGilbert Chen 0, // baseOEMUnitHandle
137eac61a4bSGilbert Chen 0, // auxUnit
138eac61a4bSGilbert Chen 0, // auxUnitModifier
139eac61a4bSGilbert Chen 0, // auxRateUnit
140eac61a4bSGilbert Chen 0, // rel
141eac61a4bSGilbert Chen 0, // auxOEMUnitHandle
142eac61a4bSGilbert Chen true, // isLinear
143eac61a4bSGilbert Chen PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize
144eac61a4bSGilbert Chen 0,
145eac61a4bSGilbert Chen 0,
146eac61a4bSGilbert Chen 0xc0,
147eac61a4bSGilbert Chen 0x3f, // resolution=1.5
148eac61a4bSGilbert Chen 0,
149eac61a4bSGilbert Chen 0,
150eac61a4bSGilbert Chen 0x80,
151eac61a4bSGilbert Chen 0x3f, // offset=1.0
152eac61a4bSGilbert Chen 0,
153eac61a4bSGilbert Chen 0, // accuracy
154eac61a4bSGilbert Chen 0, // plusTolerance
155eac61a4bSGilbert Chen 0, // minusTolerance
156eac61a4bSGilbert Chen 2, // hysteresis
157eac61a4bSGilbert Chen 0, // supportedThresholds
158eac61a4bSGilbert Chen 0, // thresholdAndHysteresisVolatility
159eac61a4bSGilbert Chen 0,
160eac61a4bSGilbert Chen 0,
161eac61a4bSGilbert Chen 0x80,
162eac61a4bSGilbert Chen 0x3f, // stateTransistionInterval=1.0
163eac61a4bSGilbert Chen 0,
164eac61a4bSGilbert Chen 0,
165eac61a4bSGilbert Chen 0x80,
166eac61a4bSGilbert Chen 0x3f, // updateInverval=1.0
167eac61a4bSGilbert Chen 255, // maxReadable
168eac61a4bSGilbert Chen 0, // minReadable
169eac61a4bSGilbert Chen PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
170eac61a4bSGilbert Chen 0, // rangeFieldsupport
171eac61a4bSGilbert Chen 0, // nominalValue
172eac61a4bSGilbert Chen 0, // normalMax
173eac61a4bSGilbert Chen 0, // normalMin
174eac61a4bSGilbert Chen 0, // warningHigh
175eac61a4bSGilbert Chen 0, // warningLow
176eac61a4bSGilbert Chen 0, // criticalHigh
177eac61a4bSGilbert Chen 0, // criticalLow
178eac61a4bSGilbert Chen 0, // fatalHigh
179eac61a4bSGilbert Chen 0 // fatalLow
180eac61a4bSGilbert Chen };
181eac61a4bSGilbert Chen
182eac61a4bSGilbert Chen auto numericSensorPdr = std::make_shared<pldm_numeric_sensor_value_pdr>();
183eac61a4bSGilbert Chen auto rc = decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(),
184eac61a4bSGilbert Chen numericSensorPdr.get());
185eac61a4bSGilbert Chen EXPECT_EQ(rc, PLDM_SUCCESS);
186eac61a4bSGilbert Chen std::string sensorName{"test1"};
187eac61a4bSGilbert Chen std::string inventoryPath{
188eac61a4bSGilbert Chen "/xyz/openbmc_project/inventroy/Item/Board/PLDM_device_1"};
189eac61a4bSGilbert Chen pldm::platform_mc::NumericSensor sensor(0x01, true, numericSensorPdr,
190eac61a4bSGilbert Chen sensorName, inventoryPath);
191eac61a4bSGilbert Chen
192eac61a4bSGilbert Chen bool highAlarm = false;
193eac61a4bSGilbert Chen bool lowAlarm = false;
194eac61a4bSGilbert Chen double highThreshold = 40;
195eac61a4bSGilbert Chen double lowThreshold = 30;
196eac61a4bSGilbert Chen double hysteresis = 2;
197eac61a4bSGilbert Chen
198eac61a4bSGilbert Chen // reading 35->40->45->38->35->30->25->32->35
199eac61a4bSGilbert Chen // highAlarm F->T ->T ->T ->F ->F ->F -> F-> F
200eac61a4bSGilbert Chen // lowAlarm F->F ->F ->F ->F ->T ->T -> T ->F
201eac61a4bSGilbert Chen double reading = 35;
202eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
203eac61a4bSGilbert Chen hysteresis);
204eac61a4bSGilbert Chen EXPECT_EQ(false, highAlarm);
205eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
206eac61a4bSGilbert Chen hysteresis);
207eac61a4bSGilbert Chen EXPECT_EQ(false, lowAlarm);
208eac61a4bSGilbert Chen
209eac61a4bSGilbert Chen reading = 40;
210eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
211eac61a4bSGilbert Chen hysteresis);
212eac61a4bSGilbert Chen EXPECT_EQ(true, highAlarm);
213eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
214eac61a4bSGilbert Chen hysteresis);
215eac61a4bSGilbert Chen EXPECT_EQ(false, lowAlarm);
216eac61a4bSGilbert Chen
217eac61a4bSGilbert Chen reading = 45;
218eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
219eac61a4bSGilbert Chen hysteresis);
220eac61a4bSGilbert Chen EXPECT_EQ(true, highAlarm);
221eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
222eac61a4bSGilbert Chen hysteresis);
223eac61a4bSGilbert Chen EXPECT_EQ(false, lowAlarm);
224eac61a4bSGilbert Chen
225eac61a4bSGilbert Chen reading = 38;
226eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
227eac61a4bSGilbert Chen hysteresis);
228eac61a4bSGilbert Chen EXPECT_EQ(true, highAlarm);
229eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
230eac61a4bSGilbert Chen hysteresis);
231eac61a4bSGilbert Chen EXPECT_EQ(false, lowAlarm);
232eac61a4bSGilbert Chen
233eac61a4bSGilbert Chen reading = 35;
234eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
235eac61a4bSGilbert Chen hysteresis);
236eac61a4bSGilbert Chen EXPECT_EQ(false, highAlarm);
237eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
238eac61a4bSGilbert Chen hysteresis);
239eac61a4bSGilbert Chen EXPECT_EQ(false, lowAlarm);
240eac61a4bSGilbert Chen
241eac61a4bSGilbert Chen reading = 30;
242eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
243eac61a4bSGilbert Chen hysteresis);
244eac61a4bSGilbert Chen EXPECT_EQ(false, highAlarm);
245eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
246eac61a4bSGilbert Chen hysteresis);
247eac61a4bSGilbert Chen EXPECT_EQ(true, lowAlarm);
248eac61a4bSGilbert Chen
249eac61a4bSGilbert Chen reading = 25;
250eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
251eac61a4bSGilbert Chen hysteresis);
252eac61a4bSGilbert Chen EXPECT_EQ(false, highAlarm);
253eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
254eac61a4bSGilbert Chen hysteresis);
255eac61a4bSGilbert Chen EXPECT_EQ(true, lowAlarm);
256eac61a4bSGilbert Chen
257eac61a4bSGilbert Chen reading = 32;
258eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
259eac61a4bSGilbert Chen hysteresis);
260eac61a4bSGilbert Chen EXPECT_EQ(false, highAlarm);
261eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
262eac61a4bSGilbert Chen hysteresis);
263eac61a4bSGilbert Chen EXPECT_EQ(true, lowAlarm);
264eac61a4bSGilbert Chen
265eac61a4bSGilbert Chen reading = 35;
266eac61a4bSGilbert Chen highAlarm = sensor.checkThreshold(highAlarm, true, reading, highThreshold,
267eac61a4bSGilbert Chen hysteresis);
268eac61a4bSGilbert Chen EXPECT_EQ(false, highAlarm);
269eac61a4bSGilbert Chen lowAlarm = sensor.checkThreshold(lowAlarm, false, reading, lowThreshold,
270eac61a4bSGilbert Chen hysteresis);
271eac61a4bSGilbert Chen EXPECT_EQ(false, lowAlarm);
272eac61a4bSGilbert Chen }
273