![](/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: prompt.cpp
- Revision:
- 103:e3cabfc2f533
- Parent:
- 102:98c7155e8bea
- Child:
- 104:62646ef786a3
diff -r 98c7155e8bea -r e3cabfc2f533 prompt.cpp --- a/prompt.cpp Thu Feb 19 14:31:42 2015 +0000 +++ b/prompt.cpp Thu Feb 19 18:04:33 2015 +0000 @@ -176,32 +176,6 @@ cat("/qspi/telemetry_port.txt"); - /* - if( debug_uart3 ) pc.printf("FW Server IP "); - if( from_eth ){ - snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "FW Server IP " ); - if( tcp_session ) while( !( tcp_client.send_all( debug_buf, strlen( debug_buf ) ) ) ); - - else if( udp_request ){ - udp_server.sendTo( udp_client, debug_buf, strlen( debug_buf ) ); - } - } - - cat("/qspi/fw_ip.txt"); - - if( debug_uart3 ) pc.printf("FW Server Port "); - if( from_eth ){ - snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "Fw Server Port " ); - if( tcp_session ) while( !( tcp_client.send_all( debug_buf, strlen( debug_buf ) ) ) ); - - else if( udp_request ){ - udp_server.sendTo( udp_client, debug_buf, strlen( debug_buf ) ); - } - } - - cat("/qspi/fw_port.txt"); - */ - if( debug_uart3 ) pc.printf("FW Server IP "); if( from_eth ){ snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "FW Server IP " ); @@ -265,7 +239,6 @@ if (type == 'c' ){ // close all files - //debug_msg(""); if( fip ) fclose( fip ); if( fmask ) fclose( fmask ); if( fgate ) fclose( fgate ); @@ -282,7 +255,6 @@ if( ftport ) fclose( ftport ); if( ffwip ) fclose( ffwip ); if( ffwport) fclose( ffwport ); - //debug_msg(""); } if (type == 'i' ){ @@ -296,119 +268,97 @@ set_ip( __MY_IP__ ); exists = false; } - //fclose( fip ); fserext = fopen("/qspi/myext.txt", "r"); if( fserext == NULL ){ set_header_ext( __MY_EXT__ ); exists = false; } - //fclose( fserext ); fport = fopen("/qspi/mysipport.txt", "r"); if (fport == NULL){ set_header_sip_port( __MY_PORT__ ); exists = false; } - //fclose( fport ); fsip = fopen("/qspi/serverip.txt", "r"); if (fsip == NULL){ set_server_ip( __SERVER_IP__ ); exists = false; } - //fclose( fsip ); fext = fopen("/qspi/peerext.txt", "r"); if ( fext == NULL ){ set_server_ext( __PEER_EXT__ ); exists = false; } - //fclose( fext ); fsport = fopen("/qspi/serverport.txt", "r"); if (fsport == NULL){ set_server_port( __SERVER_PORT__ ); exists = false; } - //fclose( fsport ); fmask = fopen("/qspi/mymask.txt", "r"); if (fmask == NULL){ set_mask( __MY_MSK__ ); exists = false; } - //fclose( fmask ); fgate = fopen("/qspi/mygateway.txt", "r"); if (fgate == NULL){ set_gateway( __MY_GTW__ ); exists = false; } - //fclose( fgate ); fudpport = fopen("/qspi/udpport.txt", "r" ); if( fudpport == NULL ){ set_udp_port_listener( UDP_PORT_LISTENER ); exists = false; } - //fclose( fudpport ); ftcpport = fopen("/qspi/tcpport.txt", "r" ); if( ftcpport == NULL ){ set_tcp_port_listener( TCP_PORT_LISTENER ); exists = false; } - //fclose( ftcpport ); ftip = fopen("/qspi/telemetry_ip.txt", "r" ); if( ftip == NULL ){ set_telemetry_ip( __TELEMETRY_SERVER_IP__ ); exists = false; } - //fclose( ftip ); ftport = fopen("/qspi/telemetry_port.txt", "r" ); if( ftport == NULL ){ set_telemetry_port( __TELEMETRY_SERVER_PORT__ ); exists = false; } - //fclose( ftport ); - //debug_msg(""); ffwip = fopen("/qspi/fw_ip.txt", "r" ); if( ffwip == NULL ){ set_fw_ip( __FW_SERVER_IP__ ); exists = false; } - //debug_msg(""); - //fclose( ffwip ); - //debug_msg(""); + ffwport = fopen("/qspi/fw_port.txt", "r" ); if( ffwport == NULL ){ set_fw_port( __FW_SERVER_PORT__ ); exists = false; } - //fclose( ffwport ); - //debug_msg(""); + fmax = fopen("/qspi/maxext.txt", "r" ); - //debug_msg(""); if( fmax == NULL ){ - //debug_msg(""); set_max_ext( MAX_EXT ); - //debug_msg(""); exists = false; - //debug_msg(""); } - //fclose( fmax ); - //debug_msg(""); + fmin = fopen("/qspi/minext.txt", "r" ); if( fmin == NULL ){ set_min_ext( MIN_EXT ); exists = false; } - //fclose( fmin ); - //debug_msg(""); + if( !exists ){ if( debug_uart3 ) pc.printf("\n\rDefault configurations set!\n\r"); if( from_eth ){ @@ -421,7 +371,6 @@ } } } - //debug_msg(""); reset_leds(); } @@ -447,7 +396,6 @@ if( type == 'w'){ // Create and write the default configs - set_ip( __MY_IP__ ); set_header_sip_port( __MY_PORT__ ); @@ -552,12 +500,13 @@ } bufptr = 0; for( uint8_t i = 0; i < DEBUGBUFSIZE; i++ ) debug_buf[ i ] = 0; - } + } } } -void init_fsystem(){ - // init file system and check if files exist +void init_fsystem() +{ + // init file system and check if files exist if (!qspifs.isformatted()) { qspifs.format(); if( debug_uart3 ) pc.printf("\n\rFile system configured!!\n\r"); @@ -567,19 +516,19 @@ send_msg("******* File System Ready *******"); } -void init_prompt_eth(){ +void init_prompt_eth() +{ tcp_server.bind( TCP_PORT_LISTENER ); tcp_server.listen(); - //tcp_server.set_blocking( false, 1 ); - //udp_server.set_blocking( false, 2 ); tcp_server.set_blocking( false, 0 ); udp_server.set_blocking( false, 0 ); - udp_server.bind( UDP_PORT_LISTENER ); + udp_server.bind( UDP_PORT_LISTENER ); if( debug_uart3 ) pc.printf("******* Prompt eth Ready *******\n\r"); } // main prompt process -char * prompt_process( char * msg_from_cb ){ +char * prompt_process( char * msg_from_cb ) +{ //FIXME dar flush na serial volatile char b = 0; static uint8_t bufret = 0; @@ -590,84 +539,64 @@ bool miss_match = true; char eth_buffer[ __PROMPT_ETH_BUFFER_SIZE__ ]; uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; - - if( flood_timeout.read() > 60 ){ + + if( flood_timeout.read() > 60 ) { pflood = 0; flood_timeout.stop(); flood_timeout.reset(); } - - if( tcp_timer.read() >= TCP_IDLE_MAX_TIME ){ - bool close_tcp = true; - //if( tcp_alive && ( tcp_timer.read() < TCP_ALIVE_IDLE_MAX_TIME ) ){ - // close_tcp = false; - //} - if( close_tcp ){ - tcp_alive = false; - tcp_timer.stop(); - tcp_timer.reset(); - tcp_session = false; - debug_buf = __debug_buf__; - bufptr = last_bufptr; - bufret = 0; - from_eth = false; - tcp_client.close(); - return( NULL ); - } - } - if( !tcp_server.accept( tcp_client ) ){ + + if( tcp_timer.read() >= TCP_IDLE_MAX_TIME ) { + tcp_alive = false; + tcp_timer.stop(); + tcp_timer.reset(); + tcp_session = false; + debug_buf = __debug_buf__; + bufptr = last_bufptr; + bufret = 0; + from_eth = false; + tcp_client.close(); + return( NULL ); + } + if( !tcp_server.accept( tcp_client ) ) { tcp_session = true; tcp_timer.start(); last_bufptr = bufptr; eth_buffer[ 0 ] = 0; bufret = 0; } - if( tcp_session ){ - //if( !( (int)tcp_timer.read_ms() % 100 ) ){ - //tcp_client.set_blocking( false, 1 ); - tcp_client.set_blocking( false, 0 ); - int n = tcp_client.receive( eth_buffer, sizeof( eth_buffer ) - 1 ); - if( n > 0 ){ - tcp_timer.reset(); - - if( ( n > 2 ) && ( eth_buffer[ n - 2 ] == '\r' ) ) eth_buffer[ n - 2 ] = 0; - - if( ( n > 1 ) && ( eth_buffer[ n - 1 ] == '\n' ) ) eth_buffer[ n - 1 ] = 0; - - if( ( n == 1 ) && eth_buffer[ 0 ] == 0x0A ) strcpy( eth_buffer, "\r" ); - - //if( eth_buffer[ 0 ] == '\r' || eth_buffer[ 1 ] == '\n' || eth_buffer[ 2 ] == 'r' || eth_buffer[ 2 ] == '\n' ) strcpy( eth_buffer, "\r" ); + if( tcp_session ) { + tcp_client.set_blocking( false, 0 ); + int n = tcp_client.receive( eth_buffer, sizeof( eth_buffer ) - 1 ); + if( n > 0 ) { + tcp_timer.reset(); + + if( ( n > 2 ) && ( eth_buffer[ n - 2 ] == '\r' ) ) eth_buffer[ n - 2 ] = 0; + + if( ( n > 1 ) && ( eth_buffer[ n - 1 ] == '\n' ) ) eth_buffer[ n - 1 ] = 0; - bufptr = strlen( eth_buffer ); - debug_buf = eth_buffer; - from_eth = true; - bufret = 1; - - if( !( strcmp( eth_buffer, "quit" ) ) ){ - tcp_timer.reset(); - debug_buf = __debug_buf__; - bufptr = last_bufptr; - from_eth = false; - tcp_session = false; - tcp_client.close(); - bufret = 0; - return( NULL ); - } - /*else if( !( strcmp( eth_buffer, "alive" ) ) ){ - char wake_msg[ 8 ]; - external_wdt = EXTERN_WDT_IDLE; - strcpy( wake_msg, "wakeup\n" ); - while( !( tcp_client.send_all( wake_msg, strlen( wake_msg ) ) ) ); - strcpy( eth_buffer, "0" ); - bufret = 0; - } - */ - }else{ + if( ( n == 1 ) && eth_buffer[ 0 ] == 0x0A ) strcpy( eth_buffer, "\r" ); + + bufptr = strlen( eth_buffer ); + debug_buf = eth_buffer; + from_eth = true; + bufret = 1; + + if( !( strcmp( eth_buffer, "quit" ) ) ) { + tcp_timer.reset(); debug_buf = __debug_buf__; bufptr = last_bufptr; from_eth = false; + tcp_session = false; + tcp_client.close(); + bufret = 0; + return( NULL ); } - //} + } else { + debug_buf = __debug_buf__; + bufptr = last_bufptr; + from_eth = false; + } } if( !from_eth ){ @@ -712,17 +641,14 @@ if( tcp_session ){ snprintf( eth_buffer, __PROMPT_ETH_BUFFER_SIZE__, "\r\nSilence sent to ext=%d port=%d", ext_count, port ); - //eth_buffer[ strlen( eth_buffer ) - 1 ] = '\0'; - tcp_client.send_all( eth_buffer, strlen( eth_buffer ) ); }else if( udp_request ){ snprintf( eth_buffer, __PROMPT_ETH_BUFFER_SIZE__, "\r\nSilence sent to ext=%d port=%d", ext_count, port ); - //eth_buffer[ strlen( eth_buffer ) - 1 ] = '\0'; - udp_server.sendTo( udp_client, eth_buffer, strlen( eth_buffer ) ); } flood_silence_timer.reset(); + // importante para o uso da funçao silence if( ++ext_count > end_ext ){ flood_silence = false; flood_silence_timer.stop(); @@ -750,7 +676,6 @@ if( debug_uart3 ) pc.printf("\n\rFlood ON\n\r"); if( from_eth ){ snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "Flood On\n\r" ); - //debug_buf[ strlen( debug_buf ) - 1 ] = '\0'; } } @@ -784,7 +709,6 @@ if( debug_uart3 ) pc.printf("\n\rFlood OFF\n\r"); if( from_eth ){ snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "Flood OFF\n\r" ); - //debug_buf[ strlen( debug_buf ) - 1 ] = '\0'; } pflood = 0; tflood.stop(); @@ -1023,7 +947,6 @@ if( from_eth ){ snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rhex :: dec :: Type \n\r0x00 :: 0 :: boot \n\r0x02 :: 2 :: registry \n\r0x04 :: 4 :: invite \n\r0x08 :: 8 :: audio \n\r0x10 :: 16 :: telemetry \n\r0x20 :: 32 :: cb_bye \n\r0x01 :: 1 :: prompt \n\r0x40 :: 64 :: flood \n\r" ); - //debug_buf[ strlen( debug_buf ) - 1 ] = '\0'; } } @@ -1034,7 +957,6 @@ if( from_eth ){ snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\r| E | E | P | P | C | C | T | [ Seq_num | Audio ] | 14[ Clock | Audio ] | [ TS | Audio ] | ... |\n\r \n\rE = Ext = Ramal\n\r \n\rP = Port = Porta\n\r \n\rC = Checksum\n\r \n\rT = Type = Tipo\n\r \n\rSeq_num = Sequence Number = Numero de sequencia\n\r \n\rClock = 14 bytes to sync\n\r \n\r... = demais __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__ bytes\n\r" ); - //debug_buf[ strlen( debug_buf ) - 1 ] = '\0'; } } @@ -1058,7 +980,6 @@ if( debug_uart3 ) pc.printf("\n\rFlood ON\n\r"); if( from_eth ){ snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "Flood On\n\r" ); - //debug_buf[ strlen( debug_buf ) - 1 ] = '\0'; } tflood.start(); tflood.reset(); @@ -1498,7 +1419,6 @@ else if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "promptcb ", 9 ) ){ miss_match = false; - //static uint8_t id_msg = 0x10; uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; int ext,port; char *split, *ref, *cmd; @@ -2116,17 +2036,6 @@ udp_server.sendTo( udp_client, debug_buf, strlen( debug_buf ) ); } } - /* - if( debug_uart3 ) pc.printf("status - lista resumida de cbx registrados na header\n\r"); - if( from_eth ){ - snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "showcb - lista resumida de cbx registrados na header\n\r" ); - if( tcp_session ) tcp_client.send_all( debug_buf, strlen( debug_buf ) ); - - else if( udp_request ){ - udp_server.sendTo( udp_client, debug_buf, strlen( debug_buf ) ); - } - } - */ if( debug_uart3 ) pc.printf("cks - exibe estatisticas de check sum\n\r"); if( from_eth ){ snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "cks - exibe estatisticas de check sum\n\r" );