//  ==========================================================================
//  
//  @(#) $Id: L2_Codec.C,v 1.3 2000/10/13 15:45:51 brian Exp $
//  
//  --------------------------------------------------------------------------
//  
//  Copyright (C) 1997-2000  Brian Bidulock <bidulock@dallas.net>
//  
//  All Rights Reserved.
//  
//  This library is free software; you can redistribute it and/or modify it
//  under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation; either version 2.1 of the License, or (at
//  your option) any later version.
//  
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
//  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser Public License
//  for more details.
//  
//  You should have received a copy of the GNU Lesser General Public License
//  along with this library; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
//  Last Modified $Date: 2000/10/13 15:45:51 $ by $Author: brian $
// 
//  --------------------------------------------------------------------------
// 
//  $Log: L2_Codec.C,v $
//  Revision 1.3  2000/10/13 15:45:51  brian
//  First public release.
//
//  ==========================================================================

static char const ident[] = "$Id: L2_Codec.C,v 1.3 2000/10/13 15:45:51 brian Exp $";

#pragma implementation
#include "head.h"

#include "L2_Codec.H"

#include <unistd.h>

Codec_SU::Codec_SU() :
    Header("SU","Signal Unit",24,6,4,(Codec**)&BSN)
{
    FISU = new Codec_FISU("FISU","Fill-In Signal Unit");
    LSSU = new Codec_LSSU("LSSU","Link Status Signal Unit (1 byte)",1);
    LSS2 = new Codec_LSSU("LSS2","Link Status Signal Unit (2 byte)",2);
    MSU  = new Codec_MSU ("MSU", "Message Signal Unit");

    BSN  = new Field("BSN","Backward Seq Number",   7,0);
    BIB  = new Field("BIB","Backward Indicator Bit",1,0);
    FSN  = new Field("FSN","Forward Seq Number",    7,0);
    FIB  = new Field("FIB","Forward Indicator Bit" ,1,0);
    LI   = new Field("LI" ,"Length Indicator"      ,6,2);
    LI_B = new Bra("LI:","Length Indicator Branch",(Codec*)LI,
        FISU,  0,
        LSSU,  1,
        LSS2,  2,
        MSU ,  3,
        MSU , -1,
        NULL);
};

void
Codec_SU::decode(FramePointer& p) {
    Header::decode(p);
    if (MSU->exists) {
        int v = MSU->frame.size>>3;
        if (v>63) v = 63;
        if (!LI->test(v)) { LI->invalid = true; invalid = true; throw Invalid(label); }
    }
};

void
Codec_SU::encode(FramePointer& p) {
    Header::encode(p);
    if (MSU->exists) {
        int v = MSU->frame.size>>3;
        if (v>63) v = 63;
        LI->set(v);
        FramePointer x(LI->frame);
        LI->encode(x);
    }
};

Codec_FISU::Codec_FISU(char* l,char* t) :
    Content(l,t,0,0,0,NULL) {
};

Codec_LSSU::Codec_LSSU(char* l,char* t,int s) :
    Content(l,t,s*8,1,0,(Codec**)&SF)
{
    SF  = new Set("SF","Sig Info Octet Field",5,(s*8)-5,
        "SIO",  0,  "'0' (out of alignment)",
        "SIN",  1,  "'N' (normal alignment)",
        "SIE",  2,  "'E' (emergency alignment)",
        "SIOS", 3,  "'OS' (out of service)",
        "SIPO", 4,  "'PO' (processor outage)",
        "SIB",  5,  "'B' (busy)",
        NULL);
};

Codec_MSU::Codec_MSU(char* l,char* t) :
    Content(l,t,-1,1,0,(Codec**)&PDU)
{
    PDU = new OctetString("PDU","Protocol Data Unit",-1);
};

