blob: 6edd1b847bd72ce6bbb05241e9d4ec548deb6790 [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"
22#include "mocked_i2c_interface.hpp"
23#include "pmbus_error.hpp"
Bob Kingd6820bb2020-04-28 15:37:02 +080024#include "pmbus_read_sensor_action.hpp"
25#include "pmbus_utils.hpp"
26
27#include <cstdint>
Bob King717d2da2020-06-02 11:11:15 +080028#include <memory>
Bob Kingd6820bb2020-04-28 15:37:02 +080029#include <optional>
30#include <stdexcept>
31#include <string>
Bob King717d2da2020-06-02 11:11:15 +080032#include <utility>
Bob Kingd6820bb2020-04-28 15:37:02 +080033
Bob King717d2da2020-06-02 11:11:15 +080034#include <gmock/gmock.h>
Bob Kingd6820bb2020-04-28 15:37:02 +080035#include <gtest/gtest.h>
36
37using namespace phosphor::power::regulators;
38
Bob King717d2da2020-06-02 11:11:15 +080039using ::testing::A;
40using ::testing::Return;
41using ::testing::SetArgReferee;
42using ::testing::Throw;
43using ::testing::TypedEq;
44
Bob Kingd6820bb2020-04-28 15:37:02 +080045TEST(PMBusReadSensorActionTests, Constructor)
46{
47 // Test where works: exponent value is specified
48 try
49 {
50 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
51 uint8_t command = 0x8C;
52 pmbus_utils::SensorDataFormat format{
53 pmbus_utils::SensorDataFormat::linear_16};
54 std::optional<int8_t> exponent{-8};
55 PMBusReadSensorAction action{type, command, format, exponent};
56 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::iout);
57 EXPECT_EQ(action.getCommand(), 0x8C);
58 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16);
59 EXPECT_EQ(action.getExponent().has_value(), true);
60 EXPECT_EQ(action.getExponent().value(), -8);
61 }
62 catch (...)
63 {
64 ADD_FAILURE() << "Should not have caught exception.";
65 }
66
67 // Test where works: exponent value is not specified
68 try
69 {
70 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
71 uint8_t command = 0x8C;
72 pmbus_utils::SensorDataFormat format{
73 pmbus_utils::SensorDataFormat::linear_11};
74 std::optional<int8_t> exponent{};
75 PMBusReadSensorAction action{type, command, format, exponent};
76 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::iout);
77 EXPECT_EQ(action.getCommand(), 0x8C);
78 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_11);
79 EXPECT_EQ(action.getExponent().has_value(), false);
80 }
81 catch (...)
82 {
83 ADD_FAILURE() << "Should not have caught exception.";
84 }
85}
86
87TEST(PMBusReadSensorActionTests, Execute)
88{
Bob King717d2da2020-06-02 11:11:15 +080089 // Test where works: linear_11 defined in action
90 try
91 {
92 // Create mock I2CInterface.
93 // * will read 0xD2E0 from READ_IOUT (command/register 0x8C)
94 // * will not read from VOUT_MODE (command/register 0x20)
95 // assume output current is 11.5 amps,
96 // exponent = -6 = 11010, mantissa = 736 = 010 1110 0000
97 // linear data format = 1101 0010 1110 0000 = 0xD2E0
98 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
99 std::make_unique<i2c::MockedI2CInterface>();
100 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
101 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
102 .Times(1)
103 .WillOnce(SetArgReferee<1>(0xD2E0));
104 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
105
106 // Create Device, IDMap, and ActionEnvironment
107 Device device{"reg1", true, "/system/chassis/motherboard/reg1",
108 std::move(i2cInterface)};
109 IDMap idMap{};
110 idMap.addDevice(device);
111 ActionEnvironment env{idMap, "reg1"};
112
113 // Create and execute action
114 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
115 uint8_t command = 0x8C;
116 pmbus_utils::SensorDataFormat format{
117 pmbus_utils::SensorDataFormat::linear_11};
118 std::optional<int8_t> exponent{};
119 PMBusReadSensorAction action{type, command, format, exponent};
120 EXPECT_EQ(action.execute(env), true);
121 EXPECT_EQ(env.getSensorReadings().size(), 1);
122 EXPECT_EQ(env.getSensorReadings()[0].type,
123 pmbus_utils::SensorValueType::iout);
124 EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 11.5);
125 }
126 catch (...)
127 {
128 ADD_FAILURE() << "Should not have caught exception.";
129 }
130
131 // Test where works: linear_16 with exponent defined in action
132 try
133 {
134 // Create mock I2CInterface.
135 // * will read 0x0002 from READ_VOUT (command/register 0x8B)
136 // * will not read from VOUT_MODE (command/register 0x20)
137 // assume output voltage is 16 volts,
138 // exponent = 3
139 // linear data format = 0000 0000 0000 0010 = 0x0002 = 2
140 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
141 std::make_unique<i2c::MockedI2CInterface>();
142 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
143 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8B), A<uint16_t&>()))
144 .Times(1)
145 .WillOnce(SetArgReferee<1>(0x0002));
146 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
147
148 // Create Device, IDMap, and ActionEnvironment
149 Device device{"reg1", true, "/system/chassis/motherboard/reg1",
150 std::move(i2cInterface)};
151 IDMap idMap{};
152 idMap.addDevice(device);
153 ActionEnvironment env{idMap, "reg1"};
154
155 // Create and execute action
156 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout};
157 uint8_t command = 0x8B;
158 pmbus_utils::SensorDataFormat format{
159 pmbus_utils::SensorDataFormat::linear_16};
160 std::optional<int8_t> exponent{3};
161 PMBusReadSensorAction action{type, command, format, exponent};
162 EXPECT_EQ(action.execute(env), true);
163 EXPECT_EQ(env.getSensorReadings().size(), 1);
164 EXPECT_EQ(env.getSensorReadings()[0].type,
165 pmbus_utils::SensorValueType::vout);
166 EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 16);
167 }
168 catch (...)
169 {
170 ADD_FAILURE() << "Should not have caught exception.";
171 }
172
173 // Test where works: linear_16 with no exponent defined in action
174 try
175 {
176 // Create mock I2CInterface.
177 // * will read 0xB877 from vout_peak (command/register 0xC6)
178 // * will read 0b0001'0111 (linear format, -9 exponent) from VOUT_MODE
179 // assume output voltage is 0.232421875 volts,
180 // linear data format = 0000 0000 0111 0111 = 0x0077
181 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
182 std::make_unique<i2c::MockedI2CInterface>();
183 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
184 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0xC6), A<uint16_t&>()))
185 .Times(1)
186 .WillOnce(SetArgReferee<1>(0x0077));
187 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>()))
188 .Times(1)
189 .WillOnce(SetArgReferee<1>(0b0001'0111));
190 // Create Device, IDMap, and ActionEnvironment
191 Device device{"reg1", true, "/system/chassis/motherboard/reg1",
192 std::move(i2cInterface)};
193 IDMap idMap{};
194 idMap.addDevice(device);
195 ActionEnvironment env{idMap, "reg1"};
196
197 // Create and execute action
198 pmbus_utils::SensorValueType type{
199 pmbus_utils::SensorValueType::vout_peak};
200 uint8_t command = 0xC6;
201 pmbus_utils::SensorDataFormat format{
202 pmbus_utils::SensorDataFormat::linear_16};
203 std::optional<int8_t> exponent{};
204 PMBusReadSensorAction action{type, command, format, exponent};
205 EXPECT_EQ(action.execute(env), true);
206 EXPECT_EQ(env.getSensorReadings().size(), 1);
207 EXPECT_EQ(env.getSensorReadings()[0].type,
208 pmbus_utils::SensorValueType::vout_peak);
209 EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 0.232421875);
210 }
211 catch (...)
212 {
213 ADD_FAILURE() << "Should not have caught exception.";
214 }
215
216 // Test where fails: Unable to get I2C interface to current device
217 try
218 {
219 // Create IDMap and ActionEnvironment
220 IDMap idMap{};
221 ActionEnvironment env{idMap, "reg1"};
222
223 // Create and execute action
224 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout};
225 uint8_t command = 0x96;
226 pmbus_utils::SensorDataFormat format{
227 pmbus_utils::SensorDataFormat::linear_11};
228 std::optional<int8_t> exponent{};
229 PMBusReadSensorAction action{type, command, format, exponent};
230 action.execute(env);
231 ADD_FAILURE() << "Should not have reached this line.";
232 }
233 catch (const std::invalid_argument& e)
234 {
235 EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\"");
236 }
237 catch (...)
238 {
239 ADD_FAILURE() << "Should not have caught exception.";
240 }
241
242 // Test where fails: VOUT_MODE data format is not linear
243 try
244 {
245 // Create mock I2CInterface. Expect action to do the following:
246 // * will read 0b0010'0000 (vid data format) from VOUT_MODE
247 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
248 std::make_unique<i2c::MockedI2CInterface>();
249 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
250 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>()))
251 .Times(1)
252 .WillOnce(SetArgReferee<1>(0b0010'0000));
253
254 // Create Device, IDMap, and ActionEnvironment
255 Device device{"reg1", true, "/system/chassis/motherboard/reg1",
256 std::move(i2cInterface)};
257 IDMap idMap{};
258 idMap.addDevice(device);
259 ActionEnvironment env{idMap, "reg1"};
260
261 // Create and execute action
262 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout};
263 uint8_t command = 0x8B;
264 pmbus_utils::SensorDataFormat format{
265 pmbus_utils::SensorDataFormat::linear_16};
266 std::optional<int8_t> exponent{};
267 PMBusReadSensorAction action{type, command, format, exponent};
268 action.execute(env);
269 ADD_FAILURE() << "Should not have reached this line.";
270 }
271 catch (const ActionError& e)
272 {
273 EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: vout, "
274 "command: 0x8B, format: linear_16 }");
275 try
276 {
277 // Re-throw inner PMBusError
278 std::rethrow_if_nested(e);
279 ADD_FAILURE() << "Should not have reached this line.";
280 }
281 catch (const PMBusError& pe)
282 {
283 EXPECT_STREQ(
284 pe.what(),
285 "PMBusError: VOUT_MODE contains unsupported data format");
286 }
287 catch (...)
288 {
289 ADD_FAILURE() << "Should not have caught exception.";
290 }
291 }
292 catch (...)
293 {
294 ADD_FAILURE() << "Should not have caught exception.";
295 }
296
297 // Test where fails: Reading VOUT_MODE fails
298 try
299 {
300 // Create mock I2CInterface. Expect action to do the following:
301 // * will try to read VOUT_MODE; exception will be thrown
302 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
303 std::make_unique<i2c::MockedI2CInterface>();
304 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
305 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>()))
306 .Times(1)
307 .WillOnce(Throw(
308 i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70}));
309
310 // Create Device, IDMap, and ActionEnvironment
311 Device device{"reg1", true, "/system/chassis/motherboard/reg1",
312 std::move(i2cInterface)};
313 IDMap idMap{};
314 idMap.addDevice(device);
315 ActionEnvironment env{idMap, "reg1"};
316
317 // Create and execute action
318 pmbus_utils::SensorValueType type{
319 pmbus_utils::SensorValueType::vout_peak};
320 uint8_t command = 0xC6;
321 pmbus_utils::SensorDataFormat format{
322 pmbus_utils::SensorDataFormat::linear_16};
323 std::optional<int8_t> exponent{};
324 PMBusReadSensorAction action{type, command, format, exponent};
325 action.execute(env);
326 ADD_FAILURE() << "Should not have reached this line.";
327 }
328 catch (const ActionError& e)
329 {
330 EXPECT_STREQ(e.what(),
331 "ActionError: pmbus_read_sensor: { type: vout_peak, "
332 "command: 0xC6, format: linear_16 }");
333 try
334 {
335 // Re-throw inner I2CException
336 std::rethrow_if_nested(e);
337 ADD_FAILURE() << "Should not have reached this line.";
338 }
339 catch (const i2c::I2CException& ie)
340 {
341 EXPECT_STREQ(
342 ie.what(),
343 "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70");
344 }
345 catch (...)
346 {
347 ADD_FAILURE() << "Should not have caught exception.";
348 }
349 }
350 catch (...)
351 {
352 ADD_FAILURE() << "Should not have caught exception.";
353 }
354
355 // Test where fails: Reading PMBus command code with sensor value fails
356 try
357 {
358 // Create mock I2CInterface. Expect action to do the following:
359 // * will try to read PMBus command(0x96); exception will be thrown
360 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
361 std::make_unique<i2c::MockedI2CInterface>();
362 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
363 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x96), A<uint16_t&>()))
364 .Times(1)
365 .WillOnce(Throw(
366 i2c::I2CException{"Failed to read word", "/dev/i2c-1", 0x70}));
367
368 // Create Device, IDMap, and ActionEnvironment
369 Device device{"reg1", true, "/system/chassis/motherboard/reg1",
370 std::move(i2cInterface)};
371 IDMap idMap{};
372 idMap.addDevice(device);
373 ActionEnvironment env{idMap, "reg1"};
374
375 // Create and execute action
376 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout};
377 uint8_t command = 0x96;
378 pmbus_utils::SensorDataFormat format{
379 pmbus_utils::SensorDataFormat::linear_11};
380 std::optional<int8_t> exponent{};
381 PMBusReadSensorAction action{type, command, format, exponent};
382 action.execute(env);
383 ADD_FAILURE() << "Should not have reached this line.";
384 }
385 catch (const ActionError& e)
386 {
387 EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: pout, "
388 "command: 0x96, format: linear_11 }");
389 try
390 {
391 // Re-throw inner I2CException
392 std::rethrow_if_nested(e);
393 ADD_FAILURE() << "Should not have reached this line.";
394 }
395 catch (const i2c::I2CException& ie)
396 {
397 EXPECT_STREQ(
398 ie.what(),
399 "I2CException: Failed to read word: bus /dev/i2c-1, addr 0x70");
400 }
401 catch (...)
402 {
403 ADD_FAILURE() << "Should not have caught exception.";
404 }
405 }
406 catch (...)
407 {
408 ADD_FAILURE() << "Should not have caught exception.";
409 }
Bob Kingd6820bb2020-04-28 15:37:02 +0800410}
411
412TEST(PMBusReadSensorActionTests, GetCommand)
413{
414 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
415 uint8_t command = 0x8C;
416 pmbus_utils::SensorDataFormat format{
417 pmbus_utils::SensorDataFormat::linear_16};
418 std::optional<int8_t> exponent{-8};
419 PMBusReadSensorAction action{type, command, format, exponent};
420 EXPECT_EQ(action.getCommand(), 0x8C);
421}
422
423TEST(PMBusReadSensorActionTests, GetExponent)
424{
425 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
426 uint8_t command = 0x8C;
427 pmbus_utils::SensorDataFormat format{
428 pmbus_utils::SensorDataFormat::linear_16};
429
430 // Exponent value is specified
431 {
432 std::optional<int8_t> exponent{-9};
433 PMBusReadSensorAction action{type, command, format, exponent};
434 EXPECT_EQ(action.getExponent().has_value(), true);
435 EXPECT_EQ(action.getExponent().value(), -9);
436 }
437
438 // Exponent value is not specified
439 {
440 std::optional<int8_t> exponent{};
441 PMBusReadSensorAction action{type, command, format, exponent};
442 EXPECT_EQ(action.getExponent().has_value(), false);
443 }
444}
445
446TEST(PMBusReadSensorActionTests, GetFormat)
447{
448 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
449 uint8_t command = 0x8C;
450 pmbus_utils::SensorDataFormat format{
451 pmbus_utils::SensorDataFormat::linear_16};
452 std::optional<int8_t> exponent{-8};
453 PMBusReadSensorAction action{type, command, format, exponent};
454 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16);
455}
456
457TEST(PMBusReadSensorActionTests, GetType)
458{
459 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout};
460 uint8_t command = 0x8C;
461 pmbus_utils::SensorDataFormat format{
462 pmbus_utils::SensorDataFormat::linear_16};
463 std::optional<int8_t> exponent{-8};
464 PMBusReadSensorAction action{type, command, format, exponent};
465 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::pout);
466}
467
468TEST(PMBusReadSensorActionTests, ToString)
469{
470 // Test where exponent value is specified
471 {
Bob King717d2da2020-06-02 11:11:15 +0800472 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout};
473 uint8_t command = 0x8B;
Bob Kingd6820bb2020-04-28 15:37:02 +0800474 pmbus_utils::SensorDataFormat format{
475 pmbus_utils::SensorDataFormat::linear_16};
476 std::optional<int8_t> exponent{-8};
477 PMBusReadSensorAction action{type, command, format, exponent};
478 EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: "
Bob King717d2da2020-06-02 11:11:15 +0800479 "vout, command: 0x8B, format: "
Bob Kingd6820bb2020-04-28 15:37:02 +0800480 "linear_16, exponent: -8 }");
481 }
482
483 // Test where exponent value is not specified
484 {
Bob King717d2da2020-06-02 11:11:15 +0800485 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
Bob Kingd6820bb2020-04-28 15:37:02 +0800486 uint8_t command = 0x8C;
487 pmbus_utils::SensorDataFormat format{
488 pmbus_utils::SensorDataFormat::linear_11};
489 std::optional<int8_t> exponent{};
490 PMBusReadSensorAction action{type, command, format, exponent};
Bob King717d2da2020-06-02 11:11:15 +0800491 EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: iout, "
Bob Kingd6820bb2020-04-28 15:37:02 +0800492 "command: 0x8C, format: linear_11 }");
493 }
494}