Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

eth.cpp

Committer:
klauss
Date:
2014-12-16
Revision:
78:1353744f01e1
Parent:
74:81c47fff88a5
Child:
81:3656f00ab3db

File content as of revision 78:1353744f01e1:

#include "eth.h"

EthernetInterface eth;

int __init_eth__(){    
    static bool initialized = false;
    char buff_ip[ 16 ] = "";
    char buff_msk[ 16 ] = "";
    char buff_gtw[ 16 ] = "";
    wdt.kick();
    pc.printf("\n\r");
    FILE *fp = fopen( "/qspi/myip.txt", "r");
    if( fp == NULL ){
        if( debug_file ) debug_msg("Failed to open /qspi/myip.txt" );
        strncpy( buff_ip, __MY_IP__, 20 );    
    }else{
        int read = fread( buff_ip, 1, 512, fp );
        if( read > 0 ){
            for( int i = 0; i < read; i++ ) 
                if( buff_ip[ i ] == '\n' || buff_ip[ i ] == '\r' ){
                    buff_ip[ i ] = '\0';
                    break;
                }
            if( debug_file ) debug_msg("Eth ip %s", buff_ip );
        }else{
            if( debug_file ) debug_msg("Failed to read /qspi/myip.txt" );
            strncpy( buff_ip, __MY_IP__, 20 );
        }
    }
    fclose( fp );
    buff_ip[ 15 ] = 0;
    fp = fopen( "/qspi/mymask.txt", "r");
    if( fp == NULL ){
        if( debug_file ) debug_msg("Failed to open /qspi/mymask.txt" );
        strncpy( buff_msk, __MY_MSK__, 20 );    
    }else{
        int read = fread( buff_msk, 1, 512, fp );
        if( read > 0 ){
            for( int i = 0; i < read; i++ ) 
                if( buff_msk[ i ] == '\n' || buff_msk[ i ] == '\r' ){
                    buff_msk[ i ] = '\0';
                    break;
                }
            if( debug_file ) debug_msg("mascara de rede Eth %s", buff_msk );
        }else{
            if( debug_file ) debug_msg("Failed to read /qspi/mymask.txt" );
            strncpy( buff_msk, __MY_MSK__, 20 );
        }
    }
    fclose( fp );
    buff_msk[ 15 ] = 0;
    fp = fopen( "/qspi/mygateway.txt", "r");
    if( fp == NULL ){
        if( debug_file ) debug_msg("Failed to open /qspi/mygateway.txt" );
        strncpy( buff_gtw, __MY_GTW__, 20 );    
    }else{
        int read = fread( buff_gtw, 1, 512, fp );
        if( read > 0 ){
            for( int i = 0; i < read; i++ ) 
                if( buff_gtw[ i ] == '\n' || buff_gtw[ i ] == '\r' ){
                    buff_gtw[ i ] = '\0';
                    break;
                }
            if( debug_file ) debug_msg("Ip Gateway Eth %s", buff_gtw );
        }else{
            if( debug_file ) debug_msg("Failed to read /qspi/mygateway.txt" );
            strncpy( buff_gtw, __MY_GTW__, 20 );
        }
    }
    fclose( fp );
    buff_gtw[ 15 ] = 0;
    if( !initialized ){
        //eth.init( buff_ip, buff_msk, buff_gtw );
        eth.init( buff_ip, buff_msk, __MY_GTW__ );
        initialized = true;
        return eth.connect();
    }
    if( !eth.disconnect() ){
        return eth.connect();
    }
    else{
        return eth.connect();
    }
}