blob: d1125f1cd57a25b10c48f4e0592da36cfb70ba61 [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
Sampa Misrac0c79482021-06-02 08:01:54 -050052 auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
53 const pldm_msg* response,
54 size_t respMsgLen) {
55 if (response == nullptr || !respMsgLen)
56 {
Riya Dixit49cfb132023-03-02 04:26:53 -060057 error("Failed to receive response for platform event message");
Sampa Misrac0c79482021-06-02 08:01:54 -050058 return;
59 }
60 uint8_t completionCode{};
61 uint8_t status{};
62 auto rc = decode_platform_event_message_resp(response, respMsgLen,
63 &completionCode, &status);
64 if (rc || completionCode)
65 {
Riya Dixit49cfb132023-03-02 04:26:53 -060066 error(
67 "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}",
68 "RC", rc, "CC", static_cast<unsigned>(completionCode));
Sampa Misrac0c79482021-06-02 08:01:54 -050069 }
70 };
George Liucae18662020-05-15 09:32:57 +080071
Sampa Misrac0c79482021-06-02 08:01:54 -050072 rc = handler->registerRequest(
73 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
74 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
75 if (rc)
George Liucae18662020-05-15 09:32:57 +080076 {
Riya Dixit49cfb132023-03-02 04:26:53 -060077 error("Failed to send the platform event message");
George Liucae18662020-05-15 09:32:57 +080078 }
79}
80
81void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId,
82 const DbusObjMaps& dbusMaps)
83{
84 // Encode PLDM platform event msg to indicate a state sensor change.
85 // DSP0248_1.2.0 Table 19
George Liubd5e2ea2021-04-22 20:33:06 +080086 if (!dbusMaps.contains(sensorId))
87 {
Manojkiran Edaf34867d2021-12-12 15:15:07 +053088 // this is not an error condition, if we end up here
89 // that means that the sensor with the sensor id has
90 // custom behaviour(or probably an oem sensor) in
91 // sending events that cannot be captured via standard
92 // dbus-json infastructure
George Liubd5e2ea2021-04-22 20:33:06 +080093 return;
94 }
95
George Liucae18662020-05-15 09:32:57 +080096 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
97 const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
98 for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
99 {
100 std::vector<uint8_t> sensorEventDataVec{};
101 sensorEventDataVec.resize(sensorEventSize);
102 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
103 sensorEventDataVec.data());
104 eventData->sensor_id = sensorId;
105 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
106 eventData->event_class[0] = offset;
107 eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
108 eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
109
110 const auto& dbusMapping = dbusMappings[offset];
111 const auto& dbusValueMapping = dbusValMaps[offset];
Patrick Williams84b790c2022-07-22 19:26:56 -0500112 auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liucae18662020-05-15 09:32:57 +0800113 pldm::utils::DBusHandler::getBus(),
114 propertiesChanged(dbusMapping.objectPath.c_str(),
115 dbusMapping.interface.c_str()),
George Liu681715c2021-11-25 16:00:55 +0800116 [this, sensorEventDataVec, dbusValueMapping,
117 dbusMapping](auto& msg) mutable {
George Liucae18662020-05-15 09:32:57 +0800118 DbusChangedProps props{};
119 std::string intf;
120 msg.read(intf, props);
George Liu681715c2021-11-25 16:00:55 +0800121 if (!props.contains(dbusMapping.propertyName))
122 {
123 return;
124 }
George Liucae18662020-05-15 09:32:57 +0800125 for (const auto& itr : dbusValueMapping)
126 {
George Liu99999912021-11-25 16:39:57 +0800127 bool findValue = false;
128 if (dbusMapping.propertyType == "string")
129 {
130 std::string src = std::get<std::string>(itr.second);
131 std::string dst = std::get<std::string>(
132 props.at(dbusMapping.propertyName));
133
134 auto values = pldm::utils::split(src, "||", " ");
135 for (auto& value : values)
136 {
137 if (value == dst)
138 {
139 findValue = true;
140 break;
141 }
142 }
143 }
144 else
145 {
146 findValue =
147 itr.second == props.at(dbusMapping.propertyName)
148 ? true
149 : false;
150 }
151
152 if (findValue)
George Liucae18662020-05-15 09:32:57 +0800153 {
154 auto eventData =
155 reinterpret_cast<struct pldm_sensor_event_data*>(
156 sensorEventDataVec.data());
157 eventData->event_class[1] = itr.first;
158 eventData->event_class[2] = itr.first;
159 this->sendEventMsg(PLDM_SENSOR_EVENT,
160 sensorEventDataVec);
George Liu99999912021-11-25 16:39:57 +0800161 break;
George Liucae18662020-05-15 09:32:57 +0800162 }
163 }
164 });
165 stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
166 }
167}
168
169void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
170 const DbusObjMaps& dbusMaps)
171{
172 const std::map<Type, sensorEvent> sensorHandlers = {
173 {PLDM_STATE_SENSOR_PDR,
174 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
175 this->sendStateSensorEvent(sensorId, dbusMaps);
176 }}};
177
178 pldm_state_sensor_pdr* pdr = nullptr;
179 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
180 pldm_pdr_init(), pldm_pdr_destroy);
181
182 for (auto pdrType : pdrTypes)
183 {
184 Repo sensorPDRs(sensorPdrRepo.get());
185 getRepoByType(repo, sensorPDRs, pdrType);
186 if (sensorPDRs.empty())
187 {
188 return;
189 }
190
191 PdrEntry pdrEntry{};
192 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
193 while (pdrRecord)
194 {
195 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
196 SensorId sensorId = LE16TOH(pdr->sensor_id);
George Liubd5e2ea2021-04-22 20:33:06 +0800197 if (sensorHandlers.contains(pdrType))
198 {
199 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
200 }
201
George Liucae18662020-05-15 09:32:57 +0800202 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
203 }
204 }
205}
206
207} // namespace state_sensor
208} // namespace pldm