blob: 6d4711efc3bc00a9307e27dd17acba8e01798896 [file] [log] [blame]
Matt Spinler6852d722019-09-30 15:35:53 -05001#include "ascii_string.hpp"
2
3#include "pel_types.hpp"
4
5#include <phosphor-logging/log.hpp>
6
7namespace openpower
8{
9namespace pels
10{
11namespace src
12{
13
14using namespace phosphor::logging;
15
16AsciiString::AsciiString(Stream& stream)
17{
18 unflatten(stream);
19}
20
21AsciiString::AsciiString(const message::Entry& entry)
22{
23 // Power Error: 1100RRRR
24 // BMC Error: BDSSRRRR
25 // where:
26 // RRRR = reason code
27 // SS = subsystem ID
28
29 // First is type, like 'BD'
30 setByte(0, entry.src.type);
31
32 // Next is '00', or subsystem ID
33 if (entry.src.type == static_cast<uint8_t>(SRCType::powerError))
34 {
35 setByte(2, 0x00);
36 }
37 else // BMC Error
38 {
39 setByte(2, entry.subsystem);
40 }
41
42 // Then the reason code
43 setByte(4, entry.src.reasonCode >> 8);
44 setByte(6, entry.src.reasonCode & 0xFF);
45
46 // Padded with spaces
47 for (size_t offset = 8; offset < asciiStringSize; offset++)
48 {
49 _string[offset] = ' ';
50 }
51}
52
53void AsciiString::flatten(Stream& stream)
54{
55 stream.write(_string.data(), _string.size());
56}
57
58void AsciiString::unflatten(Stream& stream)
59{
60 stream.read(_string.data(), _string.size());
61}
62
63std::string AsciiString::get() const
64{
65 std::string string{_string.begin(), _string.begin() + _string.size()};
66 return string;
67}
68
69void AsciiString::setByte(size_t byteOffset, uint8_t value)
70{
71 assert(byteOffset < asciiStringSize);
72
73 char characters[3];
74 sprintf(characters, "%02X", value);
75
76 auto writeOffset = byteOffset;
77 _string[writeOffset++] = characters[0];
78 _string[writeOffset] = characters[1];
79}
80
81} // namespace src
82} // namespace pels
83} // namespace openpower