xref: /openbmc/phosphor-logging/extensions/openpower-pels/ascii_string.cpp (revision 40fb54935ce7367636a7156039396ee91cc4d5e2)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3 
4 #include "ascii_string.hpp"
5 
6 #include "pel_types.hpp"
7 
8 namespace openpower
9 {
10 namespace pels
11 {
12 namespace src
13 {
14 
AsciiString(Stream & stream)15 AsciiString::AsciiString(Stream& stream)
16 {
17     unflatten(stream);
18 }
19 
AsciiString(const message::Entry & entry)20 AsciiString::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     {
38         // 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);
41     }
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 
flatten(Stream & stream) const54 void AsciiString::flatten(Stream& stream) const
55 {
56     stream.write(_string.data(), _string.size());
57 }
58 
unflatten(Stream & stream)59 void AsciiString::unflatten(Stream& stream)
60 {
61     stream.read(_string.data(), _string.size());
62 
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     });
71 }
72 
get() const73 std::string AsciiString::get() const
74 {
75     std::string string{_string.begin(), _string.begin() + _string.size()};
76     return string;
77 }
78 
setByte(size_t byteOffset,uint8_t value)79 void 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
94