OEM IBM: Platform: event framework to send events to PHYP

This commit consists of APIs that will be used to send code update
event change/update PlatformEventMessage to Phyp for different
stages of inband code update progress.
The event is a state sensor event.

Signed-off-by: Varsha Kaverappa <vkaverap@in.ibm.com>
Change-Id: I9b4b0493dd6886805044011d994df58b31f80a6e
diff --git a/oem/ibm/libpldmresponder/oem_ibm_handler.cpp b/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
index 9c312c7..53c1bfc 100644
--- a/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
+++ b/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
@@ -1,6 +1,7 @@
 #include "oem_ibm_handler.hpp"
 
 #include "libpldm/entity.h"
+#include "libpldm/requester/pldm.h"
 
 #include "libpldmresponder/pdr_utils.hpp"
 
@@ -189,6 +190,107 @@
     platformHandler = handler;
 }
 
+int pldm::responder::oem_ibm_platform::Handler::sendEventToHost(
+    std::vector<uint8_t>& requestMsg)
+{
+    uint8_t* responseMsg = nullptr;
+    size_t responseMsgSize{};
+    if (requestMsg.size())
+    {
+        std::ostringstream tempStream;
+        for (int byte : requestMsg)
+        {
+            tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
+                       << " ";
+        }
+        std::cout << tempStream.str() << std::endl;
+    }
+
+    auto requesterRc =
+        pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(), requestMsg.size(),
+                       &responseMsg, &responseMsgSize);
+    std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{responseMsg,
+                                                                  std::free};
+    if (requesterRc != PLDM_REQUESTER_SUCCESS)
+    {
+        std::cerr << "Failed to send message/receive response. RC = "
+                  << requesterRc << ", errno = " << errno
+                  << "for sending event to host \n";
+        return requesterRc;
+    }
+    uint8_t completionCode{};
+    uint8_t status{};
+    auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsgPtr.get());
+    auto rc = decode_platform_event_message_resp(
+        responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), &completionCode,
+        &status);
+
+    if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
+    {
+        std::cerr << "Failure in decode platform event message response, rc= "
+                  << rc << " cc=" << static_cast<unsigned>(completionCode)
+                  << "\n";
+        return rc;
+    }
+    std::cout << "returning rc= " << rc << " from sendEventToHost \n";
+
+    return rc;
+}
+
+void pldm::responder::oem_ibm_platform::Handler::sendStateSensorEvent(
+    uint16_t sensorId, enum sensor_event_class_states sensorEventClass,
+    uint8_t sensorOffset, uint8_t eventState, uint8_t prevEventState)
+{
+
+    std::vector<uint8_t> sensorEventDataVec{};
+    size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
+    sensorEventDataVec.resize(sensorEventSize);
+    auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
+        sensorEventDataVec.data());
+    eventData->sensor_id = sensorId;
+    eventData->sensor_event_class_type = sensorEventClass;
+    auto eventClassStart = eventData->event_class;
+    auto eventClass =
+        reinterpret_cast<struct pldm_sensor_event_state_sensor_state*>(
+            eventClassStart);
+    eventClass->sensor_offset = sensorOffset;
+    eventClass->event_state = eventState;
+    eventClass->previous_event_state = prevEventState;
+    auto instanceId = requester.getInstanceId(mctp_eid);
+    std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
+                                    PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
+                                    sensorEventDataVec.size());
+    auto rc = encodeEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec, requestMsg,
+                             instanceId);
+    if (rc != PLDM_SUCCESS)
+    {
+        std::cerr << "Failed to encode state sensor event, rc = " << rc
+                  << std::endl;
+        return;
+    }
+    rc = sendEventToHost(requestMsg);
+    if (rc != PLDM_SUCCESS)
+    {
+        std::cerr << "Failed to send event to host: "
+                  << "rc=" << rc << std::endl;
+    }
+    requester.markFree(mctp_eid, instanceId);
+    return;
+}
+
+int encodeEventMsg(uint8_t eventType, const std::vector<uint8_t>& eventDataVec,
+                   std::vector<uint8_t>& requestMsg, uint8_t instanceId)
+{
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_platform_event_message_req(
+        instanceId, 1 /*formatVersion*/, pldm::responder::pdr::BmcTerminusId,
+        eventType, eventDataVec.data(), eventDataVec.size(), request,
+        eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
+
+    return rc;
+}
+
 } // namespace oem_ibm_platform
 
 } // namespace responder
diff --git a/oem/ibm/libpldmresponder/oem_ibm_handler.hpp b/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
index 2220adb..bacc3f0 100644
--- a/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
+++ b/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
@@ -1,7 +1,10 @@
 #pragma once
 
+#include "libpldm/platform.h"
+
 #include "inband_code_update.hpp"
 #include "libpldmresponder/oem_handler.hpp"
+#include "libpldmresponder/pdr_utils.hpp"
 #include "libpldmresponder/platform.hpp"
 
 namespace pldm
@@ -18,13 +21,25 @@
 constexpr uint16_t ENTITY_INSTANCE_0 = 0;
 constexpr uint16_t ENTITY_INSTANCE_1 = 1;
 
+enum class CodeUpdateState : uint8_t
+{
+    START = 0x1,
+    END = 0x2,
+    FAIL = 0x3,
+    ABORT = 0x4,
+    ACCEPT = 0x5,
+    REJECT = 0x6
+};
+
 class Handler : public oem_platform::Handler
 {
   public:
     Handler(const pldm::utils::DBusHandler* dBusIntf,
-            pldm::responder::CodeUpdate* codeUpdate) :
+            pldm::responder::CodeUpdate* codeUpdate, int mctp_fd,
+            uint8_t mctp_eid, Requester& requester) :
         oem_platform::Handler(dBusIntf),
-        codeUpdate(codeUpdate), platformHandler(nullptr)
+        codeUpdate(codeUpdate), platformHandler(nullptr), mctp_fd(mctp_fd),
+        mctp_eid(mctp_eid), requester(requester)
     {
         codeUpdate->setVersions();
     }
@@ -71,13 +86,53 @@
      */
     void buildOEMPDR(pdr_utils::Repo& repo);
 
+    /** @brief Method to send code update event to host
+     * @param[in] sensorId - sendor ID
+     * @param[in] sensorEventClass - event class of sensor
+     * @param[in] sensorOffset - sensor offset
+     * @param[in] eventState - new code update event state
+     * @param[in] prevEventState - previous code update event state
+     * @return none
+     */
+    void sendStateSensorEvent(uint16_t sensorId,
+                              enum sensor_event_class_states sensorEventClass,
+                              uint8_t sensorOffset, uint8_t eventState,
+                              uint8_t prevEventState);
+
+    /** @brief Method to send encoded request msg of code update event to host
+     *  @param[in] requestMsg - encoded request msg
+     *  @return PLDM status code
+     */
+    int sendEventToHost(std::vector<uint8_t>& requestMsg);
+
     ~Handler() = default;
 
     pldm::responder::CodeUpdate* codeUpdate; //!< pointer to CodeUpdate object
     pldm::responder::platform::Handler*
         platformHandler; //!< pointer to PLDM platform handler
+
+    /** @brief fd of MCTP communications socket */
+    int mctp_fd;
+
+    /** @brief MCTP EID of host firmware */
+    uint8_t mctp_eid;
+
+    /** @brief reference to Requester object, primarily used to access API to
+     *  obtain PLDM instance id.
+     */
+    Requester& requester;
 };
 
+/** @brief Method to encode code update event msg
+ *  @param[in] eventType - type of event
+ *  @param[in] eventDataVec - vector of event data to be sent to host
+ *  @param[in/out] requestMsg - request msg to be encoded
+ *  @param[in] instanceId - instance ID
+ *  @return PLDM status code
+ */
+int encodeEventMsg(uint8_t eventType, const std::vector<uint8_t>& eventDataVec,
+                   std::vector<uint8_t>& requestMsg, uint8_t instanceId);
+
 } // namespace oem_ibm_platform
 
 } // namespace responder
diff --git a/oem/ibm/test/libpldmresponder_oem_platform_test.cpp b/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
index 7963313..5f80f3b 100644
--- a/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
+++ b/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
@@ -33,6 +33,8 @@
     uint16_t stateSetId_ = PLDM_OEM_IBM_BOOT_STATE;
     uint16_t entityInstance_ = 0;
     uint8_t compSensorCnt_ = 1;
+    sdbusplus::bus::bus bus(sdbusplus::bus::new_default());
+    Requester requester(bus, "/abc/def");
 
     std::vector<get_sensor_state_field> stateField;
 
@@ -42,7 +44,7 @@
     std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
 
     oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
-        mockDbusHandler.get(), mockCodeUpdate.get());
+        mockDbusHandler.get(), mockCodeUpdate.get(), 0x1, 0x9, requester);
 
     auto rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
         entityID_, entityInstance_, stateSetId_, compSensorCnt_, stateField);
@@ -101,3 +103,41 @@
         setEffecterStateField);
     ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
 }
+
+TEST(EncodeCodeUpdateEvent, testGoodRequest)
+{
+    size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
+    std::vector<uint8_t> sensorEventDataVec{};
+    sensorEventDataVec.resize(sensorEventSize);
+
+    auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
+        sensorEventDataVec.data());
+    eventData->sensor_id = 0xA;
+    eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE;
+
+    auto opStateSensorEventData =
+        reinterpret_cast<struct pldm_sensor_event_sensor_op_state*>(
+            sensorEventDataVec.data());
+    opStateSensorEventData->present_op_state = uint8_t(CodeUpdateState::START);
+    opStateSensorEventData->previous_op_state = uint8_t(CodeUpdateState::END);
+
+    std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
+                                    PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
+                                    sensorEventDataVec.size());
+
+    auto rc =
+        encodeEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec, requestMsg, 0x1);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+}
+
+TEST(EncodeCodeUpdate, testBadRequest)
+{
+    std::vector<uint8_t> requestMsg;
+    std::vector<uint8_t> sensorEventDataVec{};
+
+    auto rc =
+        encodeEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec, requestMsg, 0x1);
+
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
diff --git a/pldmd/pldmd.cpp b/pldmd/pldmd.cpp
index 7bf2aa4..59018b6 100644
--- a/pldmd/pldmd.cpp
+++ b/pldmd/pldmd.cpp
@@ -201,8 +201,7 @@
     std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
         std::make_unique<pldm::responder::CodeUpdate>(dbusHandler.get());
     oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
-        dbusHandler.get(), codeUpdate.get());
-
+        dbusHandler.get(), codeUpdate.get(), sockfd, hostEID, dbusImplReq);
     codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
     invoker.registerHandler(
         PLDM_OEM, std::make_unique<oem_ibm::Handler>(oemPlatformHandler.get()));