1cae1866fSGeorge Liu #include "dbus_to_event_handler.hpp"
2cae1866fSGeorge Liu
3cae1866fSGeorge Liu #include "libpldmresponder/pdr.hpp"
4cae1866fSGeorge Liu
549cfb138SRiya Dixit #include <phosphor-logging/lg2.hpp>
649cfb138SRiya Dixit
749cfb138SRiya Dixit PHOSPHOR_LOG2_USING;
849cfb138SRiya Dixit
9cae1866fSGeorge Liu namespace pldm
10cae1866fSGeorge Liu {
115079ac4aSBrad Bishop using namespace pldm::responder;
12cae1866fSGeorge Liu using namespace pldm::responder::pdr;
135079ac4aSBrad Bishop using namespace pldm::responder::pdr_utils;
14cae1866fSGeorge Liu using namespace pldm::utils;
15cae1866fSGeorge Liu using namespace sdbusplus::bus::match::rules;
16cae1866fSGeorge Liu
17cae1866fSGeorge Liu namespace state_sensor
18cae1866fSGeorge Liu {
19cae1866fSGeorge Liu const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR};
20cae1866fSGeorge Liu
DbusToPLDMEvent(int,uint8_t mctp_eid,pldm::InstanceIdDb & instanceIdDb,pldm::requester::Handler<pldm::requester::Request> * handler)21c0c79481SSampa Misra DbusToPLDMEvent::DbusToPLDMEvent(
22bb9daa4cSAndrew Jeffery int /* mctp_fd */, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb,
23c0c79481SSampa Misra pldm::requester::Handler<pldm::requester::Request>* handler) :
2416c2a0a0SPatrick Williams mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), handler(handler)
25cae1866fSGeorge Liu {}
26cae1866fSGeorge Liu
sendEventMsg(uint8_t eventType,const std::vector<uint8_t> & eventDataVec)27cae1866fSGeorge Liu void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
28cae1866fSGeorge Liu const std::vector<uint8_t>& eventDataVec)
29cae1866fSGeorge Liu {
30a330b2f0SAndrew Jeffery auto instanceId = instanceIdDb.next(mctp_eid);
3116c2a0a0SPatrick Williams std::vector<uint8_t> requestMsg(
3216c2a0a0SPatrick Williams sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
33cae1866fSGeorge Liu eventDataVec.size());
34*49575833SPavithra Barithaya auto request = new (requestMsg.data()) pldm_msg;
35cae1866fSGeorge Liu
36cae1866fSGeorge Liu auto rc = encode_platform_event_message_req(
376c39c7a7SArchanaKakani instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
38cae1866fSGeorge Liu eventDataVec.data(), eventDataVec.size(), request,
39cae1866fSGeorge Liu eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
40cae1866fSGeorge Liu if (rc != PLDM_SUCCESS)
41cae1866fSGeorge Liu {
42a330b2f0SAndrew Jeffery instanceIdDb.free(mctp_eid, instanceId);
43d6e10adaSRiya Dixit error(
44d6e10adaSRiya Dixit "Failed to encode platform event message request, response code '{RC}'",
45d6e10adaSRiya Dixit "RC", rc);
46cae1866fSGeorge Liu return;
47cae1866fSGeorge Liu }
48cae1866fSGeorge Liu
4916c2a0a0SPatrick Williams auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
5016c2a0a0SPatrick Williams const pldm_msg* response,
5116c2a0a0SPatrick Williams size_t respMsgLen) {
52c0c79481SSampa Misra if (response == nullptr || !respMsgLen)
53cae1866fSGeorge Liu {
5449cfb138SRiya Dixit error("Failed to receive response for platform event message");
55cae1866fSGeorge Liu return;
56cae1866fSGeorge Liu }
57cae1866fSGeorge Liu uint8_t completionCode{};
58cae1866fSGeorge Liu uint8_t status{};
59c0c79481SSampa Misra auto rc = decode_platform_event_message_resp(response, respMsgLen,
60c0c79481SSampa Misra &completionCode, &status);
61c0c79481SSampa Misra if (rc || completionCode)
62cae1866fSGeorge Liu {
6349cfb138SRiya Dixit error(
641e5c81e0SRiya Dixit "Failed to decode response of platform event message, response code '{RC}' and completion code '{CC}'",
65d6e10adaSRiya Dixit "RC", rc, "CC", completionCode);
66cae1866fSGeorge Liu }
67c0c79481SSampa Misra };
68c0c79481SSampa Misra
69c0c79481SSampa Misra rc = handler->registerRequest(
70c0c79481SSampa Misra mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
71c0c79481SSampa Misra std::move(requestMsg), std::move(platformEventMessageResponseHandler));
72c0c79481SSampa Misra if (rc)
73c0c79481SSampa Misra {
74d6e10adaSRiya Dixit error("Failed to send the platform event message, response code '{RC}'",
75d6e10adaSRiya Dixit "RC", rc);
76c0c79481SSampa Misra }
77cae1866fSGeorge Liu }
78cae1866fSGeorge Liu
sendStateSensorEvent(SensorId sensorId,const DbusObjMaps & dbusMaps)79cae1866fSGeorge Liu void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId,
80cae1866fSGeorge Liu const DbusObjMaps& dbusMaps)
81cae1866fSGeorge Liu {
82cae1866fSGeorge Liu // Encode PLDM platform event msg to indicate a state sensor change.
83cae1866fSGeorge Liu // DSP0248_1.2.0 Table 19
84bd5e2eaaSGeorge Liu if (!dbusMaps.contains(sensorId))
85bd5e2eaaSGeorge Liu {
86f34867dfSManojkiran Eda // this is not an error condition, if we end up here
87f34867dfSManojkiran Eda // that means that the sensor with the sensor id has
88f34867dfSManojkiran Eda // custom behaviour(or probably an oem sensor) in
89f34867dfSManojkiran Eda // sending events that cannot be captured via standard
90f34867dfSManojkiran Eda // dbus-json infastructure
91bd5e2eaaSGeorge Liu return;
92bd5e2eaaSGeorge Liu }
93bd5e2eaaSGeorge Liu
94cae1866fSGeorge Liu size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
95cae1866fSGeorge Liu const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
9603b02d59SPavithra Barithaya for (size_t offset = 0; offset < dbusMappings.size(); ++offset)
97cae1866fSGeorge Liu {
98cae1866fSGeorge Liu std::vector<uint8_t> sensorEventDataVec{};
99cae1866fSGeorge Liu sensorEventDataVec.resize(sensorEventSize);
100*49575833SPavithra Barithaya auto eventData = new (sensorEventDataVec.data()) pldm_sensor_event_data;
101cae1866fSGeorge Liu eventData->sensor_id = sensorId;
102cae1866fSGeorge Liu eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
10303b02d59SPavithra Barithaya eventData->event_class[0] = static_cast<uint8_t>(offset);
104cae1866fSGeorge Liu eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
105cae1866fSGeorge Liu eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
106cae1866fSGeorge Liu
107cae1866fSGeorge Liu const auto& dbusMapping = dbusMappings[offset];
108cae1866fSGeorge Liu const auto& dbusValueMapping = dbusValMaps[offset];
10984b790cbSPatrick Williams auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>(
110cae1866fSGeorge Liu pldm::utils::DBusHandler::getBus(),
111cae1866fSGeorge Liu propertiesChanged(dbusMapping.objectPath.c_str(),
112cae1866fSGeorge Liu dbusMapping.interface.c_str()),
113ae933cc2SManojkiran Eda [this, sensorEventDataVec, dbusValueMapping, dbusMapping, sensorId,
114ae933cc2SManojkiran Eda offset](auto& msg) mutable {
115cae1866fSGeorge Liu DbusChangedProps props{};
116cae1866fSGeorge Liu std::string intf;
117ae933cc2SManojkiran Eda uint8_t previousState = PLDM_SENSOR_UNKNOWN;
118cae1866fSGeorge Liu msg.read(intf, props);
119681715c0SGeorge Liu if (!props.contains(dbusMapping.propertyName))
120681715c0SGeorge Liu {
121681715c0SGeorge Liu return;
122681715c0SGeorge Liu }
123cae1866fSGeorge Liu for (const auto& itr : dbusValueMapping)
124cae1866fSGeorge Liu {
12599999915SGeorge Liu bool findValue = false;
12699999915SGeorge Liu if (dbusMapping.propertyType == "string")
12799999915SGeorge Liu {
12899999915SGeorge Liu std::string src = std::get<std::string>(itr.second);
12999999915SGeorge Liu std::string dst = std::get<std::string>(
13099999915SGeorge Liu props.at(dbusMapping.propertyName));
13199999915SGeorge Liu
13299999915SGeorge Liu auto values = pldm::utils::split(src, "||", " ");
13315b94111SPavithra Barithaya for (const auto& value : values)
13499999915SGeorge Liu {
13599999915SGeorge Liu if (value == dst)
13699999915SGeorge Liu {
13799999915SGeorge Liu findValue = true;
13899999915SGeorge Liu break;
13999999915SGeorge Liu }
14099999915SGeorge Liu }
14199999915SGeorge Liu }
14299999915SGeorge Liu else
14399999915SGeorge Liu {
14416c2a0a0SPatrick Williams findValue =
14516c2a0a0SPatrick Williams itr.second == props.at(dbusMapping.propertyName)
14699999915SGeorge Liu ? true
14799999915SGeorge Liu : false;
14899999915SGeorge Liu }
14999999915SGeorge Liu
15099999915SGeorge Liu if (findValue)
151cae1866fSGeorge Liu {
152*49575833SPavithra Barithaya auto eventData = new (sensorEventDataVec.data())
153*49575833SPavithra Barithaya pldm_sensor_event_data;
154cae1866fSGeorge Liu eventData->event_class[1] = itr.first;
155ae933cc2SManojkiran Eda if (sensorCacheMap.contains(sensorId) &&
15616c2a0a0SPatrick Williams sensorCacheMap[sensorId][offset] !=
15716c2a0a0SPatrick Williams PLDM_SENSOR_UNKNOWN)
158ae933cc2SManojkiran Eda {
159ae933cc2SManojkiran Eda previousState = sensorCacheMap[sensorId][offset];
160ae933cc2SManojkiran Eda }
161ae933cc2SManojkiran Eda else
162ae933cc2SManojkiran Eda {
163ae933cc2SManojkiran Eda previousState = itr.first;
164ae933cc2SManojkiran Eda }
165ae933cc2SManojkiran Eda eventData->event_class[2] = previousState;
16616c2a0a0SPatrick Williams this->sendEventMsg(PLDM_SENSOR_EVENT,
16716c2a0a0SPatrick Williams sensorEventDataVec);
168ae933cc2SManojkiran Eda updateSensorCacheMaps(sensorId, offset, previousState);
16999999915SGeorge Liu break;
170cae1866fSGeorge Liu }
171cae1866fSGeorge Liu }
172cae1866fSGeorge Liu });
173cae1866fSGeorge Liu stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
174cae1866fSGeorge Liu }
175cae1866fSGeorge Liu }
176cae1866fSGeorge Liu
listenSensorEvent(const pdr_utils::Repo & repo,const DbusObjMaps & dbusMaps)177cae1866fSGeorge Liu void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
178cae1866fSGeorge Liu const DbusObjMaps& dbusMaps)
179cae1866fSGeorge Liu {
180cae1866fSGeorge Liu const std::map<Type, sensorEvent> sensorHandlers = {
181cae1866fSGeorge Liu {PLDM_STATE_SENSOR_PDR,
182cae1866fSGeorge Liu [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
183cae1866fSGeorge Liu this->sendStateSensorEvent(sensorId, dbusMaps);
184cae1866fSGeorge Liu }}};
185cae1866fSGeorge Liu
186cae1866fSGeorge Liu pldm_state_sensor_pdr* pdr = nullptr;
187cae1866fSGeorge Liu std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
188cae1866fSGeorge Liu pldm_pdr_init(), pldm_pdr_destroy);
189acb20299SAndrew Jeffery if (!sensorPdrRepo)
190acb20299SAndrew Jeffery {
191acb20299SAndrew Jeffery throw std::runtime_error("Unable to instantiate sensor PDR repository");
192acb20299SAndrew Jeffery }
193cae1866fSGeorge Liu
194cae1866fSGeorge Liu for (auto pdrType : pdrTypes)
195cae1866fSGeorge Liu {
196cae1866fSGeorge Liu Repo sensorPDRs(sensorPdrRepo.get());
197cae1866fSGeorge Liu getRepoByType(repo, sensorPDRs, pdrType);
198cae1866fSGeorge Liu if (sensorPDRs.empty())
199cae1866fSGeorge Liu {
200cae1866fSGeorge Liu return;
201cae1866fSGeorge Liu }
202cae1866fSGeorge Liu
203cae1866fSGeorge Liu PdrEntry pdrEntry{};
204cae1866fSGeorge Liu auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
205cae1866fSGeorge Liu while (pdrRecord)
206cae1866fSGeorge Liu {
207*49575833SPavithra Barithaya pdr = new (pdrEntry.data) pldm_state_sensor_pdr;
208cae1866fSGeorge Liu SensorId sensorId = LE16TOH(pdr->sensor_id);
209bd5e2eaaSGeorge Liu if (sensorHandlers.contains(pdrType))
210bd5e2eaaSGeorge Liu {
211cae1866fSGeorge Liu sensorHandlers.at(pdrType)(sensorId, dbusMaps);
212bd5e2eaaSGeorge Liu }
213bd5e2eaaSGeorge Liu
214cae1866fSGeorge Liu pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
215cae1866fSGeorge Liu }
216cae1866fSGeorge Liu }
217cae1866fSGeorge Liu }
218cae1866fSGeorge Liu
219cae1866fSGeorge Liu } // namespace state_sensor
220cae1866fSGeorge Liu } // namespace pldm
221