//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / bisup
//
//File: bisupiecoder.cpp
//
//Version: $Revision: 1.19 $
//
//State: $State: Exp $
//
//Date: $Date: 1999/03/08 07:42:30 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
// 
//Author:
//      Sami Raatikainen
//
//Description:
//      See corresponding header file.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History:
//

#include "bisupiecoder.h"
#include "protocol/sig/sigexceptions.h"
#include "pf/debug.h"

bisupIECoder :: bisupIECoder(
    const string &IEname_,
    const pfByte &IEtype_,
    bool isMandatory_)
    : IECoder(IEname_, IEtype_, isMandatory_),
      _parameterCompatibilityInf1(B0000_0000),
      _parameterCompatibilityInf2(B1000_0000)
{
    setHeaderMinLength(5);
    return;
}

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


//
//Function: decode
//
//Description:
//      Decodes first the common header part for all parameters, and 
//      then the specific coder will decode the content part.
//

void bisupIECoder :: decodeIE(pfFrame &frame_, pfMessenger *msg_)
{
    checkHeaderMinLength(frame_.length());

    // Type already checked and defined
    frame_.getFirst(); 

    // Get the length indication and check it.
    pfUlong IElength = frame_.getFirst16bit();
    if (IElength > frame_.length())
    {
        debugUser("Incorrect IE length");
        throwContentError();
    }
    checkContentLength(IElength);
    
    // Decode IE if allowed (sequence / max repetitions).
    if (_maxTimeExist != 0)
    {
        // Header part
        _parameterCompatibilityInf1 = frame_.getFirst();
        --IElength;
        _parameterCompatibilityInf2 = frame_.getFirst();
        --IElength;
        
        // Content part
        decodeContent(frame_, IElength, msg_);
        --_maxTimeExist;
    }

    // Or skip IE
    else
    {
        debugUser("_maxTimeExist == 0, IE discarded");
        while (IElength > 0)
        {
            frame_.getFirst();
            --IElength;
        }
    }
    return;
}

//
//Function: encode
//
//Description:
//      Encodes IE. First content (iecoder-specific) and then header
//      (protocol-specific).
//

void bisupIECoder :: encodeIE(pfFrame &frame_, pfMessenger *msg_)
{ 
    pfUlong contentLength(0);    
    pfUlong frameLenAtStart(frame_.length());

    encodeContent(frame_, msg_);
    
    frame_.putFirst(_parameterCompatibilityInf2);
    frame_.putFirst(_parameterCompatibilityInf1);
    contentLength = frame_.length() - frameLenAtStart;
    frame_.putFirst16bit(contentLength);
    frame_.putFirst(getType());
    
    return;
}
