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