blob: cf3064ed06f14758c0a1326ff106810e38a48645 [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
George Liucae18662020-05-15 09:32:57 +08007namespace pldm
8{
Brad Bishop5079ac42021-08-19 18:35:06 -04009using namespace pldm::dbus_api;
10using namespace pldm::responder;
George Liucae18662020-05-15 09:32:57 +080011using namespace pldm::responder::pdr;
Brad Bishop5079ac42021-08-19 18:35:06 -040012using namespace pldm::responder::pdr_utils;
George Liucae18662020-05-15 09:32:57 +080013using namespace pldm::utils;
14using namespace sdbusplus::bus::match::rules;
15
16namespace state_sensor
17{
18const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR};
19
Sampa Misrac0c79482021-06-02 08:01:54 -050020DbusToPLDMEvent::DbusToPLDMEvent(
21 int mctp_fd, uint8_t mctp_eid, Requester& requester,
22 pldm::requester::Handler<pldm::requester::Request>* handler) :
George Liucae18662020-05-15 09:32:57 +080023 mctp_fd(mctp_fd),
Sampa Misrac0c79482021-06-02 08:01:54 -050024 mctp_eid(mctp_eid), requester(requester), 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{
30 auto instanceId = requester.getInstanceId(mctp_eid);
31 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
32 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
33 eventDataVec.size());
34 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 {
42 requester.markFree(mctp_eid, instanceId);
43 std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc
44 << std::endl;
45 return;
46 }
47
Sampa Misrac0c79482021-06-02 08:01:54 -050048 auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
49 const pldm_msg* response,
50 size_t respMsgLen) {
51 if (response == nullptr || !respMsgLen)
52 {
53 std::cerr
54 << "Failed to receive response for platform event message \n";
55 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 {
63 std::cerr << "Failed to decode_platform_event_message_resp: "
64 << "rc=" << rc
65 << ", cc=" << static_cast<unsigned>(completionCode)
66 << std::endl;
67 }
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 {
Sampa Misrac0c79482021-06-02 08:01:54 -050075 std::cerr << "Failed to send the platform event message \n";
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 {
George Liucae18662020-05-15 09:32:57 +0800116 DbusChangedProps props{};
117 std::string intf;
118 msg.read(intf, props);
George Liu681715c2021-11-25 16:00:55 +0800119 if (!props.contains(dbusMapping.propertyName))
120 {
121 return;
122 }
George Liucae18662020-05-15 09:32:57 +0800123 for (const auto& itr : dbusValueMapping)
124 {
George Liu99999912021-11-25 16:39:57 +0800125 bool findValue = false;
126 if (dbusMapping.propertyType == "string")
127 {
128 std::string src = std::get<std::string>(itr.second);
129 std::string dst = std::get<std::string>(
130 props.at(dbusMapping.propertyName));
131
132 auto values = pldm::utils::split(src, "||", " ");
133 for (auto& value : values)
134 {
135 if (value == dst)
136 {
137 findValue = true;
138 break;
139 }
140 }
141 }
142 else
143 {
144 findValue =
145 itr.second == props.at(dbusMapping.propertyName)
146 ? true
147 : false;
148 }
149
150 if (findValue)
George Liucae18662020-05-15 09:32:57 +0800151 {
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,
158 sensorEventDataVec);
George Liu99999912021-11-25 16:39:57 +0800159 break;
George Liucae18662020-05-15 09:32:57 +0800160 }
161 }
162 });
163 stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
164 }
165}
166
167void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
168 const DbusObjMaps& dbusMaps)
169{
170 const std::map<Type, sensorEvent> sensorHandlers = {
171 {PLDM_STATE_SENSOR_PDR,
172 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
173 this->sendStateSensorEvent(sensorId, dbusMaps);
174 }}};
175
176 pldm_state_sensor_pdr* pdr = nullptr;
177 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
178 pldm_pdr_init(), pldm_pdr_destroy);
179
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