blob: 9ad607e58288cdf78c4d66406d9f81982511b910 [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 Jefferybb9daa42024-07-25 22:40:46 +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) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040024 mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), handler(handler)
George Liucae18662020-05-15 09:32:57 +080025{}
26
27void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
28 const std::vector<uint8_t>& eventDataVec)
29{
Andrew Jefferya330b2f2023-05-04 14:55:37 +093030 auto instanceId = instanceIdDb.next(mctp_eid);
Patrick Williams16c2a0a2024-08-16 15:20:59 -040031 std::vector<uint8_t> requestMsg(
32 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
33 eventDataVec.size());
George Liucae18662020-05-15 09:32:57 +080034 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
35
36 auto rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -060037 instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
George Liucae18662020-05-15 09:32:57 +080038 eventDataVec.data(), eventDataVec.size(), request,
39 eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
40 if (rc != PLDM_SUCCESS)
41 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +093042 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -050043 error(
44 "Failed to encode platform event message request, response code '{RC}'",
45 "RC", rc);
George Liucae18662020-05-15 09:32:57 +080046 return;
47 }
48
Patrick Williams16c2a0a2024-08-16 15:20:59 -040049 auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
50 const pldm_msg* response,
51 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 Dixit1e5c81e2024-05-03 07:54:00 -050064 "Failed to decode response of platform event message, response code '{RC}' and completion code '{CC}'",
Riya Dixitd6e10ad2024-03-28 19:44:16 -050065 "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 Williams16c2a0a2024-08-16 15:20:59 -0400116 DbusChangedProps props{};
117 std::string intf;
118 uint8_t previousState = PLDM_SENSOR_UNKNOWN;
119 msg.read(intf, props);
120 if (!props.contains(dbusMapping.propertyName))
George Liu681715c2021-11-25 16:00:55 +0800121 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400122 return;
123 }
124 for (const auto& itr : dbusValueMapping)
125 {
126 bool findValue = false;
127 if (dbusMapping.propertyType == "string")
Patrick Williams6da4f912023-05-10 07:50:53 -0500128 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400129 std::string src = std::get<std::string>(itr.second);
130 std::string dst = std::get<std::string>(
131 props.at(dbusMapping.propertyName));
132
133 auto values = pldm::utils::split(src, "||", " ");
134 for (const auto& value : values)
George Liu99999912021-11-25 16:39:57 +0800135 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400136 if (value == dst)
137 {
138 findValue = true;
139 break;
140 }
George Liu99999912021-11-25 16:39:57 +0800141 }
142 }
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530143 else
144 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400145 findValue =
146 itr.second == props.at(dbusMapping.propertyName)
147 ? true
148 : false;
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530149 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400150
151 if (findValue)
152 {
153 auto eventData =
154 reinterpret_cast<struct pldm_sensor_event_data*>(
155 sensorEventDataVec.data());
156 eventData->event_class[1] = itr.first;
157 if (sensorCacheMap.contains(sensorId) &&
158 sensorCacheMap[sensorId][offset] !=
159 PLDM_SENSOR_UNKNOWN)
160 {
161 previousState = sensorCacheMap[sensorId][offset];
162 }
163 else
164 {
165 previousState = itr.first;
166 }
167 eventData->event_class[2] = previousState;
168 this->sendEventMsg(PLDM_SENSOR_EVENT,
169 sensorEventDataVec);
170 updateSensorCacheMaps(sensorId, offset, previousState);
171 break;
172 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500173 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400174 });
George Liucae18662020-05-15 09:32:57 +0800175 stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
176 }
177}
178
179void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
180 const DbusObjMaps& dbusMaps)
181{
182 const std::map<Type, sensorEvent> sensorHandlers = {
183 {PLDM_STATE_SENSOR_PDR,
184 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400185 this->sendStateSensorEvent(sensorId, dbusMaps);
186 }}};
George Liucae18662020-05-15 09:32:57 +0800187
188 pldm_state_sensor_pdr* pdr = nullptr;
189 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
190 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930191 if (!sensorPdrRepo)
192 {
193 throw std::runtime_error("Unable to instantiate sensor PDR repository");
194 }
George Liucae18662020-05-15 09:32:57 +0800195
196 for (auto pdrType : pdrTypes)
197 {
198 Repo sensorPDRs(sensorPdrRepo.get());
199 getRepoByType(repo, sensorPDRs, pdrType);
200 if (sensorPDRs.empty())
201 {
202 return;
203 }
204
205 PdrEntry pdrEntry{};
206 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
207 while (pdrRecord)
208 {
209 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
210 SensorId sensorId = LE16TOH(pdr->sensor_id);
George Liubd5e2ea2021-04-22 20:33:06 +0800211 if (sensorHandlers.contains(pdrType))
212 {
213 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
214 }
215
George Liucae18662020-05-15 09:32:57 +0800216 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
217 }
218 }
219}
220
221} // namespace state_sensor
222} // namespace pldm