![](/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@86:bf7b0d4c3232, 2015-01-05 (annotated)
- Committer:
- klauss
- Date:
- Mon Jan 05 12:43:12 2015 +0000
- Revision:
- 86:bf7b0d4c3232
- Parent:
- 81:3656f00ab3db
- Child:
- 89:0fe315117b00
rx buffer personal config
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 | 78:1353744f01e1 | 5 | |
klauss | 74:81c47fff88a5 | 6 | Call_Box * __find_CB__( Vector * v_cb, int ext ){ |
klauss | 74:81c47fff88a5 | 7 | Call_Box * cb = NULL; |
klauss | 74:81c47fff88a5 | 8 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 74:81c47fff88a5 | 9 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 74:81c47fff88a5 | 10 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 74:81c47fff88a5 | 11 | } |
klauss | 74:81c47fff88a5 | 12 | return( NULL ); |
klauss | 74:81c47fff88a5 | 13 | } |
klauss | 74:81c47fff88a5 | 14 | |
klauss | 80:61d61c9eb75c | 15 | int refresh( Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Call_Box * cb ){ |
klauss | 80:61d61c9eb75c | 16 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 81:3656f00ab3db | 17 | static int lst_index = 0; |
klauss | 81:3656f00ab3db | 18 | |
klauss | 81:3656f00ab3db | 19 | //eu nao deveria precisar disso |
klauss | 80:61d61c9eb75c | 20 | if( cb != NULL ){ |
klauss | 80:61d61c9eb75c | 21 | timer_refresh.reset(); |
klauss | 80:61d61c9eb75c | 22 | |
klauss | 81:3656f00ab3db | 23 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 81:3656f00ab3db | 24 | |
klauss | 80:61d61c9eb75c | 25 | send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 80:61d61c9eb75c | 26 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 80:61d61c9eb75c | 27 | |
klauss | 81:3656f00ab3db | 28 | if( debug_refresh ) send_msg("Refresh specific %d", cb->get_ext() ); |
klauss | 81:3656f00ab3db | 29 | |
klauss | 80:61d61c9eb75c | 30 | return( 0x10 ); |
klauss | 81:3656f00ab3db | 31 | //}else if( timer_refresh.read() > 1 ){ |
klauss | 81:3656f00ab3db | 32 | }else if( timer_refresh.read() > 1 ){ |
klauss | 80:61d61c9eb75c | 33 | timer_refresh.reset(); |
klauss | 80:61d61c9eb75c | 34 | if( v_cb->size() > 0 ){ |
klauss | 81:3656f00ab3db | 35 | if( lst_index >= v_cb->size() ) lst_index = 0; |
klauss | 80:61d61c9eb75c | 36 | |
klauss | 80:61d61c9eb75c | 37 | Call_Box * cb = (Call_Box * )v_cb->get_element( lst_index++ ); |
klauss | 80:61d61c9eb75c | 38 | |
klauss | 86:bf7b0d4c3232 | 39 | //if( cb != NULL && cb->status != cb_on_call ){ |
klauss | 86:bf7b0d4c3232 | 40 | if( cb != NULL && cb->status == cb_idle ){ |
klauss | 81:3656f00ab3db | 41 | if( cb->get_ext() % 2 == 1 ){ |
klauss | 81:3656f00ab3db | 42 | Call_Box * cb_master = __find_CB__( v_cb, cb->get_ext() - 1 ); |
klauss | 81:3656f00ab3db | 43 | |
klauss | 86:bf7b0d4c3232 | 44 | if( cb_master != NULL && cb_master->status != cb_idle ) return( 0x20 ); |
klauss | 81:3656f00ab3db | 45 | } |
klauss | 81:3656f00ab3db | 46 | |
klauss | 80:61d61c9eb75c | 47 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 81:3656f00ab3db | 48 | |
klauss | 80:61d61c9eb75c | 49 | send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 80:61d61c9eb75c | 50 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 80:61d61c9eb75c | 51 | |
klauss | 81:3656f00ab3db | 52 | if( debug_refresh ) send_msg("Refresh %d", cb->get_ext() ); |
klauss | 81:3656f00ab3db | 53 | |
klauss | 80:61d61c9eb75c | 54 | return( 0 ); |
klauss | 80:61d61c9eb75c | 55 | }else return( -3 ); |
klauss | 80:61d61c9eb75c | 56 | }else return( 1 ); |
klauss | 80:61d61c9eb75c | 57 | }else return( -1 ); |
klauss | 80:61d61c9eb75c | 58 | }else return( -3 ); |
klauss | 80:61d61c9eb75c | 59 | } |
klauss | 80:61d61c9eb75c | 60 | |
klauss | 80:61d61c9eb75c | 61 | //FiXME se validar, nao preciso de tantos parametros ... |
klauss | 74:81c47fff88a5 | 62 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 80:61d61c9eb75c | 63 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 80:61d61c9eb75c | 64 | Call_Box * cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 80:61d61c9eb75c | 65 | if( cb->is_timeout() ){ |
klauss | 80:61d61c9eb75c | 66 | if( debug_aging ) debug_msg("sands for %d", cb->get_ext() ); |
klauss | 80:61d61c9eb75c | 67 | if( cb->has_next_try() == false ){ |
klauss | 80:61d61c9eb75c | 68 | if( debug_aging ) debug_msg( "time to really go ( ext %d removed )", cb->get_ext() ); |
klauss | 80:61d61c9eb75c | 69 | v_cb->remove_element( i ); |
klauss | 80:61d61c9eb75c | 70 | delete( cb ); |
klauss | 80:61d61c9eb75c | 71 | break; |
klauss | 81:3656f00ab3db | 72 | }//else{ |
klauss | 81:3656f00ab3db | 73 | //refresh( v_cb, data, write_buffer, cb ); |
klauss | 81:3656f00ab3db | 74 | //} |
klauss | 80:61d61c9eb75c | 75 | } |
klauss | 80:61d61c9eb75c | 76 | } |
klauss | 80:61d61c9eb75c | 77 | /* |
klauss | 78:1353744f01e1 | 78 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 78:1353744f01e1 | 79 | uint8_t enough = 0; |
klauss | 78:1353744f01e1 | 80 | |
klauss | 78:1353744f01e1 | 81 | static int lst_ext = 0; |
klauss | 78:1353744f01e1 | 82 | |
klauss | 78:1353744f01e1 | 83 | if( timer_aging.read_ms() > 200 ){ |
klauss | 78:1353744f01e1 | 84 | timer_aging.reset(); |
klauss | 80:61d61c9eb75c | 85 | |
klauss | 78:1353744f01e1 | 86 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 78:1353744f01e1 | 87 | Call_Box * cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 78:1353744f01e1 | 88 | if( cb->is_timeout() ){ |
klauss | 78:1353744f01e1 | 89 | if( cb->has_next_try() == false ){ |
klauss | 80:61d61c9eb75c | 90 | if( debug_aging ) debug_msg( "time to really go ( ext %d removed )", cb->get_ext() ); |
klauss | 78:1353744f01e1 | 91 | v_cb->remove_element( i ); |
klauss | 78:1353744f01e1 | 92 | delete( cb ); |
klauss | 78:1353744f01e1 | 93 | break; |
klauss | 78:1353744f01e1 | 94 | }else{ |
klauss | 80:61d61c9eb75c | 95 | if( lst_ext == max_ext ) lst_ext = 0; |
klauss | 78:1353744f01e1 | 96 | |
klauss | 78:1353744f01e1 | 97 | if( lst_ext == 0 ) lst_ext = cb->get_ext(); |
klauss | 78:1353744f01e1 | 98 | |
klauss | 80:61d61c9eb75c | 99 | if( cb->get_ext() < lst_ext ) continue; |
klauss | 78:1353744f01e1 | 100 | |
klauss | 80:61d61c9eb75c | 101 | lst_ext = cb->get_ext(); |
klauss | 78:1353744f01e1 | 102 | |
klauss | 78:1353744f01e1 | 103 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 78:1353744f01e1 | 104 | |
klauss | 78:1353744f01e1 | 105 | int8_t type = cb->get_next_aging_type(); |
klauss | 78:1353744f01e1 | 106 | |
klauss | 78:1353744f01e1 | 107 | uint8_t msg_type = 0x00; |
klauss | 78:1353744f01e1 | 108 | |
klauss | 78:1353744f01e1 | 109 | if( type == REQUEST_REGISTRY ){ |
klauss | 78:1353744f01e1 | 110 | msg_type = __REGISTRY__; |
klauss | 78:1353744f01e1 | 111 | if( debug_aging ) debug_msg("Registry request Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); |
klauss | 78:1353744f01e1 | 112 | }else if( type == REQUEST_PEND ){ |
klauss | 78:1353744f01e1 | 113 | msg_type = __PROMPT__; |
klauss | 78:1353744f01e1 | 114 | strcpy( ( char * )data, "pend\r\r\r\n" ); |
klauss | 78:1353744f01e1 | 115 | if( debug_aging ) debug_msg("Pend Request Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); |
klauss | 78:1353744f01e1 | 116 | }else if( ( type == REQUEST_PING ) ){ |
klauss | 78:1353744f01e1 | 117 | msg_type = __PROMPT__; |
klauss | 78:1353744f01e1 | 118 | strcpy( ( char * )data, "ping\r\r\r\n" ); |
klauss | 78:1353744f01e1 | 119 | if( debug_aging ) debug_msg("Ping on Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); |
klauss | 78:1353744f01e1 | 120 | } |
klauss | 78:1353744f01e1 | 121 | |
klauss | 78:1353744f01e1 | 122 | send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), msg_type, |
klauss | 80:61d61c9eb75c | 123 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 78:1353744f01e1 | 124 | return; |
klauss | 78:1353744f01e1 | 125 | } |
klauss | 78:1353744f01e1 | 126 | } |
klauss | 78:1353744f01e1 | 127 | } |
klauss | 78:1353744f01e1 | 128 | if( debug_aging ) if( enough > 1 ) debug_msg( " out( %d ) ", enough ); |
klauss | 78:1353744f01e1 | 129 | } |
klauss | 78:1353744f01e1 | 130 | } |
klauss | 80:61d61c9eb75c | 131 | */ |
klauss | 78:1353744f01e1 | 132 | } |
klauss | 78:1353744f01e1 | 133 | |
klauss | 78:1353744f01e1 | 134 | void wake_all_up( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 78:1353744f01e1 | 135 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 78:1353744f01e1 | 136 | int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; |
klauss | 78:1353744f01e1 | 137 | uint8_t size = v_cb->size(); |
klauss | 78:1353744f01e1 | 138 | |
klauss | 78:1353744f01e1 | 139 | bool once = true; |
klauss | 78:1353744f01e1 | 140 | |
klauss | 78:1353744f01e1 | 141 | if( size > 0 ){ |
klauss | 78:1353744f01e1 | 142 | for( register int i = 0; i < size; i++ ){ |
klauss | 78:1353744f01e1 | 143 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 78:1353744f01e1 | 144 | } |
klauss | 78:1353744f01e1 | 145 | |
klauss | 78:1353744f01e1 | 146 | qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); |
klauss | 78:1353744f01e1 | 147 | |
klauss | 78:1353744f01e1 | 148 | for( register int i = 0; i < size; i++ ){ |
klauss | 78:1353744f01e1 | 149 | // testa se eh o master |
klauss | 78:1353744f01e1 | 150 | if( ( ext_list[ i ] % 2 ) == 0 ){ |
klauss | 78:1353744f01e1 | 151 | //testa se o slave nao esta registrado |
klauss | 78:1353744f01e1 | 152 | if( ( ( i + 1 ) < size ) && ( ext_list[ i + 1 ] != ( ext_list[ i ] + 1 ) ) || ( size == 1 ) ){ |
klauss | 78:1353744f01e1 | 153 | if( debug_wake ) debug_msg("Mestre %d encontrado sem slave %d", ext_list[ i ], ext_list[ i ] + 1 ); |
klauss | 78:1353744f01e1 | 154 | if( once ){ |
klauss | 78:1353744f01e1 | 155 | strcpy( ( char * )data, "ping\r\r\r\n" ); |
klauss | 78:1353744f01e1 | 156 | Call_Box * cb = __find_CB__( v_cb, ext_list[ i ] ); |
klauss | 78:1353744f01e1 | 157 | if( cb != NULL ){ |
klauss | 78:1353744f01e1 | 158 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 78:1353744f01e1 | 159 | |
klauss | 78:1353744f01e1 | 160 | send2callboxes( __build_cb_package__( ( cb->get_ext() + 1 ), ( cb->get_port() + 1 ), __PROMPT__, |
klauss | 78:1353744f01e1 | 161 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 78:1353744f01e1 | 162 | |
klauss | 78:1353744f01e1 | 163 | if( debug_wake ) debug_msg("Slave %d pingado", cb->get_ext() + 1 ); |
klauss | 78:1353744f01e1 | 164 | } |
klauss | 78:1353744f01e1 | 165 | once = false; |
klauss | 78:1353744f01e1 | 166 | } |
klauss | 78:1353744f01e1 | 167 | } |
klauss | 74:81c47fff88a5 | 168 | }else{ |
klauss | 78:1353744f01e1 | 169 | if( ( ( i - 1 ) > 0 ) && ( ext_list[ i - 1 ] != ( ext_list[ i ] - 1 ) ) || ( size == 1 ) ){ |
klauss | 78:1353744f01e1 | 170 | if( debug_wake ) debug_msg("Escravo revolucionario %d encontrado sem master %d", ext_list[ i ], ext_list[ i ] - 1 ); |
klauss | 78:1353744f01e1 | 171 | if( once ){ |
klauss | 78:1353744f01e1 | 172 | strcpy( ( char * )data, "ping\r\r\r\n" ); |
klauss | 78:1353744f01e1 | 173 | Call_Box * cb = __find_CB__( v_cb, ( ext_list[ i ] ) ); |
klauss | 78:1353744f01e1 | 174 | if( cb != NULL ){ |
klauss | 78:1353744f01e1 | 175 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 78:1353744f01e1 | 176 | |
klauss | 78:1353744f01e1 | 177 | send2callboxes( __build_cb_package__( ( cb->get_ext() - 1 ), ( cb->get_port() - 1 ), __PROMPT__, |
klauss | 78:1353744f01e1 | 178 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 78:1353744f01e1 | 179 | |
klauss | 78:1353744f01e1 | 180 | if( debug_wake ) debug_msg("Master %d pingado", cb->get_ext() - 1 ); |
klauss | 78:1353744f01e1 | 181 | } |
klauss | 78:1353744f01e1 | 182 | once = false; |
klauss | 78:1353744f01e1 | 183 | } |
klauss | 78:1353744f01e1 | 184 | } |
klauss | 74:81c47fff88a5 | 185 | } |
klauss | 74:81c47fff88a5 | 186 | } |
klauss | 74:81c47fff88a5 | 187 | } |
klauss | 74:81c47fff88a5 | 188 | } |
klauss | 74:81c47fff88a5 | 189 | } |