net_appl.c

来自「MIPS YAMON, a famous monitor inc. source」· C语言 代码 · 共 632 行 · 第 1/2 页

C
632
字号
 *   plus the maximum possible timer value will not wrap. *  Timeout is measured in milliseconds. *  The raw timer wraps every 16777216 microsecs ~ 16777 millisec * *  Parameters : *  ------------ *  - * * *  Return values : *  --------------- *  'OK'(=0)  * ************************************************************************/UINT32TEST_ping( UINT32 ipadr, UINT32 data_size, UINT32 ping_ms_timeout){    UINT32 rcode, latest, previous, mstimout ;    /* check data_size */    if ( data_size > PING_MAX_DATAGRAM_SIZE )    {        data_size = PING_DEFAULT_DATAGRAM_SIZE ;    }    /* Set context before request is called */    appl_reply_received = FALSE ;    appl_sequence = data_size ;    appl_length   = data_size ;    /* Request ICMP to 'send' */    NET_ICMP_ECHO_send( ipadr,                        0,                        appl_sequence,                        appl_length );    /* Get millisecond count right now */    SYSCON_read( SYSCON_BOARD_GET_MILLISEC_ID,                 &latest,                 sizeof(latest) ) ;    mstimout = latest + ping_ms_timeout ;  /* typically 1000 = one second */    do    {        /* Poll for any reply */        NET_poll() ;        if ( appl_reply_received )        {            if ( ( appl_sequence == appl_reply_sequence ) &&                 ( appl_length   == appl_reply_length   ) &&                 ( ipadr         == appl_reply_ip       ) )            {                rcode = OK ;                break ;            }        }         previous = latest;        /* get millisecond count right now */        rcode = SYSCON_read( SYSCON_BOARD_GET_MILLISEC_ID,                             &latest,                             sizeof(latest) ) ;        if ( latest < previous )        {            mstimout -= previous;  /* may cause a few millisec extra delay */        }        if (latest > mstimout)         {            rcode = ERROR_NET_PING_TIME_OUT ;        }    } while (rcode == OK);    return( rcode ) ;}/************************************************************************ * *                          NET_APPL_echo_reply *  Description : *  ------------- *  Receive 'ECHO'-reply * * *  Parameters : *  ------------ *  - * * *  Return values : *  --------------- *  'OK'(=0) * ************************************************************************/staticUINT32 NET_APPL_echo_reply( UINT32     src_ip_adr,  /* BE-format */                            t_mac_addr *src_mac_adr,/* MAC */                            UINT16     sequence,    /* CPU-format */                            UINT16     usr_length ){#ifdef NET_DEBUG    int i ;    UINT8 *p ;    printf("NET_APPL_echo_reply\n") ;    printf("src_ip_adr = %x\n", (UINT32)BE32_TO_CPU(src_ip_adr) ) ;    printf("sequence   = %d\n", sequence ) ;    printf("usr_length = %d\n", usr_length ) ;    printf("src MAC = " ) ;    p = (UINT8*) src_mac_adr ;    for (i=0; i<6; i++)    {        printf(" %02x",*p++ ) ;    }    printf("\n" ) ;#endif    /* save reply context */    if ( !appl_reply_received )    {        appl_reply_received = TRUE ;        appl_reply_sequence = sequence ;        appl_reply_length   = usr_length ;        appl_reply_ip       = src_ip_adr ;        memcpy( &appl_reply_mac_adr, src_mac_adr, SYS_MAC_ADDR_SIZE ) ;    }    return( OK ) ; }UINT32net_open( UINT32 ipadr, char *filename ){    UINT32 compl, state, cause, sp_hd ;    /* clear error context */    net_last_error  = OK ;    net_diag_msg[0] = 0 ;    compl = NET_TFTPC_open( ipadr, filename, &sp_hd ) ;    if ( (compl != OK) &&         (compl != ERROR_NET_ARP_MAC_NOT_RESOLUTED) )    {        NET_TFTPC_close( 0 ) ;        return( compl ) ;    }    while (1)    {        /* Poll for any reply */        NET_poll() ;        if (GETCHAR_CTRLC( DEFAULT_PORT ))        {            NET_TFTPC_close( 0 ) ;            net_last_error = ERROR_NET_USER_BREAK ;            compl = net_last_error ;            break ;        }        NET_TFTPC_poll( &state, &compl, &cause ) ;        if (state != TFTPC_SAP_STATE_WAIT_FIRST_BLOCK)        {            if (state == TFTPC_SAP_STATE_ERROR)            {               net_last_error = cause ;               compl = net_last_error ;               NET_TFTPC_close( 0 ) ;            }            else            {                compl = OK ;            }            break ;        }    }    /* We need to reply OK, as loader will terminate by any error */    return( compl ) ;}char net_getchar( void ){    char   byte ;    UINT32 rcode, state, compl, cause ;    rcode = NET_TFTPC_readbyte( 0,                                &byte ) ;    if (!rcode)    {        return( byte ) ;    }    while (1)    {        /* Poll for any reply */        NET_poll() ;        if (GETCHAR_CTRLC( DEFAULT_PORT ))        {            NET_TFTPC_close( 0 ) ;            net_last_error  = ERROR_NET_USER_BREAK ;            return( EOF_SREC ) ;        }        rcode = NET_TFTPC_readbyte( 0,                                    &byte ) ;        if (!rcode)        {            return( byte ) ;        }        if (rcode == ERROR_NET_TFTPC_NO_DATA)        {            continue ;        }        if (rcode == ERROR_NET_TFTPC_EOF)        {            NET_TFTPC_close( 0 ) ;            return( EOF_SREC ) ;        }        if (rcode == ERROR_NET_TFTPC_SAP_ERROR_STATE)        {            NET_TFTPC_poll( &state, &compl, &cause ) ;            net_last_error = cause ;            NET_TFTPC_close( 0 ) ;            return( EOF_SREC ) ;        }    }}UINT32net_getbyte( UINT8 *byte ){    UINT32 rcode, state, compl, cause ;    rcode = NET_TFTPC_readbyte( 0,                                byte ) ;    if (!rcode)    {        return( rcode ) ;    }    while (1)    {        /* Poll for any reply */        NET_poll() ;        if (GETCHAR_CTRLC( DEFAULT_PORT ))        {            NET_TFTPC_close( 0 ) ;            net_last_error  = ERROR_NET_USER_BREAK ;            return( ERROR_NET_USER_BREAK ) ;        }        rcode = NET_TFTPC_readbyte( 0,                                    byte ) ;        if (!rcode)        {            return( rcode ) ;        }        if (rcode == ERROR_NET_TFTPC_NO_DATA)        {            continue ;        }        if (rcode == ERROR_NET_TFTPC_EOF)        {            NET_TFTPC_close( 0 ) ;            return( rcode ) ;        }        if (rcode == ERROR_NET_TFTPC_SAP_ERROR_STATE)        {            NET_TFTPC_poll( &state, &compl, &cause ) ;            net_last_error = cause ;            NET_TFTPC_close( 0 ) ;            return( rcode ) ;        }    }}UINT32NET_file_read( UINT32 ipadr,               char   *filename,               UINT8  *buffer,               UINT32 *size ){    return( NET_TFTPC_file_read( ipadr,                                 filename,                                 buffer,                                 size,                                 NET_poll ) ) ;}UINT32NET_file_write( UINT32 ipadr,                char   *filename,                UINT8  *buffer,                UINT32 *size ){    return( NET_TFTPC_file_write( ipadr,                                  filename,                                  buffer,                                  size,                                  NET_poll ) ) ;}

⌨️ 快捷键说明

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