![](/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
utils.cpp
- Committer:
- klauss
- Date:
- 2014-10-07
- Revision:
- 46:a670f187a704
- Parent:
- 44:cc4996469404
- Child:
- 48:195c97f12e8e
File content as of revision 46:a670f187a704:
#include "utils.h" #include "prompt.h" void reverse( char str[], int length ){ int start = 0; int end = length -1; while (start < end){ swap(*(str+start), *(str+end)); start++; end--; } } char* itoa(int num, char* str, int base){ int i = 0; bool isNegative = false; if( num == 0 ){ str[i++] = '0'; str[i] = '\0'; return str; } if( num < 0 && base == 10 ){ isNegative = true; num = -num; } while( num != 0 ){ int rem = num % base; str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; num = num/base; } if( isNegative ) str[i++] = '-'; str[i] = '\0'; // Append string terminator reverse( str, i ); return( str ); } void reset_leds( void ){ led1 = led2 = 1; led3 = led4 = 0; } int __init_eth__( EthernetInterface * eth ){ static bool initialized = false; char buff_ip[ 16 ]; char buff_msk[ 16 ]; char buff_gtw[ 16 ]; 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 ) 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/mygate.txt", "r"); if( fp == NULL ){ if( debug_file ) debug_msg("Failed to open /qspi/mygate.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/mygate.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( __MY_IP__, __MY_MSK__, __MY_GTW__ ); initialized = true; } return eth->connect(); } Call_Box * __find_CB__( Vector * v_cb, int ext ){ Call_Box * cb = NULL; for( register int i = 0; i < v_cb->size(); i++ ){ cb = ( Call_Box * )v_cb->get_element( i ); if( cb->get_ext() == ext ) return( cb ); } return( NULL ); } VZ_call * __find_Call__( Vector * v_call, int ext ){ VZ_call * call = NULL; for( register int i = 0; i < v_call->size(); i++ ){ call = ( VZ_call * )v_call->get_element( i ); if( call->get_cb_ext() == ext ) return( call ); } return( NULL ); } void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ Call_Box * cb = NULL; if( v_cb && data && write_buffer ){ for( register int i = 0; i < v_cb->size(); i++ ){ cb = (Call_Box * )v_cb->get_element( i ); if( cb->is_timeout() ){ if( cb->reconfigure_timeout() == 0x00 ){ if( debug_alive ) debug_msg("( ext %d removed )", cb->get_ext() ); //cb->unregistry(); v_cb->remove_element( i ); delete( cb ); break; }else{ if( debug_alive ) debug_msg("Ping Cbx %d", cb->get_ext() ); cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); cb->registry(); } } } } } int sip_manager( Vector * v_cb, Vector * v_call, uint8_t * write_buffer ){ static int index = 0; if( v_cb && write_buffer ){ if( v_cb->size() > 0 ){ if( index >= v_cb->size() ) index = 0; }else return 0; Call_Box * cb = (Call_Box * )v_cb->get_element( index ); /* Retorna = 0 :: ok < 0 :: tive problemas > 0 :: devo remover essa call do vetor de calls */ int returned_value = 0; if( cb->status == cb_on_call || cb->status == cb_idle ){ //debug_msg(""); returned_value = cb->listen_SIP_server(); } index++; return( returned_value ); } led1 = !led1; return( -1 ); } /* remove calls por timeout */ void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ for( register int i = 0; i < v_call->size(); i++ ){ VZ_call * call = ( VZ_call * )v_call->get_element( i ); if( call->is_timetofinish() ){ v_call->remove_element( i ); Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); if( cb ){ cb->status = cb_idle; ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); data[ __TIMESLICE_PLACE__ ] = 0x00; cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); cb->send_bye(); set_status( cb->sip->status, sip_idle ); cb->re_start_timer(); } delete( call ); } } } void build_telemetry_report( int ext, int port, char * data ){ pc.printf("Telemetry pkg from ( %i, %i ) ::\n\r", ext, port ); for( register int i = 0; i < __TELEMETRY_SIZE__; i++ ) pc.printf("%3i ", data[ i ] ); pc.printf("\n\r"); } void __send_to_cb__( uint8_t * buffer ){ xmemcpy( TXBuffer, buffer, DATA_SIZE ); send2callboxes(); }