Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

file_system_manager.cpp

Committer:
klauss
Date:
2015-05-11
Revision:
122:480c44b0e205
Parent:
121:ee02790d00b7
Child:
124:c1b6c893e1c3

File content as of revision 122:480c44b0e205:

#include "file_system_manager.h"

QSPIFileSystem qspifs("qspi");

char file_buffer [ FILE_BUFFER_SIZE ];

void set_ip ( const char * new_header_ip )
{
    FILE * fip = fopen("/qspi/myip.txt", "w");
    if ( fip )
    {
        fprintf( fip, "%s\n\r", new_header_ip  );
        fclose( fip );
    }    
}

int get_ip ( char * header_ip, const size_t length )
{
    FILE * fip = fopen ("/qspi/myip.txt", "r" );
    if ( fip != NULL )
    {
        int read = fread ( header_ip, 1, length, fip );
        if( read > 0 )
        {
            for ( register int i = 0; i < read; i++ )
            {
                if( header_ip [ i ] == '\n' || header_ip [ i ] == '\r' )
                {
                    header_ip [ i ] = '\0';
                    break;
                }
            }
 
            if ( debug_file ) debug_msg ("Eth ip %s", header_ip );
        } else {
            if ( fip ) fclose ( fip );
            return ( -3 );
        }
        
        header_ip [ length ] = 0;

        if ( fip ) fclose ( fip );
        
        return ( read );
    } 
        
    if ( debug_file ) debug_msg ("Failed to open /qspi/myip.txt" );
    
    return ( -1 );
}

int get_clock_server_ip ( char * clock_server_ip, const size_t length )
{
    return ( get_server_ip ( clock_server_ip, length ) );
}

int get_server_ip ( char * server_ip, const size_t length )
{
    FILE * fsip = fopen ("/qspi/serverip.txt", "r" );
    if ( fsip != NULL )
    {
        int read = fread ( server_ip, 1, length, fsip );
        if( read > 0 )
        {
            for ( register int i = 0; i < read; i++ )
            {
                if( server_ip [ i ] == '\n' || server_ip [ i ] == '\r' )
                {
                    server_ip [ i ] = '\0';
                    break;
                }
            }
 
            if ( debug_file ) debug_msg ("Eth ip %s", server_ip );
        } else {
            if ( fsip ) fclose ( fsip );
            return ( -3 );
        }
        
        server_ip [ length ] = 0;

        fclose ( fsip );
        
        return ( read );
    } 
        
    if ( debug_file ) debug_msg ("Failed to open /qspi/serverip.txt" );
    
    return ( -1 );
}

int get_mask ( char * eth_mask, const size_t length )
{
    FILE * fmsk = fopen("/qspi/mymask.txt", "r");
    if ( fmsk != NULL )
    {
        int read = fread ( eth_mask, 1, length, fmsk );
        if( read > 0 )
        {
            for ( register int i = 0; i < read; i++ )
            {
                if( eth_mask [ i ] == '\n' || eth_mask [ i ] == '\r' )
                {
                    eth_mask [ i ] = '\0';
                    break;
                }
            }
 
            if ( debug_file ) debug_msg ("Eth mask %s", eth_mask );
        } else {
            if ( fmsk ) fclose ( fmsk );
            return ( -3 );
        }
        
        eth_mask [ length ] = 0;

        if ( fmsk ) fclose ( fmsk );
        
        return ( read );
    } 
        
    if ( debug_file ) debug_msg ("Failed to open /qspi/mymask.txt" );
    
    return ( -1 );    
}
int get_gateway ( char * eth_gw, const size_t length )
{
    FILE * fgw = fopen("/qspi/mygateway.txt", "r");
    if ( fgw != NULL )
    {
        int read = fread ( eth_gw, 1, length, fgw );
        if( read > 0 )
        {
            for ( register int i = 0; i < read; i++ )
            {
                if( eth_gw [ i ] == '\n' || eth_gw [ i ] == '\r' )
                {
                    eth_gw [ i ] = '\0';
                    break;
                }
            }
 
        if ( debug_file ) debug_msg ("Eth gateway %s", eth_gw );
        } else { 
            if ( fgw ) fclose ( fgw );
            return ( -3 );
        }
        
        eth_gw [ length ] = 0;

        if ( fgw ) fclose ( fgw );
        
        return ( read );
    } 
        
    if ( debug_file ) debug_msg ("Failed to open /qspi/mygateway.txt" );
    
    return ( -1 );        
}

void set_clock_server_ip ( const char * new_server_ip );

void set_header_ext ( const int new_header_ext )
{
    FILE * fext = fopen( "/qspi/myext.txt", "w" );
    if ( fext )
    {
        fprintf( fext, "%i\n\r", new_header_ext );
        fclose( fext );
    }
}


int get_header_ext ( void )
{
    char get_header_ext_buffer [ 32 ] = "";
    
    FILE * fext = fopen ( "/qspi/myext.txt", "r" );
    if ( fext == NULL )
    {
        if( debug_file ) debug_msg ( "Failed to open /qspi/myext.txt" );
        
        return (  PEER_EXT );
    } else {
        if( fread ( ( void * ) get_header_ext_buffer, 1, sizeof ( get_header_ext_buffer ) - 1, fext ) > 0 )
        {
            if ( fext ) fclose ( fext );
            return ( atoi ( get_header_ext_buffer ) );
        } else {
            if ( debug_file ) debug_msg ("Failed to read /qspi/myext.txt" );
            
            if ( fext ) fclose ( fext );
            return ( PEER_EXT );
        }
    }
}

void set_header_sip_port ( const int new_header_sip_port )
{
    FILE * fport = fopen("/qspi/mysipport.txt", "w");
    if ( fport )
    {
        fprintf( fport,"%i\n\r", new_header_sip_port );
        fclose( fport );
    }
}

int get_header_sip_port ( void )
{
    char get_header_sip_port_buffer [ 32 ] = "";
    
    FILE * fport = fopen ( "/qspi/mysipport.txt", "r" );
    if ( fport == NULL )
    {
        if( debug_file ) debug_msg ( "Failed to open /qspi/mysipport.txt" );
        
        return (  MY_PORT );
    } else {
        if( fread ( ( void * ) get_header_sip_port_buffer, 1, sizeof ( get_header_sip_port_buffer ) - 1, fport ) > 0 )
        {
            if ( fport ) fclose ( fport );
            return ( atoi ( get_header_sip_port_buffer ) );
        } else {
            if ( debug_file ) debug_msg ("Failed to read /qspi/mysipport.txt" );
            
            if ( fport ) fclose ( fport );
            return ( MY_PORT );
        }
    }
}

void set_server_ip ( const char * new_server_ip )
{
    FILE * fsip = fopen("/qspi/serverip.txt", "w");
    if ( fsip )
    {
        fprintf( fsip,"%s\n\r", new_server_ip );
        fclose( fsip );
    }
}

void set_server_ext ( const int new_server_ext )
{
    FILE * fserext = fopen("/qspi/peerext.txt", "w");
    if ( fserext )
    {
        fprintf( fserext , "%i\n\r", new_server_ext );
        fclose( fserext );
    }
}

void set_server_port ( const int new_server_port )
{
    FILE * fsport = fopen("/qspi/serverport.txt", "w");   
    if ( fsport )
    {
        fprintf(fsport,"%i\n\r", new_server_port );
        fclose( fsport );
    }
}

void set_mask ( const char * new_mask )
{
    FILE * fmask = fopen("/qspi/mymask.txt", "w");
    if ( fmask )
    {
        fprintf(fmask,"%s\n\r",new_mask );
        fclose( fmask );
    }
}

void set_gateway ( const char * new_gateway )
{
    //fgate = fopen("/qspi/mygateway.txt", "w");
    FILE * fgate = fopen("/qspi/mygateway.txt", "w");
    if ( fgate )
    {
        fprintf(fgate,"%s\n\r", new_gateway );
        fclose( fgate );
    }
}

void set_udp_port_listener ( const int new_udp_port_listener )
{
    FILE * fudpport = fopen( "/qspi/udpport.txt", "w" );
    if ( fudpport )
    {
        fprintf( fudpport, "%i\n\r",new_udp_port_listener );
        fclose( fudpport );
    }
}

void set_tcp_port_listener ( const int new_tcp_port_listener )
{
    FILE * ftcpport = fopen( "/qspi/tcpport.txt", "w" );
    if ( ftcpport )
    {
        fprintf( ftcpport, "%i\n\r",new_tcp_port_listener );
        fclose( ftcpport );
    }
}

void set_fw_ip ( const char * new_fw_ip )
{
    FILE * ffwip = fopen("/qspi/fw_ip.txt", "w" );
    if ( ffwip )
    {
        fprintf(ffwip,"%s\n\r", new_fw_ip );
        fclose( ffwip );
    }
}

void set_fw_port ( const int new_fw_port )
{
    FILE * ffwport = fopen("/qspi/fw_port.txt", "w" );
    if ( ffwport )
    {
        fprintf(ffwport,"%i\n\r", new_fw_port );
        fclose( ffwport );
    }
}

void set_max_ext ( const int new_max_ext )
{
    FILE * fmex = fopen( "/qspi/maxext.txt", "w" );
    if ( fmex ){
        fprintf( fmex, "%i\n\r", new_max_ext );
        fclose( fmex );
    }
}

void set_min_ext ( const int new_min_ext )
{
    FILE * fmin = fopen( "/qspi/minext.txt", "w" );
    if ( fmin ){
        fprintf( fmin, "%i\n\r", new_min_ext );
        fclose( fmin );
    }
}

// Print the content of a given file
void cat ( const char * fname )
{
    char buff[513];
    int num;
    
    FILE *fp = fopen( fname, "r" );
    if (fp == NULL) {
        if( debug_file ) if( debug_uart3 && !( from_eth ) )  pc.printf( "Failed to open %s", fname);
        return;
    }
    
    while( ( num = fread( buff, 1, 512, fp ) ) > 0 ){
        buff[ num ] = '\0';
        if( debug_uart3 && !( from_eth ) )  pc.printf( buff );
        if( from_eth ){
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( buff, strlen( buff ) ) ) );
            
            else if( udp_query ){
                udp_query_send_msg( buff );
            }
        }
    }
    fclose(fp);
}

void set_shift_port( const int new_shift_port )
{
    FILE * shift_port = fopen("/qspi/shift_port.txt", "w" );
    if( shift_port )
    {
        fprintf( shift_port,"%i\n\r", new_shift_port );
        fclose( shift_port );
        if( debug_file ) debug_msg("Set /qspi/shift_port.txt");
    }
        else
    {
        if( debug_file ) debug_msg("Cannot open /qspi/shift_port.txt");
    }
}

void files ( const char type )
{
    FILE *fip = NULL;
    FILE *fmask = NULL;
    FILE *fgate = NULL;
    FILE *fport = NULL;
    FILE *fsip = NULL;
    FILE *fsport = NULL;
    FILE *fext = NULL;
    FILE *fserext = NULL;
    FILE *fudpport = NULL;
    FILE *ftcpport = NULL;
    FILE *ffwip = NULL;
    FILE *ffwport = NULL;
    FILE *fmax = NULL;
    FILE *fmin = NULL;
    FILE *fshift_port = NULL;
    
    // show files
    if ( type == 's' ) {
         
        if( debug_uart3 && !( from_eth ) ) pc.printf("Header IP " );
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Header IP " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat ("/qspi/myip.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Header ext ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Header ext " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/myext.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Header port ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Header port " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/mysipport.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Server IP ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Server IP " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/serverip.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Server ext ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Server ext " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/peerext.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Server port ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Server port " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }
        cat("/qspi/serverport.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Mascara de rede ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Mascara de rede " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }
        cat("/qspi/mymask.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Gateway IP ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Gateway IP " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }
        cat("/qspi/mygateway.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("UDP Port ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "UDP Port " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/udpport.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("TCP Port ");
        if( from_eth  ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "TCP Port " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/tcpport.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("FW Server IP ");
        if( from_eth  ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "FW Server IP " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/fw_ip.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("FW Server Port ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "FW Server Port " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/fw_port.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Max Ext ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Max Ext " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/maxext.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Min Ext ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Min Ext " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/minext.txt");
        
        if( debug_uart3 && !( from_eth ) ) pc.printf("Shift Port ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Shift Port " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/shift_port.txt");

        if( debug_uart3 && !( from_eth ) ) pc.printf("Clock Server IP ");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Clock Server IP " );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        cat("/qspi/serverip.txt");
        
        /*
        if( debug_uart3 && !( from_eth ) ) pc.printf("Clock Server Port %d\r\n", CLOCK_SERVER_PORT  );
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Clock Server Port %d\r\n", CLOCK_SERVER_PORT  );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }
        
        if( debug_uart3 && !( from_eth ) ) pc.printf("Clock Header Port %d\r\n", CLOCK_HEADER_PORT  );
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Clock Header Port %d\r\n", CLOCK_HEADER_PORT  );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }

        if( debug_uart3 && !( from_eth ) ) pc.printf("Bootloader Port %d\r\n", BL_PORT );
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "Bootloader Port %d\r\n", BL_PORT );
            if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );

            else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
        }
        */
        
        //*------------ formatando a saida ----------------*//
        for( register int i = 0; i < FILE_BUFFER_SIZE; i++ ) file_buffer[ i ] = 0;

        if( from_eth ) {
            if( tcp_session && !udp_query ) {
                snprintf( file_buffer, FILE_BUFFER_SIZE, "> " );
                if( tcp_session && !udp_query ) while( !( tcp_client.send_all( file_buffer, strlen( file_buffer ) ) ) );
            } else if( udp_query ) {
                udp_query_send_msg( "> " );
                
            }
        }
    }

    if (type == 'c' ) {
        // close all files
        if( fip ) fclose( fip );
        if( fmask ) fclose( fmask );
        if( fgate ) fclose( fgate );
        if( fport )fclose( fport );
        if( fsip ) fclose( fsip );
        if( fsport ) fclose( fsport );
        if( fext ) fclose( fext );
        if( fserext ) fclose( fserext );
        if( fudpport ) fclose( fudpport );
        if( ftcpport ) fclose( ftcpport );
        if( fmax ) fclose( fmax );
        if( fmin ) fclose( fmin );
        if( ffwip ) fclose( ffwip );
        if( ffwport) fclose( ffwport );
        if( fshift_port ) fclose ( fshift_port );
    }
    
    
    // Check if files exist, if not create the files
    //fixme criar um bool pra cada file
    if (type == 'i' ) 
    {    
        bool exists = true;

        fip = fopen ("/qspi/myip.txt", "r");
        if( fip == NULL) {
            set_ip( MY_IP );
            exists = false;
        }
        if ( fip ) fclose ( fip );
        fip = NULL;

        fserext = fopen("/qspi/myext.txt", "r");
        if( fserext == NULL ) {
            set_header_ext( MY_EXT );
            exists = false;
        }
        if ( fserext ) fclose ( fserext );
        fserext = NULL;

        fport = fopen("/qspi/mysipport.txt", "r");
        if (fport == NULL) {
            set_header_sip_port( MY_PORT );
            exists = false;
        }
        if ( fport ) fclose ( fport );
        fport = NULL;

        fsip = fopen("/qspi/serverip.txt", "r");
        if (fsip == NULL) {
            set_server_ip( SERVER_IP );
            exists = false;
        }
        if ( fsip ) fclose ( fsip );
        fsip = NULL;

        fext = fopen("/qspi/peerext.txt", "r");
        if ( fext == NULL ) {
            set_server_ext( PEER_EXT );
            exists = false;
        }
        if ( fext ) fclose ( fext );
        fext = NULL;

        fsport = fopen("/qspi/serverport.txt", "r");
        if (fsport == NULL) {
            set_server_port( SERVER_PORT );
            exists = false;
        }
        if ( fsport ) fclose ( fsport );
        fsport = NULL;

        fmask = fopen("/qspi/mymask.txt", "r");
        if (fmask == NULL) {
            set_mask( MY_MSK );
            exists = false;
        }
        if ( fmask ) fclose ( fmask );
        fmask = NULL;

        fgate = fopen("/qspi/mygateway.txt", "r");
        if (fgate == NULL) {
            set_gateway( MY_GTW );
            exists = false;
        }
        if ( fgate ) fclose ( fgate );
        fgate = NULL;

        fudpport = fopen("/qspi/udpport.txt", "r" );
        if( fudpport == NULL ) {
            set_udp_port_listener( UDP_PORT_LISTENER );
            exists = false;
        }
        if ( fudpport ) fclose ( fudpport );
        fudpport = NULL;

        ftcpport = fopen("/qspi/tcpport.txt", "r" );
        if( ftcpport == NULL ) {
            set_tcp_port_listener( TCP_PORT_LISTENER );
            exists = false;
        }
        if ( ftcpport ) fclose ( ftcpport );
        ftcpport = NULL;

        ffwip = fopen("/qspi/fw_ip.txt", "r" );
        if( ffwip == NULL ) {
            set_fw_ip( __FW_SERVER_IP__ );
            exists = false;
        }
        if ( ffwip ) fclose ( ffwip );
        ffwip = NULL;

        ffwport = fopen("/qspi/fw_port.txt", "r" );
        if( ffwport == NULL ) {
            set_fw_port( __FW_SERVER_PORT__ );
            exists = false;
        }
        if ( ffwport ) fclose ( ffwport );
        ffwport = NULL;

        fmax = fopen("/qspi/maxext.txt", "r" );
        if( fmax == NULL ) {
            set_max_ext( MAX_EXT );
            exists = false;
        }
        if ( fmax ) fclose ( fmax );
        fmax = NULL;

        fmin = fopen("/qspi/minext.txt", "r" );
        if( fmin == NULL ) {
            if( debug_file ) debug_msg("Don't exist /qspi/minext.txt");
            set_min_ext( MIN_EXT );
            exists = false;
        }
        if ( fmin ) fclose ( fmin );
        fmin = NULL;
        
        fshift_port = fopen("/qspi/shift_port.txt", "r" );
        if( fshift_port == NULL )
        {
            if( debug_file ) debug_msg("Don't exist /qspi/shift_port.txt");
            set_shift_port( SHIFT_PORT );
            exists = false;
        }
        if ( fshift_port ) fclose ( fshift_port );
        fshift_port = NULL;
         
        if( !exists ) {
            if( debug_uart3 && !( from_eth ) ) pc.printf("\n\rDefault configurations set!\n\r");
            if( from_eth ) {
                snprintf( file_buffer, FILE_BUFFER_SIZE, "Default configurations set!\n\r");
                file_buffer[ strlen( file_buffer ) - 1 ] = '\0';
                if( tcp_session && !udp_query ) {
                    tcp_client.send_all( file_buffer, strlen( file_buffer ) );
                    for( register int i = 0; i < FILE_BUFFER_SIZE; i++ ) file_buffer[ i ] = 0;
                }
            }
        }
    }

    if (type == 'r' ) {
        // Just open for read
        fip = fopen ("/qspi/myip.txt", "r");
        fmask = fopen("/qspi/mymask.txt", "r");
        fgate = fopen("/qspi/mygateway.txt", "r");
        fport = fopen("/qspi/mysipport.txt", "r");
        fsip = fopen("/qspi/serverip.txt", "r");
        fsport = fopen("/qspi/serverport.txt", "r");
        fext = fopen( "/qspi/myext.txt", "r" );
        fserext = fopen( "/qspi/peerext.txt", "r" );
        fudpport = fopen( "/qspi/udpport.txt", "r" );
        ftcpport = fopen( "/qspi/tcpport.txt", "r" );
        ffwip = fopen("/qspi/fw_ip.txt", "r" );
        ffwport = fopen("/qspi/fw_port.txt", "r" );
        fmax = fopen("/qspi/maxext.txt", "r" );
        fmin = fopen("/qspi/minext.txt", "r" );
        fshift_port = fopen("/qspi/shift_port.txt", "r" );
         
    }

    if( type == 'w') {
         
        // Create and write the default configs
        set_ip( MY_IP );

        set_header_sip_port( MY_PORT );

        set_header_ext( MY_EXT );

        set_server_ip( SERVER_IP );

        set_server_ext( PEER_EXT );

        set_server_port( SERVER_PORT );

        set_mask( MY_MSK );

        set_gateway( MY_GTW );

        set_udp_port_listener( UDP_PORT_LISTENER );

        set_tcp_port_listener( TCP_PORT_LISTENER );

        set_fw_ip( __FW_SERVER_IP__ );

        set_fw_port( __FW_SERVER_PORT__ );

        set_max_ext( MAX_EXT );

        set_min_ext( MIN_EXT );
        
        set_shift_port( SHIFT_PORT );

        if( debug_uart3 && !( from_eth ) ) pc.printf("\n\rDefault configurations set!\n\r");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "\n\rDefault configurations set!\n\r");
            file_buffer[ strlen( file_buffer ) - 1 ] = '\0';
            if( tcp_session && !udp_query ) {
                tcp_client.send_all( file_buffer, strlen( file_buffer ) );
            } else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
            for( register int i = 0; i < FILE_BUFFER_SIZE; i++ ) file_buffer[ i ] = 0;
        }
         
    }

    if (type == 'e')
    {
        //Erase configs    
        fip = fopen ("/qspi/myip.txt", "w");
        fmask = fopen("/qspi/mymask.txt", "w");
        fgate = fopen("/qspi/mygateway.txt", "w");
        fport = fopen("/qspi/mysipport.txt", "w");
        fsip = fopen("/qspi/serverip.txt", "w");
        fsport = fopen("/qspi/serverport.txt", "w");
        fext = fopen( "/qspi/myext.txt", "w" );
        fserext = fopen( "/qspi/peerext.txt", "w" );
        fudpport = fopen( "/qspi/udpport.txt", "w" );
        ftcpport = fopen( "/qspi/tcpport.txt", "w" );
        ffwip = fopen("/qspi/fw_ip.txt", "w" );
        ffwport = fopen("/qspi/fw_port.txt", "w" );
        fmax = fopen("/qspi/maxext.txt", "w" );
        fmin = fopen("/qspi/minext.txt", "w" );
        fshift_port = fopen("/qspi/shift_port.txt", "w" );

        if( fip ) fprintf( fip, "%s\n\r", ""  );
        if( fport ) fprintf( fport, "%i\n\r", 0 );
        if( fext ) fprintf( fext, "%i\n\r", 0 );

        if( fsip ) fprintf( fsip, "%s\n\r", ""  );
        if( fserext ) fprintf( fserext, "%i\n\r", 0 );
        if( fsport ) fprintf( fsport, "%i\n\r", 0 );

        if( fmask ) fprintf( fmask, "%s\n\r","" );
        if( fgate ) fprintf( fgate, "%s\n\r", "" );

        if( fudpport ) fprintf( fudpport, "%i\n\r", 0 );
        if( ftcpport ) fprintf( ftcpport, "%i\n\r", 0 );

        if( ffwip ) fprintf( ffwip, "%s\n\r","" );
        if( ffwport ) fprintf( ffwport, "%i\n\r",0 );

        if( fmax ) fprintf( fmax, "%i\n\r", 0 );
        if( fmin ) fprintf( fmin, "%i\n\r", 0 );
        
        if( fshift_port ) fprintf( fshift_port, "%i\n\r", 0 );

        files('c');

        if( debug_uart3 && !( from_eth ) ) pc.printf("\n\rErased configurations set!\n\r");
        if( from_eth ) {
            snprintf( file_buffer, FILE_BUFFER_SIZE, "\n\rErased configurations set!\n\r");
            file_buffer[ strlen( file_buffer ) - 1 ] = '\0';
            if( tcp_session && !udp_query ) {
                tcp_client.send_all( file_buffer, strlen( file_buffer ) );
            } else if( udp_query ) {
                udp_query_send_msg( file_buffer );
            }
            for( register int i = 0; i < FILE_BUFFER_SIZE; i++ ) file_buffer[ i ] = 0;
        }
         
    }
}

int init_fsystem ( void )
{
    //  init file system and check if files exist
    int return_value = 0;
    
    if (!qspifs.isformatted()) {
        qspifs.format();
        return_value |= BIT0;
    }
    // check if the files are created
    files('i');
    files('c');
    
    return ( return_value |= BIT1 );
}