#ifndef MISSION_OBC_CCSDSIPCOREBRIDGE_H_
#define MISSION_OBC_CCSDSIPCOREBRIDGE_H_

#include <fsfw/tmtcservices/TmTcBridge.h>
#include <fsfw_hal/common/gpio/gpioDefinitions.h>
#include <fsfw_hal/linux/gpio/LinuxLibgpioIF.h>
#include <string.h>
#include "OBSWConfig.h"

/**
 * @brief   This class handles the interfacing to the telemetry (PTME) and telecommand (PDEC) IP
 *          cores responsible for the CCSDS encoding and decoding. The IP cores are implemented
 *          on the programmable logic and are accessible through the linux UIO driver.
 */
class CCSDSIPCoreBridge: public TmTcBridge {
public:
    /**
     * @brief   Constructor
     *
     * @param objectId
     * @param tcDestination
     * @param tmStoreId
     * @param tcStoreId
     * @param uioPtme   Name of the uio device file which provides access to the PTME IP Core.
     * @param papbBusyId    The ID of the GPIO which is connected to the PAPBBusy_N signal of the
     *                      PTME IP Core. A low logic level indicates the PTME is not ready to
     *                      receive more data.
     * @param papbEmptyId   The ID of the GPIO which is connected to the PAPBEmpty signal of the
     *                      PTME IP Core. The signal is high when there are no packets in the
     *                      external buffer memory (BRAM).
     */
    CCSDSIPCoreBridge(object_id_t objectId, object_id_t tcDestination, object_id_t tmStoreId,
            object_id_t tcStoreId, LinuxLibgpioIF* gpioComIF, std::string uioPtme,
            gpioId_t papbBusyId, gpioId_t papbEmptyId);
    virtual ~CCSDSIPCoreBridge();

    ReturnValue_t initialize() override;

protected:

    /**
     * Overwriting this function to provide the capability of testing the PTME IP Core
     * implementation.
     */
    virtual ReturnValue_t handleTm() override;

    virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override;

private:

    static const uint8_t INTERFACE_ID = CLASS_ID::CCSDS_IP_CORE_BRIDGE;

    static const ReturnValue_t PAPB_BUSY = MAKE_RETURN_CODE(0xA0);


    /** Size of mapped address space. 4k (minimal size of pl device) */
//    static const int MAP_SIZE = 0xFA0;
    static const int MAP_SIZE = 0x1000;

    /**
     * Configuration bits:
     * bit[1:0]: Size of data (1,2,3 or 4 bytes). 1 Byte <=> b00
     * bit[2]: Set this bit to 1 to abort a transfered packet
     * bit[3]: Signals to PTME the start of a new telemetry packet
     */
    static const uint32_t PTME_CONFIG_START = 0x8;

    /**
     * Writing this word to the ptme base address signals to the PTME that a complete tm packet has
     * been transferred.
     */
    static const uint32_t PTME_CONFIG_END = 0x0;

    /**
     * Writing to this offset within the PTME memory space will insert data for encoding to the
     * PTME IP core.
     * The address offset is 0x400 (= 4 * 256)
     */
    static const int PTME_DATA_REG_OFFSET = 256;

    LinuxLibgpioIF* gpioComIF = nullptr;

    /** The uio device file related to the PTME IP Core */
    std::string uioPtme;

    /** Pulled to low when PTME not ready to receive data */
    gpioId_t papbBusyId = gpio::NO_GPIO;

    /** High when externally buffer memory of PTME is empty */
    gpioId_t papbEmptyId = gpio::NO_GPIO;

    /** The file descriptor of the UIO driver */
    int fd;

    uint32_t* ptmeBaseAddress = nullptr;

    /**
     * @brief   This function sends the config byte to the PTME IP Core to initiate a packet
     *          transfer.
     */
    void startPacketTransfer();

    /**
     * @brief   This function sends the config byte to the PTME IP Core to signal the end of a
     *          packet transfer.
     */
    void endPacketTransfer();

    /**
     * @brief   This function reads the papb busy signal indicating whether the PAPB interface is
     *          ready to receive more data or not. PAPB is ready when PAPB_Busy_N == '1'.
     *
     * @return  RETURN_OK when ready to receive data else PAPB_BUSY.
     */
    ReturnValue_t pollPapbBusySignal();

    /**
     * @brief   This function can be used for debugging to check wheter there are packets in
     *          the packet buffer of the PTME or not.
     */
    void isPtmeBufferEmpty();

    /**
     * @brief   This function sends a complete telemetry transfer frame data field (1105 bytes)
     *          to the input of the PTME IP Core. Can be used to test the implementation.
     */
    ReturnValue_t sendTestFrame();
};

#endif /* MISSION_OBC_CCSDSIPCOREBRIDGE_H_ */