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