blob: b75757e1b6a451ceb0fe8cb251ae27db211a5617 [file] [log] [blame]
George Liuadbe1722020-05-09 19:20:19 +08001#pragma once
2
George Liuadbe1722020-05-09 19:20:19 +08003#include "libpldmresponder/pdr_utils.hpp"
4
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/platform.h>
6
Riya Dixit49cfb132023-03-02 04:26:53 -06007#include <phosphor-logging/lg2.hpp>
8
9PHOSPHOR_LOG2_USING;
10
George Liuadbe1722020-05-09 19:20:19 +080011namespace pldm
12{
George Liuadbe1722020-05-09 19:20:19 +080013namespace responder
14{
George Liuadbe1722020-05-09 19:20:19 +080015namespace pdr_state_sensor
16{
George Liuadbe1722020-05-09 19:20:19 +080017using Json = nlohmann::json;
18
19static const Json empty{};
20
21/** @brief Parse PDR JSON file and generate state sensor PDR structure
22 *
23 * @param[in] json - the JSON Object with the state sensor PDR
24 * @param[out] handler - the Parser of PLDM command handler
25 * @param[out] repo - pdr::RepoInterface
26 *
27 */
28template <class DBusInterface, class Handler>
29void generateStateSensorPDR(const DBusInterface& dBusIntf, const Json& json,
30 Handler& handler, pdr_utils::RepoInterface& repo)
31{
32 static const std::vector<Json> emptyList{};
33 auto entries = json.value("entries", emptyList);
34 for (const auto& e : entries)
35 {
36 size_t pdrSize = 0;
37 auto sensors = e.value("sensors", emptyList);
38 for (const auto& sensor : sensors)
39 {
40 auto set = sensor.value("set", empty);
41 auto statesSize = set.value("size", 0);
42 if (!statesSize)
43 {
Riya Dixit49cfb132023-03-02 04:26:53 -060044 error(
45 "Malformed PDR JSON return pdrEntry;- no state set info, TYPE={STATE_SENSOR_PDR}",
46 "STATE_SENSOR_PDR",
47 static_cast<int>(PLDM_STATE_SENSOR_PDR));
George Liuadbe1722020-05-09 19:20:19 +080048 throw InternalFailure();
49 }
50 pdrSize += sizeof(state_sensor_possible_states) -
51 sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize);
52 }
53 pdrSize += sizeof(pldm_state_sensor_pdr) - sizeof(uint8_t);
54
55 std::vector<uint8_t> entry{};
56 entry.resize(pdrSize);
57
58 pldm_state_sensor_pdr* pdr =
59 reinterpret_cast<pldm_state_sensor_pdr*>(entry.data());
Manojkiran Edabcf91ac2021-03-14 13:50:48 +053060 if (!pdr)
61 {
Riya Dixit49cfb132023-03-02 04:26:53 -060062 error("Failed to get state sensor PDR.");
Manojkiran Edabcf91ac2021-03-14 13:50:48 +053063 continue;
64 }
George Liuadbe1722020-05-09 19:20:19 +080065 pdr->hdr.record_handle = 0;
66 pdr->hdr.version = 1;
67 pdr->hdr.type = PLDM_STATE_SENSOR_PDR;
68 pdr->hdr.record_change_num = 0;
69 pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr);
70
71 HTOLE32(pdr->hdr.record_handle);
72 HTOLE16(pdr->hdr.record_change_num);
73 HTOLE16(pdr->hdr.length);
74
Manojkiran Edacc5f1582021-09-29 17:03:06 +053075 pdr->terminus_handle = TERMINUS_HANDLE;
George Liuadbe1722020-05-09 19:20:19 +080076 pdr->sensor_id = handler.getNextSensorId();
George Liuc4ea6a92020-07-14 15:48:44 +080077
78 try
79 {
80 std::string entity_path = e.value("entity_path", "");
81 auto& associatedEntityMap = handler.getAssociateEntityMap();
82 if (entity_path != "" && associatedEntityMap.find(entity_path) !=
83 associatedEntityMap.end())
84 {
85 pdr->entity_type =
86 associatedEntityMap.at(entity_path).entity_type;
87 pdr->entity_instance =
88 associatedEntityMap.at(entity_path).entity_instance_num;
89 pdr->container_id =
90 associatedEntityMap.at(entity_path).entity_container_id;
91 }
92 else
93 {
94 pdr->entity_type = e.value("type", 0);
95 pdr->entity_instance = e.value("instance", 0);
96 pdr->container_id = e.value("container", 0);
Pavithra Barithaya5f213472022-08-29 02:20:19 -050097
98 // do not create the PDR when the FRU or the entity path is not
99 // present
100 if (!pdr->entity_type)
101 {
Riya Dixit797f3382023-08-22 22:27:51 -0500102 error("The entity path for the FRU is not present.");
Pavithra Barithaya5f213472022-08-29 02:20:19 -0500103 continue;
104 }
George Liuc4ea6a92020-07-14 15:48:44 +0800105 }
106 }
107 catch (const std::exception& ex)
108 {
109 pdr->entity_type = e.value("type", 0);
110 pdr->entity_instance = e.value("instance", 0);
111 pdr->container_id = e.value("container", 0);
112 }
113
George Liuadbe1722020-05-09 19:20:19 +0800114 pdr->sensor_init = PLDM_NO_INIT;
115 pdr->sensor_auxiliary_names_pdr = false;
116 if (sensors.size() > 8)
117 {
118 throw std::runtime_error("sensor size must be less than 8");
119 }
120 pdr->composite_sensor_count = sensors.size();
121
122 HTOLE16(pdr->terminus_handle);
123 HTOLE16(pdr->sensor_id);
124 HTOLE16(pdr->entity_type);
125 HTOLE16(pdr->entity_instance);
126 HTOLE16(pdr->container_id);
127
Brad Bishop5079ac42021-08-19 18:35:06 -0400128 pldm::responder::pdr_utils::DbusMappings dbusMappings{};
129 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
Patrick Williams6da4f912023-05-10 07:50:53 -0500130 uint8_t* start = entry.data() + sizeof(pldm_state_sensor_pdr) -
131 sizeof(uint8_t);
George Liuadbe1722020-05-09 19:20:19 +0800132 for (const auto& sensor : sensors)
133 {
134 auto set = sensor.value("set", empty);
135 state_sensor_possible_states* possibleStates =
136 reinterpret_cast<state_sensor_possible_states*>(start);
137 possibleStates->state_set_id = set.value("id", 0);
138 HTOLE16(possibleStates->state_set_id);
139 possibleStates->possible_states_size = set.value("size", 0);
140
141 start += sizeof(possibleStates->state_set_id) +
142 sizeof(possibleStates->possible_states_size);
143 static const std::vector<uint8_t> emptyStates{};
Brad Bishop5079ac42021-08-19 18:35:06 -0400144 pldm::responder::pdr_utils::PossibleValues stateValues;
George Liuadbe1722020-05-09 19:20:19 +0800145 auto states = set.value("states", emptyStates);
146 for (const auto& state : states)
147 {
148 auto index = state / 8;
149 auto bit = state - (index * 8);
150 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
151 bf->byte |= 1 << bit;
152 stateValues.emplace_back(state);
153 }
154 start += possibleStates->possible_states_size;
155 auto dbusEntry = sensor.value("dbus", empty);
156 auto objectPath = dbusEntry.value("path", "");
157 auto interface = dbusEntry.value("interface", "");
158 auto propertyName = dbusEntry.value("property_name", "");
159 auto propertyType = dbusEntry.value("property_type", "");
160
Brad Bishop5079ac42021-08-19 18:35:06 -0400161 pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{};
George Liu821ebc42021-01-26 14:36:11 +0800162 pldm::utils::DBusMapping dbusMapping{};
George Liuadbe1722020-05-09 19:20:19 +0800163 try
164 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500165 auto service = dBusIntf.getService(objectPath.c_str(),
166 interface.c_str());
George Liu821ebc42021-01-26 14:36:11 +0800167
168 dbusMapping = pldm::utils::DBusMapping{
169 objectPath, interface, propertyName, propertyType};
Brad Bishop5079ac42021-08-19 18:35:06 -0400170 dbusIdToValMap = pldm::responder::pdr_utils::populateMapping(
George Liu821ebc42021-01-26 14:36:11 +0800171 propertyType, dbusEntry["property_values"], stateValues);
George Liuadbe1722020-05-09 19:20:19 +0800172 }
173 catch (const std::exception& e)
174 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600175 error(
176 "D-Bus object path does not exist, sensor ID: {SENSOR_ID}",
177 "SENSOR_ID", static_cast<uint16_t>(pdr->sensor_id));
George Liuadbe1722020-05-09 19:20:19 +0800178 }
179
George Liuadbe1722020-05-09 19:20:19 +0800180 dbusMappings.emplace_back(std::move(dbusMapping));
George Liu821ebc42021-01-26 14:36:11 +0800181 dbusValMaps.emplace_back(std::move(dbusIdToValMap));
George Liuadbe1722020-05-09 19:20:19 +0800182 }
183
184 handler.addDbusObjMaps(
185 pdr->sensor_id,
186 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)),
Brad Bishop5079ac42021-08-19 18:35:06 -0400187 pldm::responder::pdr_utils::TypeId::PLDM_SENSOR_ID);
188 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
George Liuadbe1722020-05-09 19:20:19 +0800189 pdrEntry.data = entry.data();
190 pdrEntry.size = pdrSize;
191 repo.addRecord(pdrEntry);
192 }
193}
194
195} // namespace pdr_state_sensor
196} // namespace responder
197} // namespace pldm