blob: 4bfc9cf148e58aab2a3c975890d1665ecdfab312 [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 {
54 setByte(2, entry.subsystem);
55 }
56
57 // Then the reason code
58 setByte(4, entry.src.reasonCode >> 8);
59 setByte(6, entry.src.reasonCode & 0xFF);
60
61 // Padded with spaces
62 for (size_t offset = 8; offset < asciiStringSize; offset++)
63 {
64 _string[offset] = ' ';
65 }
66}
67
68void AsciiString::flatten(Stream& stream)
69{
70 stream.write(_string.data(), _string.size());
71}
72
73void AsciiString::unflatten(Stream& stream)
74{
75 stream.read(_string.data(), _string.size());
76}
77
78std::string AsciiString::get() const
79{
80 std::string string{_string.begin(), _string.begin() + _string.size()};
81 return string;
82}
83
84void AsciiString::setByte(size_t byteOffset, uint8_t value)
85{
86 assert(byteOffset < asciiStringSize);
87
88 char characters[3];
89 sprintf(characters, "%02X", value);
90
91 auto writeOffset = byteOffset;
92 _string[writeOffset++] = characters[0];
93 _string[writeOffset] = characters[1];
94}
95
96} // namespace src
97} // namespace pels
98} // namespace openpower