blob: 85740cfa06b000b87db2120a6ba8decffb72f32e [file] [log] [blame]
George Liucae18662020-05-15 09:32:57 +08001#include "dbus_to_event_handler.hpp"
2
George Liucae18662020-05-15 09:32:57 +08003#include "libpldmresponder/pdr.hpp"
4
Riya Dixit49cfb132023-03-02 04:26:53 -06005#include <phosphor-logging/lg2.hpp>
6
7PHOSPHOR_LOG2_USING;
8
George Liucae18662020-05-15 09:32:57 +08009namespace pldm
10{
Brad Bishop5079ac42021-08-19 18:35:06 -040011using namespace pldm::responder;
George Liucae18662020-05-15 09:32:57 +080012using namespace pldm::responder::pdr;
Brad Bishop5079ac42021-08-19 18:35:06 -040013using namespace pldm::responder::pdr_utils;
George Liucae18662020-05-15 09:32:57 +080014using namespace pldm::utils;
15using namespace sdbusplus::bus::match::rules;
16
17namespace state_sensor
18{
19const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR};
20
Sampa Misrac0c79482021-06-02 08:01:54 -050021DbusToPLDMEvent::DbusToPLDMEvent(
Andrew Jefferya330b2f2023-05-04 14:55:37 +093022 int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb,
Sampa Misrac0c79482021-06-02 08:01:54 -050023 pldm::requester::Handler<pldm::requester::Request>* handler) :
George Liucae18662020-05-15 09:32:57 +080024 mctp_fd(mctp_fd),
Andrew Jefferya330b2f2023-05-04 14:55:37 +093025 mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), handler(handler)
George Liucae18662020-05-15 09:32:57 +080026{}
27
28void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
29 const std::vector<uint8_t>& eventDataVec)
30{
Andrew Jefferya330b2f2023-05-04 14:55:37 +093031 auto instanceId = instanceIdDb.next(mctp_eid);
George Liucae18662020-05-15 09:32:57 +080032 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
33 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
34 eventDataVec.size());
35 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
36
37 auto rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -060038 instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
George Liucae18662020-05-15 09:32:57 +080039 eventDataVec.data(), eventDataVec.size(), request,
40 eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
41 if (rc != PLDM_SUCCESS)
42 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +093043 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -050044 error(
45 "Failed to encode platform event message request, response code '{RC}'",
46 "RC", rc);
George Liucae18662020-05-15 09:32:57 +080047 return;
48 }
49
Patrick Williams6da4f912023-05-10 07:50:53 -050050 auto platformEventMessageResponseHandler =
51 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Sampa Misrac0c79482021-06-02 08:01:54 -050052 if (response == nullptr || !respMsgLen)
53 {
Riya Dixit49cfb132023-03-02 04:26:53 -060054 error("Failed to receive response for platform event message");
Sampa Misrac0c79482021-06-02 08:01:54 -050055 return;
56 }
57 uint8_t completionCode{};
58 uint8_t status{};
59 auto rc = decode_platform_event_message_resp(response, respMsgLen,
60 &completionCode, &status);
61 if (rc || completionCode)
62 {
Riya Dixit49cfb132023-03-02 04:26:53 -060063 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -050064 "Failed to decode response of platform_event message, response code '{RC}' and completion code '{CC}'",
65 "RC", rc, "CC", completionCode);
Sampa Misrac0c79482021-06-02 08:01:54 -050066 }
67 };
George Liucae18662020-05-15 09:32:57 +080068
Sampa Misrac0c79482021-06-02 08:01:54 -050069 rc = handler->registerRequest(
70 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
71 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
72 if (rc)
George Liucae18662020-05-15 09:32:57 +080073 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -050074 error("Failed to send the platform event message, response code '{RC}'",
75 "RC", rc);
George Liucae18662020-05-15 09:32:57 +080076 }
77}
78
79void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId,
80 const DbusObjMaps& dbusMaps)
81{
82 // Encode PLDM platform event msg to indicate a state sensor change.
83 // DSP0248_1.2.0 Table 19
George Liubd5e2ea2021-04-22 20:33:06 +080084 if (!dbusMaps.contains(sensorId))
85 {
Manojkiran Edaf34867d2021-12-12 15:15:07 +053086 // this is not an error condition, if we end up here
87 // that means that the sensor with the sensor id has
88 // custom behaviour(or probably an oem sensor) in
89 // sending events that cannot be captured via standard
90 // dbus-json infastructure
George Liubd5e2ea2021-04-22 20:33:06 +080091 return;
92 }
93
George Liucae18662020-05-15 09:32:57 +080094 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
95 const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
96 for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
97 {
98 std::vector<uint8_t> sensorEventDataVec{};
99 sensorEventDataVec.resize(sensorEventSize);
100 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
101 sensorEventDataVec.data());
102 eventData->sensor_id = sensorId;
103 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
104 eventData->event_class[0] = offset;
105 eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
106 eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
107
108 const auto& dbusMapping = dbusMappings[offset];
109 const auto& dbusValueMapping = dbusValMaps[offset];
Patrick Williams84b790c2022-07-22 19:26:56 -0500110 auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liucae18662020-05-15 09:32:57 +0800111 pldm::utils::DBusHandler::getBus(),
112 propertiesChanged(dbusMapping.objectPath.c_str(),
113 dbusMapping.interface.c_str()),
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530114 [this, sensorEventDataVec, dbusValueMapping, dbusMapping, sensorId,
115 offset](auto& msg) mutable {
Patrick Williams6da4f912023-05-10 07:50:53 -0500116 DbusChangedProps props{};
117 std::string intf;
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530118 uint8_t previousState = PLDM_SENSOR_UNKNOWN;
Patrick Williams6da4f912023-05-10 07:50:53 -0500119 msg.read(intf, props);
120 if (!props.contains(dbusMapping.propertyName))
121 {
122 return;
123 }
124 for (const auto& itr : dbusValueMapping)
125 {
126 bool findValue = false;
127 if (dbusMapping.propertyType == "string")
George Liu681715c2021-11-25 16:00:55 +0800128 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500129 std::string src = std::get<std::string>(itr.second);
130 std::string dst = std::get<std::string>(
131 props.at(dbusMapping.propertyName));
George Liu99999912021-11-25 16:39:57 +0800132
Patrick Williams6da4f912023-05-10 07:50:53 -0500133 auto values = pldm::utils::split(src, "||", " ");
Pavithra Barithaya15b94112024-04-10 02:42:12 -0500134 for (const auto& value : values)
Patrick Williams6da4f912023-05-10 07:50:53 -0500135 {
136 if (value == dst)
George Liu99999912021-11-25 16:39:57 +0800137 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500138 findValue = true;
139 break;
George Liu99999912021-11-25 16:39:57 +0800140 }
141 }
George Liucae18662020-05-15 09:32:57 +0800142 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500143 else
144 {
145 findValue = itr.second == props.at(dbusMapping.propertyName)
146 ? true
147 : false;
148 }
149
150 if (findValue)
151 {
152 auto eventData =
153 reinterpret_cast<struct pldm_sensor_event_data*>(
154 sensorEventDataVec.data());
155 eventData->event_class[1] = itr.first;
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530156 if (sensorCacheMap.contains(sensorId) &&
157 sensorCacheMap[sensorId][offset] != PLDM_SENSOR_UNKNOWN)
158 {
159 previousState = sensorCacheMap[sensorId][offset];
160 }
161 else
162 {
163 previousState = itr.first;
164 }
165 eventData->event_class[2] = previousState;
Patrick Williams6da4f912023-05-10 07:50:53 -0500166 this->sendEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec);
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530167 updateSensorCacheMaps(sensorId, offset, previousState);
Patrick Williams6da4f912023-05-10 07:50:53 -0500168 break;
169 }
170 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500171 });
George Liucae18662020-05-15 09:32:57 +0800172 stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
173 }
174}
175
176void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
177 const DbusObjMaps& dbusMaps)
178{
179 const std::map<Type, sensorEvent> sensorHandlers = {
180 {PLDM_STATE_SENSOR_PDR,
181 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500182 this->sendStateSensorEvent(sensorId, dbusMaps);
Patrick Williamsa6756622023-10-20 11:19:15 -0500183 }}};
George Liucae18662020-05-15 09:32:57 +0800184
185 pldm_state_sensor_pdr* pdr = nullptr;
186 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
187 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930188 if (!sensorPdrRepo)
189 {
190 throw std::runtime_error("Unable to instantiate sensor PDR repository");
191 }
George Liucae18662020-05-15 09:32:57 +0800192
193 for (auto pdrType : pdrTypes)
194 {
195 Repo sensorPDRs(sensorPdrRepo.get());
196 getRepoByType(repo, sensorPDRs, pdrType);
197 if (sensorPDRs.empty())
198 {
199 return;
200 }
201
202 PdrEntry pdrEntry{};
203 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
204 while (pdrRecord)
205 {
206 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
207 SensorId sensorId = LE16TOH(pdr->sensor_id);
George Liubd5e2ea2021-04-22 20:33:06 +0800208 if (sensorHandlers.contains(pdrType))
209 {
210 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
211 }
212
George Liucae18662020-05-15 09:32:57 +0800213 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
214 }
215 }
216}
217
218} // namespace state_sensor
219} // namespace pldm