/**
 * \file    M_Port.cpp
 * \author  Silvan Murer
 * \date    01 Aug 2022
 *
 * \brief   Internal class used to link GenTL and GenApi
 */

#include "M_Port.hpp"

using namespace GenApi;
using namespace GenTL;
using namespace genSDK;

namespace mimic {

M_Port::M_Port(std::shared_ptr<GTL_Producer> gtl_producer, PORT_HANDLE hPort)
 : gtl_producer(gtl_producer), hPort(hPort)
{ }

M_Port::~M_Port(void) { }

void M_Port::Read(void* pBuffer, int64_t Address, int64_t Length)
{
    size_t iSize = static_cast<size_t>(Length);
    GC_ERROR err = gtl_producer->pGCReadPort(hPort, Address, pBuffer, &iSize);
    if (err != GC_ERR_SUCCESS) {
        size_t errSize = 0;
        gtl_producer->pGCGetLastError(&err, nullptr, &errSize);
        std::string errData(errSize, 0);
        gtl_producer->pGCGetLastError(&err, &errData.front(), &errSize);
        throw Mimic_GenTL_Exception(errData, err);
    }
}
void M_Port::Write(const void* pBuffer, int64_t Address, int64_t Length)
{
    size_t iSize = static_cast<size_t>(Length);
    GC_ERROR err = gtl_producer->pGCWritePort(hPort, Address, pBuffer, &iSize);
    if (err != GC_ERR_SUCCESS) {
        size_t errSize = 0;
        gtl_producer->pGCGetLastError(&err, nullptr, &errSize);
        std::string errData(errSize, 0);
        gtl_producer->pGCGetLastError(&err, &errData.front(), &errSize);
        throw Mimic_GenTL_Exception(errData, err);
    }
}

EAccessMode M_Port::GetAccessMode(void) const
{
    return RW;
}

} /* namespace genSDK */
