![](/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
call_box_manager.cpp
- Committer:
- klauss
- Date:
- 2015-04-23
- Revision:
- 117:e9facba9db27
- Parent:
- 116:39a41ebb675c
- Child:
- 119:ee6a53069455
File content as of revision 117:e9facba9db27:
#include "call_box_manager.h" Timer timer_refresh; Timer timer_aging; Timer timer_sync_refresh; 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 ); } int refresh( Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Call_Box * cb ){ if( v_cb != NULL && data != NULL && write_buffer != NULL ){ static int lst_index = 0; if( cb != NULL ){ timer_refresh.reset(); //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_refresh ) send_msg("Refresh specific %d", cb->get_ext() ); return( 0x10 ); }else{ if( v_cb->size() > 0 ){ if( lst_index >= v_cb->size() ) lst_index = 0; Call_Box * cb = (Call_Box * )v_cb->get_element( lst_index++ ); if( cb != NULL && cb->status == cb_idle ){ if( cb->get_ext() % 2 == 1 ){ Call_Box * cb_master = __find_CB__( v_cb, cb->get_ext() - 1 ); if( cb_master != NULL && cb_master->status != cb_idle ) return( 0x20 ); } //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_refresh ) send_msg("Refresh %d", cb->get_ext() ); return( 0 ); }else return( -5 ); }else return( -3 ); } }else return( -1 ); } //FiXME se validar, nao preciso de tantos parametros ... void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ for( register int i = 0; i < v_cb->size(); i++ ){ Call_Box * cb = (Call_Box * )v_cb->get_element( i ); if( cb->is_timeout() ){ if( debug_aging ) debug_msg("sands for %d", cb->get_ext() ); if( cb->has_next_try() == false ){ if( debug_aging ) debug_msg( "time to really go ( ext %d removed )", cb->get_ext() ); v_cb->remove_element( i ); delete( cb ); break; } } } } /* A ideia desse cara aqui ... a cada x sec. ele vai passar pelos cbx registrados na logica, contidos em v_cb e verificar quais estao ausentes ( pares, masters, slaves ) e mandar um registry pra cada um deles ... um por ver ... mantendo uma lista com os que estao ainda off, e deveriam estar on, e atualizar essa lista ... por exemplo a cada 3 sec. */ void wake_all_up( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ if( v_cb != NULL && data != NULL && write_buffer != NULL ){ static int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; uint8_t size = v_cb->size(); bool once = true; if( size > 0 ){ for( register int i = 0; i < size; i++ ){ ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); } qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); for( register int i = 0; i < size; i++ ){ // testa se eh o master if( ( ext_list[ i ] % 2 ) == 0 ){ //testa se o slave nao esta registrado //FIXME resolver o que acontece com o ultimo carinha == master, provavelmente fazendo um || ( i == size ) if( ( ( i + 1 ) < size ) && ( ext_list[ i + 1 ] != ( ext_list[ i ] + 1 ) ) || ( size == 1 ) ){ if( debug_wake ) debug_msg("Mestre %d encontrado sem slave %d", ext_list[ i ], ext_list[ i ] + 1 ); if( once ){ strcpy( ( char * )data, "ping\r\r\r\n" ); Call_Box * cb = __find_CB__( v_cb, ext_list[ i ] ); if( cb != NULL ){ //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); send2callboxes( __build_cb_package__( ( cb->get_ext() + 1 ), ( cb->get_port() + 1 ), __PROMPT__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_wake ) debug_msg("Slave %d pingado", cb->get_ext() + 1 ); } once = false; } } }else{ if( ( ( i - 1 ) > 0 ) && ( ext_list[ i - 1 ] != ( ext_list[ i ] - 1 ) ) || ( size == 1 ) ){ if( debug_wake ) debug_msg("Escravo revolucionario %d encontrado sem master %d", ext_list[ i ], ext_list[ i ] - 1 ); if( once ){ strcpy( ( char * )data, "ping\r\r\r\n" ); Call_Box * cb = __find_CB__( v_cb, ( ext_list[ i ] ) ); if( cb != NULL ){ //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); send2callboxes( __build_cb_package__( ( cb->get_ext() - 1 ), ( cb->get_port() - 1 ), __PROMPT__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_wake ) debug_msg("Master %d pingado", cb->get_ext() - 1 ); } once = false; } } } } } } }