George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 3 | #include "common/utils.hpp" |
| 4 | #include "libpldmresponder/pdr.hpp" |
| 5 | #include "pdr_utils.hpp" |
| 6 | #include "pldmd/handler.hpp" |
| 7 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 8 | #include <libpldm/platform.h> |
| 9 | #include <libpldm/states.h> |
| 10 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
| 12 | |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 13 | #include <cstdint> |
| 14 | #include <map> |
| 15 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 16 | PHOSPHOR_LOG2_USING; |
| 17 | |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 18 | namespace pldm |
| 19 | { |
| 20 | namespace responder |
| 21 | { |
| 22 | namespace platform_state_sensor |
| 23 | { |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 24 | /** @brief Function to get the sensor state |
| 25 | * |
| 26 | * @tparam[in] DBusInterface - DBus interface type |
| 27 | * @param[in] dBusIntf - The interface object of DBusInterface |
| 28 | * @param[in] stateToDbusValue - Map of DBus property State to attribute value |
| 29 | * @param[in] dbusMapping - The d-bus object |
| 30 | * |
| 31 | * @return - Enumeration of SensorState |
| 32 | */ |
| 33 | template <class DBusInterface> |
| 34 | uint8_t getStateSensorEventState( |
| 35 | const DBusInterface& dBusIntf, |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 36 | const std::map<pldm::responder::pdr_utils::State, |
| 37 | pldm::utils::PropertyValue>& stateToDbusValue, |
| 38 | const pldm::utils::DBusMapping& dbusMapping) |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 39 | { |
| 40 | try |
| 41 | { |
| 42 | auto propertyValue = dBusIntf.getDbusPropertyVariant( |
| 43 | dbusMapping.objectPath.c_str(), dbusMapping.propertyName.c_str(), |
| 44 | dbusMapping.interface.c_str()); |
| 45 | |
| 46 | for (const auto& stateValue : stateToDbusValue) |
| 47 | { |
| 48 | if (stateValue.second == propertyValue) |
| 49 | { |
| 50 | return stateValue.first; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | catch (const std::exception& e) |
| 55 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 56 | error( |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 57 | "Failed to get state sensor event state from dbus interface '{PATH}', error - {ERROR}.", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 58 | "PATH", dbusMapping.objectPath, "ERROR", e); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 59 | } |
| 60 | |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 61 | return PLDM_SENSOR_UNKNOWN; |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** @brief Function to get the state sensor readings requested by pldm requester |
| 65 | * |
| 66 | * @tparam[in] DBusInterface - DBus interface type |
| 67 | * @tparam[in] Handler - pldm::responder::platform::Handler |
| 68 | * @param[in] dBusIntf - The interface object of DBusInterface |
| 69 | * @param[in] handler - The interface object of |
| 70 | * pldm::responder::platform::Handler |
| 71 | * @param[in] sensorId - Sensor ID sent by the requester to act on |
| 72 | * @param[in] sensorRearmCnt - Each bit location in this field corresponds to a |
| 73 | * particular sensor within the state sensor |
| 74 | * @param[out] compSensorCnt - composite sensor count |
| 75 | * @param[out] stateField - The state field data for each of the states, |
| 76 | * equal to composite sensor count in number |
| 77 | * @return - Success or failure in setting the states. Returns failure in |
| 78 | * terms of PLDM completion codes if atleast one state fails to be set |
| 79 | */ |
| 80 | template <class DBusInterface, class Handler> |
| 81 | int getStateSensorReadingsHandler( |
| 82 | const DBusInterface& dBusIntf, Handler& handler, uint16_t sensorId, |
| 83 | uint8_t sensorRearmCnt, uint8_t& compSensorCnt, |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 84 | std::vector<get_sensor_state_field>& stateField, |
| 85 | const stateSensorCacheMaps& sensorCache) |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 86 | { |
| 87 | using namespace pldm::responder::pdr; |
| 88 | using namespace pldm::utils; |
| 89 | |
| 90 | pldm_state_sensor_pdr* pdr = nullptr; |
| 91 | |
| 92 | std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo( |
| 93 | pldm_pdr_init(), pldm_pdr_destroy); |
Andrew Jeffery | acb2029 | 2023-06-30 11:47:44 +0930 | [diff] [blame] | 94 | if (!stateSensorPdrRepo) |
| 95 | { |
| 96 | throw std::runtime_error( |
| 97 | "Failed to instantiate state sensor PDR repository"); |
| 98 | } |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 99 | pldm::responder::pdr_utils::Repo stateSensorPDRs(stateSensorPdrRepo.get()); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 100 | getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR); |
| 101 | if (stateSensorPDRs.empty()) |
| 102 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 103 | error("Failed to get StateSensorPDR record."); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 104 | return PLDM_PLATFORM_INVALID_SENSOR_ID; |
| 105 | } |
| 106 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 107 | pldm::responder::pdr_utils::PdrEntry pdrEntry{}; |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 108 | auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry); |
| 109 | while (pdrRecord) |
| 110 | { |
| 111 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data); |
| 112 | assert(pdr != NULL); |
| 113 | if (pdr->sensor_id != sensorId) |
| 114 | { |
| 115 | pdr = nullptr; |
| 116 | pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry); |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | compSensorCnt = pdr->composite_sensor_count; |
| 121 | if (sensorRearmCnt > compSensorCnt) |
| 122 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 123 | error( |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 124 | "The requester sent wrong sensor rearm count '{SENSOR_REARM_COUNT}' for the sensor ID '{SENSORID}'", |
| 125 | "SENSORID", sensorId, "SENSOR_REARM_COUNT", sensorRearmCnt); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 126 | return PLDM_PLATFORM_REARM_UNAVAILABLE_IN_PRESENT_STATE; |
| 127 | } |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 128 | |
| 129 | if (sensorRearmCnt == 0) |
| 130 | { |
| 131 | sensorRearmCnt = compSensorCnt; |
| 132 | stateField.resize(sensorRearmCnt); |
| 133 | } |
| 134 | |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | |
| 138 | if (!pdr) |
| 139 | { |
| 140 | return PLDM_PLATFORM_INVALID_SENSOR_ID; |
| 141 | } |
| 142 | |
| 143 | int rc = PLDM_SUCCESS; |
| 144 | try |
| 145 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 146 | const auto& [dbusMappings, dbusValMaps] = handler.getDbusObjMaps( |
| 147 | sensorId, pldm::responder::pdr_utils::TypeId::PLDM_SENSOR_ID); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 148 | |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 149 | if (dbusMappings.empty() || dbusValMaps.empty()) |
| 150 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 151 | error("DbusMappings for sensor ID '{SENSOR_ID}' is missing", |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 152 | "SENSOR_ID", sensorId); |
| 153 | return PLDM_ERROR; |
| 154 | } |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 155 | pldm::responder::pdr_utils::EventStates sensorCacheforSensor{}; |
| 156 | if (sensorCache.contains(sensorId)) |
| 157 | { |
| 158 | sensorCacheforSensor = sensorCache.at(sensorId); |
| 159 | } |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 160 | stateField.clear(); |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 161 | for (std::size_t offset{0}; |
| 162 | offset < sensorRearmCnt && offset < dbusMappings.size() && |
| 163 | offset < dbusValMaps.size(); |
| 164 | offset++) |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 165 | { |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 166 | auto& dbusMapping = dbusMappings[offset]; |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 167 | |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 168 | uint8_t sensorEvent = getStateSensorEventState<DBusInterface>( |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 169 | dBusIntf, dbusValMaps[offset], dbusMapping); |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 170 | |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 171 | uint8_t previousState = PLDM_SENSOR_UNKNOWN; |
| 172 | |
| 173 | // if sensor cache is empty, then its the first |
| 174 | // get_state_sensor_reading on this sensor, set the previous state |
| 175 | // as the current state |
| 176 | |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 177 | if (sensorCacheforSensor.at(offset) == PLDM_SENSOR_UNKNOWN) |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 178 | { |
| 179 | previousState = sensorEvent; |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 180 | handler.updateSensorCache(sensorId, offset, previousState); |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | { |
| 184 | // sensor cache is not empty, so get the previous state from |
| 185 | // the sensor cache |
Manojkiran Eda | 3daf7a1 | 2024-04-17 20:33:44 +0530 | [diff] [blame] | 186 | previousState = sensorCacheforSensor[offset]; |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 187 | } |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 188 | uint8_t opState = PLDM_SENSOR_ENABLED; |
| 189 | if (sensorEvent == PLDM_SENSOR_UNKNOWN) |
| 190 | { |
| 191 | opState = PLDM_SENSOR_UNAVAILABLE; |
| 192 | } |
| 193 | |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 194 | stateField.push_back( |
| 195 | {opState, PLDM_SENSOR_NORMAL, previousState, sensorEvent}); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | catch (const std::out_of_range& e) |
| 199 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 200 | error("The sensor ID '{SENSORID}' does not exist, error - {ERROR}", |
| 201 | "SENSORID", sensorId, "ERROR", e); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 202 | rc = PLDM_ERROR; |
| 203 | } |
| 204 | |
| 205 | return rc; |
| 206 | } |
| 207 | |
| 208 | } // namespace platform_state_sensor |
| 209 | } // namespace responder |
| 210 | } // namespace pldm |