//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project
//
//File: unistate_n6.cpp
//
//Version: $Revision: 1.15 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/12/14 06:56:33 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Jari Pusa
//
//Description:
//      State Call Present (N6)
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include "unistate_n6.h"

#include "unidefs.h"
#include "unistrings.h"
#include "uniprotocol.h"
#include "unipdu.h"

uniNetworkCallPresent *uniNetworkCallPresent :: _only = 0;

uniNetworkCallPresent *uniNetworkCallPresent :: instance(void)
{
    if (_only == 0)
    {
        _only = new uniNetworkCallPresent;
    }
    return _only;
}

uniNetworkCallPresent :: uniNetworkCallPresent(void)
    : uniNetworkState()
{
    return;
}

uniNetworkCallPresent :: ~uniNetworkCallPresent(void)
{
    return;
}

void uniNetworkCallPresent :: uniCONNECTpduAct(uniCONNECTpdu *message_,
                                               pfProtocol *protocol_)
{
    uniProtocol *protocol = dynamic_cast<uniProtocol*>(protocol_);
    protocol->verifyMessage(message_, uniState_ConnectRequest);
    protocol->stopTimer(uniT303Str);

    if (protocol->compareCI(message_) != 0)
    {
        protocol->sendSETUPconfToCC(message_);
        protocol->changeToConnectRequestState();
    }
    else
    {
        protocol->startReleaseProcess(
            uniCauseValue_VPCI_VCI_AssignmentFailure);
    }  

    return;
}

void uniNetworkCallPresent :: uniALERTINGpduAct(uniALERTINGpdu *message_,
                                                pfProtocol *protocol_)
{
    uniProtocol *protocol = dynamic_cast<uniProtocol*>(protocol_);
    if (protocol->getBoolean(uniQ2931ConnectionFlagStr) == 0)
    {
        protocol->reportIncompatibleMessage(message_);
    }
    else
    {
        protocol->verifyMessage(message_, uniState_CallReceived);
        protocol->stopTimer(uniT303Str);
        protocol->sendALERTINGindToCC(message_);
        protocol->startTimer(uniT301Str);
        protocol->changeToCallReceivedState();
    }
    return;
}

void uniNetworkCallPresent :: uniCALL_PROCEEDINGpduAct(
    uniCALL_PROCEEDINGpdu *message_,
    pfProtocol *protocol_)
{
    uniProtocol *protocol = dynamic_cast<uniProtocol*>(protocol_);
    protocol->verifyMessage(message_, uniState_IncomingCallProceeding);
    protocol->stopTimer(uniT303Str);

    if (protocol->compareCI(message_) != 0)
    {
        protocol->sendPROCEEDINGindToCC(message_);
        protocol->startTimer(uniT310Str);
        protocol->changeToIncomingCallProceedingState();        
    }
    else
    {
        protocol->startReleaseProcess(
            uniCauseValue_VPCI_VCI_AssignmentFailure);
    }
    return;
}

void uniNetworkCallPresent :: uniT303timeoutAct(pfProtocol *protocol_)
{
    uniProtocol *protocol = dynamic_cast<uniProtocol*>(protocol_);
    if (protocol->getInteger(uniT303TimeoutsStr) == 0)
    {
        protocol->sendSETUPpduToCoOrd();
        protocol->startTimer(uniT303Str);
        protocol->setInteger(uniT303TimeoutsStr, 1);
    }
    else
    {
        protocol->clearCall(uniCauseValue_NoUserResponding);
    }
    return;
}

