blob: 85af37f009d6f70a48fdec479287f212fb95340d [file] [log] [blame]
George Liue53193f2020-02-24 09:23:26 +08001#include "pdr.hpp"
2
3namespace pldm
4{
5
6namespace responder
7{
8
9namespace pdr_utils
10{
11
Deepak Kodihallic682fe22020-03-04 00:42:54 -060012pldm_pdr* Repo::getPdr() const
George Liue53193f2020-02-24 09:23:26 +080013{
14 return repo;
15}
16
17RecordHandle Repo::addRecord(const PdrEntry& pdrEntry)
18{
19 return pldm_pdr_add(repo, pdrEntry.data, pdrEntry.size,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050020 pdrEntry.handle.recordHandle, false);
George Liue53193f2020-02-24 09:23:26 +080021}
22
23const pldm_pdr_record* Repo::getFirstRecord(PdrEntry& pdrEntry)
24{
25 constexpr uint32_t firstNum = 0;
26 uint8_t* pdrData = nullptr;
27 auto record =
28 pldm_pdr_find_record(getPdr(), firstNum, &pdrData, &pdrEntry.size,
29 &pdrEntry.handle.nextRecordHandle);
30 if (record)
31 {
32 pdrEntry.data = pdrData;
33 }
34
35 return record;
36}
37
38const pldm_pdr_record* Repo::getNextRecord(const pldm_pdr_record* currRecord,
39 PdrEntry& pdrEntry)
40{
41 uint8_t* pdrData = nullptr;
42 auto record =
43 pldm_pdr_get_next_record(getPdr(), currRecord, &pdrData, &pdrEntry.size,
44 &pdrEntry.handle.nextRecordHandle);
45 if (record)
46 {
47 pdrEntry.data = pdrData;
48 }
49
50 return record;
51}
52
Deepak Kodihallic682fe22020-03-04 00:42:54 -060053uint32_t Repo::getRecordHandle(const pldm_pdr_record* record) const
George Liue53193f2020-02-24 09:23:26 +080054{
55 return pldm_pdr_get_record_handle(getPdr(), record);
56}
57
58uint32_t Repo::getRecordCount()
59{
60 return pldm_pdr_get_record_count(getPdr());
61}
62
63bool Repo::empty()
64{
65 return !getRecordCount();
66}
67
George Liu1ec85d42020-02-12 16:05:32 +080068StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues,
69 const PossibleValues& pv)
70{
71 size_t pos = 0;
72 pldm::utils::PropertyValue value;
73 StatestoDbusVal valueMap;
74 if (dBusValues.size() != pv.size())
75 {
76 std::cerr
77 << "dBusValues size is not equal to pv size, dBusValues Size: "
78 << dBusValues.size() << ", pv Size: " << pv.size() << "\n";
79
80 return {};
81 }
82
83 for (auto it = dBusValues.begin(); it != dBusValues.end(); ++it, ++pos)
84 {
85 if (type == "uint8_t")
86 {
87 value = static_cast<uint8_t>(it.value());
88 }
89 else if (type == "uint16_t")
90 {
91 value = static_cast<uint16_t>(it.value());
92 }
93 else if (type == "uint32_t")
94 {
95 value = static_cast<uint32_t>(it.value());
96 }
97 else if (type == "uint64_t")
98 {
99 value = static_cast<uint64_t>(it.value());
100 }
101 else if (type == "int16_t")
102 {
103 value = static_cast<int16_t>(it.value());
104 }
105 else if (type == "int32_t")
106 {
107 value = static_cast<int32_t>(it.value());
108 }
109 else if (type == "int64_t")
110 {
111 value = static_cast<int64_t>(it.value());
112 }
113 else if (type == "bool")
114 {
115 value = static_cast<bool>(it.value());
116 }
117 else if (type == "double")
118 {
119 value = static_cast<double>(it.value());
120 }
121 else if (type == "string")
122 {
123 value = static_cast<std::string>(it.value());
124 }
125 else
126 {
127 std::cerr << "Unknown D-Bus property type, TYPE=" << type.c_str()
128 << "\n";
129 return {};
130 }
131
132 valueMap.emplace(pv[pos], value);
133 }
134
135 return valueMap;
136}
137
George Liue53193f2020-02-24 09:23:26 +0800138} // namespace pdr_utils
139} // namespace responder
140} // namespace pldm