George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 3 | #include "libpldmresponder/pdr_utils.hpp" |
| 4 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 5 | #include <libpldm/platform.h> |
| 6 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
| 8 | |
| 9 | PHOSPHOR_LOG2_USING; |
| 10 | |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 11 | namespace pldm |
| 12 | { |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 13 | namespace responder |
| 14 | { |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 15 | namespace pdr_state_sensor |
| 16 | { |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 17 | using Json = nlohmann::json; |
| 18 | |
| 19 | static 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 | */ |
| 28 | template <class DBusInterface, class Handler> |
| 29 | void 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 Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 44 | 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 Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 48 | 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 Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 60 | if (!pdr) |
| 61 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 62 | error("Failed to get state sensor PDR."); |
Manojkiran Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 63 | continue; |
| 64 | } |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 65 | 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 Eda | cc5f158 | 2021-09-29 17:03:06 +0530 | [diff] [blame] | 75 | pdr->terminus_handle = TERMINUS_HANDLE; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 76 | pdr->sensor_id = handler.getNextSensorId(); |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 77 | |
| 78 | try |
| 79 | { |
| 80 | std::string entity_path = e.value("entity_path", ""); |
| 81 | auto& associatedEntityMap = handler.getAssociateEntityMap(); |
Sagar Srinivas | 06f9b29 | 2024-03-31 11:35:28 -0500 | [diff] [blame] | 82 | if (entity_path != "" && associatedEntityMap.contains(entity_path)) |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 83 | { |
| 84 | pdr->entity_type = |
| 85 | associatedEntityMap.at(entity_path).entity_type; |
| 86 | pdr->entity_instance = |
| 87 | associatedEntityMap.at(entity_path).entity_instance_num; |
| 88 | pdr->container_id = |
| 89 | associatedEntityMap.at(entity_path).entity_container_id; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | pdr->entity_type = e.value("type", 0); |
| 94 | pdr->entity_instance = e.value("instance", 0); |
| 95 | pdr->container_id = e.value("container", 0); |
Pavithra Barithaya | 5f21347 | 2022-08-29 02:20:19 -0500 | [diff] [blame] | 96 | |
| 97 | // do not create the PDR when the FRU or the entity path is not |
| 98 | // present |
| 99 | if (!pdr->entity_type) |
| 100 | { |
Pavithra Barithaya | 5f21347 | 2022-08-29 02:20:19 -0500 | [diff] [blame] | 101 | continue; |
| 102 | } |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 103 | } |
| 104 | } |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 105 | catch (const std::exception&) |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 106 | { |
| 107 | pdr->entity_type = e.value("type", 0); |
| 108 | pdr->entity_instance = e.value("instance", 0); |
| 109 | pdr->container_id = e.value("container", 0); |
| 110 | } |
| 111 | |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 112 | pdr->sensor_init = PLDM_NO_INIT; |
| 113 | pdr->sensor_auxiliary_names_pdr = false; |
| 114 | if (sensors.size() > 8) |
| 115 | { |
| 116 | throw std::runtime_error("sensor size must be less than 8"); |
| 117 | } |
| 118 | pdr->composite_sensor_count = sensors.size(); |
| 119 | |
| 120 | HTOLE16(pdr->terminus_handle); |
| 121 | HTOLE16(pdr->sensor_id); |
| 122 | HTOLE16(pdr->entity_type); |
| 123 | HTOLE16(pdr->entity_instance); |
| 124 | HTOLE16(pdr->container_id); |
| 125 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 126 | pldm::responder::pdr_utils::DbusMappings dbusMappings{}; |
| 127 | pldm::responder::pdr_utils::DbusValMaps dbusValMaps{}; |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 128 | uint8_t* start = entry.data() + sizeof(pldm_state_sensor_pdr) - |
| 129 | sizeof(uint8_t); |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 130 | for (const auto& sensor : sensors) |
| 131 | { |
| 132 | auto set = sensor.value("set", empty); |
| 133 | state_sensor_possible_states* possibleStates = |
| 134 | reinterpret_cast<state_sensor_possible_states*>(start); |
| 135 | possibleStates->state_set_id = set.value("id", 0); |
| 136 | HTOLE16(possibleStates->state_set_id); |
| 137 | possibleStates->possible_states_size = set.value("size", 0); |
| 138 | |
| 139 | start += sizeof(possibleStates->state_set_id) + |
| 140 | sizeof(possibleStates->possible_states_size); |
| 141 | static const std::vector<uint8_t> emptyStates{}; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 142 | pldm::responder::pdr_utils::PossibleValues stateValues; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 143 | auto states = set.value("states", emptyStates); |
| 144 | for (const auto& state : states) |
| 145 | { |
| 146 | auto index = state / 8; |
| 147 | auto bit = state - (index * 8); |
| 148 | bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index); |
| 149 | bf->byte |= 1 << bit; |
| 150 | stateValues.emplace_back(state); |
| 151 | } |
| 152 | start += possibleStates->possible_states_size; |
| 153 | auto dbusEntry = sensor.value("dbus", empty); |
| 154 | auto objectPath = dbusEntry.value("path", ""); |
| 155 | auto interface = dbusEntry.value("interface", ""); |
| 156 | auto propertyName = dbusEntry.value("property_name", ""); |
| 157 | auto propertyType = dbusEntry.value("property_type", ""); |
| 158 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 159 | pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{}; |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 160 | pldm::utils::DBusMapping dbusMapping{}; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 161 | try |
| 162 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 163 | auto service = dBusIntf.getService(objectPath.c_str(), |
| 164 | interface.c_str()); |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 165 | |
| 166 | dbusMapping = pldm::utils::DBusMapping{ |
| 167 | objectPath, interface, propertyName, propertyType}; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 168 | dbusIdToValMap = pldm::responder::pdr_utils::populateMapping( |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 169 | propertyType, dbusEntry["property_values"], stateValues); |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 170 | } |
| 171 | catch (const std::exception& e) |
| 172 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 173 | error( |
Manojkiran Eda | 819bb6f | 2024-02-21 11:28:03 +0530 | [diff] [blame] | 174 | "Failed to create sensor PDR, D-Bus object '{PATH}' returned {ERROR}", |
| 175 | "PATH", objectPath, "ERROR", e); |
| 176 | continue; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 177 | } |
| 178 | |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 179 | dbusMappings.emplace_back(std::move(dbusMapping)); |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 180 | dbusValMaps.emplace_back(std::move(dbusIdToValMap)); |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | handler.addDbusObjMaps( |
| 184 | pdr->sensor_id, |
| 185 | std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)), |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 186 | pldm::responder::pdr_utils::TypeId::PLDM_SENSOR_ID); |
| 187 | pldm::responder::pdr_utils::PdrEntry pdrEntry{}; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 188 | pdrEntry.data = entry.data(); |
| 189 | pdrEntry.size = pdrSize; |
| 190 | repo.addRecord(pdrEntry); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | } // namespace pdr_state_sensor |
| 195 | } // namespace responder |
| 196 | } // namespace pldm |