blob: ea24497b7597bc5cf40a6fcfca8f632f02b0e7a7 [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 Liuc4ea6a92020-07-14 15:48:44 +080076
77 try
78 {
79 std::string entity_path = e.value("entity_path", "");
80 auto& associatedEntityMap = handler.getAssociateEntityMap();
Sagar Srinivas06f9b292024-03-31 11:35:28 -050081 if (entity_path != "" && associatedEntityMap.contains(entity_path))
George Liuc4ea6a92020-07-14 15:48:44 +080082 {
83 pdr->entity_type =
84 associatedEntityMap.at(entity_path).entity_type;
85 pdr->entity_instance =
86 associatedEntityMap.at(entity_path).entity_instance_num;
87 pdr->container_id =
88 associatedEntityMap.at(entity_path).entity_container_id;
89 }
90 else
91 {
92 pdr->entity_type = e.value("type", 0);
93 pdr->entity_instance = e.value("instance", 0);
94 pdr->container_id = e.value("container", 0);
Pavithra Barithaya5f213472022-08-29 02:20:19 -050095
96 // do not create the PDR when the FRU or the entity path is not
97 // present
98 if (!pdr->entity_type)
99 {
Pavithra Barithaya5f213472022-08-29 02:20:19 -0500100 continue;
101 }
George Liuc4ea6a92020-07-14 15:48:44 +0800102 }
103 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500104 catch (const std::exception&)
George Liuc4ea6a92020-07-14 15:48:44 +0800105 {
106 pdr->entity_type = e.value("type", 0);
107 pdr->entity_instance = e.value("instance", 0);
108 pdr->container_id = e.value("container", 0);
109 }
110
George Liuadbe1722020-05-09 19:20:19 +0800111 pdr->sensor_init = PLDM_NO_INIT;
112 pdr->sensor_auxiliary_names_pdr = false;
113 if (sensors.size() > 8)
114 {
115 throw std::runtime_error("sensor size must be less than 8");
116 }
117 pdr->composite_sensor_count = sensors.size();
118
119 HTOLE16(pdr->terminus_handle);
George Liuadbe1722020-05-09 19:20:19 +0800120 HTOLE16(pdr->entity_type);
121 HTOLE16(pdr->entity_instance);
122 HTOLE16(pdr->container_id);
123
Brad Bishop5079ac42021-08-19 18:35:06 -0400124 pldm::responder::pdr_utils::DbusMappings dbusMappings{};
125 pldm::responder::pdr_utils::DbusValMaps dbusValMaps{};
Patrick Williams6da4f912023-05-10 07:50:53 -0500126 uint8_t* start = entry.data() + sizeof(pldm_state_sensor_pdr) -
127 sizeof(uint8_t);
George Liuadbe1722020-05-09 19:20:19 +0800128 for (const auto& sensor : sensors)
129 {
130 auto set = sensor.value("set", empty);
131 state_sensor_possible_states* possibleStates =
132 reinterpret_cast<state_sensor_possible_states*>(start);
133 possibleStates->state_set_id = set.value("id", 0);
134 HTOLE16(possibleStates->state_set_id);
135 possibleStates->possible_states_size = set.value("size", 0);
136
137 start += sizeof(possibleStates->state_set_id) +
138 sizeof(possibleStates->possible_states_size);
139 static const std::vector<uint8_t> emptyStates{};
Brad Bishop5079ac42021-08-19 18:35:06 -0400140 pldm::responder::pdr_utils::PossibleValues stateValues;
George Liuadbe1722020-05-09 19:20:19 +0800141 auto states = set.value("states", emptyStates);
142 for (const auto& state : states)
143 {
144 auto index = state / 8;
145 auto bit = state - (index * 8);
146 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
147 bf->byte |= 1 << bit;
148 stateValues.emplace_back(state);
149 }
150 start += possibleStates->possible_states_size;
151 auto dbusEntry = sensor.value("dbus", empty);
152 auto objectPath = dbusEntry.value("path", "");
153 auto interface = dbusEntry.value("interface", "");
154 auto propertyName = dbusEntry.value("property_name", "");
155 auto propertyType = dbusEntry.value("property_type", "");
156
Brad Bishop5079ac42021-08-19 18:35:06 -0400157 pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{};
George Liu821ebc42021-01-26 14:36:11 +0800158 pldm::utils::DBusMapping dbusMapping{};
George Liuadbe1722020-05-09 19:20:19 +0800159 try
160 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500161 auto service = dBusIntf.getService(objectPath.c_str(),
162 interface.c_str());
George Liu821ebc42021-01-26 14:36:11 +0800163
164 dbusMapping = pldm::utils::DBusMapping{
165 objectPath, interface, propertyName, propertyType};
Brad Bishop5079ac42021-08-19 18:35:06 -0400166 dbusIdToValMap = pldm::responder::pdr_utils::populateMapping(
George Liu821ebc42021-01-26 14:36:11 +0800167 propertyType, dbusEntry["property_values"], stateValues);
George Liuadbe1722020-05-09 19:20:19 +0800168 }
169 catch (const std::exception& e)
170 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600171 error(
Manojkiran Eda819bb6f2024-02-21 11:28:03 +0530172 "Failed to create sensor PDR, D-Bus object '{PATH}' returned {ERROR}",
173 "PATH", objectPath, "ERROR", e);
Manojkiran Eda3daf7a12024-04-17 20:33:44 +0530174 break;
George Liuadbe1722020-05-09 19:20:19 +0800175 }
George Liuadbe1722020-05-09 19:20:19 +0800176 dbusMappings.emplace_back(std::move(dbusMapping));
George Liu821ebc42021-01-26 14:36:11 +0800177 dbusValMaps.emplace_back(std::move(dbusIdToValMap));
George Liuadbe1722020-05-09 19:20:19 +0800178 }
179
Manojkiran Eda3daf7a12024-04-17 20:33:44 +0530180 if (!(dbusMappings.empty() && dbusValMaps.empty()))
181 {
182 pdr->sensor_id = handler.getNextSensorId();
183 HTOLE16(pdr->sensor_id);
184 handler.addDbusObjMaps(
185 pdr->sensor_id,
186 std::make_tuple(std::move(dbusMappings),
187 std::move(dbusValMaps)),
188 pldm::responder::pdr_utils::TypeId::PLDM_SENSOR_ID);
189 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
190 pdrEntry.data = entry.data();
191 pdrEntry.size = pdrSize;
192 repo.addRecord(pdrEntry);
193 }
George Liuadbe1722020-05-09 19:20:19 +0800194 }
195}
196
197} // namespace pdr_state_sensor
198} // namespace responder
199} // namespace pldm