blob: 2c9d92e404c9fb0727fa4b5913ba84058e4482e6 [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
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/pldm.h>
6
Riya Dixit49cfb132023-03-02 04:26:53 -06007#include <phosphor-logging/lg2.hpp>
8
9PHOSPHOR_LOG2_USING;
10
George Liucae18662020-05-15 09:32:57 +080011namespace pldm
12{
Brad Bishop5079ac42021-08-19 18:35:06 -040013using namespace pldm::responder;
George Liucae18662020-05-15 09:32:57 +080014using namespace pldm::responder::pdr;
Brad Bishop5079ac42021-08-19 18:35:06 -040015using namespace pldm::responder::pdr_utils;
George Liucae18662020-05-15 09:32:57 +080016using namespace pldm::utils;
17using namespace sdbusplus::bus::match::rules;
18
19namespace state_sensor
20{
21const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR};
22
Sampa Misrac0c79482021-06-02 08:01:54 -050023DbusToPLDMEvent::DbusToPLDMEvent(
Andrew Jefferya330b2f2023-05-04 14:55:37 +093024 int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb,
Sampa Misrac0c79482021-06-02 08:01:54 -050025 pldm::requester::Handler<pldm::requester::Request>* handler) :
George Liucae18662020-05-15 09:32:57 +080026 mctp_fd(mctp_fd),
Andrew Jefferya330b2f2023-05-04 14:55:37 +093027 mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), handler(handler)
George Liucae18662020-05-15 09:32:57 +080028{}
29
30void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
31 const std::vector<uint8_t>& eventDataVec)
32{
Andrew Jefferya330b2f2023-05-04 14:55:37 +093033 auto instanceId = instanceIdDb.next(mctp_eid);
George Liucae18662020-05-15 09:32:57 +080034 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
35 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
36 eventDataVec.size());
37 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
38
39 auto rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -060040 instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
George Liucae18662020-05-15 09:32:57 +080041 eventDataVec.data(), eventDataVec.size(), request,
42 eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
43 if (rc != PLDM_SUCCESS)
44 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +093045 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -060046 error("Failed to encode_platform_event_message_req, rc = {RC}", "RC",
47 rc);
George Liucae18662020-05-15 09:32:57 +080048 return;
49 }
50
Patrick Williams6da4f912023-05-10 07:50:53 -050051 auto platformEventMessageResponseHandler =
52 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Sampa Misrac0c79482021-06-02 08:01:54 -050053 if (response == nullptr || !respMsgLen)
54 {
Riya Dixit49cfb132023-03-02 04:26:53 -060055 error("Failed to receive response for platform event message");
Sampa Misrac0c79482021-06-02 08:01:54 -050056 return;
57 }
58 uint8_t completionCode{};
59 uint8_t status{};
60 auto rc = decode_platform_event_message_resp(response, respMsgLen,
61 &completionCode, &status);
62 if (rc || completionCode)
63 {
Riya Dixit49cfb132023-03-02 04:26:53 -060064 error(
65 "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}",
66 "RC", rc, "CC", static_cast<unsigned>(completionCode));
Sampa Misrac0c79482021-06-02 08:01:54 -050067 }
68 };
George Liucae18662020-05-15 09:32:57 +080069
Sampa Misrac0c79482021-06-02 08:01:54 -050070 rc = handler->registerRequest(
71 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
72 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
73 if (rc)
George Liucae18662020-05-15 09:32:57 +080074 {
Riya Dixit49cfb132023-03-02 04:26:53 -060075 error("Failed to send the platform event message");
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()),
George Liu681715c2021-11-25 16:00:55 +0800114 [this, sensorEventDataVec, dbusValueMapping,
115 dbusMapping](auto& msg) mutable {
Patrick Williams6da4f912023-05-10 07:50:53 -0500116 DbusChangedProps props{};
117 std::string intf;
118 msg.read(intf, props);
119 if (!props.contains(dbusMapping.propertyName))
120 {
121 return;
122 }
123 for (const auto& itr : dbusValueMapping)
124 {
125 bool findValue = false;
126 if (dbusMapping.propertyType == "string")
George Liu681715c2021-11-25 16:00:55 +0800127 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500128 std::string src = std::get<std::string>(itr.second);
129 std::string dst = std::get<std::string>(
130 props.at(dbusMapping.propertyName));
George Liu99999912021-11-25 16:39:57 +0800131
Patrick Williams6da4f912023-05-10 07:50:53 -0500132 auto values = pldm::utils::split(src, "||", " ");
133 for (auto& value : values)
134 {
135 if (value == dst)
George Liu99999912021-11-25 16:39:57 +0800136 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500137 findValue = true;
138 break;
George Liu99999912021-11-25 16:39:57 +0800139 }
140 }
George Liucae18662020-05-15 09:32:57 +0800141 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500142 else
143 {
144 findValue = itr.second == props.at(dbusMapping.propertyName)
145 ? true
146 : false;
147 }
148
149 if (findValue)
150 {
151 auto eventData =
152 reinterpret_cast<struct pldm_sensor_event_data*>(
153 sensorEventDataVec.data());
154 eventData->event_class[1] = itr.first;
155 eventData->event_class[2] = itr.first;
156 this->sendEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec);
157 break;
158 }
159 }
George Liucae18662020-05-15 09:32:57 +0800160 });
161 stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
162 }
163}
164
165void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
166 const DbusObjMaps& dbusMaps)
167{
168 const std::map<Type, sensorEvent> sensorHandlers = {
169 {PLDM_STATE_SENSOR_PDR,
170 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500171 this->sendStateSensorEvent(sensorId, dbusMaps);
George Liucae18662020-05-15 09:32:57 +0800172 }}};
173
174 pldm_state_sensor_pdr* pdr = nullptr;
175 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
176 pldm_pdr_init(), pldm_pdr_destroy);
177
178 for (auto pdrType : pdrTypes)
179 {
180 Repo sensorPDRs(sensorPdrRepo.get());
181 getRepoByType(repo, sensorPDRs, pdrType);
182 if (sensorPDRs.empty())
183 {
184 return;
185 }
186
187 PdrEntry pdrEntry{};
188 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
189 while (pdrRecord)
190 {
191 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
192 SensorId sensorId = LE16TOH(pdr->sensor_id);
George Liubd5e2ea2021-04-22 20:33:06 +0800193 if (sensorHandlers.contains(pdrType))
194 {
195 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
196 }
197
George Liucae18662020-05-15 09:32:57 +0800198 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
199 }
200 }
201}
202
203} // namespace state_sensor
204} // namespace pldm