blob: cc999a8d664c8c65406507ba9a30bf177c582c40 [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/platform.h"
2
George Liue53193f2020-02-24 09:23:26 +08003#include "pdr.hpp"
4
Manojkiran Eda3ca40452021-10-04 22:51:37 +05305#include <config.h>
6
Tom Josephb4268602020-04-17 17:20:45 +05307#include <climits>
8
Brad Bishop5079ac42021-08-19 18:35:06 -04009using namespace pldm::pdr;
10
George Liue53193f2020-02-24 09:23:26 +080011namespace pldm
12{
13
14namespace responder
15{
16
17namespace pdr_utils
18{
19
Deepak Kodihallic682fe22020-03-04 00:42:54 -060020pldm_pdr* Repo::getPdr() const
George Liue53193f2020-02-24 09:23:26 +080021{
22 return repo;
23}
24
25RecordHandle Repo::addRecord(const PdrEntry& pdrEntry)
26{
27 return pldm_pdr_add(repo, pdrEntry.data, pdrEntry.size,
Manojkiran Eda3ca40452021-10-04 22:51:37 +053028 pdrEntry.handle.recordHandle, false, TERMINUS_HANDLE);
George Liue53193f2020-02-24 09:23:26 +080029}
30
31const pldm_pdr_record* Repo::getFirstRecord(PdrEntry& pdrEntry)
32{
33 constexpr uint32_t firstNum = 0;
34 uint8_t* pdrData = nullptr;
35 auto record =
36 pldm_pdr_find_record(getPdr(), firstNum, &pdrData, &pdrEntry.size,
37 &pdrEntry.handle.nextRecordHandle);
38 if (record)
39 {
40 pdrEntry.data = pdrData;
41 }
42
43 return record;
44}
45
46const pldm_pdr_record* Repo::getNextRecord(const pldm_pdr_record* currRecord,
47 PdrEntry& pdrEntry)
48{
49 uint8_t* pdrData = nullptr;
50 auto record =
51 pldm_pdr_get_next_record(getPdr(), currRecord, &pdrData, &pdrEntry.size,
52 &pdrEntry.handle.nextRecordHandle);
53 if (record)
54 {
55 pdrEntry.data = pdrData;
56 }
57
58 return record;
59}
60
Deepak Kodihallic682fe22020-03-04 00:42:54 -060061uint32_t Repo::getRecordHandle(const pldm_pdr_record* record) const
George Liue53193f2020-02-24 09:23:26 +080062{
63 return pldm_pdr_get_record_handle(getPdr(), record);
64}
65
66uint32_t Repo::getRecordCount()
67{
68 return pldm_pdr_get_record_count(getPdr());
69}
70
71bool Repo::empty()
72{
73 return !getRecordCount();
74}
75
George Liu1ec85d42020-02-12 16:05:32 +080076StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues,
77 const PossibleValues& pv)
78{
79 size_t pos = 0;
80 pldm::utils::PropertyValue value;
81 StatestoDbusVal valueMap;
82 if (dBusValues.size() != pv.size())
83 {
84 std::cerr
85 << "dBusValues size is not equal to pv size, dBusValues Size: "
86 << dBusValues.size() << ", pv Size: " << pv.size() << "\n";
87
88 return {};
89 }
90
91 for (auto it = dBusValues.begin(); it != dBusValues.end(); ++it, ++pos)
92 {
93 if (type == "uint8_t")
94 {
95 value = static_cast<uint8_t>(it.value());
96 }
97 else if (type == "uint16_t")
98 {
99 value = static_cast<uint16_t>(it.value());
100 }
101 else if (type == "uint32_t")
102 {
103 value = static_cast<uint32_t>(it.value());
104 }
105 else if (type == "uint64_t")
106 {
107 value = static_cast<uint64_t>(it.value());
108 }
109 else if (type == "int16_t")
110 {
111 value = static_cast<int16_t>(it.value());
112 }
113 else if (type == "int32_t")
114 {
115 value = static_cast<int32_t>(it.value());
116 }
117 else if (type == "int64_t")
118 {
119 value = static_cast<int64_t>(it.value());
120 }
121 else if (type == "bool")
122 {
123 value = static_cast<bool>(it.value());
124 }
125 else if (type == "double")
126 {
127 value = static_cast<double>(it.value());
128 }
129 else if (type == "string")
130 {
131 value = static_cast<std::string>(it.value());
132 }
133 else
134 {
135 std::cerr << "Unknown D-Bus property type, TYPE=" << type.c_str()
136 << "\n";
137 return {};
138 }
139
140 valueMap.emplace(pv[pos], value);
141 }
142
143 return valueMap;
144}
145
Tom Josephb4268602020-04-17 17:20:45 +0530146std::tuple<TerminusHandle, SensorID, SensorInfo>
147 parseStateSensorPDR(const std::vector<uint8_t>& stateSensorPdr)
148{
149 auto pdr =
150 reinterpret_cast<const pldm_state_sensor_pdr*>(stateSensorPdr.data());
151 CompositeSensorStates sensors{};
152 auto statesPtr = pdr->possible_states;
153 auto compositeSensorCount = pdr->composite_sensor_count;
154
155 while (compositeSensorCount--)
156 {
157 auto state =
158 reinterpret_cast<const state_sensor_possible_states*>(statesPtr);
159 PossibleStates possibleStates{};
160 uint8_t possibleStatesPos{};
161 auto updateStates = [&possibleStates,
162 &possibleStatesPos](const bitfield8_t& val) {
163 for (int i = 0; i < CHAR_BIT; i++)
164 {
165 if (val.byte & (1 << i))
166 {
167 possibleStates.insert(possibleStatesPos * CHAR_BIT + i);
168 }
169 }
170 possibleStatesPos++;
171 };
172 std::for_each(&state->states[0],
173 &state->states[state->possible_states_size],
174 updateStates);
175
176 sensors.emplace_back(std::move(possibleStates));
177 if (compositeSensorCount)
178 {
179 statesPtr += sizeof(state_sensor_possible_states) +
180 state->possible_states_size - 1;
181 }
182 }
183
184 auto entityInfo =
185 std::make_tuple(static_cast<ContainerID>(pdr->container_id),
186 static_cast<EntityType>(pdr->entity_type),
187 static_cast<EntityInstance>(pdr->entity_instance));
188 auto sensorInfo =
189 std::make_tuple(std::move(entityInfo), std::move(sensors));
190 return std::make_tuple(pdr->terminus_handle, pdr->sensor_id,
191 std::move(sensorInfo));
192}
193
George Liue53193f2020-02-24 09:23:26 +0800194} // namespace pdr_utils
195} // namespace responder
196} // namespace pldm