blob: dc6294e80d8e3f78bd972d3bd9f20ac7a533a302 [file] [log] [blame]
George Liue53193f2020-02-24 09:23:26 +08001#include "pdr.hpp"
2
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/platform.h>
Manojkiran Eda3ca40452021-10-04 22:51:37 +05304
Riya Dixit49cfb132023-03-02 04:26:53 -06005#include <phosphor-logging/lg2.hpp>
6
Tom Josephb4268602020-04-17 17:20:45 +05307#include <climits>
8
Riya Dixit49cfb132023-03-02 04:26:53 -06009PHOSPHOR_LOG2_USING;
10
Brad Bishop5079ac42021-08-19 18:35:06 -040011using namespace pldm::pdr;
12
George Liue53193f2020-02-24 09:23:26 +080013namespace pldm
14{
George Liue53193f2020-02-24 09:23:26 +080015namespace responder
16{
George Liue53193f2020-02-24 09:23:26 +080017namespace pdr_utils
18{
Deepak Kodihallic682fe22020-03-04 00:42:54 -060019pldm_pdr* Repo::getPdr() const
George Liue53193f2020-02-24 09:23:26 +080020{
21 return repo;
22}
23
24RecordHandle Repo::addRecord(const PdrEntry& pdrEntry)
25{
26 return pldm_pdr_add(repo, pdrEntry.data, pdrEntry.size,
Manojkiran Eda3ca40452021-10-04 22:51:37 +053027 pdrEntry.handle.recordHandle, false, TERMINUS_HANDLE);
George Liue53193f2020-02-24 09:23:26 +080028}
29
30const pldm_pdr_record* Repo::getFirstRecord(PdrEntry& pdrEntry)
31{
32 constexpr uint32_t firstNum = 0;
33 uint8_t* pdrData = nullptr;
Patrick Williams6da4f912023-05-10 07:50:53 -050034 auto record = pldm_pdr_find_record(getPdr(), firstNum, &pdrData,
35 &pdrEntry.size,
36 &pdrEntry.handle.nextRecordHandle);
George Liue53193f2020-02-24 09:23:26 +080037 if (record)
38 {
39 pdrEntry.data = pdrData;
40 }
41
42 return record;
43}
44
45const pldm_pdr_record* Repo::getNextRecord(const pldm_pdr_record* currRecord,
46 PdrEntry& pdrEntry)
47{
48 uint8_t* pdrData = nullptr;
Patrick Williams6da4f912023-05-10 07:50:53 -050049 auto record = pldm_pdr_get_next_record(getPdr(), currRecord, &pdrData,
50 &pdrEntry.size,
51 &pdrEntry.handle.nextRecordHandle);
George Liue53193f2020-02-24 09:23:26 +080052 if (record)
53 {
54 pdrEntry.data = pdrData;
55 }
56
57 return record;
58}
59
Deepak Kodihallic682fe22020-03-04 00:42:54 -060060uint32_t Repo::getRecordHandle(const pldm_pdr_record* record) const
George Liue53193f2020-02-24 09:23:26 +080061{
62 return pldm_pdr_get_record_handle(getPdr(), record);
63}
64
65uint32_t Repo::getRecordCount()
66{
67 return pldm_pdr_get_record_count(getPdr());
68}
69
70bool Repo::empty()
71{
72 return !getRecordCount();
73}
74
George Liu1ec85d42020-02-12 16:05:32 +080075StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues,
76 const PossibleValues& pv)
77{
78 size_t pos = 0;
79 pldm::utils::PropertyValue value;
80 StatestoDbusVal valueMap;
81 if (dBusValues.size() != pv.size())
82 {
Riya Dixit49cfb132023-03-02 04:26:53 -060083 error(
84 "dBusValues size is not equal to pv size, dBusValues Size: {DBUS_VAL_SIZE}, pv Size: {PV_SIZE}",
85 "DBUS_VAL_SIZE", dBusValues.size(), "PV_SIZE", pv.size());
George Liu1ec85d42020-02-12 16:05:32 +080086 return {};
87 }
88
89 for (auto it = dBusValues.begin(); it != dBusValues.end(); ++it, ++pos)
90 {
91 if (type == "uint8_t")
92 {
93 value = static_cast<uint8_t>(it.value());
94 }
95 else if (type == "uint16_t")
96 {
97 value = static_cast<uint16_t>(it.value());
98 }
99 else if (type == "uint32_t")
100 {
101 value = static_cast<uint32_t>(it.value());
102 }
103 else if (type == "uint64_t")
104 {
105 value = static_cast<uint64_t>(it.value());
106 }
107 else if (type == "int16_t")
108 {
109 value = static_cast<int16_t>(it.value());
110 }
111 else if (type == "int32_t")
112 {
113 value = static_cast<int32_t>(it.value());
114 }
115 else if (type == "int64_t")
116 {
117 value = static_cast<int64_t>(it.value());
118 }
119 else if (type == "bool")
120 {
121 value = static_cast<bool>(it.value());
122 }
123 else if (type == "double")
124 {
125 value = static_cast<double>(it.value());
126 }
127 else if (type == "string")
128 {
129 value = static_cast<std::string>(it.value());
130 }
131 else
132 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600133 error("Unknown D-Bus property type, TYPE={OTHER_TYPE}",
134 "OTHER_TYPE", type.c_str());
George Liu1ec85d42020-02-12 16:05:32 +0800135 return {};
136 }
137
138 valueMap.emplace(pv[pos], value);
139 }
140
141 return valueMap;
142}
143
Tom Josephb4268602020-04-17 17:20:45 +0530144std::tuple<TerminusHandle, SensorID, SensorInfo>
145 parseStateSensorPDR(const std::vector<uint8_t>& stateSensorPdr)
146{
147 auto pdr =
148 reinterpret_cast<const pldm_state_sensor_pdr*>(stateSensorPdr.data());
149 CompositeSensorStates sensors{};
150 auto statesPtr = pdr->possible_states;
151 auto compositeSensorCount = pdr->composite_sensor_count;
152
153 while (compositeSensorCount--)
154 {
155 auto state =
156 reinterpret_cast<const state_sensor_possible_states*>(statesPtr);
157 PossibleStates possibleStates{};
158 uint8_t possibleStatesPos{};
Patrick Williams6da4f912023-05-10 07:50:53 -0500159 auto updateStates =
160 [&possibleStates, &possibleStatesPos](const bitfield8_t& val) {
Tom Josephb4268602020-04-17 17:20:45 +0530161 for (int i = 0; i < CHAR_BIT; i++)
162 {
163 if (val.byte & (1 << i))
164 {
165 possibleStates.insert(possibleStatesPos * CHAR_BIT + i);
166 }
167 }
168 possibleStatesPos++;
169 };
170 std::for_each(&state->states[0],
171 &state->states[state->possible_states_size],
172 updateStates);
173
174 sensors.emplace_back(std::move(possibleStates));
175 if (compositeSensorCount)
176 {
177 statesPtr += sizeof(state_sensor_possible_states) +
178 state->possible_states_size - 1;
179 }
180 }
181
182 auto entityInfo =
183 std::make_tuple(static_cast<ContainerID>(pdr->container_id),
184 static_cast<EntityType>(pdr->entity_type),
185 static_cast<EntityInstance>(pdr->entity_instance));
Patrick Williams6da4f912023-05-10 07:50:53 -0500186 auto sensorInfo = std::make_tuple(std::move(entityInfo),
187 std::move(sensors));
Tom Josephb4268602020-04-17 17:20:45 +0530188 return std::make_tuple(pdr->terminus_handle, pdr->sensor_id,
189 std::move(sensorInfo));
190}
191
George Liue53193f2020-02-24 09:23:26 +0800192} // namespace pdr_utils
193} // namespace responder
194} // namespace pldm