//Editor-Info: -*- C++ -*-
//
//Subject: TOVE-project / MTP3 protocol
//
//File: mtp3block_hmdt.cpp
//
//Version: $Revision: 1.10 $
//
//Date: $Date: 1998/09/01 09:27:39 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications and Multimedia
//
//Author:
//      Timo Pärnänen
//      Petteri Koponen
//
//Description:
//      See corresponding header file.
//
//Copyright:
//
//
//Licence:
//
//
//History:

#include "mtp3defs.h"
#include "mtp3protocol.h"
#include "iface/mtpif/mtpupprimitives.h"
#include "mtp3block_hmdt.h"
#include "mtp3state_hmdt_idle.h"

//Constructor: mtp3BlockHMDT
//
//Description:
//      Initializes member variables. The initial state is idle.
//

mtp3BlockHMDT :: mtp3BlockHMDT(mtp3Protocol *protocol_,
                               ss7Configure &configure_)
    : mtp3Colleague(protocol_, configure_)
{
    // Increment block's reference counter, because there is no proxy 
    // to block in main function
    incRefCount();
    changeState(mtp3_HMDT_Idle::instance());
    return;
}

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

void  mtp3BlockHMDT :: toLevel4(pfFrame &frame_,
                                pfUlong serviceIndicator_,
                                pfUlong AALidentifier_)
{
    if (_configure.isUserPartAvailable(serviceIndicator_) != 0)
    {
        if (frame_.length() >= 5)
        {
            mtpTRANSFERind *messenger = new mtpTRANSFERind;
            messenger->setAALidentifier(AALidentifier_);
            decodeUserPartMessage(frame_, messenger); 
            _protocol->toLevel4(messenger);
        }
        // else discard message
    }
    else
    {
        cerr << "--not implemented-- : ";
        cerr << "User part unavailable --> discard" << endl;
        // ++TODO++ Send UserPartUnavilableMessage
    }
    return;
}

void mtp3BlockHMDT :: decodeUserPartMessage(pfFrame &frame_,
                                            mtpTRANSFERind *messenger_)
{
    messenger_->setServiceInformationOctet(frame_.getFirst());
    
    pfUlong DPC = frame_.getFirst();
    pfUlong tmp = frame_.getFirst();
    DPC = ((tmp & MTP3_6_BITS) << 8) + DPC;
    
    pfUlong OPC = tmp >> 6;
    tmp = frame_.getFirst();
    OPC = (tmp << 2) + OPC;
    tmp = frame_.getFirst();
    OPC = ((tmp & MTP3_4_BITS) << 10) + OPC;
    
    messenger_->setDestinationPointCode(DPC);
    messenger_->setOriginatingPointCode(OPC);
    messenger_->setSignallingLinkSelection((tmp >> 4));
    if (frame_.length() > 0)
    {
        messenger_->setSignallingInformation(frame_);
    }
    return;
}
