blob: 2b55c6b1eeb2c8f6bd010408f3af8be68aa52cbe [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 Dixit49cfb132023-03-02 04:26:53 -060044 error("Failed to encode_platform_event_message_req, rc = {RC}", "RC",
45 rc);
George Liucae18662020-05-15 09:32:57 +080046 return;
47 }
48
Patrick Williams6da4f912023-05-10 07:50:53 -050049 auto platformEventMessageResponseHandler =
50 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Sampa Misrac0c79482021-06-02 08:01:54 -050051 if (response == nullptr || !respMsgLen)
52 {
Riya Dixit49cfb132023-03-02 04:26:53 -060053 error("Failed to receive response for platform event message");
Sampa Misrac0c79482021-06-02 08:01:54 -050054 return;
55 }
56 uint8_t completionCode{};
57 uint8_t status{};
58 auto rc = decode_platform_event_message_resp(response, respMsgLen,
59 &completionCode, &status);
60 if (rc || completionCode)
61 {
Riya Dixit49cfb132023-03-02 04:26:53 -060062 error(
63 "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}",
64 "RC", rc, "CC", static_cast<unsigned>(completionCode));
Sampa Misrac0c79482021-06-02 08:01:54 -050065 }
66 };
George Liucae18662020-05-15 09:32:57 +080067
Sampa Misrac0c79482021-06-02 08:01:54 -050068 rc = handler->registerRequest(
69 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
70 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
71 if (rc)
George Liucae18662020-05-15 09:32:57 +080072 {
Riya Dixit49cfb132023-03-02 04:26:53 -060073 error("Failed to send the platform event message");
George Liucae18662020-05-15 09:32:57 +080074 }
75}
76
77void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId,
78 const DbusObjMaps& dbusMaps)
79{
80 // Encode PLDM platform event msg to indicate a state sensor change.
81 // DSP0248_1.2.0 Table 19
George Liubd5e2ea2021-04-22 20:33:06 +080082 if (!dbusMaps.contains(sensorId))
83 {
Manojkiran Edaf34867d2021-12-12 15:15:07 +053084 // this is not an error condition, if we end up here
85 // that means that the sensor with the sensor id has
86 // custom behaviour(or probably an oem sensor) in
87 // sending events that cannot be captured via standard
88 // dbus-json infastructure
George Liubd5e2ea2021-04-22 20:33:06 +080089 return;
90 }
91
George Liucae18662020-05-15 09:32:57 +080092 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
93 const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
94 for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
95 {
96 std::vector<uint8_t> sensorEventDataVec{};
97 sensorEventDataVec.resize(sensorEventSize);
98 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
99 sensorEventDataVec.data());
100 eventData->sensor_id = sensorId;
101 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
102 eventData->event_class[0] = offset;
103 eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
104 eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
105
106 const auto& dbusMapping = dbusMappings[offset];
107 const auto& dbusValueMapping = dbusValMaps[offset];
Patrick Williams84b790c2022-07-22 19:26:56 -0500108 auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liucae18662020-05-15 09:32:57 +0800109 pldm::utils::DBusHandler::getBus(),
110 propertiesChanged(dbusMapping.objectPath.c_str(),
111 dbusMapping.interface.c_str()),
George Liu681715c2021-11-25 16:00:55 +0800112 [this, sensorEventDataVec, dbusValueMapping,
113 dbusMapping](auto& msg) mutable {
Patrick Williams6da4f912023-05-10 07:50:53 -0500114 DbusChangedProps props{};
115 std::string intf;
116 msg.read(intf, props);
117 if (!props.contains(dbusMapping.propertyName))
118 {
119 return;
120 }
121 for (const auto& itr : dbusValueMapping)
122 {
123 bool findValue = false;
124 if (dbusMapping.propertyType == "string")
George Liu681715c2021-11-25 16:00:55 +0800125 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500126 std::string src = std::get<std::string>(itr.second);
127 std::string dst = std::get<std::string>(
128 props.at(dbusMapping.propertyName));
George Liu99999912021-11-25 16:39:57 +0800129
Patrick Williams6da4f912023-05-10 07:50:53 -0500130 auto values = pldm::utils::split(src, "||", " ");
131 for (auto& value : values)
132 {
133 if (value == dst)
George Liu99999912021-11-25 16:39:57 +0800134 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500135 findValue = true;
136 break;
George Liu99999912021-11-25 16:39:57 +0800137 }
138 }
George Liucae18662020-05-15 09:32:57 +0800139 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500140 else
141 {
142 findValue = itr.second == props.at(dbusMapping.propertyName)
143 ? true
144 : false;
145 }
146
147 if (findValue)
148 {
149 auto eventData =
150 reinterpret_cast<struct pldm_sensor_event_data*>(
151 sensorEventDataVec.data());
152 eventData->event_class[1] = itr.first;
153 eventData->event_class[2] = itr.first;
154 this->sendEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec);
155 break;
156 }
157 }
George Liucae18662020-05-15 09:32:57 +0800158 });
159 stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
160 }
161}
162
163void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
164 const DbusObjMaps& dbusMaps)
165{
166 const std::map<Type, sensorEvent> sensorHandlers = {
167 {PLDM_STATE_SENSOR_PDR,
168 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500169 this->sendStateSensorEvent(sensorId, dbusMaps);
George Liucae18662020-05-15 09:32:57 +0800170 }}};
171
172 pldm_state_sensor_pdr* pdr = nullptr;
173 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
174 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930175 if (!sensorPdrRepo)
176 {
177 throw std::runtime_error("Unable to instantiate sensor PDR repository");
178 }
George Liucae18662020-05-15 09:32:57 +0800179
180 for (auto pdrType : pdrTypes)
181 {
182 Repo sensorPDRs(sensorPdrRepo.get());
183 getRepoByType(repo, sensorPDRs, pdrType);
184 if (sensorPDRs.empty())
185 {
186 return;
187 }
188
189 PdrEntry pdrEntry{};
190 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
191 while (pdrRecord)
192 {
193 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
194 SensorId sensorId = LE16TOH(pdr->sensor_id);
George Liubd5e2ea2021-04-22 20:33:06 +0800195 if (sensorHandlers.contains(pdrType))
196 {
197 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
198 }
199
George Liucae18662020-05-15 09:32:57 +0800200 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
201 }
202 }
203}
204
205} // namespace state_sensor
206} // namespace pldm