blob: 7e0f4930e10afc58865ced6df7949406d9d0aebc [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 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 */
Matt Spinler6852d722019-09-30 15:35:53 -050016#include "ascii_string.hpp"
17
18#include "pel_types.hpp"
19
20#include <phosphor-logging/log.hpp>
21
22namespace openpower
23{
24namespace pels
25{
26namespace src
27{
28
29using namespace phosphor::logging;
30
31AsciiString::AsciiString(Stream& stream)
32{
33 unflatten(stream);
34}
35
36AsciiString::AsciiString(const message::Entry& entry)
37{
38 // Power Error: 1100RRRR
39 // BMC Error: BDSSRRRR
40 // where:
41 // RRRR = reason code
42 // SS = subsystem ID
43
44 // First is type, like 'BD'
45 setByte(0, entry.src.type);
46
47 // Next is '00', or subsystem ID
48 if (entry.src.type == static_cast<uint8_t>(SRCType::powerError))
49 {
50 setByte(2, 0x00);
51 }
52 else // BMC Error
53 {
Matt Spinler23970b02022-02-25 16:34:46 -060054 // If subsystem wasn't specified, it should get set later in
55 // the SRC constructor. Default to 'other' in case it doesn't.
56 setByte(2, entry.subsystem ? entry.subsystem.value() : 0x70);
Matt Spinler6852d722019-09-30 15:35:53 -050057 }
58
59 // Then the reason code
60 setByte(4, entry.src.reasonCode >> 8);
61 setByte(6, entry.src.reasonCode & 0xFF);
62
63 // Padded with spaces
64 for (size_t offset = 8; offset < asciiStringSize; offset++)
65 {
66 _string[offset] = ' ';
67 }
68}
69
Matt Spinler724d0d82019-11-06 10:05:36 -060070void AsciiString::flatten(Stream& stream) const
Matt Spinler6852d722019-09-30 15:35:53 -050071{
72 stream.write(_string.data(), _string.size());
73}
74
75void AsciiString::unflatten(Stream& stream)
76{
77 stream.read(_string.data(), _string.size());
Matt Spinler5b561822020-02-12 16:05:51 -060078
79 // Only allow certain ASCII characters as other entities will
80 // eventually want to display this.
81 std::for_each(_string.begin(), _string.end(), [](auto& c) {
82 if (!isalnum(c) && (c != ' ') && (c != '.') && (c != ':') && (c != '/'))
83 {
84 c = ' ';
85 }
86 });
Matt Spinler6852d722019-09-30 15:35:53 -050087}
88
89std::string AsciiString::get() const
90{
91 std::string string{_string.begin(), _string.begin() + _string.size()};
92 return string;
93}
94
95void AsciiString::setByte(size_t byteOffset, uint8_t value)
96{
97 assert(byteOffset < asciiStringSize);
98
99 char characters[3];
100 sprintf(characters, "%02X", value);
101
102 auto writeOffset = byteOffset;
103 _string[writeOffset++] = characters[0];
104 _string[writeOffset] = characters[1];
105}
106
107} // namespace src
108} // namespace pels
109} // namespace openpower