xref: /openbmc/phosphor-logging/extensions/openpower-pels/section_factory.cpp (revision 40fb54935ce7367636a7156039396ee91cc4d5e2)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3 
4 #include "section_factory.hpp"
5 
6 #include "extended_user_data.hpp"
7 #include "extended_user_header.hpp"
8 #include "failing_mtms.hpp"
9 #include "generic.hpp"
10 #include "pel_types.hpp"
11 #include "private_header.hpp"
12 #include "src.hpp"
13 #include "user_data.hpp"
14 #include "user_header.hpp"
15 
16 namespace openpower
17 {
18 namespace pels
19 {
20 namespace section_factory
21 {
create(Stream & pelData)22 std::unique_ptr<Section> create(Stream& pelData)
23 {
24     std::unique_ptr<Section> section;
25 
26     // Peek the section ID to create the appriopriate object.
27     // If not enough data remains to do so, an invalid
28     // Generic object will be created in the default case.
29     uint16_t sectionID = 0;
30 
31     if (pelData.remaining() >= 2)
32     {
33         pelData >> sectionID;
34         pelData.offset(pelData.offset() - 2);
35     }
36 
37     switch (sectionID)
38     {
39         case static_cast<uint16_t>(SectionID::privateHeader):
40             section = std::make_unique<PrivateHeader>(pelData);
41             break;
42         case static_cast<uint16_t>(SectionID::userData):
43             section = std::make_unique<UserData>(pelData);
44             break;
45         case static_cast<uint16_t>(SectionID::userHeader):
46             section = std::make_unique<UserHeader>(pelData);
47             break;
48         case static_cast<uint16_t>(SectionID::failingMTMS):
49             section = std::make_unique<FailingMTMS>(pelData);
50             break;
51         case static_cast<uint16_t>(SectionID::primarySRC):
52         case static_cast<uint16_t>(SectionID::secondarySRC):
53             section = std::make_unique<SRC>(pelData);
54             break;
55         case static_cast<uint16_t>(SectionID::extendedUserHeader):
56             section = std::make_unique<ExtendedUserHeader>(pelData);
57             break;
58         case static_cast<uint16_t>(SectionID::extUserData):
59             section = std::make_unique<ExtendedUserData>(pelData);
60             break;
61         default:
62             // A generic object, but at least an object.
63             section = std::make_unique<Generic>(pelData);
64             break;
65     }
66 
67     return section;
68 }
69 
70 } // namespace section_factory
71 } // namespace pels
72 } // namespace openpower
73