//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / SCCP protocol
//
//File: sccpstate_sclc_idle.cpp
//
//Version: $Revision: 1.9 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/11/09 09:45:57 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Pärnänen
//
//Description:
//      See corresponding header file.
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include "pf/exception.h"
#include "pf/debug.h"
#include "sccppdus.h"
#include "sccpblock_sclc.h"
#include "sccpstate_sclc_idle.h"
#include "iface/sccpif/sccpupprimitives.h"

// Implementation of the singleton pattern

sccp_SCLC_Idle *sccp_SCLC_Idle::_only = 0;

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

sccp_SCLC_Idle :: sccp_SCLC_Idle(void)
{
    return;
}

sccp_SCLC_Idle :: ~sccp_SCLC_Idle(void)
{
    _only = 0;
    return;
}

// pdu inputs

void sccp_SCLC_Idle :: sccpUDTpduAct(sccpUDTpdu *messenger_,
                                     pfProtocol *protocol_)
{
    sccpN_UNITDATAind *messenger = new sccpN_UNITDATAind;

    pfStorage calledAddress = messenger_->getStorage("calledAddress");
    pfStorage callingAddress = messenger_->getStorage("callingAddress");

    messenger->setCalledAddress(calledAddress);
    messenger->setCallingAddress(callingAddress);
    pfFrame frame = messenger_->getFrame("userData");
    messenger->setUserData(frame);
    
    ((sccpBlockSCLC *)protocol_)->toSubSystem(messenger);
    return;
}

// primitives from upper layer

void sccp_SCLC_Idle :: sccpN_UNITDATAreqAct(sccpN_UNITDATAreq *messenger_,
                                            pfProtocol *protocol_)
{
    sccpPdu *pdu = 0;
    try
    {
        pdu = sccpUDTpdu::createUDTpdu(messenger_);
        sccpBlockSCLC *block = (sccpBlockSCLC *)protocol_;
        block->assignSLS(pdu);
        block->toSCRC(pdu);
    }
    catch (pfException &exception)
    {
        // Message is not passed to the MTP3 layer or
        // local node, maybe insufficient or invalid
        // compination of information.

        debugUser("Message discarded (sccpN_UNITDATAreqAct)");
    }

    // Pdu is transfered synchronously (locally inside SCCP)
    // It is not destroyed automatically.
    delete pdu;
    return;
}
