blob: 0bb24b134c78e5b0a531866898220ff994d8110c [file] [log] [blame]
Shawn McCarney3b242b72021-08-27 09:55:22 -05001/**
2 * Copyright © 2021 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
17#include "i2c_capture_bytes_action.hpp"
18
19#include "action_error.hpp"
20#include "i2c_interface.hpp"
21
22#include <exception>
23#include <ios>
24#include <sstream>
25
26namespace phosphor::power::regulators
27{
28
29bool I2CCaptureBytesAction::execute(ActionEnvironment& environment)
30{
31 try
32 {
33 // Read device register values. Use I2C mode where the number of bytes
34 // to read is explicitly specified.
35 i2c::I2CInterface& interface = getI2CInterface(environment);
36 uint8_t size{count}; // byte count parameter is input/output
37 uint8_t values[UINT8_MAX];
38 interface.read(reg, size, values, i2c::I2CInterface::Mode::I2C);
39
Shawn McCarney630c46f2021-09-02 11:20:45 -050040 // Build additional error data key: <deviceID>_register_<register>
Shawn McCarney3b242b72021-08-27 09:55:22 -050041 std::ostringstream kss;
42 kss << environment.getDeviceID() << "_register_0x" << std::hex
43 << std::uppercase << static_cast<uint16_t>(reg);
44 std::string key = kss.str();
45
Shawn McCarney630c46f2021-09-02 11:20:45 -050046 // Build additional error data value: [ <byte 0>, <byte 1>, ... ]
Shawn McCarney3b242b72021-08-27 09:55:22 -050047 std::ostringstream vss;
48 vss << "[ " << std::hex << std::uppercase;
49 for (unsigned int i = 0; i < count; ++i)
50 {
51 vss << ((i > 0) ? ", " : "") << "0x"
52 << static_cast<uint16_t>(values[i]);
53 }
54 vss << " ]";
55 std::string value = vss.str();
56
57 // Store additional error data in action environment
58 environment.addAdditionalErrorData(key, value);
59 }
60 catch (const i2c::I2CException& e)
61 {
62 // Nest I2CException within an ActionError so caller will have both the
63 // low level I2C error information and the action information
64 std::throw_with_nested(ActionError(*this));
65 }
66 return true;
67}
68
69std::string I2CCaptureBytesAction::toString() const
70{
71 std::ostringstream ss;
72 ss << "i2c_capture_bytes: { register: 0x" << std::hex << std::uppercase
73 << static_cast<uint16_t>(reg) << ", count: " << std::dec
74 << static_cast<uint16_t>(count) << " }";
75 return ss.str();
76}
77
78} // namespace phosphor::power::regulators