![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
debugs removidos - ligacao em pares ok
Dependencies: EthernetInterface NTPClient mbed-rtos mbed
Fork of header_main by
Diff: utils.cpp
- Revision:
- 0:4d17cd9c8f9d
- Child:
- 2:93bec7313ccc
diff -r 000000000000 -r 4d17cd9c8f9d utils.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils.cpp Tue Sep 09 20:01:24 2014 +0000 @@ -0,0 +1,144 @@ +#include "utils.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; +} + +void __init_eth__( EthernetInterface * eth ){ + eth->init( __MY_IP__, "255.255.255.0", "192.168.120.1" ); + eth->connect(); + // pensar em o que acontece e como sinalizar que nao consegui conectar na web + // aparente so da time out, so que algo vai travar quando eu tentar usar a conexao +} + +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; + for( register int i = 0; i < v_cb->size(); i++ ){ + cb = (Call_Box * )v_cb->get_element( i ); + if( cb->is_timeout() ){ + cb->reset_elapsed_time(); + cb->registry(); + /*if( cb->reconfigure_timeout() == 0x00 ){ + v_cb->remove_element( i ); + cb->unregistry(); + delete( cb ); + }else{ + __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, + ( char * )data, __REGISTRY__, __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 ) 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_timeout() || 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 ); + } + delete( call ); + } + } +} + +void build_telemetry_report( int ext, int port, char * data ){ + // construir o pacote de telemetria aqui + // decidir se isso sera escrito em disco e de tempos em tempos enviado para o servidor ... + // ou se sera enviado toda vez que determinado cb se registrar ... +} + +void __send_to_cb__( uint8_t * buffer ){ + xmemcpy( TXBuffer, buffer, DATA_SIZE ); + send2callboxes(); +} \ No newline at end of file