
/**************************************************/
/*Title:    sccpdebug.c                           */
/*Descr:    SCCP Debug Tool                       */
/*Author:   Liang Hanxi                           */
/*Create:   2002-2-19                             */
/*Modify:   2003-5-9                              */

/**************************************************/
#include "./include/sccp.h"
#include "./include/scoc.h"

int sccp_send_netcap = 0;
char sccp_debug_buf[5120];
static char asc_in[256];
static DWORD title_id[20] = { 1, 3, 6, 1, 4, 1, 1373, 1, 3, 2, 3, 1, 2, 4, 8, 1 };
unsigned long spLogDebug_mask = 0;
static char sccp_ver[16];

/* by simon at 23/9/26 */
extern struct sccp_msg_statistic
{
    DWORD msg_mtp_scoc;
    DWORD msg_scoc_up;
    DWORD mnp_send;
    DWORD mnp_route;
} sccpmsg_st;
extern SCCP_Capture_Buf SCCPCapBuf;
extern DWORD SccpCstaInd;
extern LOCAL_SSN LocalSSN[256];
extern int RelatedSSNNum;
extern RL_SSN RelatedSSN[SCCP_RLSSN_NUM];
extern SCOC_BUF ScocBuf;
extern SCCP_ROUTE RouteList[SCCP_ROUTE_NUM];
extern SCCP_OPT SCCP_Param;

extern void spLog ( char *info );

void spLogDebug_WatchDog ( BYTE npage );
SCCP_FILTER sccpFilter[8];
BYTE sccpFilterIndex = 0;
int spLog_fd = -1;
static char help_info[] = "Commands provided now:\r\n\
help\r\n\
log all                         Output all debug information\r\n\
log none                        Turn off all information\r\n\
log error on/off                Show error information\r\n\
log addr on/off                 Show address of sccp message\r\n\
log data on/off                 Show data party of sccp message\r\n\
log time on/off                 Show time when generate log\r\n\
log udt on/off                  Show UDT message\r\n\
log user on/off                 Show (UDT) message from user\r\n\
log mtp3 on/off                 Show (UDT) message from mtp3\r\n\
log aif on/off                  Show AIF message\r\n\
log scmg on/of                  Show SCMG message\r\n\
log snmp on/off                 Show SNMP message\r\n\
log cond1 (and cond2...)        Show message due to specified condition\r\n\
                                One or more conditions could be taken:\r\n\
                                src ssn=xx/dst ssn=xx/opc=xx/dpc=xx.\r\n\
                                For example:log src ssn=7 and dst ssn=6\r\n\
list route.x                    Print GTT Table, start from x, default 0 if not specified\r\n\
list spc                        Print SPC of local node\r\n\
save                            Save parameter\r\n\
reset                           Reset SCCP Module\
";

/*===========export===============*/
void spLogDebug_setup (  );
void spLogDebug_timer (  );
void spLogDebug_csta ( BYTE npage, int instance );
void spLogDebug_local ( BYTE npage, BYTE nline );
/* by simon at 23/09/26 */
extern SCCP_CSTA SccpCsta[96];

/*===========import================*/
extern void sccp_saveparam (  );
extern void sccp_reset (  );
extern int ParseLine ( char *str, char ( *row )[128], int maxrow );
//extern int Bcd2Str ( char *pStr, const BYTE * pBcd, int len );
extern int Digits2Str ( char *pStr, BYTE * pDigits, int DigitsLen );

/*===========private===============*/
static int parsecmd ( char *cmdstr );
static void spLogDebug_info ( BYTE npage, BYTE nline, BYTE * pt, BYTE len );
static void spLogDebug_related ( BYTE npage, BYTE nline );
static void spLogDebug_buffer ( BYTE npage );
static void spLogDebug_errorCapture ( BYTE npage, BYTE nline );
static void sccp_list_route ( int pos );

void spLogDebug_setup (  )
{
    DWORD ascout_id[20] = { 1, 3, 6, 1, 4, 1, 1373, 1, 3, 2, 3, 1, 2, 4, 4 };
    DWORD ascin_id[20] = { 1, 3, 6, 1, 4, 1, 1373, 1, 3, 2, 3, 1, 2, 4, 3 };
    DWORD status_id[20] = { 1, 3, 6, 1, 4, 1, 1373, 1, 3, 2, 3, 1, 2, 4, 1 };
    DWORD name_id[20] = { 1, 3, 6, 1, 4, 1, 1373, 1, 3, 2, 3, 1, 2, 4, 2 };
    BYTE debug_status = 1;

    sprintf ( sccp_ver, "R%dV%d_%02d", SCCP_Param.version[0], SCCP_Param.version[1], SCCP_Param.version[2] );
    debug_set_response ( 15, ascout_id, sccp_debug_buf, 4096 );
    debug_set_response ( 15, ascin_id, asc_in, 1 );
    debug_set_response ( 15, status_id, &debug_status, 1 );
    debug_set_response ( 15, name_id, sccp_ver, strlen ( sccp_ver ) );

    spLogDebug_local ( 2, 1 );
    spLogDebug_related ( 3, 0 );
    spLogDebug_buffer ( 4 );
    spLogDebug_errorCapture ( 5, 0 );
    spLogDebug_WatchDog ( 6 );
}

void spLogDebug_timer (  )
{
    BYTE npage, nline;

    if( strlen ( asc_in ) <= 1 )
        return;
    sccp_debug_buf[0] = '\0';
    npage = asc_in[0] - 1;
    if( npage == 0 )
    {
        parsecmd ( asc_in + 1 );
        asc_in[0] = '\0';
        return;
    }
    else if( asc_in[1] == 'c' || asc_in[1] == 'C' )
    {
        memset ( &sccpmsg_st, 0, sizeof ( sccpmsg_st ) );
        memset ( &SCCPCapBuf, 0, sizeof ( SCCP_Capture_Buf ) );
    }
    else
        nline = atoi ( asc_in + 1 );

    switch ( npage )
    {
    case 1:                    //csta
        spLogDebug_csta ( npage, nline );
        break;
    case 2:                    //local subsystem
        spLogDebug_local ( npage, nline );
        break;
    case 3:                    //related subsystem
        spLogDebug_related ( npage, nline );
        break;
    case 5:                    //capture buffer
        spLogDebug_errorCapture ( npage, nline );
        break;
    }
    asc_in[0] = '\0';
}

static int str2value ( char *str )
{
    BYTE i = 0;
    char *pHead = str, xstr[8] = "0";

    while ( !isxdigit ( *pHead ) )      //8 lead-space max
    {
        if( i++ >= 8 )
            return -1;
        pHead++;
    }
    i = 0;
    while ( isxdigit ( pHead[i] ) && i < 6 )
    {
        xstr[i] = pHead[i];
        i++;
    }
    xstr[6] = 0;

    return strtoul ( xstr, NULL, 16 );
}

static int parsecond ( char *cmdstr )
{
    char *pKey = cmdstr, *pEQ, info[256] = "log ", tmpStr[64];

    sccpFilter[sccpFilterIndex].flag = 0;

    if( ( pKey = strstr ( cmdstr, "opc" ) ) != NULL && ( pEQ = strchr ( pKey, '=' ) ) != NULL )
    {
        sccpFilter[sccpFilterIndex].opc = str2value ( pEQ + 1 );
        if( sccpFilter[sccpFilterIndex].flag > 0 )
            strcat ( info, " and " );
        sprintf ( tmpStr, "opc = 0x%06x", sccpFilter[sccpFilterIndex].opc );
        strcat ( info, tmpStr );
        sccpFilter[sccpFilterIndex].flag |= 0x01;
    }
    if( ( pKey = strstr ( cmdstr, "dpc" ) ) != NULL && ( pEQ = strchr ( pKey, '=' ) ) != NULL )
    {
        sccpFilter[sccpFilterIndex].dpc = str2value ( pEQ + 1 );
        if( sccpFilter[sccpFilterIndex].flag > 0 )
            strcat ( info, " and " );
        sprintf ( tmpStr, "dpc = 0x%06x", sccpFilter[sccpFilterIndex].opc );
        strcat ( info, tmpStr );
        sccpFilter[sccpFilterIndex].flag |= 0x02;
    }
    if( ( pKey = strstr ( cmdstr, "src ssn" ) ) != NULL && ( pEQ = strchr ( pKey, '=' ) ) != NULL )
    {
        sccpFilter[sccpFilterIndex].src_ssn = str2value ( pEQ + 1 );
        if( sccpFilter[sccpFilterIndex].flag > 0 )
            strcat ( info, " and " );
        sprintf ( tmpStr, "src ssn = 0x%02x", sccpFilter[sccpFilterIndex].src_ssn );
        strcat ( info, tmpStr );
        sccpFilter[sccpFilterIndex].flag |= 0x04;
    }
    if( ( pKey = strstr ( cmdstr, "dst ssn" ) ) != NULL && ( pEQ = strchr ( pKey, '=' ) ) != NULL )
    {
        sccpFilter[sccpFilterIndex].dst_ssn = str2value ( pEQ + 1 );
        if( sccpFilter[sccpFilterIndex].flag > 0 )
            strcat ( info, " and " );
        sprintf ( tmpStr, "dst ssn = 0x%02x", sccpFilter[sccpFilterIndex].dst_ssn );
        strcat ( info, tmpStr );
        sccpFilter[sccpFilterIndex].flag |= 0x08;
    }

    if( sccpFilter[sccpFilterIndex].flag == 0 )
    {
        spLog ( "Wrong condition" );
        return 0;
    }

    sccpFilterIndex++;
    spLog ( info );
    spLog ( "command OK" );
    return 1;
}

int parsecmd ( char *cmdstr )
{
    int i;
    char info[256], *pCh;

	if( strstr ( cmdstr, "list route" ) != NULL ){
		int i = 0;
		const int start_pos = 11;
		while(isdigit(cmdstr[start_pos+i++])){			
			if(i > 3){
				break;
			}
		}
		
		if (i > 0)
		{
			cmdstr[start_pos + i] = '\0';
			i = strtoul(cmdstr+start_pos, NULL, 10);
			if (i > SCCP_ROUTE_NUM){
				char tmp_str[128];
				sprintf(tmp_str, "Invalid parameter value %d, max %d. replace with default 0", i, SCCP_ROUTE_NUM);
				spLog ( tmp_str );
				i = 0;
			}	
		}				
		sccp_list_route ( i );
	}
    else if( strstr ( cmdstr, "help" ) != NULL || strstr ( cmdstr, "?" ) != NULL )
        spLog ( help_info );
    else if( strstr ( cmdstr, "save" ) != NULL )
        sccp_saveparam (  );
    else if( strstr ( cmdstr, "reset" ) != NULL )
        sccp_reset (  );
    else if( strstr ( cmdstr, "log all" ) != NULL )
    {
        sccpFilterIndex = 0;
        spLogDebug_mask = 0xFFFFFFFF;
    }
    else if( strstr ( cmdstr, "log none" ) != NULL )
    {
        sccpFilterIndex = 0;
        spLogDebug_mask = 0;
    }
    else if( strstr ( cmdstr, "log error on" ) != NULL )
        spLogDebug_mask |= SCCPDB_ERR;
    else if( strstr ( cmdstr, "log error off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_ERR );
    else if( strstr ( cmdstr, "log udt on" ) != NULL )
        spLogDebug_mask |= SCCPDB_UDT;
    else if( strstr ( cmdstr, "log udt off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_UDT );
    else if( strstr ( cmdstr, "log aif on" ) != NULL )
        spLogDebug_mask |= SCCPDB_AIF;
    else if( strstr ( cmdstr, "log aif off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_AIF );
    else if( strstr ( cmdstr, "log mtp3 on" ) != NULL )
        spLogDebug_mask |= SCCPDB_MTP3;
    else if( strstr ( cmdstr, "log mtp3 off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_MTP3 );
    else if( strstr ( cmdstr, "log user on" ) != NULL )
        spLogDebug_mask |= SCCPDB_USER;
    else if( strstr ( cmdstr, "log user off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_USER );

    else if( strstr ( cmdstr, "log scmg on" ) != NULL )
        spLogDebug_mask |= SCCPDB_SCMG;
    else if( strstr ( cmdstr, "log scmg off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_SCMG );
    else if( strstr ( cmdstr, "log snmp on" ) != NULL )
        spLogDebug_mask |= SCCPDB_SNMP;
    else if( strstr ( cmdstr, "log snmp off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_SNMP );
    else if( strstr ( cmdstr, "log data on" ) != NULL )
        spLogDebug_mask |= SCCPDB_MSG;
    else if( strstr ( cmdstr, "log data off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_MSG );
    else if( strstr ( cmdstr, "log addr on" ) != NULL )
        spLogDebug_mask |= SCCPDB_ADDR;
    else if( strstr ( cmdstr, "log addr off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_ADDR );
    else if( strstr ( cmdstr, "log time on" ) != NULL )
        spLogDebug_mask |= SCCPDB_TIME;
    else if( strstr ( cmdstr, "log time off" ) != NULL )
        spLogDebug_mask &= ( ~SCCPDB_TIME );
    else if( strstr(cmdstr, "net cap start") !=NULL )
	{
	    sccp_send_netcap = 1;
	    spLog("sccp send netcap start");
    }
    else if( strstr(cmdstr, "net cap stop") !=NULL )
	{
	    sccp_send_netcap = 0;
	    spLog("sccp send netcap end");   
	}
    else if( strncasecmp ( cmdstr, "log file on", 11 ) == 0 )
    {
        if( spLog_fd != -1 )
        {
            close ( spLog_fd );
        }
        spLog_fd = open ( "./sccp.log", O_WRONLY | O_CREAT | O_TRUNC );
    }
    else if( strncasecmp ( cmdstr, "log file off", 12 ) == 0 )
    {
        if( spLog_fd != -1 )
        {
            close ( spLog_fd );
            spLog_fd = -1;
        }
    }
    else if( ( pCh = strstr ( cmdstr, "log" ) ) != NULL && strstr ( cmdstr, "=" ) != NULL )
    {
        if( sccpFilterIndex >= 8 )
        {
            spLog ( "Only 8 conditions can be set" );
            return 0;
        }
        return parsecond ( pCh + 4 );
    }
    else if( strstr ( cmdstr, "list spc" ) != NULL )
    {
        spLog ( "NI SPC_Len  SPC" );
        for ( i = 0; i < 4; i++ )
        {
            sprintf ( info, "%d   %d       %06lx", i, SCCP_Param.spclen[i], SCCP_Param.spc[i] );
            spLog ( info );
        }
    }
    else
    {
        spLog ( "Invalid command/parameter(s)." );
        return 0;
    }
    spLog ( "Command OK." );
    return 1;
}

int sccp_filter_msg ( SCCP_ADDR * pSrcAddr, SCCP_ADDR * pDstAddr )
{
    int i;

    for ( i = 0; i < sccpFilterIndex % 9; i++ )
    {
        if( sccpFilter[i].flag & 0x01 )
        {
            if( sccpFilter[i].opc != pSrcAddr->DPC )
                continue;
        }
        if( sccpFilter[i].flag & 0x02 )
        {
            if( sccpFilter[i].dpc != pDstAddr->DPC )
                continue;
        }
        if( sccpFilter[i].flag & 0x04 )
        {
            if( sccpFilter[i].src_ssn != pSrcAddr->SSN )
                continue;
        }
        if( sccpFilter[i].flag & 0x08 )
        {
            if( sccpFilter[i].dst_ssn != pDstAddr->SSN )
                continue;
        }
        return 1;
    }
    return 0;
}

void spLogDebug_info ( BYTE npage, BYTE nline, BYTE * pt, BYTE len )
{
    DWORD oid[20] = { 1, 3, 6, 1, 4, 1, 1373, 1, 3, 2, 3, 1, 2, 4, 5, 2, 2 };

    oid[14] = 5 + npage;
    oid[15] = 2 + nline;
    debug_set_response ( 17, oid, pt, len );
}

/*-----------------------------------------------*/

/*-------------------CSTA------------------------*/

/*-----------------------------------------------*/
void spLogDebug_csta ( BYTE npage, int instance )
{
    char title[256];
    SCCP_CSTA *pcsta = &SccpCsta[SccpCstaInd % 96];

    sprintf ( title, "\tSCCP-%s\tPage %02d\t\tMIB-CSTA(ID:1 Instance:%d)\r\n"
              "                   [----1----] [----2----] [----3----] [----4----] [----5----]\r\n"
              "Error(1)\r\n" "\r\n" "\r\n" "\r\n" "\r\n" "availability(2)\r\n" "\r\n" "\r\n" "Utilization(3)\r\n" "\r\n" "\r\n" "Quality(4)\r\n" "\r\n" "\r\n" "\r\n", sccp_ver, npage, instance );

    title_id[14] = 5 + npage;
    debug_set_response ( 16, title_id, title, strlen ( title ) );

    spLogDebug_info ( npage, 0, ( BYTE * ) pcsta->error, 20 );
    spLogDebug_info ( npage, 1, ( BYTE * ) pcsta->error + 20, 20 );
    spLogDebug_info ( npage, 2, ( BYTE * ) pcsta->error + 40, 20 );
    spLogDebug_info ( npage, 3, ( BYTE * ) pcsta->error + 60, 20 );
    spLogDebug_info ( npage, 4, ( BYTE * ) pcsta->error + 80, 4 );
    spLogDebug_info ( npage, 5, ( BYTE * ) pcsta->avail, 20 );
    spLogDebug_info ( npage, 6, ( BYTE * ) pcsta->avail + 20, 20 );
    spLogDebug_info ( npage, 7, ( BYTE * ) pcsta->avail + 40, 8 );
    spLogDebug_info ( npage, 8, ( BYTE * ) pcsta->utilization, 20 );
    spLogDebug_info ( npage, 9, ( BYTE * ) pcsta->utilization + 20, 20 );
    spLogDebug_info ( npage, 10, ( BYTE * ) pcsta->utilization + 40, 16 );
    spLogDebug_info ( npage, 11, ( BYTE * ) pcsta->quality, 20 );
    spLogDebug_info ( npage, 12, ( BYTE * ) pcsta->quality + 20, 20 );
    spLogDebug_info ( npage, 13, ( BYTE * ) pcsta->quality + 40, 20 );
    spLogDebug_info ( npage, 14, ( BYTE * ) pcsta->quality + 60, 20 );
}

/*-----------------------------------------------*/

/*-------------------Local subsystem-------------*/

/*-----------------------------------------------*/
void spLogDebug_local ( BYTE npage, BYTE nline )
{
    short loop, ssnindex = 0;
    char str[256], digits[24];
    char title[1024], *pCh = NULL;

    nline = nline % 256;
    if( nline < 2 )
        nline = 2;

    spLog ( "Refresh Local Node List..." );
    sprintf ( title, "\tSCCP-%s\tPage %02d\tMIB-Status Of Local Subsystem\r\n\
[   SSN   ]        S1 S2 N  Lc NI RG [   IP1   ] [   IP2   ] F\r\n", sccp_ver, npage );

    for ( loop = nline; ssnindex < 22 && loop < 256; loop++ )
    {
        if( LocalSSN[loop].status[0] == 0 && LocalSSN[loop].status[1] == 0 )
            continue;

        Bcd2Str ( digits, LocalSSN[loop].msisdn, LocalSSN[loop].msisdn_len % ( MAX_DIGITS + 1 ) );
        if( ( pCh = strchr ( digits, 'F' ) ) != NULL )
            *pCh = 0;
        if( strlen ( digits ) == 0 )
            strcpy ( digits, " " );

        sprintf ( str, "%02X-%-18s\n\r", loop, digits );
        strcat ( title, str );

        spLogDebug_info ( npage, ssnindex++, LocalSSN[loop].status, 14 );
    }

    while ( ssnindex < 22 )
        spLogDebug_info ( npage, ssnindex++, LocalSSN[0].status, 0 );

    title_id[14] = 5 + npage;
    debug_set_response ( 16, title_id, title, strlen ( title ) );

    return;
}

void spLogDebug_related ( BYTE npage, BYTE nline )
{
    short loop;
    char str[64];
    char title[500];

    nline = nline % SCCP_RLSSN_NUM;

    sprintf ( title, "\tSCCP-%s\tPage %02d\tMIB-Status Of Related Subsystem(Num=%d)\r\n" "                   S SST ID SN [   SPC   ] [Timeoute]\r\n", sccp_ver, npage, RelatedSSNNum );
    for ( loop = nline; loop < nline + 20 && loop < SCCP_RLSSN_NUM; loop++ )
    {
        sprintf ( str, "Instance=%d\r\n", loop );
        strcat ( title, str );
    }

    title_id[14] = 5 + npage;
    debug_set_response ( 16, title_id, title, strlen ( title ) );

    for ( loop = nline; loop < nline + 20 && loop < RelatedSSNNum; loop++ )
        spLogDebug_info ( npage, loop - nline, ( BYTE * ) & RelatedSSN[loop], 12 );
}

void spLogDebug_buffer ( BYTE npage )
{
    char title[500];

    sprintf ( title, "\tSCCP-%s\tPage %02d\t\tBuffer\r\n"
              "                   [ R ] [ W ]\r\n" "SCLC(in-0)\r\n" "SCLC(in-1)\r\n" "SCLC(in-2)\r\n" "SCLC(out)\r\n" "SCOC\r\n" "AIF Msg\r\n" "\r\n" "OMCIP\r\n", sccp_ver, npage );

    title_id[14] = 5 + npage;
    debug_set_response ( 16, title_id, title, strlen ( title ) );

    //      spLogDebug_info(npage,0,(BYTE *)&SCLC_InBuf[0].readsub, 4);
    //      spLogDebug_info(npage,1,(BYTE *)&SCLC_InBuf[1].readsub, 4);
    //      spLogDebug_info(npage,2,(BYTE *)&SCLC_InBuf[2].readsub, 4);
    //      spLogDebug_info(npage,3,(BYTE *)&SCLC_OutBuf.readsub, 4);
    spLogDebug_info ( npage, 4, ( BYTE * ) ScocBuf.rcvsub, 4 );
    spLogDebug_info ( npage, 5, ( BYTE * ) & sccpmsg_st.msg_mtp_scoc, 8 );
    spLogDebug_info ( npage, 6, NULL, 0 );
    spLogDebug_info ( npage, 7, ( BYTE * ) & SCCP_Param.OMCIP, 8 );
}

void spLogDebug_errorCapture ( BYTE npage, BYTE nline )
{
    char title[500];
    int i;
    static BYTE subpage;
    int base;

    subpage = nline % 10;

    sprintf ( title, "\tSCCP-%s\tPage %02d\tSubPage %02d\tErrCap\r\n"
              "		   0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r"
              "Pointer\n\r"
              "ErrCap\r\n" "01\r\n" "02\r\n" "03\r\n" "04\r\n" "05\r\n" "06\r\n" "07\r\n" "08\r\n" "09\r\n" "10\r\n" "11\r\n" "12\r\n" "13\r\n" "14\r\n" "15\r\n" "Dog\r\n", sccp_ver, npage, subpage );

    title_id[14] = 5 + npage;
    debug_set_response ( 16, title_id, title, strlen ( title ) );

    spLogDebug_info ( npage, 0, ( BYTE * ) & SCCPCapBuf.Pointer, 1 );
    base = subpage * 256;
    for ( i = 1; i < 17; i++ )
    {
        spLogDebug_info ( npage, i, ( BYTE * ) & SCCPCapBuf.Buffer[base + ( i - 1 ) * 16], 16 );
    }
    for ( i = 17; i < 22; i++ )
    {
        spLogDebug_info ( npage, i, ( BYTE * ) & SCCPCapBuf.watch_dog[( i - 17 ) * 16], 16 );
    }
}

void spLogDebug_WatchDog ( BYTE npage )
{
    char title[500];
    CO_DebugStatic *pDs = &pSp->tCo.tCoDebugStatic;

    sprintf ( title, "\tSCCP-%s\tPage %02d\t\tDebug watchdog\r\n\r\n"
              "Get InPool\r\n"
              "Put InPool\r\n"
              "Read InPool\r\n"
              "Write InPool\r\n"
              "Get OutPool\r\n"
              "Put OutPool\r\n"
              "Put element atp\r\n"
              "Put element suc\r\n"
              "CoSubmitUl attempt\r\n" 
              "CoSubmitUl \r\n" 
              "UlPostCo attempt\r\n" 
              "UlPostCo \r\n" 
              "Co2Cr attempt\r\n" 
              "Co2Cr \r\n" 
              "Cr2Co attempt\r\n" 
              "Cr2Co \r\n"
              "Timer Out\r\n"
			  "Busy element\r\n"
			  "Busy connection\r\n", sccp_ver, npage );

    title_id[14] = 5 + npage;
    debug_set_response ( 17, title_id, title, strlen ( title ) );

    spLogDebug_info ( npage, 0, ( BYTE * ) & pDs->dGetInPool, 4 );
    spLogDebug_info ( npage, 1, ( BYTE * ) & pDs->dPutInPool, 4 );
    spLogDebug_info ( npage, 2, ( BYTE * ) & pDs->dReadInPool, 4 );
    spLogDebug_info ( npage, 3, ( BYTE * ) & pDs->dWriteInPool, 4 );
    spLogDebug_info ( npage, 4, ( BYTE * ) & pDs->dGetOutPool, 4 );
    spLogDebug_info ( npage, 5, ( BYTE * ) & pDs->dPutOutPool, 4 );
    spLogDebug_info ( npage, 6, ( BYTE * ) & pDs->dPutEleApt, 4 );
    spLogDebug_info ( npage, 7, ( BYTE * ) & pDs->dPutEleSuc, 4 );
    spLogDebug_info ( npage, 8, ( BYTE * ) & pDs->dCoSubmitUlApt, 4 );
	spLogDebug_info ( npage, 9, ( BYTE * ) & pDs->dCoSubmitUl, 4 );
	spLogDebug_info ( npage, 10, ( BYTE * ) & pDs->dUlPostCoApt, 4 );
	spLogDebug_info ( npage, 11, ( BYTE * ) & pDs->dUlPostCo, 4 );
    spLogDebug_info ( npage, 12, ( BYTE * ) & pDs->dCo2CrApt, 4 );
    spLogDebug_info ( npage, 13, ( BYTE * ) & pDs->dCo2Cr, 4 );
    spLogDebug_info ( npage, 14, ( BYTE * ) & pDs->dCr2CoApt, 4 );
    spLogDebug_info ( npage, 15, ( BYTE * ) & pDs->dCr2Co, 4 );
	spLogDebug_info ( npage, 16, ( BYTE * ) & pDs->dTimerOut, 4 );
	spLogDebug_info ( npage, 17, ( BYTE * ) & pDs->dbusyElem, 4 );
	spLogDebug_info ( npage, 18, ( BYTE * ) & pSp->tCo.wConnIdCount, 4 );
}

void sccp_list_route ( int pos )
{
    SCCP_ROUTE *pRoute;
    int i;
    char tempstr[1024], str1[64], str2[256], *pCh = NULL;
    char cNP[8], cTT[8];
    if(pos >= SCCP_ROUTE_NUM)
	return;

    spLog ( "#No NP TT StartDigits     EndDigits       NI SPC  SSN RI AI S E N Rep" );
    for ( i = pos; i < SCCP_ROUTE_NUM; i++ )
    {
        if( RouteList[i].avail == 0 )
            continue;
        pRoute = &RouteList[i];
        if( pRoute->len > 0 )
        {
            Bcd2Str ( str1, pRoute->StartDigits, pRoute->len % MAX_DIGITS );
            if( ( pCh = strchr ( str1, 'F' ) ) != NULL )
                *pCh = 0;
            Bcd2Str ( str2, pRoute->EndDigits, pRoute->len % MAX_DIGITS );
            if( ( pCh = strchr ( str2, 'F' ) ) != NULL )
                *pCh = 0;
        }
        else
        {
            strcpy ( str1, "-" );
            strcpy ( str2, "-" );
        }

        if( pRoute->NP == 0 )
            sprintf ( cNP, "-" );
        else
            sprintf ( cNP, "%d", pRoute->NP );

        if( pRoute->TT == 0 )
            sprintf ( cTT, "-" );
        else
            sprintf ( cTT, "%d", pRoute->TT );

        sprintf ( tempstr, "%-3d %s %s  %-15s %-15s %d %02X%02x%02X %d  %d  %d  ", i, cNP, cTT, str1, str2, pRoute->NetID, pRoute->DPC[0], pRoute->DPC[1], pRoute->DPC[2], pRoute->SSN, pRoute->RI,
                  pRoute->AI );
        if( pRoute->convert )
        {
            Bcd2Str ( str1, pRoute->ReplaceDigits, pRoute->replace_len % MAX_DIGITS );
            if( ( pCh = strchr ( str1, 'F' ) ) != NULL )
                *pCh = 0;
            sprintf ( str2, "%-1d% -1d %-1d %-15s", pRoute->ReplaceStart, pRoute->ReplaceEnd, pRoute->OutNP, str1 );
            strcat ( tempstr, str2 );
        }
        spLog ( tempstr );
    }

}

void sccp_put_capture_logs ( BYTE person, BYTE * pictures, int len )
{
    int base;

    if( len >= 512 )
        return;

    base = SCCPCapBuf.Pointer * 16;

    SCCPCapBuf.Buffer[base + 0] = person;
    SCCPCapBuf.Buffer[base + 1] = len;
    base = base + 16;
    SCCPCapBuf.Pointer = SCCPCapBuf.Pointer + 1;

    if( SCCPCapBuf.Pointer >= 128 )
        SCCPCapBuf.Pointer = 0;

    memcpy ( &SCCPCapBuf.Buffer[base], pictures, len );

    SCCPCapBuf.Pointer = ( SCCPCapBuf.Pointer ) + ( ( len + 15 ) / 16 );

    if( SCCPCapBuf.Pointer >= 128 )
        SCCPCapBuf.Pointer = 0;

}
