![](/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@105:a930035b6556, 2015-03-10 (annotated)
- Committer:
- klauss
- Date:
- Tue Mar 10 18:10:57 2015 +0000
- Revision:
- 105:a930035b6556
- Parent:
- 104:62646ef786a3
- Child:
- 106:a34fcf9f0e02
sprint 0;
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 | 74:81c47fff88a5 | 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 | 80:61d61c9eb75c | 16 | int refresh( Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Call_Box * cb ){ |
klauss | 80:61d61c9eb75c | 17 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 81:3656f00ab3db | 18 | static int lst_index = 0; |
klauss | 81:3656f00ab3db | 19 | |
klauss | 80:61d61c9eb75c | 20 | if( cb != NULL ){ |
klauss | 80:61d61c9eb75c | 21 | timer_refresh.reset(); |
klauss | 80:61d61c9eb75c | 22 | |
klauss | 92:92df17f538a8 | 23 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 81:3656f00ab3db | 24 | |
klauss | 105:a930035b6556 | 25 | send2callboxes( build_cb_package( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 105:a930035b6556 | 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 | 89:0fe315117b00 | 31 | }else{ |
klauss | 80:61d61c9eb75c | 32 | if( v_cb->size() > 0 ){ |
klauss | 81:3656f00ab3db | 33 | if( lst_index >= v_cb->size() ) lst_index = 0; |
klauss | 80:61d61c9eb75c | 34 | |
klauss | 80:61d61c9eb75c | 35 | Call_Box * cb = (Call_Box * )v_cb->get_element( lst_index++ ); |
klauss | 80:61d61c9eb75c | 36 | |
klauss | 89:0fe315117b00 | 37 | if( cb != NULL && cb->status == cb_idle ){ |
klauss | 89:0fe315117b00 | 38 | |
klauss | 81:3656f00ab3db | 39 | if( cb->get_ext() % 2 == 1 ){ |
klauss | 81:3656f00ab3db | 40 | Call_Box * cb_master = __find_CB__( v_cb, cb->get_ext() - 1 ); |
klauss | 86:bf7b0d4c3232 | 41 | if( cb_master != NULL && cb_master->status != cb_idle ) return( 0x20 ); |
klauss | 81:3656f00ab3db | 42 | } |
klauss | 89:0fe315117b00 | 43 | |
klauss | 92:92df17f538a8 | 44 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 81:3656f00ab3db | 45 | |
klauss | 105:a930035b6556 | 46 | send2callboxes( build_cb_package( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 105:a930035b6556 | 47 | ( char * )data, cb->get_msg_id(), CB_BUFFER_SIZE - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 80:61d61c9eb75c | 48 | |
klauss | 81:3656f00ab3db | 49 | if( debug_refresh ) send_msg("Refresh %d", cb->get_ext() ); |
klauss | 81:3656f00ab3db | 50 | |
klauss | 80:61d61c9eb75c | 51 | return( 0 ); |
klauss | 89:0fe315117b00 | 52 | }else return( -5 ); |
klauss | 89:0fe315117b00 | 53 | }else return( -3 ); |
klauss | 89:0fe315117b00 | 54 | } |
klauss | 89:0fe315117b00 | 55 | }else return( -1 ); |
klauss | 80:61d61c9eb75c | 56 | } |
klauss | 80:61d61c9eb75c | 57 | |
klauss | 80:61d61c9eb75c | 58 | //FiXME se validar, nao preciso de tantos parametros ... |
klauss | 74:81c47fff88a5 | 59 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 80:61d61c9eb75c | 60 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 80:61d61c9eb75c | 61 | Call_Box * cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 80:61d61c9eb75c | 62 | if( cb->is_timeout() ){ |
klauss | 80:61d61c9eb75c | 63 | if( debug_aging ) debug_msg("sands for %d", cb->get_ext() ); |
klauss | 80:61d61c9eb75c | 64 | if( cb->has_next_try() == false ){ |
klauss | 80:61d61c9eb75c | 65 | if( debug_aging ) debug_msg( "time to really go ( ext %d removed )", cb->get_ext() ); |
klauss | 80:61d61c9eb75c | 66 | v_cb->remove_element( i ); |
klauss | 80:61d61c9eb75c | 67 | delete( cb ); |
klauss | 80:61d61c9eb75c | 68 | break; |
klauss | 89:0fe315117b00 | 69 | } |
klauss | 80:61d61c9eb75c | 70 | } |
klauss | 80:61d61c9eb75c | 71 | } |
klauss | 78:1353744f01e1 | 72 | } |
klauss | 78:1353744f01e1 | 73 | |
klauss | 78:1353744f01e1 | 74 | void wake_all_up( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 105:a930035b6556 | 75 | /*** |
klauss | 105:a930035b6556 | 76 | [ Principio ] |
klauss | 105:a930035b6556 | 77 | -- Procurar por CBx que constem sem seus pares registrados na lógica. |
klauss | 105:a930035b6556 | 78 | -- Mandar uma mensagem do tipo prompt-ping para este elemento |
klauss | 105:a930035b6556 | 79 | |
klauss | 105:a930035b6556 | 80 | -- Fluxo -- |
klauss | 105:a930035b6556 | 81 | - Verificar se possui uma lista ordenada de CBx atualizada |
klauss | 105:a930035b6556 | 82 | - Caso nao possua, gerar esta lista. |
klauss | 105:a930035b6556 | 83 | - Para cada iteraçao |
klauss | 105:a930035b6556 | 84 | - Verifica se o CBx é master ou slave |
klauss | 105:a930035b6556 | 85 | - Verifica se o par deste esta registrado |
klauss | 105:a930035b6556 | 86 | - Caso contratio |
klauss | 105:a930035b6556 | 87 | - Verifica se o CBx encontrado esta em ligaçao |
klauss | 105:a930035b6556 | 88 | - Caso nao esteja, encaminha mensagem de ping para o CBx ausente da dupla. |
klauss | 105:a930035b6556 | 89 | ***/ |
klauss | 105:a930035b6556 | 90 | |
klauss | 78:1353744f01e1 | 91 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 104:62646ef786a3 | 92 | static int ext_list[ __MAX_CB_IN_A_BRANCH__ + 1 ]; |
klauss | 104:62646ef786a3 | 93 | static uint8_t last_size = 0; |
klauss | 78:1353744f01e1 | 94 | uint8_t size = v_cb->size(); |
klauss | 104:62646ef786a3 | 95 | static int wake_all_up_index = 0; |
klauss | 78:1353744f01e1 | 96 | |
klauss | 104:62646ef786a3 | 97 | if( last_size != size ) |
klauss | 104:62646ef786a3 | 98 | { |
klauss | 78:1353744f01e1 | 99 | for( register int i = 0; i < size; i++ ){ |
klauss | 78:1353744f01e1 | 100 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 78:1353744f01e1 | 101 | } |
klauss | 78:1353744f01e1 | 102 | |
klauss | 78:1353744f01e1 | 103 | qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); |
klauss | 104:62646ef786a3 | 104 | } |
klauss | 104:62646ef786a3 | 105 | |
klauss | 104:62646ef786a3 | 106 | if( size > 0 ) |
klauss | 104:62646ef786a3 | 107 | { |
klauss | 104:62646ef786a3 | 108 | // testa se eh o master |
klauss | 104:62646ef786a3 | 109 | if( ( ext_list[ wake_all_up_index ] % 2 ) == 0 ) |
klauss | 104:62646ef786a3 | 110 | { |
klauss | 104:62646ef786a3 | 111 | //testa se o slave nao esta registrado |
klauss | 104:62646ef786a3 | 112 | if( ( size == 1 ) || ( ( wake_all_up_index + 1 ) == size ) || ( ext_list[ wake_all_up_index + 1 ] != ( ext_list[ wake_all_up_index ] + 1 ) ) ) |
klauss | 104:62646ef786a3 | 113 | { |
klauss | 105:a930035b6556 | 114 | strcpy( ( char * )data, "ping\r" ); |
klauss | 104:62646ef786a3 | 115 | Call_Box * cb = __find_CB__( v_cb, ext_list[ wake_all_up_index ] ); |
klauss | 105:a930035b6556 | 116 | |
klauss | 105:a930035b6556 | 117 | // verifica se o master esta em chamada. |
klauss | 105:a930035b6556 | 118 | if( cb != NULL && cb->status == cb_idle ) |
klauss | 104:62646ef786a3 | 119 | { |
klauss | 104:62646ef786a3 | 120 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 104:62646ef786a3 | 121 | |
klauss | 105:a930035b6556 | 122 | send2callboxes( build_cb_package( ( cb->get_ext() + 1 ), ( cb->get_port() + 1 ), __PROMPT__, |
klauss | 105:a930035b6556 | 123 | ( char * )data, cb->get_msg_id(), CB_BUFFER_SIZE - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 104:62646ef786a3 | 124 | |
klauss | 104:62646ef786a3 | 125 | if( debug_wake ) send_msg("%d without %d - ping sent to %d", |
klauss | 104:62646ef786a3 | 126 | ext_list[ wake_all_up_index ], ext_list[ wake_all_up_index ] + 1, cb->get_ext() + 1 ); |
klauss | 104:62646ef786a3 | 127 | } |
klauss | 104:62646ef786a3 | 128 | } |
klauss | 78:1353744f01e1 | 129 | |
klauss | 104:62646ef786a3 | 130 | }else{ |
klauss | 104:62646ef786a3 | 131 | if( ( size == 1 ) || ( wake_all_up_index == 0 ) || ( wake_all_up_index >= 1 ) && ( ext_list[ wake_all_up_index - 1 ] != ( ext_list[ wake_all_up_index ] - 1 ) ) ) |
klauss | 104:62646ef786a3 | 132 | { |
klauss | 105:a930035b6556 | 133 | strcpy( ( char * )data, "ping\r" ); |
klauss | 104:62646ef786a3 | 134 | Call_Box * cb = __find_CB__( v_cb, ( ext_list[ wake_all_up_index ] ) ); |
klauss | 105:a930035b6556 | 135 | |
klauss | 105:a930035b6556 | 136 | // verifica se o slave esta em chamada. |
klauss | 105:a930035b6556 | 137 | if( cb != NULL && cb->status == cb_idle ) |
klauss | 104:62646ef786a3 | 138 | { |
klauss | 104:62646ef786a3 | 139 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 104:62646ef786a3 | 140 | |
klauss | 105:a930035b6556 | 141 | send2callboxes( build_cb_package( ( cb->get_ext() - 1 ), ( cb->get_port() - 1 ), __PROMPT__, |
klauss | 105:a930035b6556 | 142 | ( char * )data, cb->get_msg_id(), CB_BUFFER_SIZE - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 104:62646ef786a3 | 143 | |
klauss | 104:62646ef786a3 | 144 | if( debug_wake ) send_msg("%d without %d - ping sent to %d", |
klauss | 104:62646ef786a3 | 145 | ext_list[ wake_all_up_index ], ext_list[ wake_all_up_index ] - 1, cb->get_ext() - 1 ); |
klauss | 78:1353744f01e1 | 146 | } |
klauss | 74:81c47fff88a5 | 147 | } |
klauss | 74:81c47fff88a5 | 148 | } |
klauss | 74:81c47fff88a5 | 149 | } |
klauss | 104:62646ef786a3 | 150 | if( ++wake_all_up_index >= size ) wake_all_up_index = 0; |
klauss | 74:81c47fff88a5 | 151 | } |
klauss | 74:81c47fff88a5 | 152 | } |