![](/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@109:a5b8264ffbbc, 2015-04-07 (annotated)
- Committer:
- klauss
- Date:
- Tue Apr 07 14:27:44 2015 +0000
- Revision:
- 109:a5b8264ffbbc
- Parent:
- 108:18a3702650f3
- Child:
- 113:db67ae00550e
bl wip
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 74:81c47fff88a5 | 1 | #include "call_box_manager.h" |
klauss | 74:81c47fff88a5 | 2 | |
klauss | 80:61d61c9eb75c | 3 | Timer timer_refresh; |
klauss | 78:1353744f01e1 | 4 | Timer timer_aging; |
klauss | 89:0fe315117b00 | 5 | Timer timer_sync_refresh; |
klauss | 78:1353744f01e1 | 6 | |
klauss | 74:81c47fff88a5 | 7 | Call_Box * __find_CB__( Vector * v_cb, int ext ){ |
klauss | 74:81c47fff88a5 | 8 | Call_Box * cb = NULL; |
klauss | 74:81c47fff88a5 | 9 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 74:81c47fff88a5 | 10 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 109:a5b8264ffbbc | 11 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 74:81c47fff88a5 | 12 | } |
klauss | 74:81c47fff88a5 | 13 | return( NULL ); |
klauss | 74:81c47fff88a5 | 14 | } |
klauss | 74:81c47fff88a5 | 15 | |
klauss | 109:a5b8264ffbbc | 16 | int refresh( Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Call_Box * cb ){ |
klauss | 109:a5b8264ffbbc | 17 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 81:3656f00ab3db | 18 | static int lst_index = 0; |
klauss | 109:a5b8264ffbbc | 19 | |
klauss | 109:a5b8264ffbbc | 20 | if( cb != NULL ){ |
klauss | 109:a5b8264ffbbc | 21 | timer_refresh.reset(); |
klauss | 81:3656f00ab3db | 22 | |
klauss | 109:a5b8264ffbbc | 23 | //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 109:a5b8264ffbbc | 24 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 81:3656f00ab3db | 25 | |
klauss | 109:a5b8264ffbbc | 26 | send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 109:a5b8264ffbbc | 27 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 106:a34fcf9f0e02 | 28 | |
klauss | 109:a5b8264ffbbc | 29 | if( debug_refresh ) send_msg("Refresh specific %d", cb->get_ext() ); |
klauss | 106:a34fcf9f0e02 | 30 | |
klauss | 109:a5b8264ffbbc | 31 | return( 0x10 ); |
klauss | 109:a5b8264ffbbc | 32 | }else{ |
klauss | 109:a5b8264ffbbc | 33 | if( v_cb->size() > 0 ){ |
klauss | 109:a5b8264ffbbc | 34 | if( lst_index >= v_cb->size() ) lst_index = 0; |
klauss | 109:a5b8264ffbbc | 35 | |
klauss | 109:a5b8264ffbbc | 36 | Call_Box * cb = (Call_Box * )v_cb->get_element( lst_index++ ); |
klauss | 80:61d61c9eb75c | 37 | |
klauss | 109:a5b8264ffbbc | 38 | if( cb != NULL && cb->status == cb_idle ){ |
klauss | 109:a5b8264ffbbc | 39 | |
klauss | 109:a5b8264ffbbc | 40 | if( cb->get_ext() % 2 == 1 ){ |
klauss | 109:a5b8264ffbbc | 41 | Call_Box * cb_master = __find_CB__( v_cb, cb->get_ext() - 1 ); |
klauss | 109:a5b8264ffbbc | 42 | if( cb_master != NULL && cb_master->status != cb_idle ) return( 0x20 ); |
klauss | 109:a5b8264ffbbc | 43 | } |
klauss | 109:a5b8264ffbbc | 44 | |
klauss | 109:a5b8264ffbbc | 45 | //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 109:a5b8264ffbbc | 46 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 109:a5b8264ffbbc | 47 | |
klauss | 109:a5b8264ffbbc | 48 | send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 109:a5b8264ffbbc | 49 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 109:a5b8264ffbbc | 50 | |
klauss | 109:a5b8264ffbbc | 51 | if( debug_refresh ) send_msg("Refresh %d", cb->get_ext() ); |
klauss | 109:a5b8264ffbbc | 52 | |
klauss | 109:a5b8264ffbbc | 53 | return( 0 ); |
klauss | 109:a5b8264ffbbc | 54 | }else return( -5 ); |
klauss | 109:a5b8264ffbbc | 55 | }else return( -3 ); |
klauss | 109:a5b8264ffbbc | 56 | } |
klauss | 89:0fe315117b00 | 57 | }else return( -1 ); |
klauss | 80:61d61c9eb75c | 58 | } |
klauss | 80:61d61c9eb75c | 59 | |
klauss | 109:a5b8264ffbbc | 60 | //FiXME se validar, nao preciso de tantos parametros ... |
klauss | 74:81c47fff88a5 | 61 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 80:61d61c9eb75c | 62 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 80:61d61c9eb75c | 63 | Call_Box * cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 109:a5b8264ffbbc | 64 | if( cb->is_timeout() ){ |
klauss | 80:61d61c9eb75c | 65 | if( debug_aging ) debug_msg("sands for %d", cb->get_ext() ); |
klauss | 80:61d61c9eb75c | 66 | if( cb->has_next_try() == false ){ |
klauss | 80:61d61c9eb75c | 67 | if( debug_aging ) debug_msg( "time to really go ( ext %d removed )", cb->get_ext() ); |
klauss | 80:61d61c9eb75c | 68 | v_cb->remove_element( i ); |
klauss | 80:61d61c9eb75c | 69 | delete( cb ); |
klauss | 80:61d61c9eb75c | 70 | break; |
klauss | 89:0fe315117b00 | 71 | } |
klauss | 80:61d61c9eb75c | 72 | } |
klauss | 80:61d61c9eb75c | 73 | } |
klauss | 78:1353744f01e1 | 74 | } |
klauss | 78:1353744f01e1 | 75 | |
klauss | 109:a5b8264ffbbc | 76 | /* |
klauss | 109:a5b8264ffbbc | 77 | A ideia desse cara aqui ... a cada x sec. ele vai passar pelos cbx registrados na logica, |
klauss | 109:a5b8264ffbbc | 78 | contidos em v_cb e verificar quais estao ausentes ( pares, masters, slaves ) e mandar um |
klauss | 109:a5b8264ffbbc | 79 | registry pra cada um deles ... um por ver ... mantendo uma lista com os que estao ainda |
klauss | 109:a5b8264ffbbc | 80 | off, e deveriam estar on, e atualizar essa lista ... por exemplo a cada 3 sec. |
klauss | 109:a5b8264ffbbc | 81 | |
klauss | 109:a5b8264ffbbc | 82 | */ |
klauss | 109:a5b8264ffbbc | 83 | void wake_all_up( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 109:a5b8264ffbbc | 84 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 109:a5b8264ffbbc | 85 | static int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; |
klauss | 109:a5b8264ffbbc | 86 | uint8_t size = v_cb->size(); |
klauss | 105:a930035b6556 | 87 | |
klauss | 109:a5b8264ffbbc | 88 | bool once = true; |
klauss | 78:1353744f01e1 | 89 | |
klauss | 109:a5b8264ffbbc | 90 | if( size > 0 ){ |
klauss | 78:1353744f01e1 | 91 | for( register int i = 0; i < size; i++ ){ |
klauss | 78:1353744f01e1 | 92 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 78:1353744f01e1 | 93 | } |
klauss | 78:1353744f01e1 | 94 | |
klauss | 78:1353744f01e1 | 95 | qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); |
klauss | 109:a5b8264ffbbc | 96 | |
klauss | 109:a5b8264ffbbc | 97 | for( register int i = 0; i < size; i++ ){ |
klauss | 109:a5b8264ffbbc | 98 | // testa se eh o master |
klauss | 109:a5b8264ffbbc | 99 | if( ( ext_list[ i ] % 2 ) == 0 ){ |
klauss | 109:a5b8264ffbbc | 100 | //testa se o slave nao esta registrado |
klauss | 109:a5b8264ffbbc | 101 | //FIXME resolver o que acontece com o ultimo carinha == master, provavelmente fazendo um || ( i == size ) |
klauss | 109:a5b8264ffbbc | 102 | if( ( ( i + 1 ) < size ) && ( ext_list[ i + 1 ] != ( ext_list[ i ] + 1 ) ) || ( size == 1 ) ){ |
klauss | 109:a5b8264ffbbc | 103 | if( debug_wake ) debug_msg("Mestre %d encontrado sem slave %d", ext_list[ i ], ext_list[ i ] + 1 ); |
klauss | 109:a5b8264ffbbc | 104 | if( once ){ |
klauss | 109:a5b8264ffbbc | 105 | strcpy( ( char * )data, "ping\r\r\r\n" ); |
klauss | 109:a5b8264ffbbc | 106 | Call_Box * cb = __find_CB__( v_cb, ext_list[ i ] ); |
klauss | 109:a5b8264ffbbc | 107 | if( cb != NULL ){ |
klauss | 109:a5b8264ffbbc | 108 | //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 109:a5b8264ffbbc | 109 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 109:a5b8264ffbbc | 110 | |
klauss | 109:a5b8264ffbbc | 111 | send2callboxes( __build_cb_package__( ( cb->get_ext() + 1 ), ( cb->get_port() + 1 ), __PROMPT__, |
klauss | 109:a5b8264ffbbc | 112 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 109:a5b8264ffbbc | 113 | |
klauss | 109:a5b8264ffbbc | 114 | if( debug_wake ) debug_msg("Slave %d pingado", cb->get_ext() + 1 ); |
klauss | 109:a5b8264ffbbc | 115 | } |
klauss | 109:a5b8264ffbbc | 116 | once = false; |
klauss | 109:a5b8264ffbbc | 117 | } |
klauss | 104:62646ef786a3 | 118 | } |
klauss | 109:a5b8264ffbbc | 119 | }else{ |
klauss | 109:a5b8264ffbbc | 120 | if( ( ( i - 1 ) > 0 ) && ( ext_list[ i - 1 ] != ( ext_list[ i ] - 1 ) ) || ( size == 1 ) ){ |
klauss | 109:a5b8264ffbbc | 121 | if( debug_wake ) debug_msg("Escravo revolucionario %d encontrado sem master %d", ext_list[ i ], ext_list[ i ] - 1 ); |
klauss | 109:a5b8264ffbbc | 122 | if( once ){ |
klauss | 109:a5b8264ffbbc | 123 | strcpy( ( char * )data, "ping\r\r\r\n" ); |
klauss | 109:a5b8264ffbbc | 124 | Call_Box * cb = __find_CB__( v_cb, ( ext_list[ i ] ) ); |
klauss | 109:a5b8264ffbbc | 125 | if( cb != NULL ){ |
klauss | 109:a5b8264ffbbc | 126 | //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 109:a5b8264ffbbc | 127 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 78:1353744f01e1 | 128 | |
klauss | 109:a5b8264ffbbc | 129 | send2callboxes( __build_cb_package__( ( cb->get_ext() - 1 ), ( cb->get_port() - 1 ), __PROMPT__, |
klauss | 109:a5b8264ffbbc | 130 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 109:a5b8264ffbbc | 131 | |
klauss | 109:a5b8264ffbbc | 132 | if( debug_wake ) debug_msg("Master %d pingado", cb->get_ext() - 1 ); |
klauss | 109:a5b8264ffbbc | 133 | } |
klauss | 109:a5b8264ffbbc | 134 | once = false; |
klauss | 109:a5b8264ffbbc | 135 | } |
klauss | 78:1353744f01e1 | 136 | } |
klauss | 74:81c47fff88a5 | 137 | } |
klauss | 74:81c47fff88a5 | 138 | } |
klauss | 74:81c47fff88a5 | 139 | } |
klauss | 74:81c47fff88a5 | 140 | } |
klauss | 74:81c47fff88a5 | 141 | } |