blob: f2cb28251e322676ad97f7bd6380f1eb35ea90af [file] [log] [blame]
Aatir Manzur51c92632019-09-06 13:30:48 -05001#include "user_data.hpp"
2
3#include "pel_types.hpp"
4
5#include <phosphor-logging/log.hpp>
6
7namespace openpower
8{
9namespace pels
10{
11
12using namespace phosphor::logging;
13
14void UserData::unflatten(Stream& stream)
15{
16 stream >> _header;
17
18 if (_header.size <= SectionHeader::flattenedSize())
19 {
20 throw std::out_of_range(
21 "UserData::unflatten: SectionHeader::size field too small");
22 }
23
24 size_t dataLength = _header.size - SectionHeader::flattenedSize();
25 _data.resize(dataLength);
26
27 stream >> _data;
28}
29
30void UserData::flatten(Stream& stream)
31{
32 stream << _header << _data;
33}
34
35UserData::UserData(Stream& pel)
36{
37 try
38 {
39 unflatten(pel);
40 validate();
41 }
42 catch (const std::exception& e)
43 {
44 log<level::ERR>("Cannot unflatten user data",
45 entry("ERROR=%s", e.what()));
46 _valid = false;
47 }
48}
49
50UserData::UserData(uint16_t componentID, uint8_t subType, uint8_t version,
51 const std::vector<uint8_t>& data)
52{
53 _header.id = static_cast<uint16_t>(SectionID::userData);
54 _header.size = Section::flattenedSize() + data.size();
55 _header.version = version;
56 _header.subType = subType;
57 _header.componentID = componentID;
58
59 _data = data;
60
61 _valid = true;
62}
63
64void UserData::validate()
65{
66 if (header().id != static_cast<uint16_t>(SectionID::userData))
67 {
68 log<level::ERR>("Invalid user data section ID",
69 entry("ID=0x%X", header().id));
70 _valid = false;
71 }
72 else
73 {
74 _valid = true;
75 }
76}
77
78} // namespace pels
79} // namespace openpower