blob: f0ad7c24ea4075bbbbf7a5c0af86450525f531d3 [file] [log] [blame]
Bob Kingd6820bb2020-04-28 15:37:02 +08001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "action_environment.hpp"
Bob King717d2da2020-06-02 11:11:15 +080017#include "action_error.hpp"
18#include "device.hpp"
Bob Kingd6820bb2020-04-28 15:37:02 +080019#include "i2c_action.hpp"
20#include "i2c_interface.hpp"
Bob King717d2da2020-06-02 11:11:15 +080021#include "id_map.hpp"
Bob King73eacee2020-10-23 13:58:02 +080022#include "mock_services.hpp"
Bob King717d2da2020-06-02 11:11:15 +080023#include "mocked_i2c_interface.hpp"
24#include "pmbus_error.hpp"
Bob Kingd6820bb2020-04-28 15:37:02 +080025#include "pmbus_read_sensor_action.hpp"
26#include "pmbus_utils.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050027#include "sensors.hpp"
Bob Kingd6820bb2020-04-28 15:37:02 +080028
29#include <cstdint>
Bob King717d2da2020-06-02 11:11:15 +080030#include <memory>
Bob Kingd6820bb2020-04-28 15:37:02 +080031#include <optional>
32#include <stdexcept>
33#include <string>
Bob King717d2da2020-06-02 11:11:15 +080034#include <utility>
Bob Kingd6820bb2020-04-28 15:37:02 +080035
Bob King717d2da2020-06-02 11:11:15 +080036#include <gmock/gmock.h>
Bob Kingd6820bb2020-04-28 15:37:02 +080037#include <gtest/gtest.h>
38
39using namespace phosphor::power::regulators;
40
Bob King717d2da2020-06-02 11:11:15 +080041using ::testing::A;
42using ::testing::Return;
43using ::testing::SetArgReferee;
44using ::testing::Throw;
45using ::testing::TypedEq;
46
Bob Kingd6820bb2020-04-28 15:37:02 +080047TEST(PMBusReadSensorActionTests, Constructor)
48{
49 // Test where works: exponent value is specified
50 try
51 {
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050052 SensorType type{SensorType::iout};
Bob Kingd6820bb2020-04-28 15:37:02 +080053 uint8_t command = 0x8C;
54 pmbus_utils::SensorDataFormat format{
55 pmbus_utils::SensorDataFormat::linear_16};
56 std::optional<int8_t> exponent{-8};
57 PMBusReadSensorAction action{type, command, format, exponent};
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050058 EXPECT_EQ(action.getType(), SensorType::iout);
Bob Kingd6820bb2020-04-28 15:37:02 +080059 EXPECT_EQ(action.getCommand(), 0x8C);
60 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16);
61 EXPECT_EQ(action.getExponent().has_value(), true);
62 EXPECT_EQ(action.getExponent().value(), -8);
63 }
64 catch (...)
65 {
66 ADD_FAILURE() << "Should not have caught exception.";
67 }
68
69 // Test where works: exponent value is not specified
70 try
71 {
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050072 SensorType type{SensorType::iout};
Bob Kingd6820bb2020-04-28 15:37:02 +080073 uint8_t command = 0x8C;
74 pmbus_utils::SensorDataFormat format{
75 pmbus_utils::SensorDataFormat::linear_11};
76 std::optional<int8_t> exponent{};
77 PMBusReadSensorAction action{type, command, format, exponent};
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050078 EXPECT_EQ(action.getType(), SensorType::iout);
Bob Kingd6820bb2020-04-28 15:37:02 +080079 EXPECT_EQ(action.getCommand(), 0x8C);
80 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_11);
81 EXPECT_EQ(action.getExponent().has_value(), false);
82 }
83 catch (...)
84 {
85 ADD_FAILURE() << "Should not have caught exception.";
86 }
87}
88
89TEST(PMBusReadSensorActionTests, Execute)
90{
Bob King717d2da2020-06-02 11:11:15 +080091 // Test where works: linear_11 defined in action
92 try
93 {
94 // Create mock I2CInterface.
95 // * will read 0xD2E0 from READ_IOUT (command/register 0x8C)
96 // * will not read from VOUT_MODE (command/register 0x20)
97 // assume output current is 11.5 amps,
98 // exponent = -6 = 11010, mantissa = 736 = 010 1110 0000
99 // linear data format = 1101 0010 1110 0000 = 0xD2E0
100 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
101 std::make_unique<i2c::MockedI2CInterface>();
102 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
103 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
104 .Times(1)
105 .WillOnce(SetArgReferee<1>(0xD2E0));
106 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
107
Bob King73eacee2020-10-23 13:58:02 +0800108 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800109 Device device{
110 "reg1", true,
111 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
112 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800113 IDMap idMap{};
114 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800115 MockServices services{};
116 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800117
118 // Create and execute action
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500119 SensorType type{SensorType::iout};
Bob King717d2da2020-06-02 11:11:15 +0800120 uint8_t command = 0x8C;
121 pmbus_utils::SensorDataFormat format{
122 pmbus_utils::SensorDataFormat::linear_11};
123 std::optional<int8_t> exponent{};
124 PMBusReadSensorAction action{type, command, format, exponent};
125 EXPECT_EQ(action.execute(env), true);
Shawn McCarney5135df62021-04-28 15:53:24 -0500126 /**
127 * TODO: Replace with EXPECT calls using MockSensors
128 *
129 * EXPECT_EQ(env.getSensorReadings().size(), 1);
130 * EXPECT_EQ(env.getSensorReadings()[0].type,
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500131 * SensorType::iout);
Shawn McCarney5135df62021-04-28 15:53:24 -0500132 * EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 11.5);
133 */
Bob King717d2da2020-06-02 11:11:15 +0800134 }
135 catch (...)
136 {
137 ADD_FAILURE() << "Should not have caught exception.";
138 }
139
140 // Test where works: linear_16 with exponent defined in action
141 try
142 {
143 // Create mock I2CInterface.
144 // * will read 0x0002 from READ_VOUT (command/register 0x8B)
145 // * will not read from VOUT_MODE (command/register 0x20)
146 // assume output voltage is 16 volts,
147 // exponent = 3
148 // linear data format = 0000 0000 0000 0010 = 0x0002 = 2
149 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
150 std::make_unique<i2c::MockedI2CInterface>();
151 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
152 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8B), A<uint16_t&>()))
153 .Times(1)
154 .WillOnce(SetArgReferee<1>(0x0002));
155 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
156
Bob King73eacee2020-10-23 13:58:02 +0800157 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800158 Device device{
159 "reg1", true,
160 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
161 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800162 IDMap idMap{};
163 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800164 MockServices services{};
165 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800166
167 // Create and execute action
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500168 SensorType type{SensorType::vout};
Bob King717d2da2020-06-02 11:11:15 +0800169 uint8_t command = 0x8B;
170 pmbus_utils::SensorDataFormat format{
171 pmbus_utils::SensorDataFormat::linear_16};
172 std::optional<int8_t> exponent{3};
173 PMBusReadSensorAction action{type, command, format, exponent};
174 EXPECT_EQ(action.execute(env), true);
Shawn McCarney5135df62021-04-28 15:53:24 -0500175 /**
176 * TODO: Replace with EXPECT calls using MockSensors
177 *
178 * EXPECT_EQ(env.getSensorReadings().size(), 1);
179 * EXPECT_EQ(env.getSensorReadings()[0].type,
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500180 * SensorType::vout);
Shawn McCarney5135df62021-04-28 15:53:24 -0500181 * EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 16);
182 */
Bob King717d2da2020-06-02 11:11:15 +0800183 }
184 catch (...)
185 {
186 ADD_FAILURE() << "Should not have caught exception.";
187 }
188
189 // Test where works: linear_16 with no exponent defined in action
190 try
191 {
192 // Create mock I2CInterface.
193 // * will read 0xB877 from vout_peak (command/register 0xC6)
194 // * will read 0b0001'0111 (linear format, -9 exponent) from VOUT_MODE
195 // assume output voltage is 0.232421875 volts,
196 // linear data format = 0000 0000 0111 0111 = 0x0077
197 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
198 std::make_unique<i2c::MockedI2CInterface>();
199 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
200 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0xC6), A<uint16_t&>()))
201 .Times(1)
202 .WillOnce(SetArgReferee<1>(0x0077));
203 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>()))
204 .Times(1)
205 .WillOnce(SetArgReferee<1>(0b0001'0111));
Bob King73eacee2020-10-23 13:58:02 +0800206 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800207 Device device{
208 "reg1", true,
209 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
210 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800211 IDMap idMap{};
212 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800213 MockServices services{};
214 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800215
216 // Create and execute action
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500217 SensorType type{SensorType::vout_peak};
Bob King717d2da2020-06-02 11:11:15 +0800218 uint8_t command = 0xC6;
219 pmbus_utils::SensorDataFormat format{
220 pmbus_utils::SensorDataFormat::linear_16};
221 std::optional<int8_t> exponent{};
222 PMBusReadSensorAction action{type, command, format, exponent};
223 EXPECT_EQ(action.execute(env), true);
Shawn McCarney5135df62021-04-28 15:53:24 -0500224 /**
225 * TODO: Replace with EXPECT calls using MockSensors
226 *
227 * EXPECT_EQ(env.getSensorReadings().size(), 1);
228 * EXPECT_EQ(env.getSensorReadings()[0].type,
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500229 * SensorType::vout_peak);
Shawn McCarney5135df62021-04-28 15:53:24 -0500230 * EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 0.232421875);
231 */
Bob King717d2da2020-06-02 11:11:15 +0800232 }
233 catch (...)
234 {
235 ADD_FAILURE() << "Should not have caught exception.";
236 }
237
238 // Test where fails: Unable to get I2C interface to current device
239 try
240 {
Bob King73eacee2020-10-23 13:58:02 +0800241 // Create IDMap, MockServices, and ActionEnvironment
Bob King717d2da2020-06-02 11:11:15 +0800242 IDMap idMap{};
Bob King73eacee2020-10-23 13:58:02 +0800243 MockServices services{};
244 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800245
246 // Create and execute action
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500247 SensorType type{SensorType::pout};
Bob King717d2da2020-06-02 11:11:15 +0800248 uint8_t command = 0x96;
249 pmbus_utils::SensorDataFormat format{
250 pmbus_utils::SensorDataFormat::linear_11};
251 std::optional<int8_t> exponent{};
252 PMBusReadSensorAction action{type, command, format, exponent};
253 action.execute(env);
254 ADD_FAILURE() << "Should not have reached this line.";
255 }
256 catch (const std::invalid_argument& e)
257 {
258 EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\"");
259 }
260 catch (...)
261 {
262 ADD_FAILURE() << "Should not have caught exception.";
263 }
264
265 // Test where fails: VOUT_MODE data format is not linear
266 try
267 {
268 // Create mock I2CInterface. Expect action to do the following:
269 // * will read 0b0010'0000 (vid data format) from VOUT_MODE
270 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
271 std::make_unique<i2c::MockedI2CInterface>();
272 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
Bob Kingab7d6cb2020-07-17 10:24:07 +0800273 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8B), A<uint16_t&>()))
274 .Times(1);
Bob King717d2da2020-06-02 11:11:15 +0800275 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>()))
276 .Times(1)
277 .WillOnce(SetArgReferee<1>(0b0010'0000));
278
Bob King73eacee2020-10-23 13:58:02 +0800279 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800280 Device device{
281 "reg1", true,
282 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
283 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800284 IDMap idMap{};
285 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800286 MockServices services{};
287 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800288
289 // Create and execute action
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500290 SensorType type{SensorType::vout};
Bob King717d2da2020-06-02 11:11:15 +0800291 uint8_t command = 0x8B;
292 pmbus_utils::SensorDataFormat format{
293 pmbus_utils::SensorDataFormat::linear_16};
294 std::optional<int8_t> exponent{};
295 PMBusReadSensorAction action{type, command, format, exponent};
296 action.execute(env);
297 ADD_FAILURE() << "Should not have reached this line.";
298 }
299 catch (const ActionError& e)
300 {
301 EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: vout, "
302 "command: 0x8B, format: linear_16 }");
303 try
304 {
305 // Re-throw inner PMBusError
306 std::rethrow_if_nested(e);
307 ADD_FAILURE() << "Should not have reached this line.";
308 }
309 catch (const PMBusError& pe)
310 {
311 EXPECT_STREQ(
312 pe.what(),
313 "PMBusError: VOUT_MODE contains unsupported data format");
Shawn McCarney5b819f42021-03-16 14:41:15 -0500314 EXPECT_EQ(pe.getDeviceID(), "reg1");
315 EXPECT_EQ(pe.getInventoryPath(), "/xyz/openbmc_project/inventory/"
316 "system/chassis/motherboard/reg1");
Bob King717d2da2020-06-02 11:11:15 +0800317 }
318 catch (...)
319 {
320 ADD_FAILURE() << "Should not have caught exception.";
321 }
322 }
323 catch (...)
324 {
325 ADD_FAILURE() << "Should not have caught exception.";
326 }
327
328 // Test where fails: Reading VOUT_MODE fails
329 try
330 {
331 // Create mock I2CInterface. Expect action to do the following:
332 // * will try to read VOUT_MODE; exception will be thrown
333 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
334 std::make_unique<i2c::MockedI2CInterface>();
335 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
Bob Kingab7d6cb2020-07-17 10:24:07 +0800336 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0xC6), A<uint16_t&>()))
337 .Times(1);
Bob King717d2da2020-06-02 11:11:15 +0800338 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>()))
339 .Times(1)
340 .WillOnce(Throw(
341 i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70}));
342
Bob King73eacee2020-10-23 13:58:02 +0800343 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800344 Device device{
345 "reg1", true,
346 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
347 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800348 IDMap idMap{};
349 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800350 MockServices services{};
351 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800352
353 // Create and execute action
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500354 SensorType type{SensorType::vout_peak};
Bob King717d2da2020-06-02 11:11:15 +0800355 uint8_t command = 0xC6;
356 pmbus_utils::SensorDataFormat format{
357 pmbus_utils::SensorDataFormat::linear_16};
358 std::optional<int8_t> exponent{};
359 PMBusReadSensorAction action{type, command, format, exponent};
360 action.execute(env);
361 ADD_FAILURE() << "Should not have reached this line.";
362 }
363 catch (const ActionError& e)
364 {
365 EXPECT_STREQ(e.what(),
366 "ActionError: pmbus_read_sensor: { type: vout_peak, "
367 "command: 0xC6, format: linear_16 }");
368 try
369 {
370 // Re-throw inner I2CException
371 std::rethrow_if_nested(e);
372 ADD_FAILURE() << "Should not have reached this line.";
373 }
374 catch (const i2c::I2CException& ie)
375 {
376 EXPECT_STREQ(
377 ie.what(),
378 "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70");
379 }
380 catch (...)
381 {
382 ADD_FAILURE() << "Should not have caught exception.";
383 }
384 }
385 catch (...)
386 {
387 ADD_FAILURE() << "Should not have caught exception.";
388 }
389
390 // Test where fails: Reading PMBus command code with sensor value fails
391 try
392 {
393 // Create mock I2CInterface. Expect action to do the following:
394 // * will try to read PMBus command(0x96); exception will be thrown
395 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
396 std::make_unique<i2c::MockedI2CInterface>();
397 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
398 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x96), A<uint16_t&>()))
399 .Times(1)
400 .WillOnce(Throw(
401 i2c::I2CException{"Failed to read word", "/dev/i2c-1", 0x70}));
402
Bob King73eacee2020-10-23 13:58:02 +0800403 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800404 Device device{
405 "reg1", true,
406 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
407 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800408 IDMap idMap{};
409 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800410 MockServices services{};
411 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800412
413 // Create and execute action
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500414 SensorType type{SensorType::pout};
Bob King717d2da2020-06-02 11:11:15 +0800415 uint8_t command = 0x96;
416 pmbus_utils::SensorDataFormat format{
417 pmbus_utils::SensorDataFormat::linear_11};
418 std::optional<int8_t> exponent{};
419 PMBusReadSensorAction action{type, command, format, exponent};
420 action.execute(env);
421 ADD_FAILURE() << "Should not have reached this line.";
422 }
423 catch (const ActionError& e)
424 {
425 EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: pout, "
426 "command: 0x96, format: linear_11 }");
427 try
428 {
429 // Re-throw inner I2CException
430 std::rethrow_if_nested(e);
431 ADD_FAILURE() << "Should not have reached this line.";
432 }
433 catch (const i2c::I2CException& ie)
434 {
435 EXPECT_STREQ(
436 ie.what(),
437 "I2CException: Failed to read word: bus /dev/i2c-1, addr 0x70");
438 }
439 catch (...)
440 {
441 ADD_FAILURE() << "Should not have caught exception.";
442 }
443 }
444 catch (...)
445 {
446 ADD_FAILURE() << "Should not have caught exception.";
447 }
Bob Kingd6820bb2020-04-28 15:37:02 +0800448}
449
450TEST(PMBusReadSensorActionTests, GetCommand)
451{
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500452 SensorType type{SensorType::iout};
Bob Kingd6820bb2020-04-28 15:37:02 +0800453 uint8_t command = 0x8C;
454 pmbus_utils::SensorDataFormat format{
455 pmbus_utils::SensorDataFormat::linear_16};
456 std::optional<int8_t> exponent{-8};
457 PMBusReadSensorAction action{type, command, format, exponent};
458 EXPECT_EQ(action.getCommand(), 0x8C);
459}
460
461TEST(PMBusReadSensorActionTests, GetExponent)
462{
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500463 SensorType type{SensorType::iout};
Bob Kingd6820bb2020-04-28 15:37:02 +0800464 uint8_t command = 0x8C;
465 pmbus_utils::SensorDataFormat format{
466 pmbus_utils::SensorDataFormat::linear_16};
467
468 // Exponent value is specified
469 {
470 std::optional<int8_t> exponent{-9};
471 PMBusReadSensorAction action{type, command, format, exponent};
472 EXPECT_EQ(action.getExponent().has_value(), true);
473 EXPECT_EQ(action.getExponent().value(), -9);
474 }
475
476 // Exponent value is not specified
477 {
478 std::optional<int8_t> exponent{};
479 PMBusReadSensorAction action{type, command, format, exponent};
480 EXPECT_EQ(action.getExponent().has_value(), false);
481 }
482}
483
484TEST(PMBusReadSensorActionTests, GetFormat)
485{
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500486 SensorType type{SensorType::iout};
Bob Kingd6820bb2020-04-28 15:37:02 +0800487 uint8_t command = 0x8C;
488 pmbus_utils::SensorDataFormat format{
489 pmbus_utils::SensorDataFormat::linear_16};
490 std::optional<int8_t> exponent{-8};
491 PMBusReadSensorAction action{type, command, format, exponent};
492 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16);
493}
494
495TEST(PMBusReadSensorActionTests, GetType)
496{
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500497 SensorType type{SensorType::pout};
Bob Kingd6820bb2020-04-28 15:37:02 +0800498 uint8_t command = 0x8C;
499 pmbus_utils::SensorDataFormat format{
500 pmbus_utils::SensorDataFormat::linear_16};
501 std::optional<int8_t> exponent{-8};
502 PMBusReadSensorAction action{type, command, format, exponent};
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500503 EXPECT_EQ(action.getType(), SensorType::pout);
Bob Kingd6820bb2020-04-28 15:37:02 +0800504}
505
506TEST(PMBusReadSensorActionTests, ToString)
507{
508 // Test where exponent value is specified
509 {
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500510 SensorType type{SensorType::vout};
Bob King717d2da2020-06-02 11:11:15 +0800511 uint8_t command = 0x8B;
Bob Kingd6820bb2020-04-28 15:37:02 +0800512 pmbus_utils::SensorDataFormat format{
513 pmbus_utils::SensorDataFormat::linear_16};
514 std::optional<int8_t> exponent{-8};
515 PMBusReadSensorAction action{type, command, format, exponent};
516 EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: "
Bob King717d2da2020-06-02 11:11:15 +0800517 "vout, command: 0x8B, format: "
Bob Kingd6820bb2020-04-28 15:37:02 +0800518 "linear_16, exponent: -8 }");
519 }
520
521 // Test where exponent value is not specified
522 {
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500523 SensorType type{SensorType::iout};
Bob Kingd6820bb2020-04-28 15:37:02 +0800524 uint8_t command = 0x8C;
525 pmbus_utils::SensorDataFormat format{
526 pmbus_utils::SensorDataFormat::linear_11};
527 std::optional<int8_t> exponent{};
528 PMBusReadSensorAction action{type, command, format, exponent};
Bob King717d2da2020-06-02 11:11:15 +0800529 EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: iout, "
Bob Kingd6820bb2020-04-28 15:37:02 +0800530 "command: 0x8C, format: linear_11 }");
531 }
532}