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 | |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 7 | namespace pldm |
| 8 | { |
| 9 | |
| 10 | namespace responder |
| 11 | { |
| 12 | |
| 13 | namespace pdr_state_sensor |
| 14 | { |
| 15 | |
| 16 | using Json = nlohmann::json; |
| 17 | |
| 18 | static const Json empty{}; |
| 19 | |
| 20 | /** @brief Parse PDR JSON file and generate state sensor PDR structure |
| 21 | * |
| 22 | * @param[in] json - the JSON Object with the state sensor PDR |
| 23 | * @param[out] handler - the Parser of PLDM command handler |
| 24 | * @param[out] repo - pdr::RepoInterface |
| 25 | * |
| 26 | */ |
| 27 | template <class DBusInterface, class Handler> |
| 28 | void generateStateSensorPDR(const DBusInterface& dBusIntf, const Json& json, |
| 29 | Handler& handler, pdr_utils::RepoInterface& repo) |
| 30 | { |
| 31 | static const std::vector<Json> emptyList{}; |
| 32 | auto entries = json.value("entries", emptyList); |
| 33 | for (const auto& e : entries) |
| 34 | { |
| 35 | size_t pdrSize = 0; |
| 36 | auto sensors = e.value("sensors", emptyList); |
| 37 | for (const auto& sensor : sensors) |
| 38 | { |
| 39 | auto set = sensor.value("set", empty); |
| 40 | auto statesSize = set.value("size", 0); |
| 41 | if (!statesSize) |
| 42 | { |
| 43 | std::cerr << "Malformed PDR JSON return " |
| 44 | "pdrEntry;- no state set " |
| 45 | "info, TYPE=" |
| 46 | << PLDM_STATE_SENSOR_PDR << "\n"; |
| 47 | throw InternalFailure(); |
| 48 | } |
| 49 | pdrSize += sizeof(state_sensor_possible_states) - |
| 50 | sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize); |
| 51 | } |
| 52 | pdrSize += sizeof(pldm_state_sensor_pdr) - sizeof(uint8_t); |
| 53 | |
| 54 | std::vector<uint8_t> entry{}; |
| 55 | entry.resize(pdrSize); |
| 56 | |
| 57 | pldm_state_sensor_pdr* pdr = |
| 58 | reinterpret_cast<pldm_state_sensor_pdr*>(entry.data()); |
Manojkiran Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 59 | if (!pdr) |
| 60 | { |
| 61 | std::cerr << "Failed to get state sensor PDR.\n"; |
| 62 | continue; |
| 63 | } |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 64 | pdr->hdr.record_handle = 0; |
| 65 | pdr->hdr.version = 1; |
| 66 | pdr->hdr.type = PLDM_STATE_SENSOR_PDR; |
| 67 | pdr->hdr.record_change_num = 0; |
| 68 | pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr); |
| 69 | |
| 70 | HTOLE32(pdr->hdr.record_handle); |
| 71 | HTOLE16(pdr->hdr.record_change_num); |
| 72 | HTOLE16(pdr->hdr.length); |
| 73 | |
Manojkiran Eda | cc5f158 | 2021-09-29 17:03:06 +0530 | [diff] [blame] | 74 | pdr->terminus_handle = TERMINUS_HANDLE; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 75 | pdr->sensor_id = handler.getNextSensorId(); |
George Liu | c4ea6a9 | 2020-07-14 15:48:44 +0800 | [diff] [blame] | 76 | |
| 77 | try |
| 78 | { |
| 79 | std::string entity_path = e.value("entity_path", ""); |
| 80 | auto& associatedEntityMap = handler.getAssociateEntityMap(); |
| 81 | if (entity_path != "" && associatedEntityMap.find(entity_path) != |
| 82 | associatedEntityMap.end()) |
| 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); |
| 96 | } |
| 97 | } |
| 98 | catch (const std::exception& ex) |
| 99 | { |
| 100 | pdr->entity_type = e.value("type", 0); |
| 101 | pdr->entity_instance = e.value("instance", 0); |
| 102 | pdr->container_id = e.value("container", 0); |
| 103 | } |
| 104 | |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 105 | pdr->sensor_init = PLDM_NO_INIT; |
| 106 | pdr->sensor_auxiliary_names_pdr = false; |
| 107 | if (sensors.size() > 8) |
| 108 | { |
| 109 | throw std::runtime_error("sensor size must be less than 8"); |
| 110 | } |
| 111 | pdr->composite_sensor_count = sensors.size(); |
| 112 | |
| 113 | HTOLE16(pdr->terminus_handle); |
| 114 | HTOLE16(pdr->sensor_id); |
| 115 | HTOLE16(pdr->entity_type); |
| 116 | HTOLE16(pdr->entity_instance); |
| 117 | HTOLE16(pdr->container_id); |
| 118 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 119 | pldm::responder::pdr_utils::DbusMappings dbusMappings{}; |
| 120 | pldm::responder::pdr_utils::DbusValMaps dbusValMaps{}; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 121 | uint8_t* start = |
| 122 | entry.data() + sizeof(pldm_state_sensor_pdr) - sizeof(uint8_t); |
| 123 | for (const auto& sensor : sensors) |
| 124 | { |
| 125 | auto set = sensor.value("set", empty); |
| 126 | state_sensor_possible_states* possibleStates = |
| 127 | reinterpret_cast<state_sensor_possible_states*>(start); |
| 128 | possibleStates->state_set_id = set.value("id", 0); |
| 129 | HTOLE16(possibleStates->state_set_id); |
| 130 | possibleStates->possible_states_size = set.value("size", 0); |
| 131 | |
| 132 | start += sizeof(possibleStates->state_set_id) + |
| 133 | sizeof(possibleStates->possible_states_size); |
| 134 | static const std::vector<uint8_t> emptyStates{}; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 135 | pldm::responder::pdr_utils::PossibleValues stateValues; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 136 | auto states = set.value("states", emptyStates); |
| 137 | for (const auto& state : states) |
| 138 | { |
| 139 | auto index = state / 8; |
| 140 | auto bit = state - (index * 8); |
| 141 | bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index); |
| 142 | bf->byte |= 1 << bit; |
| 143 | stateValues.emplace_back(state); |
| 144 | } |
| 145 | start += possibleStates->possible_states_size; |
| 146 | auto dbusEntry = sensor.value("dbus", empty); |
| 147 | auto objectPath = dbusEntry.value("path", ""); |
| 148 | auto interface = dbusEntry.value("interface", ""); |
| 149 | auto propertyName = dbusEntry.value("property_name", ""); |
| 150 | auto propertyType = dbusEntry.value("property_type", ""); |
| 151 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 152 | pldm::responder::pdr_utils::StatestoDbusVal dbusIdToValMap{}; |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 153 | pldm::utils::DBusMapping dbusMapping{}; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 154 | try |
| 155 | { |
| 156 | auto service = |
| 157 | dBusIntf.getService(objectPath.c_str(), interface.c_str()); |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 158 | |
| 159 | dbusMapping = pldm::utils::DBusMapping{ |
| 160 | objectPath, interface, propertyName, propertyType}; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 161 | dbusIdToValMap = pldm::responder::pdr_utils::populateMapping( |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 162 | propertyType, dbusEntry["property_values"], stateValues); |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 163 | } |
| 164 | catch (const std::exception& e) |
| 165 | { |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 166 | std::cerr << "D-Bus object path does not exist, sensor ID: " |
| 167 | << pdr->sensor_id << "\n"; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 168 | } |
| 169 | |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 170 | dbusMappings.emplace_back(std::move(dbusMapping)); |
George Liu | 821ebc4 | 2021-01-26 14:36:11 +0800 | [diff] [blame] | 171 | dbusValMaps.emplace_back(std::move(dbusIdToValMap)); |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | handler.addDbusObjMaps( |
| 175 | pdr->sensor_id, |
| 176 | std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)), |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 177 | pldm::responder::pdr_utils::TypeId::PLDM_SENSOR_ID); |
| 178 | pldm::responder::pdr_utils::PdrEntry pdrEntry{}; |
George Liu | adbe172 | 2020-05-09 19:20:19 +0800 | [diff] [blame] | 179 | pdrEntry.data = entry.data(); |
| 180 | pdrEntry.size = pdrSize; |
| 181 | repo.addRecord(pdrEntry); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | } // namespace pdr_state_sensor |
| 186 | } // namespace responder |
| 187 | } // namespace pldm |