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