blob: 5dfdecd92c3b386baebc35fcc62a67cd0486d1ba [file] [log] [blame]
Alexander Hansen40fb5492025-10-28 17:56:12 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3
Matt Spinler6852d722019-09-30 15:35:53 -05004#include "ascii_string.hpp"
5
6#include "pel_types.hpp"
7
Matt Spinler6852d722019-09-30 15:35:53 -05008namespace openpower
9{
10namespace pels
11{
12namespace src
13{
14
Matt Spinler6852d722019-09-30 15:35:53 -050015AsciiString::AsciiString(Stream& stream)
16{
17 unflatten(stream);
18}
19
20AsciiString::AsciiString(const message::Entry& entry)
21{
22 // Power Error: 1100RRRR
23 // BMC Error: BDSSRRRR
24 // where:
25 // RRRR = reason code
26 // SS = subsystem ID
27
28 // First is type, like 'BD'
29 setByte(0, entry.src.type);
30
31 // Next is '00', or subsystem ID
32 if (entry.src.type == static_cast<uint8_t>(SRCType::powerError))
33 {
34 setByte(2, 0x00);
35 }
36 else // BMC Error
37 {
Matt Spinler23970b02022-02-25 16:34:46 -060038 // If subsystem wasn't specified, it should get set later in
39 // the SRC constructor. Default to 'other' in case it doesn't.
40 setByte(2, entry.subsystem ? entry.subsystem.value() : 0x70);
Matt Spinler6852d722019-09-30 15:35:53 -050041 }
42
43 // Then the reason code
44 setByte(4, entry.src.reasonCode >> 8);
45 setByte(6, entry.src.reasonCode & 0xFF);
46
47 // Padded with spaces
48 for (size_t offset = 8; offset < asciiStringSize; offset++)
49 {
50 _string[offset] = ' ';
51 }
52}
53
Matt Spinler724d0d82019-11-06 10:05:36 -060054void AsciiString::flatten(Stream& stream) const
Matt Spinler6852d722019-09-30 15:35:53 -050055{
56 stream.write(_string.data(), _string.size());
57}
58
59void AsciiString::unflatten(Stream& stream)
60{
61 stream.read(_string.data(), _string.size());
Matt Spinler5b561822020-02-12 16:05:51 -060062
63 // Only allow certain ASCII characters as other entities will
64 // eventually want to display this.
65 std::for_each(_string.begin(), _string.end(), [](auto& c) {
66 if (!isalnum(c) && (c != ' ') && (c != '.') && (c != ':') && (c != '/'))
67 {
68 c = ' ';
69 }
70 });
Matt Spinler6852d722019-09-30 15:35:53 -050071}
72
73std::string AsciiString::get() const
74{
75 std::string string{_string.begin(), _string.begin() + _string.size()};
76 return string;
77}
78
79void AsciiString::setByte(size_t byteOffset, uint8_t value)
80{
81 assert(byteOffset < asciiStringSize);
82
83 char characters[3];
84 sprintf(characters, "%02X", value);
85
86 auto writeOffset = byteOffset;
87 _string[writeOffset++] = characters[0];
88 _string[writeOffset] = characters[1];
89}
90
91} // namespace src
92} // namespace pels
93} // namespace openpower