/**
 * @file JATerminalInterfaceGAM.cpp
 * @brief Source file for class JATerminalInterfaceGAM
 * @date Feb 19, 2019
 * @author kuchida
 *
 * @copyright Copyright 2015 F4E | European Joint Undertaking for ITER and
 * the Development of Fusion Energy ('Fusion for Energy').
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved
 * by the European Commission - subsequent versions of the EUPL (the "Licence")
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
 *
 * @warning Unless required by applicable law or agreed to in writing,
 * software distributed under the Licence is distributed on an "AS IS"
 * basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the Licence permissions and limitations under the Licence.

 * @details This source file contains the definition of all the methods for
 * the class JATerminalInterfaceGAM (public, protected, and private). Be aware that some
 * methods, such as those inline could be defined on the header file, instead.
 */

/*---------------------------------------------------------------------------*/
/*                         Standard header includes                          */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/*                         Project header includes                           */
/*---------------------------------------------------------------------------*/

#include "JATerminalInterfaceGAM.h"

#include "AdvancedErrorManagement.h"

/*---------------------------------------------------------------------------*/
/*                           Static definitions                              */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/*                           Method definitions                              */
/*---------------------------------------------------------------------------*/

JATerminalInterfaceGAM::JATerminalInterfaceGAM() {

    // Fixed GAM input
    aps_hvon_term = 0;

    // Parameters which get from Input signals.
    mhvps_hvon = NULL_PTR(MARTe::uint32 *);
    aps_hvon = NULL_PTR(MARTe::uint32 *);
    aps_swon = NULL_PTR(MARTe::uint32 *);
    bps_hvon = NULL_PTR(MARTe::uint32 *);
    bps_swon = NULL_PTR(MARTe::uint32 *);

    stateMachineOutput = NULL_PTR(MARTe::uint32 *);

    // write out target.
    outputSignalNI6259 = NULL_PTR(MARTe::uint32 *);
    outputSignalNI6528P3 = NULL_PTR(MARTe::uint8 *);
    outputSignalNI6528P4 = NULL_PTR(MARTe::uint8 *);

    aps_hvon_state = 0;
    aps_swon_state = 0;
    mhvps_hvon_state = 0;
    bps_hvon_state = 0;
    bps_swon_state = 0;
    p3Value = 0;
    p4Value = 0;
}

JATerminalInterfaceGAM::~JATerminalInterfaceGAM() {
}

bool JATerminalInterfaceGAM::Initialise(MARTe::StructuredDataI & data) {
    using namespace MARTe;
    bool ok = GAM::Initialise(data);
    if (ok) {
        ok = data.Read("mhvps_hvon_term", mhvps_hvon_term);
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "The mhvps_hvon_term shall be specified");
        }
    }
    if (ok) {
        ok = data.Read("aps_hvon_term", aps_hvon_term);
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "The aps_hvon_term shall be specified");
        }
    }
    if (ok) {
        ok = data.Read("aps_swon_term", aps_swon_term);
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "The aps_swon_term shall be specified");
        }
    }
    if (ok) {
        ok = data.Read("bps_hvon_term", bps_hvon_term);
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "The bps_hvon_term shall be specified");
        }
    }
    if (ok) {
        ok = data.Read("bps_swon_term", bps_swon_term);
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "The bps_swon_term shall be specified");
        }
    }
    return ok;
}

bool JATerminalInterfaceGAM::PrepareNextState(const MARTe::char8 * const currentStateName, const MARTe::char8 * const nextStateName) {

    return true;
}

bool JATerminalInterfaceGAM::Setup() {
    using namespace MARTe;
    bool ok = (numberOfInputSignals == 9u);
    if (ok) {
        ok = (numberOfOutputSignals == 3u);
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Three output signals shall be defined");
        }
    }
    else {
        REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Nine input signals shall be defined");
    }
    if (ok) {
        uint32 c;
        for (c = 0u; c < numberOfInputSignals; c++) {
            TypeDescriptor inputType = GetSignalType(InputSignals, c);
            ok = (inputType == UnsignedInteger32Bit || inputType == UnsignedInteger8Bit);
            if (!ok) {
                StreamString signalName;
                (void) GetSignalName(InputSignals, c, signalName);
                REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Signal %s shall be defined as uint32 or uint8", signalName.Buffer());
            }

        }
    }
    if (ok) {
        uint32 c;
        for (c = 0u; c < numberOfOutputSignals; c++) {
            TypeDescriptor outputType = GetSignalType(OutputSignals, c);
            ok = (outputType == UnsignedInteger32Bit || outputType == UnsignedInteger8Bit);
            if (!ok) {
                StreamString signalName;
                (void) GetSignalName(InputSignals, c, signalName);
                REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Signal %s shall be defined as uint32 or uint8", signalName.Buffer());
            }
        }
    }
    if (ok) {
        //mhvps_manm = reinterpret_cast<uint32 *>(GetInputSignalMemory(0));
        mhvps_hvon = reinterpret_cast<uint32 *>(GetInputSignalMemory(0));
        //aps_manm = reinterpret_cast<uint32 *>(GetInputSignalMemory(2));
        aps_hvon = reinterpret_cast<uint32 *>(GetInputSignalMemory(1));
        aps_swon = reinterpret_cast<uint32 *>(GetInputSignalMemory(2));
        //bps_manm = reinterpret_cast<uint32 *>(GetInputSignalMemory(5));
        bps_hvon = reinterpret_cast<uint32 *>(GetInputSignalMemory(3));
        bps_swon = reinterpret_cast<uint32 *>(GetInputSignalMemory(4));
        short_pulse_mode = reinterpret_cast<uint32 *>(GetInputSignalMemory(5));
        stateMachineOutput = reinterpret_cast<uint32 *>(GetInputSignalMemory(6));
        ni6528p3Value = reinterpret_cast<uint8 *>(GetInputSignalMemory(7));
        ni6528p4Value = reinterpret_cast<uint8 *>(GetInputSignalMemory(8));

        outputSignalNI6259 = reinterpret_cast<uint32 *>(GetOutputSignalMemory(0));
        outputSignalNI6528P3 = reinterpret_cast<uint8 *>(GetOutputSignalMemory(1));
        outputSignalNI6528P4 = reinterpret_cast<uint8 *>(GetOutputSignalMemory(2));

    }
    return ok;
}

bool JATerminalInterfaceGAM::Execute() {
    using namespace MARTe;
    uint8 sm = *stateMachineOutput;


    //Update HVPS state
    aps_hvon_state =   ((sm >> (0))&1);
    aps_swon_state =   ((sm >> (4))&1);
    mhvps_hvon_state = ((sm >> (2))&1);
    bps_hvon_state =   ((sm >> (1))&1);
    bps_swon_state =   ((sm >> (3))&1);
    
    if(*short_pulse_mode == 1){
        p3Value = 1*aps_hvon_state + 8*bps_hvon_state +16*bps_swon_state;
        *outputSignalNI6259 = 1*aps_swon_state;
        //uint8 ni6528p3ValueTemp = 0;//*ni6528p3Value;
        //ni6528p3ValueTemp &= ~(1<<2); //Does not turn on ni6258 aps switch
        //Update terminal value
        //*outputSignalNI6528P3 = ~(ni6528p3ValueTemp | p3Value);
        *outputSignalNI6528P3 = ~p3Value;
        //REPORT_ERROR(ErrorManagement::Debug, "short pulse mode with p3: %d.", p3Value);
    } else {
        p3Value = 1*aps_hvon_state +2*aps_swon_state + 8*bps_hvon_state +16*bps_swon_state;
        //REPORT_ERROR(ErrorManagement::Debug, "long pulse mode with p3: %d.", p3Value);
    }
    
    p4Value = 8*mhvps_hvon_state;
    *outputSignalNI6528P4 = ~(*ni6528p4Value | p4Value);
    return true;
}

CLASS_REGISTER(JATerminalInterfaceGAM, "1.0")
