blob: e8f84a63220bb00bd7e482ffc60df831b7e19ed8 [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::dbus_api;
14using namespace pldm::responder;
George Liucae18662020-05-15 09:32:57 +080015using namespace pldm::responder::pdr;
Brad Bishop5079ac42021-08-19 18:35:06 -040016using namespace pldm::responder::pdr_utils;
George Liucae18662020-05-15 09:32:57 +080017using namespace pldm::utils;
18using namespace sdbusplus::bus::match::rules;
19
20namespace state_sensor
21{
22const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR};
23
Sampa Misrac0c79482021-06-02 08:01:54 -050024DbusToPLDMEvent::DbusToPLDMEvent(
25 int mctp_fd, uint8_t mctp_eid, Requester& requester,
26 pldm::requester::Handler<pldm::requester::Request>* handler) :
George Liucae18662020-05-15 09:32:57 +080027 mctp_fd(mctp_fd),
Sampa Misrac0c79482021-06-02 08:01:54 -050028 mctp_eid(mctp_eid), requester(requester), handler(handler)
George Liucae18662020-05-15 09:32:57 +080029{}
30
31void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
32 const std::vector<uint8_t>& eventDataVec)
33{
34 auto instanceId = requester.getInstanceId(mctp_eid);
35 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
36 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
37 eventDataVec.size());
38 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
39
40 auto rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -060041 instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
George Liucae18662020-05-15 09:32:57 +080042 eventDataVec.data(), eventDataVec.size(), request,
43 eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
44 if (rc != PLDM_SUCCESS)
45 {
46 requester.markFree(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -060047 error("Failed to encode_platform_event_message_req, rc = {RC}", "RC",
48 rc);
George Liucae18662020-05-15 09:32:57 +080049 return;
50 }
51
Patrick Williams6da4f912023-05-10 07:50:53 -050052 auto platformEventMessageResponseHandler =
53 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Sampa Misrac0c79482021-06-02 08:01:54 -050054 if (response == nullptr || !respMsgLen)
55 {
Riya Dixit49cfb132023-03-02 04:26:53 -060056 error("Failed to receive response for platform event message");
Sampa Misrac0c79482021-06-02 08:01:54 -050057 return;
58 }
59 uint8_t completionCode{};
60 uint8_t status{};
61 auto rc = decode_platform_event_message_resp(response, respMsgLen,
62 &completionCode, &status);
63 if (rc || completionCode)
64 {
Riya Dixit49cfb132023-03-02 04:26:53 -060065 error(
66 "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}",
67 "RC", rc, "CC", static_cast<unsigned>(completionCode));
Sampa Misrac0c79482021-06-02 08:01:54 -050068 }
69 };
George Liucae18662020-05-15 09:32:57 +080070
Sampa Misrac0c79482021-06-02 08:01:54 -050071 rc = handler->registerRequest(
72 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
73 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
74 if (rc)
George Liucae18662020-05-15 09:32:57 +080075 {
Riya Dixit49cfb132023-03-02 04:26:53 -060076 error("Failed to send the platform event message");
George Liucae18662020-05-15 09:32:57 +080077 }
78}
79
80void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId,
81 const DbusObjMaps& dbusMaps)
82{
83 // Encode PLDM platform event msg to indicate a state sensor change.
84 // DSP0248_1.2.0 Table 19
George Liubd5e2ea2021-04-22 20:33:06 +080085 if (!dbusMaps.contains(sensorId))
86 {
Manojkiran Edaf34867d2021-12-12 15:15:07 +053087 // this is not an error condition, if we end up here
88 // that means that the sensor with the sensor id has
89 // custom behaviour(or probably an oem sensor) in
90 // sending events that cannot be captured via standard
91 // dbus-json infastructure
George Liubd5e2ea2021-04-22 20:33:06 +080092 return;
93 }
94
George Liucae18662020-05-15 09:32:57 +080095 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
96 const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
97 for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
98 {
99 std::vector<uint8_t> sensorEventDataVec{};
100 sensorEventDataVec.resize(sensorEventSize);
101 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
102 sensorEventDataVec.data());
103 eventData->sensor_id = sensorId;
104 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
105 eventData->event_class[0] = offset;
106 eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
107 eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
108
109 const auto& dbusMapping = dbusMappings[offset];
110 const auto& dbusValueMapping = dbusValMaps[offset];
Patrick Williams84b790c2022-07-22 19:26:56 -0500111 auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liucae18662020-05-15 09:32:57 +0800112 pldm::utils::DBusHandler::getBus(),
113 propertiesChanged(dbusMapping.objectPath.c_str(),
114 dbusMapping.interface.c_str()),
George Liu681715c2021-11-25 16:00:55 +0800115 [this, sensorEventDataVec, dbusValueMapping,
116 dbusMapping](auto& msg) mutable {
Patrick Williams6da4f912023-05-10 07:50:53 -0500117 DbusChangedProps props{};
118 std::string intf;
119 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, "||", " ");
134 for (auto& value : values)
135 {
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;
156 eventData->event_class[2] = itr.first;
157 this->sendEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec);
158 break;
159 }
160 }
George Liucae18662020-05-15 09:32:57 +0800161 });
162 stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
163 }
164}
165
166void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
167 const DbusObjMaps& dbusMaps)
168{
169 const std::map<Type, sensorEvent> sensorHandlers = {
170 {PLDM_STATE_SENSOR_PDR,
171 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500172 this->sendStateSensorEvent(sensorId, dbusMaps);
George Liucae18662020-05-15 09:32:57 +0800173 }}};
174
175 pldm_state_sensor_pdr* pdr = nullptr;
176 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
177 pldm_pdr_init(), pldm_pdr_destroy);
178
179 for (auto pdrType : pdrTypes)
180 {
181 Repo sensorPDRs(sensorPdrRepo.get());
182 getRepoByType(repo, sensorPDRs, pdrType);
183 if (sensorPDRs.empty())
184 {
185 return;
186 }
187
188 PdrEntry pdrEntry{};
189 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
190 while (pdrRecord)
191 {
192 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
193 SensorId sensorId = LE16TOH(pdr->sensor_id);
George Liubd5e2ea2021-04-22 20:33:06 +0800194 if (sensorHandlers.contains(pdrType))
195 {
196 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
197 }
198
George Liucae18662020-05-15 09:32:57 +0800199 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
200 }
201 }
202}
203
204} // namespace state_sensor
205} // namespace pldm