blob: 09edfe71dc2a75367433bf331038b45f1e164966 [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"
27
28#include <cstdint>
Bob King717d2da2020-06-02 11:11:15 +080029#include <memory>
Bob Kingd6820bb2020-04-28 15:37:02 +080030#include <optional>
31#include <stdexcept>
32#include <string>
Bob King717d2da2020-06-02 11:11:15 +080033#include <utility>
Bob Kingd6820bb2020-04-28 15:37:02 +080034
Bob King717d2da2020-06-02 11:11:15 +080035#include <gmock/gmock.h>
Bob Kingd6820bb2020-04-28 15:37:02 +080036#include <gtest/gtest.h>
37
38using namespace phosphor::power::regulators;
39
Bob King717d2da2020-06-02 11:11:15 +080040using ::testing::A;
41using ::testing::Return;
42using ::testing::SetArgReferee;
43using ::testing::Throw;
44using ::testing::TypedEq;
45
Bob Kingd6820bb2020-04-28 15:37:02 +080046TEST(PMBusReadSensorActionTests, Constructor)
47{
48 // Test where works: exponent value is specified
49 try
50 {
51 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
52 uint8_t command = 0x8C;
53 pmbus_utils::SensorDataFormat format{
54 pmbus_utils::SensorDataFormat::linear_16};
55 std::optional<int8_t> exponent{-8};
56 PMBusReadSensorAction action{type, command, format, exponent};
57 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::iout);
58 EXPECT_EQ(action.getCommand(), 0x8C);
59 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16);
60 EXPECT_EQ(action.getExponent().has_value(), true);
61 EXPECT_EQ(action.getExponent().value(), -8);
62 }
63 catch (...)
64 {
65 ADD_FAILURE() << "Should not have caught exception.";
66 }
67
68 // Test where works: exponent value is not specified
69 try
70 {
71 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
72 uint8_t command = 0x8C;
73 pmbus_utils::SensorDataFormat format{
74 pmbus_utils::SensorDataFormat::linear_11};
75 std::optional<int8_t> exponent{};
76 PMBusReadSensorAction action{type, command, format, exponent};
77 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::iout);
78 EXPECT_EQ(action.getCommand(), 0x8C);
79 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_11);
80 EXPECT_EQ(action.getExponent().has_value(), false);
81 }
82 catch (...)
83 {
84 ADD_FAILURE() << "Should not have caught exception.";
85 }
86}
87
88TEST(PMBusReadSensorActionTests, Execute)
89{
Bob King717d2da2020-06-02 11:11:15 +080090 // Test where works: linear_11 defined in action
91 try
92 {
93 // Create mock I2CInterface.
94 // * will read 0xD2E0 from READ_IOUT (command/register 0x8C)
95 // * will not read from VOUT_MODE (command/register 0x20)
96 // assume output current is 11.5 amps,
97 // exponent = -6 = 11010, mantissa = 736 = 010 1110 0000
98 // linear data format = 1101 0010 1110 0000 = 0xD2E0
99 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
100 std::make_unique<i2c::MockedI2CInterface>();
101 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
102 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
103 .Times(1)
104 .WillOnce(SetArgReferee<1>(0xD2E0));
105 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
106
Bob King73eacee2020-10-23 13:58:02 +0800107 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800108 Device device{
109 "reg1", true,
110 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
111 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800112 IDMap idMap{};
113 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800114 MockServices services{};
115 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800116
117 // Create and execute action
118 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
119 uint8_t command = 0x8C;
120 pmbus_utils::SensorDataFormat format{
121 pmbus_utils::SensorDataFormat::linear_11};
122 std::optional<int8_t> exponent{};
123 PMBusReadSensorAction action{type, command, format, exponent};
124 EXPECT_EQ(action.execute(env), true);
Shawn McCarney5135df62021-04-28 15:53:24 -0500125 /**
126 * TODO: Replace with EXPECT calls using MockSensors
127 *
128 * EXPECT_EQ(env.getSensorReadings().size(), 1);
129 * EXPECT_EQ(env.getSensorReadings()[0].type,
130 * pmbus_utils::SensorValueType::iout);
131 * EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 11.5);
132 */
Bob King717d2da2020-06-02 11:11:15 +0800133 }
134 catch (...)
135 {
136 ADD_FAILURE() << "Should not have caught exception.";
137 }
138
139 // Test where works: linear_16 with exponent defined in action
140 try
141 {
142 // Create mock I2CInterface.
143 // * will read 0x0002 from READ_VOUT (command/register 0x8B)
144 // * will not read from VOUT_MODE (command/register 0x20)
145 // assume output voltage is 16 volts,
146 // exponent = 3
147 // linear data format = 0000 0000 0000 0010 = 0x0002 = 2
148 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
149 std::make_unique<i2c::MockedI2CInterface>();
150 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
151 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8B), A<uint16_t&>()))
152 .Times(1)
153 .WillOnce(SetArgReferee<1>(0x0002));
154 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
155
Bob King73eacee2020-10-23 13:58:02 +0800156 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800157 Device device{
158 "reg1", true,
159 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
160 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800161 IDMap idMap{};
162 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800163 MockServices services{};
164 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800165
166 // Create and execute action
167 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout};
168 uint8_t command = 0x8B;
169 pmbus_utils::SensorDataFormat format{
170 pmbus_utils::SensorDataFormat::linear_16};
171 std::optional<int8_t> exponent{3};
172 PMBusReadSensorAction action{type, command, format, exponent};
173 EXPECT_EQ(action.execute(env), true);
Shawn McCarney5135df62021-04-28 15:53:24 -0500174 /**
175 * TODO: Replace with EXPECT calls using MockSensors
176 *
177 * EXPECT_EQ(env.getSensorReadings().size(), 1);
178 * EXPECT_EQ(env.getSensorReadings()[0].type,
179 * pmbus_utils::SensorValueType::vout);
180 * EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 16);
181 */
Bob King717d2da2020-06-02 11:11:15 +0800182 }
183 catch (...)
184 {
185 ADD_FAILURE() << "Should not have caught exception.";
186 }
187
188 // Test where works: linear_16 with no exponent defined in action
189 try
190 {
191 // Create mock I2CInterface.
192 // * will read 0xB877 from vout_peak (command/register 0xC6)
193 // * will read 0b0001'0111 (linear format, -9 exponent) from VOUT_MODE
194 // assume output voltage is 0.232421875 volts,
195 // linear data format = 0000 0000 0111 0111 = 0x0077
196 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
197 std::make_unique<i2c::MockedI2CInterface>();
198 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
199 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0xC6), A<uint16_t&>()))
200 .Times(1)
201 .WillOnce(SetArgReferee<1>(0x0077));
202 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>()))
203 .Times(1)
204 .WillOnce(SetArgReferee<1>(0b0001'0111));
Bob King73eacee2020-10-23 13:58:02 +0800205 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800206 Device device{
207 "reg1", true,
208 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
209 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800210 IDMap idMap{};
211 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800212 MockServices services{};
213 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800214
215 // Create and execute action
216 pmbus_utils::SensorValueType type{
217 pmbus_utils::SensorValueType::vout_peak};
218 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,
229 * pmbus_utils::SensorValueType::vout_peak);
230 * 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
247 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout};
248 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
290 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout};
291 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
354 pmbus_utils::SensorValueType type{
355 pmbus_utils::SensorValueType::vout_peak};
356 uint8_t command = 0xC6;
357 pmbus_utils::SensorDataFormat format{
358 pmbus_utils::SensorDataFormat::linear_16};
359 std::optional<int8_t> exponent{};
360 PMBusReadSensorAction action{type, command, format, exponent};
361 action.execute(env);
362 ADD_FAILURE() << "Should not have reached this line.";
363 }
364 catch (const ActionError& e)
365 {
366 EXPECT_STREQ(e.what(),
367 "ActionError: pmbus_read_sensor: { type: vout_peak, "
368 "command: 0xC6, format: linear_16 }");
369 try
370 {
371 // Re-throw inner I2CException
372 std::rethrow_if_nested(e);
373 ADD_FAILURE() << "Should not have reached this line.";
374 }
375 catch (const i2c::I2CException& ie)
376 {
377 EXPECT_STREQ(
378 ie.what(),
379 "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70");
380 }
381 catch (...)
382 {
383 ADD_FAILURE() << "Should not have caught exception.";
384 }
385 }
386 catch (...)
387 {
388 ADD_FAILURE() << "Should not have caught exception.";
389 }
390
391 // Test where fails: Reading PMBus command code with sensor value fails
392 try
393 {
394 // Create mock I2CInterface. Expect action to do the following:
395 // * will try to read PMBus command(0x96); exception will be thrown
396 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
397 std::make_unique<i2c::MockedI2CInterface>();
398 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
399 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x96), A<uint16_t&>()))
400 .Times(1)
401 .WillOnce(Throw(
402 i2c::I2CException{"Failed to read word", "/dev/i2c-1", 0x70}));
403
Bob King73eacee2020-10-23 13:58:02 +0800404 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800405 Device device{
406 "reg1", true,
407 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
408 std::move(i2cInterface)};
Bob King717d2da2020-06-02 11:11:15 +0800409 IDMap idMap{};
410 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800411 MockServices services{};
412 ActionEnvironment env{idMap, "reg1", services};
Bob King717d2da2020-06-02 11:11:15 +0800413
414 // Create and execute action
415 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout};
416 uint8_t command = 0x96;
417 pmbus_utils::SensorDataFormat format{
418 pmbus_utils::SensorDataFormat::linear_11};
419 std::optional<int8_t> exponent{};
420 PMBusReadSensorAction action{type, command, format, exponent};
421 action.execute(env);
422 ADD_FAILURE() << "Should not have reached this line.";
423 }
424 catch (const ActionError& e)
425 {
426 EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: pout, "
427 "command: 0x96, format: linear_11 }");
428 try
429 {
430 // Re-throw inner I2CException
431 std::rethrow_if_nested(e);
432 ADD_FAILURE() << "Should not have reached this line.";
433 }
434 catch (const i2c::I2CException& ie)
435 {
436 EXPECT_STREQ(
437 ie.what(),
438 "I2CException: Failed to read word: bus /dev/i2c-1, addr 0x70");
439 }
440 catch (...)
441 {
442 ADD_FAILURE() << "Should not have caught exception.";
443 }
444 }
445 catch (...)
446 {
447 ADD_FAILURE() << "Should not have caught exception.";
448 }
Bob Kingd6820bb2020-04-28 15:37:02 +0800449}
450
451TEST(PMBusReadSensorActionTests, GetCommand)
452{
453 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
454 uint8_t command = 0x8C;
455 pmbus_utils::SensorDataFormat format{
456 pmbus_utils::SensorDataFormat::linear_16};
457 std::optional<int8_t> exponent{-8};
458 PMBusReadSensorAction action{type, command, format, exponent};
459 EXPECT_EQ(action.getCommand(), 0x8C);
460}
461
462TEST(PMBusReadSensorActionTests, GetExponent)
463{
464 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
465 uint8_t command = 0x8C;
466 pmbus_utils::SensorDataFormat format{
467 pmbus_utils::SensorDataFormat::linear_16};
468
469 // Exponent value is specified
470 {
471 std::optional<int8_t> exponent{-9};
472 PMBusReadSensorAction action{type, command, format, exponent};
473 EXPECT_EQ(action.getExponent().has_value(), true);
474 EXPECT_EQ(action.getExponent().value(), -9);
475 }
476
477 // Exponent value is not specified
478 {
479 std::optional<int8_t> exponent{};
480 PMBusReadSensorAction action{type, command, format, exponent};
481 EXPECT_EQ(action.getExponent().has_value(), false);
482 }
483}
484
485TEST(PMBusReadSensorActionTests, GetFormat)
486{
487 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
488 uint8_t command = 0x8C;
489 pmbus_utils::SensorDataFormat format{
490 pmbus_utils::SensorDataFormat::linear_16};
491 std::optional<int8_t> exponent{-8};
492 PMBusReadSensorAction action{type, command, format, exponent};
493 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16);
494}
495
496TEST(PMBusReadSensorActionTests, GetType)
497{
498 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout};
499 uint8_t command = 0x8C;
500 pmbus_utils::SensorDataFormat format{
501 pmbus_utils::SensorDataFormat::linear_16};
502 std::optional<int8_t> exponent{-8};
503 PMBusReadSensorAction action{type, command, format, exponent};
504 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::pout);
505}
506
507TEST(PMBusReadSensorActionTests, ToString)
508{
509 // Test where exponent value is specified
510 {
Bob King717d2da2020-06-02 11:11:15 +0800511 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout};
512 uint8_t command = 0x8B;
Bob Kingd6820bb2020-04-28 15:37:02 +0800513 pmbus_utils::SensorDataFormat format{
514 pmbus_utils::SensorDataFormat::linear_16};
515 std::optional<int8_t> exponent{-8};
516 PMBusReadSensorAction action{type, command, format, exponent};
517 EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: "
Bob King717d2da2020-06-02 11:11:15 +0800518 "vout, command: 0x8B, format: "
Bob Kingd6820bb2020-04-28 15:37:02 +0800519 "linear_16, exponent: -8 }");
520 }
521
522 // Test where exponent value is not specified
523 {
Bob King717d2da2020-06-02 11:11:15 +0800524 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
Bob Kingd6820bb2020-04-28 15:37:02 +0800525 uint8_t command = 0x8C;
526 pmbus_utils::SensorDataFormat format{
527 pmbus_utils::SensorDataFormat::linear_11};
528 std::optional<int8_t> exponent{};
529 PMBusReadSensorAction action{type, command, format, exponent};
Bob King717d2da2020-06-02 11:11:15 +0800530 EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: iout, "
Bob Kingd6820bb2020-04-28 15:37:02 +0800531 "command: 0x8C, format: linear_11 }");
532 }
533}