![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: file_system_manager.cpp
- Revision:
- 81:3656f00ab3db
- Parent:
- 74:81c47fff88a5
- Child:
- 99:e80850c51106
--- a/file_system_manager.cpp Tue Dec 16 19:34:30 2014 +0000 +++ b/file_system_manager.cpp Tue Dec 30 12:37:40 2014 +0000 @@ -1,3 +1,489 @@ #include "file_system_manager.h" -QSPIFileSystem qspifs("qspi"); \ No newline at end of file +QSPIFileSystem qspifs("qspi"); + +/* +void init_fsystem(){ + // init file system and check if files exist + if (!qspifs.isformatted()) { + qspifs.format(); + send_msg("\n\rFile system configured!!\n\r"); + } + debug_msg(""); + files('i'); // check if the files are created + debug_msg(""); + //files('c'); + debug_msg(""); + send_msg("\n\r*******File System Ready*******\n\r"); +} + +*/ + +void set_ip( char * new_header_ip ){ + FILE * fip = fopen("/qspi/myip.txt", "w"); + fprintf( fip, "%s\n\r", new_header_ip ); + fclose( fip ); +} + +void set_header_ext( int new_header_ext ){ + FILE * fext = fopen( "/qspi/myext.txt", "w" ); + fprintf( fext, "%i\n\r", new_header_ext ); + fclose( fext ); +} + +void set_header_sip_port( int new_header_sip_port ){ + FILE * fport = fopen("/qspi/mysipport.txt", "w"); + fprintf( fport,"%i\n\r", new_header_sip_port ); + fclose( fport ); +} + +void set_server_ip( char * new_server_ip ){ + FILE * fsip = fopen("/qspi/serverip.txt", "w"); + fprintf( fsip,"%s\n\r", new_server_ip ); + fclose( fsip ); +} + +void set_server_ext( int new_server_ext ){ + FILE * fserext = fopen("/qspi/peerext.txt", "w"); + fprintf( fserext , "%i\n\r", new_server_ext ); + fclose( fserext ); +} + +void set_server_port( int new_server_port ){ + FILE * fsport = fopen("/qspi/serverport.txt", "w"); + fprintf(fsport,"%i\n\r", new_server_port ); + fclose( fsport ); +} + +void set_mask( char * new_mask ){ + FILE * fmask = fopen("/qspi/mymask.txt", "w"); + fprintf(fmask,"%s\n\r",new_mask ); + fclose( fmask ); +} + +void set_gateway( char * new_gateway ){ + //fgate = fopen("/qspi/mygateway.txt", "w"); + FILE * fgate = fopen("/qspi/mygateway.txt", "w"); + fprintf(fgate,"%s\n\r", new_gateway ); + fclose( fgate ); +} + +void set_udp_port_listener( int new_udp_port_listener ){ + FILE * fudpport = fopen( "/qspi/udpport.txt", "w" ); + fprintf( fudpport, "%i\n\r",new_udp_port_listener ); + fclose( fudpport ); +} + +void set_tcp_port_listener( int new_tcp_port_listener ){ + FILE * ftcpport = fopen( "/qspi/tcpport.txt", "w" ); + fprintf( ftcpport, "%i\n\r",new_tcp_port_listener ); + fclose( ftcpport ); +} + +void set_telemetry_ip( char * new_telemetry_ip ){ + FILE * ftip = fopen("/qspi/telemetry_ip.txt", "w" ); + fprintf(ftip,"%s\n\r", new_telemetry_ip ); + fclose( ftip ); +} + +void set_telemetry_port( int new_telemetry_port ){ + FILE * ftport = fopen("/qspi/telemetry_port.txt", "w" ); + fprintf(ftport,"%i\n\r", new_telemetry_port ); + fclose( ftport ); +} + +void set_max_ext( int new_max_ext ){ + FILE * fmex = fopen( "/qspi/maxext.txt", "w" ); + fprintf( fmex, "%i\n\r", new_max_ext ); + fclose( fmex ); +} + +void set_min_ext( int new_min_ext ){ + FILE * fmin = fopen( "/qspi/minext.txt", "w" ); + 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 ) pc.printf( "Failed to open %s", fname); + return; + } + + while( ( num = fread( buff, 1, 512, fp ) ) > 0 ){ + buff[ num ] = '\0'; + pc.printf( buff ); + if( from_eth ){ + if( tcp_session ) while( !( tcp_client.send_all( buff, strlen( buff ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, buff, strlen( buff ) ); + } + } + } + fclose(fp); +} + +/* +//operantion with the system config files +void files( const char type ){ + FILE *fip = NULL, + *fmask = NULL, + *fgate = NULL, + *fport = NULL, + *fsip = NULL, + *fsport = NULL, + *fext = NULL, + *fserext = NULL, + *fudpport = NULL, + *ftcpport = NULL, + *ftip = NULL, + *ftport = NULL; + + //fixme mudar o nome dessa variable + + //#define MIN_EXT 5100 + //#define MAX_EXT 5139 + char string_msg[ 1024 ]; + // show files + switch( type ){ + case 's' : { + pc.printf("Header IP " ); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Header IP " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/myip.txt"); + + pc.printf("Header ext "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Header ext " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/myext.txt"); + + pc.printf("Header port "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Header port " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/mysipport.txt"); + + pc.printf("Server IP "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Server IP " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/serverip.txt"); + + pc.printf("Server ext "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Server ext " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/peerext.txt"); + + pc.printf("Server port "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Server port " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + cat("/qspi/serverport.txt"); + + pc.printf("Mascara de rede "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Mascara de rede " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + cat("/qspi/mymask.txt"); + + pc.printf("Gateway IP "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Gateway IP " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + cat("/qspi/mygateway.txt"); + + pc.printf("UDP Port "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "UDP Port " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/udpport.txt"); + + pc.printf("TCP Port "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "TCP Port " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/tcpport.txt"); + + pc.printf("Telemetry Server IP "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Telemetry Server IP " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/telemetry_ip.txt"); + + pc.printf("Telemetry Server Port "); + if( from_eth ){ + snprintf( string_msg, __PROMPT_ETH_BUFFER_SIZE__, "Telemetry Server Port " ); + if( tcp_session ) while( !( tcp_client.send_all( string_msg, strlen( string_msg ) ) ) ); + + else if( udp_request ){ + udp_server.sendTo( udp_client, string_msg, strlen( string_msg ) ); + } + } + + cat("/qspi/telemetry_port.txt"); + break; + } + + case 'c' : { + // close all files + debug_msg(""); + if( fip != NULL ) fclose( fip ); + if( fmask != NULL ) fclose( fmask ); + if( fgate != NULL ) fclose( fgate ); + if( fport != NULL ) fclose( fport ); + if( fsip != NULL ) fclose( fsip ); + if( fsport != NULL ) fclose( fsport ); + if( fext != NULL ) fclose( fext ); + if( fserext != NULL ) fclose( fserext ); + if( fudpport != NULL ) fclose( fudpport ); + if( ftcpport != NULL ) fclose( ftcpport ); + debug_msg(""); + break; + } + + case 'i' : { + // Check if files exist, if not create the files + //fixme criar um bool pra cada file + bool exists = true; + + debug_msg(""); + fip = fopen("/qspi/myip.txt", "r"); + if( fip == NULL){ + set_ip( __MY_IP__ ); + exists = false; + } + debug_msg(""); + fserext = fopen("/qspi/myext.txt", "r"); + if( fserext == NULL ){ + set_header_ext( __MY_EXT__ ); + exists = false; + } + debug_msg(""); + fport = fopen("/qspi/mysipport.txt", "r"); + if (fport == NULL){ + set_header_sip_port( __MY_PORT__ ); + exists = false; + } + debug_msg(""); + fsip = fopen("/qspi/serverip.txt", "r"); + if (fsip == NULL){ + set_server_ip( __SERVER_IP__ ); + exists = false; + } + debug_msg(""); + fext = fopen("/qspi/peerext.txt", "r"); + if ( fext == NULL ){ + set_server_ext( __PEER_EXT__ ); + exists = false; + } + debug_msg(""); + fsport = fopen("/qspi/serverport.txt", "r"); + if (fsport == NULL){ + set_server_port( __SERVER_PORT__ ); + exists = false; + } + debug_msg(""); + fmask = fopen("/qspi/mymask.txt", "r"); + if (fmask == NULL){ + set_mask( __MY_MSK__ ); + exists = false; + } + debug_msg(""); + fgate = fopen("/qspi/mygateway.txt", "r"); + if (fgate == NULL){ + set_gateway( __MY_GTW__ ); + exists = false; + } + debug_msg(""); + fudpport = fopen("/qspi/udpport.txt", "r" ); + if( fudpport == NULL ){ + set_udp_port_listener( UDP_PORT_LISTENER ); + exists = false; + } + debug_msg(""); + ftcpport = fopen("/qspi/tcpport.txt", "r" ); + if( ftcpport == NULL ){ + set_tcp_port_listener( TCP_PORT_LISTENER ); + exists = false; + } + debug_msg(""); + ftip = fopen("/qspi/telemetry_ip.txt", "r" ); + if( ftip == NULL ){ + set_telemetry_ip( __TELEMETRY_SERVER_IP__ ); + exists = false; + } + debug_msg(""); + ftport = fopen("/qspi/telemetry_port.txt", "r" ); + if( ftport == NULL ){ + set_telemetry_port( __TELEMETRY_SERVER_PORT__ ); + exists = false; + } + debug_msg(""); + if( !exists ){ + send_msg("\n\rDefault configurations set!\n\r"); + } + debug_msg(""); + break; + } + + case '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" ); + ftip = fopen("/qspi/telemetry_ip.txt", "r" ); + ftport = fopen("/qspi/telemetry_port.txt", "r" ); + break; + } + + case '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_telemetry_ip( __TELEMETRY_SERVER_IP__ ); + + set_telemetry_port( __TELEMETRY_SERVER_PORT__ ); + + send_msg( "Default configurations set!" ); + break; + } + + case '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" ); + ftip = fopen("/qspi/telemetry_ip.txt", "w" ); + ftport = fopen("/qspi/telemetry_port.txt", "w" ); + + //FIXME verificar se deveria colocar tudo para %s + fprintf( fip, "%s\n\r", "" ); + fprintf( fport, "%i\n\r", 0 ); + fprintf( fext, "%i\n\r", 0 ); + + fprintf( fsip, "%s\n\r", "" ); + fprintf( fserext, "%i\n\r", 0 ); + fprintf( fsport, "%i\n\r", 0 ); + + fprintf( fmask, "%s\n\r","" ); + fprintf( fgate, "%s\n\r", "" ); + + fprintf( fudpport, "%i\n\r", 0 ); + fprintf( ftcpport, "%i\n\r", 0 ); + + fprintf( ftip, "%s\n\r","" ); + fprintf( ftport, "%i\n\r",0 ); + + files('c'); + + send_msg("\n\rErased configurations set!\n\r"); + break; + } + } +} +*/ \ No newline at end of file