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( |
| 57 | "Get StateSensor EventState from dbus Error, interface : {DBUS_OBJ_PATH}, exception : {ERR_EXCEP}", |
| 58 | "DBUS_OBJ_PATH", dbusMapping.objectPath.c_str(), "ERR_EXCEP", |
| 59 | e.what()); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 60 | } |
| 61 | |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 62 | return PLDM_SENSOR_UNKNOWN; |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** @brief Function to get the state sensor readings requested by pldm requester |
| 66 | * |
| 67 | * @tparam[in] DBusInterface - DBus interface type |
| 68 | * @tparam[in] Handler - pldm::responder::platform::Handler |
| 69 | * @param[in] dBusIntf - The interface object of DBusInterface |
| 70 | * @param[in] handler - The interface object of |
| 71 | * pldm::responder::platform::Handler |
| 72 | * @param[in] sensorId - Sensor ID sent by the requester to act on |
| 73 | * @param[in] sensorRearmCnt - Each bit location in this field corresponds to a |
| 74 | * particular sensor within the state sensor |
| 75 | * @param[out] compSensorCnt - composite sensor count |
| 76 | * @param[out] stateField - The state field data for each of the states, |
| 77 | * equal to composite sensor count in number |
| 78 | * @return - Success or failure in setting the states. Returns failure in |
| 79 | * terms of PLDM completion codes if atleast one state fails to be set |
| 80 | */ |
| 81 | template <class DBusInterface, class Handler> |
| 82 | int getStateSensorReadingsHandler( |
| 83 | const DBusInterface& dBusIntf, Handler& handler, uint16_t sensorId, |
| 84 | uint8_t sensorRearmCnt, uint8_t& compSensorCnt, |
| 85 | std::vector<get_sensor_state_field>& stateField) |
| 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); |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 94 | pldm::responder::pdr_utils::Repo stateSensorPDRs(stateSensorPdrRepo.get()); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 95 | getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR); |
| 96 | if (stateSensorPDRs.empty()) |
| 97 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 98 | error("Failed to get record by PDR type"); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 99 | return PLDM_PLATFORM_INVALID_SENSOR_ID; |
| 100 | } |
| 101 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 102 | pldm::responder::pdr_utils::PdrEntry pdrEntry{}; |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 103 | auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry); |
| 104 | while (pdrRecord) |
| 105 | { |
| 106 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data); |
| 107 | assert(pdr != NULL); |
| 108 | if (pdr->sensor_id != sensorId) |
| 109 | { |
| 110 | pdr = nullptr; |
| 111 | pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry); |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | compSensorCnt = pdr->composite_sensor_count; |
| 116 | if (sensorRearmCnt > compSensorCnt) |
| 117 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 118 | error( |
| 119 | "The requester sent wrong sensorRearm count for the sensor, SENSOR_ID={SENSOR_ID} SENSOR_REARM_COUNT={SENSOR_REARM_CNT}", |
| 120 | "SENSOR_ID", sensorId, "SENSOR_REARM_CNT", sensorRearmCnt); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 121 | return PLDM_PLATFORM_REARM_UNAVAILABLE_IN_PRESENT_STATE; |
| 122 | } |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 123 | |
| 124 | if (sensorRearmCnt == 0) |
| 125 | { |
| 126 | sensorRearmCnt = compSensorCnt; |
| 127 | stateField.resize(sensorRearmCnt); |
| 128 | } |
| 129 | |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 130 | break; |
| 131 | } |
| 132 | |
| 133 | if (!pdr) |
| 134 | { |
| 135 | return PLDM_PLATFORM_INVALID_SENSOR_ID; |
| 136 | } |
| 137 | |
| 138 | int rc = PLDM_SUCCESS; |
| 139 | try |
| 140 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 141 | const auto& [dbusMappings, dbusValMaps] = handler.getDbusObjMaps( |
| 142 | sensorId, pldm::responder::pdr_utils::TypeId::PLDM_SENSOR_ID); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 143 | |
| 144 | stateField.clear(); |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 145 | for (size_t i = 0; i < sensorRearmCnt; i++) |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 146 | { |
| 147 | auto& dbusMapping = dbusMappings[i]; |
| 148 | |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 149 | uint8_t sensorEvent = getStateSensorEventState<DBusInterface>( |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 150 | dBusIntf, dbusValMaps[i], dbusMapping); |
George Liu | 916808c | 2021-01-19 17:56:42 +0800 | [diff] [blame] | 151 | |
| 152 | uint8_t opState = PLDM_SENSOR_ENABLED; |
| 153 | if (sensorEvent == PLDM_SENSOR_UNKNOWN) |
| 154 | { |
| 155 | opState = PLDM_SENSOR_UNAVAILABLE; |
| 156 | } |
| 157 | |
| 158 | stateField.push_back({opState, PLDM_SENSOR_NORMAL, |
| 159 | PLDM_SENSOR_UNKNOWN, sensorEvent}); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | catch (const std::out_of_range& e) |
| 163 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 164 | error("the sensorId does not exist. sensor id: {SENSOR_ID} {ERR_EXCEP}", |
| 165 | "SENSOR_ID", sensorId, "ERR_EXCEP", e.what()); |
George Liu | 362c18d | 2020-05-14 09:46:36 +0800 | [diff] [blame] | 166 | rc = PLDM_ERROR; |
| 167 | } |
| 168 | |
| 169 | return rc; |
| 170 | } |
| 171 | |
| 172 | } // namespace platform_state_sensor |
| 173 | } // namespace responder |
| 174 | } // namespace pldm |