⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ss7_mtp2.cpp

📁 实现了SS7中MTP2层的基本功能
💻 CPP
📖 第 1 页 / 共 5 页
字号:
#define DIRECTION 1// File Name: SS7_MTP2.cpp/////////////////////////////////////////////////#include "SS7_MTP2.h"#include <stdlib.h>#include <stdio.h>int outFileIndex; // for UT// The table of timer events: (FSM_X,EVENT_X)//    If Timer X expired, FSM "FSM_X" will got event "EVENT_X"MTP2_FSM_Event timer_events[7] = {    {MTP2_FSM_LSC, (int)LSCEventT1Expired},    {MTP2_FSM_IAC, (int)IACEventT2Expired},    {MTP2_FSM_IAC, (int)IACEventT3Expired},    {MTP2_FSM_IAC, (int)IACEventT4Expired},    {MTP2_FSM_CC,  (int)CCEventT5Expired},    {MTP2_FSM_TXC, (int)TXCEventT6Expired},    {MTP2_FSM_TXC, (int)TXCEventT7Expired} };// Corresponding to MTP2_FSM_TYPE, list the FSM handling functionsFSMFunc fsmProcessFunc[11] = {            NULL, // MTP2_FSM_NULL,            & SS7_MTP2::LSC, // MTP2_FSM_LSC,            & SS7_MTP2::RC, // MTP2_FSM_RC,            & SS7_MTP2::TXC, // MTP2_FSM_TXC,            & SS7_MTP2::IAC, // MTP2_FSM_IAC,            & SS7_MTP2::POC, // MTP2_FSM_POC,            & SS7_MTP2::CC, // MTP2_FSM_CC,            & SS7_MTP2::AERM, // MTP2_FSM_AERM,            & SS7_MTP2::SUERM, // MTP2_FSM_SUERM,            & SS7_MTP2::DAEDR, // MTP2_FSM_DAEDR,            & SS7_MTP2::DAEDT }; // MTP2_FSM_DAEDT void SS7_MTP2::init_data_member(void){    fd = -1;    lscState = LSCStatePowerOff;    iacState = IACStateIdle;    emergency = false;    portNumber = 0;    recvHighWaterLevel = (int)(MTP2_RECV_BUF_SIZE * HIGH_WATER_LEVEL);    recvMidWaterLevel = (int)(MTP2_RECV_BUF_SIZE * MID_WATER_LEVEL);    errorRate = 0;    pocState = POCStateIdle;    txcState = TXCStateIdle;    rcState = RCStateIdle;    aermState = AERMStateIdle;    suermState = SUERMStateIdle;    ccState = CCStateIdle;    daedrState = DAEDRStateIdle;    daedtState = DAEDTStateIdle;    Ti = Tin;    // errorRate;    octetCountingMode = false ;    alignmentComplete = false ;    emergency = false ;    linkFailure = false ;    localProcessorOutage = false ;    processorOutage = false ;    noProcessorOutage = true ;    level3IndicationReceived = false ;    LSSUAvailable = false ;    SIBreceived = false ;    RTBfull = false ;    MSUinhibited = false ;    TXC_FSNL = 127; // FSN of the last MSU in the RTB    TXC_FSNT = 127; // FSN of the last MSU transmitted    TXC_FSNX = 0; // FSN expected    TXC_FIB = 1; // Forward indicator bit    TXC_BIB = 1; // backward indicator bit    TXC_FSNF = 0; // FSN of the oldest MSU in the RTB     Cm = 0; // Counter of MSU in TB    RC_FSNX = 0;    RC_FIBX = 1;    RC_FSNF = 0;    RC_FSNT = 127;    RTR = 0; // If = 1 means retransmission expected    FSNR = 0;    FIBR = 1;    BIBT = 1;
    BSNT = 127;
    FIBT = 1;
    BSNR = 127;    BIBR = 1;    FSNC = 127;    UNB = 0;    UNF = 0;    Cp = 0;    Ca = 0;    Cs = 0;    su = 0;    furtherproving = true ;
    FISUMSUaccepted = false ;
    abnormalBSNR = false ;
    abnormalFIBR = false ;
    congestionDiscard = false ;
    congestionAccept = false ;
    for( int i=0 ; i<7 ; i++ )    {        timers[i].started = false;    }    timers[0].timer_val = T1;    timers[1].timer_val = T2;    timers[2].timer_val = T3;    timers[3].timer_val = T4;    timers[4].timer_val = T5;    timers[5].timer_val = T6;    timers[6].timer_val = T7;    pSUWithLen = (SUWithLength *) malloc( sizeof(unsigned short)                                          + sizeof(SUHeaderL2) + MAX_MSU_LEN );    pSndSigUnit = (SndSigUnit *) malloc( sizeof(unsigned short) + sizeof(unsigned char)                                     + sizeof(unsigned char) * 5 + sizeof(unsigned short)                                     + 	sizeof(SUHeaderL2) + MAX_MSU_LEN );
    outFileIndex = 0; // for UT    gettimeofday( & IACTimer, NULL );    gettimeofday( & FISUTimer, NULL );    gettimeofday( & LSCRestartTimer, NULL );}// will use SS7_MTP2::SS7_MTP2(int _portNumber) :                recvBuffer(MTP2_RECV_BUF_SIZE),                MSU2L3Buffer(MTP2_MSU2L3_BUF_SIZE),                eventQueue(MTP2_EVENT_QUEUE),                LSSUBuffer(MTP2_LSSU_BUF_SIZE),                sendBuffer(MTP2_SEND_BUF_SIZE),                resendBuffer(MTP2_SEND_BUF_SIZE){    init_data_member();    portNumber = _portNumber;}SS7_MTP2::SS7_MTP2(void) :                recvBuffer(MTP2_RECV_BUF_SIZE),                MSU2L3Buffer(MTP2_MSU2L3_BUF_SIZE),                eventQueue(MTP2_EVENT_QUEUE),                LSSUBuffer(MTP2_LSSU_BUF_SIZE),                sendBuffer(MTP2_SEND_BUF_SIZE),                resendBuffer(MTP2_SEND_BUF_SIZE){    init_data_member();}SS7_MTP2::SS7_MTP2(int _portNumber,int _recvBufSize,int _msu2L3BufSize,              int _sendBufSize,int _reLssuBufSize)                : recvBuffer(_recvBufSize),                  MSU2L3Buffer(_msu2L3BufSize),                  eventQueue(MTP2_EVENT_QUEUE),                  LSSUBuffer(_reLssuBufSize),                  sendBuffer(_sendBufSize),                resendBuffer(_sendBufSize){    init_data_member();    portNumber = _portNumber;    recvHighWaterLevel = (int)(_recvBufSize * HIGH_WATER_LEVEL);    recvMidWaterLevel = (int)(_recvBufSize * MID_WATER_LEVEL);}SS7_MTP2::~SS7_MTP2(void){    if( pSUWithLen )    {        free( pSUWithLen );    }    if( pSndSigUnit )    {        free( pSndSigUnit );    }}int SS7_MTP2::sendLSSU(void){    return 0;}int SS7_MTP2::sendFISU(void){    return 0;}int SS7_MTP2::sendSIO(void){    return 0;}int SS7_MTP2::sendSIOS(void){    return 0;}int SS7_MTP2::sendSIPO(void){    return 0;}int SS7_MTP2::sendSIN(void){    return sendSINorSIE(false);}int SS7_MTP2::sendSIE(void){    return sendSINorSIE(true);}int SS7_MTP2::sendSINorSIE(bool _emergency){    return 0;}// Initialize to workint SS7_MTP2::poweron(void){    printf("SS7_MTP2::poweron() !\n");    MTP2_FSM_Event event_out;    int n = 7;    FILE * fpFlag = fopen( LOCAL_FLAG_FILE, "wb" );    if( fpFlag == NULL )    {        printf("SS7_MTP2::poweron() -- Fail to write Flag file %s!\n", LOCAL_FLAG_FILE );        // return 0;    }    else    {        fwrite( & n,  sizeof(int), 1, fpFlag );        fclose( fpFlag );    }    AppendEvent( MTP2_FSM_TXC, TXCEventStart );    AppendEvent( MTP2_FSM_TXC, TXCEventSendSIOS );    // LSC --> AERM    AppendEvent( MTP2_FSM_AERM, AERMEventSetTi2Tin );    localProcessorOutage = false;    emergency = false;    lscState = LSCStateOutOfService;    printf("SS7_MTP2::poweron() - Link(%d) state=LSCStateOutOfService!\n", portNumber );    return 0;}int SS7_MTP2::start(void){    printf("SS7_MTP2::start() !\n");    LSC( LSCEventStart, 0 );    return 0;}int SS7_MTP2::updateStates(void){    // printf("SS7_MTP2::updateStates() !\n");    // limit the number of events to be processed in one round ??    // so that we could balance between link ports    // now we process one event at once for each     MTP2_FSM_Event event;    // read -- read out; seek -- ?    if( lscState == LSCStateOutOfService )    {        struct timeval t;        gettimeofday( & t, NULL );        long diff = (t.tv_sec - LSCRestartTimer.tv_sec) * 1000000                    + (t.tv_usec - LSCRestartTimer.tv_usec);        if( diff > 1000000 ) // 1s                {            LSCRestartTimer = t;            LSC( LSCEventStart, 0 );        }    }    if( eventQueue.read( (unsigned char *)& event, sizeof(MTP2_FSM_Event) ) == 0 )    {        // printf("SS7_MTP2::updateStates() - FSMType=%d,event=%d,param=%d!\n",        //        event.FSMType, event.name, (unsigned int)event.param );        if( fsmProcessFunc[event.FSMType] != NULL )        {            (this->*(fsmProcessFunc[event.FSMType]))( event.name, event.param );        }    }    else    {        printf("SS7_MTP2::updateStates() - no event in the eventQueue!\n" );        usleep( 100000 ); // 50ms, no event in the queue        //return 0;    }    if( lscState == LSCStateInitialAlignment )    {        struct timeval t;        gettimeofday( & t, NULL );        long diff = (t.tv_sec - IACTimer.tv_sec) * 1000000 + (t.tv_usec - IACTimer.tv_usec);        bool flag = true;        FILE * fpFlag = fopen( REMOTE_FLAG_FILE, "rb" );        if( fpFlag == NULL )        {         //   printf("SS7_MTP2::writeSUDowntoL() -- Flag file %s not there!\n",         //        REMOTE_FLAG_FILE );            flag = false;        }        else        {            fclose( fpFlag);        }        if( flag && (diff > 900000) ) {            IACTimer = t;            LSSU lssu;            MTP2_FSM_Event event_out;            BIBT = TXC_BIB;            BSNT = (TXC_FSNX - 1 + 128) % 128;            FIBT = TXC_FIB;            lssu.suHeader.FSN = TXC_FSNT;            lssu.suHeader.FIB = FIBT;            lssu.suHeader.BSN = BSNT;            lssu.suHeader.BIB = BIBT;            lssu.suHeader.LI = 1;            // populate the SndSigUnit            pSndSigUnit->Length = SND_SU_HEADER_SIZE                              + sizeof(SUHeaderL2) + lssu.suHeader.LI;            pSndSigUnit->Type = 0;
            // pSndSigUnit->TimeStamp[0] = 0; // It's for L1 or L2 to populate?
            pSndSigUnit->ChName = (unsigned short)portNumber;            switch( iacState )            {            case IACStateIdle:                printf("SS7_MTP2::updateStates() - send repeated NONE - IACStateIdle!\n" );                break;
            case IACStateNotAligned:/*************************                printf("SS7_MTP2::updateStates() - send repeated SIO - IACStateNotAligned!\n" );                // sending SIO                lssu.SF = LSSUTypeSIO;                memcpy( (unsigned char *)pSndSigUnit->PktBuf,                    (unsigned char *)& lssu, sizeof(SUHeaderL2) + lssu.suHeader.LI );                writeSUDowntoL1( pSndSigUnit );                // Need to append event to DAEDT? -- some extra msgs**************************/                break;
            case IACStateAligned:            case IACStateProving:                printf("SS7_MTP2::updateStates() - send repeated SIN/E - %s!\n",                       (iacState == IACStateAligned) ? "IACStateAligned" : "IACStateProving" );                // sending SIN/SIE                lssu.SF = emergency ? LSSUTypeSIE : LSSUTypeSIN;                memcpy( (unsigned char *)pSndSigUnit->PktBuf,                    (unsigned char *)& lssu, sizeof(SUHeaderL2) + lssu.suHeader.LI );                writeSUDowntoL1( pSndSigUnit );                // Need to append event to DAEDT? -- some extra msgs                break;
            default:               
                printf("SS7_MTP2::updateStates() - send NONE - default!\n" );                break;            }
        }    }    return 1; // processed 1 event}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -