![](/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@133:3d136f5ffd66, 2015-09-01 (annotated)
- Committer:
- klauss
- Date:
- Tue Sep 01 19:33:11 2015 +0000
- Revision:
- 133:3d136f5ffd66
- Parent:
- 132:05cd37f7e007
- Child:
- 134:395678c651d8
Implementando a fun??o que pareia os pings de mestre e escravo, contemplando do bug da radio. Vers?o com o comando de teste.
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 | 132:05cd37f7e007 | 9 | //fixme |
klauss | 132:05cd37f7e007 | 10 | // if ( v_cb == NULL ) return NULL; |
klauss | 74:81c47fff88a5 | 11 | Call_Box * cb = NULL; |
klauss | 126:1f90756250fb | 12 | for ( register int i = 0; i < v_cb -> size (); i++ ) |
klauss | 119:ee6a53069455 | 13 | { |
klauss | 126:1f90756250fb | 14 | cb = ( Call_Box * ) v_cb -> get_element ( i ); |
klauss | 126:1f90756250fb | 15 | if ( cb not_eq NULL ) |
klauss | 119:ee6a53069455 | 16 | { |
klauss | 126:1f90756250fb | 17 | if ( cb -> get_ext () == ext ) return ( cb ); |
klauss | 119:ee6a53069455 | 18 | } |
klauss | 74:81c47fff88a5 | 19 | } |
klauss | 126:1f90756250fb | 20 | return ( NULL ); |
klauss | 74:81c47fff88a5 | 21 | } |
klauss | 74:81c47fff88a5 | 22 | |
klauss | 119:ee6a53069455 | 23 | int refresh ( Vector * v_cb ) |
klauss | 119:ee6a53069455 | 24 | { |
klauss | 133:3d136f5ffd66 | 25 | if ( v_cb not_eq NULL ) |
klauss | 119:ee6a53069455 | 26 | { |
klauss | 132:05cd37f7e007 | 27 | static int list_index = 0; |
klauss | 113:db67ae00550e | 28 | |
klauss | 126:1f90756250fb | 29 | if ( v_cb -> size () > 0 ) |
klauss | 119:ee6a53069455 | 30 | { |
klauss | 126:1f90756250fb | 31 | static uint8_t data [ CB_BUFFER_SIZE ], write_buffer [ CB_BUFFER_SIZE ]; |
klauss | 116:39a41ebb675c | 32 | |
klauss | 119:ee6a53069455 | 33 | led4 = 0; |
klauss | 126:1f90756250fb | 34 | |
klauss | 132:05cd37f7e007 | 35 | if ( list_index >= v_cb -> size () ) list_index = 0; |
klauss | 119:ee6a53069455 | 36 | |
klauss | 132:05cd37f7e007 | 37 | Call_Box * cb = ( Call_Box * ) v_cb -> get_element ( list_index++ ); |
klauss | 119:ee6a53069455 | 38 | |
klauss | 126:1f90756250fb | 39 | if ( ( cb not_eq NULL ) and ( cb -> get_status () == cb_idle ) ) |
klauss | 121:ee02790d00b7 | 40 | { |
klauss | 132:05cd37f7e007 | 41 | int local_ext = cb -> get_ext (); |
klauss | 133:3d136f5ffd66 | 42 | |
klauss | 133:3d136f5ffd66 | 43 | if ( dont_refresh_5000 ) if ( local_ext == 5000 ) return 5000; |
klauss | 133:3d136f5ffd66 | 44 | if ( dont_refresh_5001 ) if ( local_ext == 5001 ) return 5001; |
klauss | 133:3d136f5ffd66 | 45 | |
klauss | 132:05cd37f7e007 | 46 | if ( local_ext % 2 == 1 ) |
klauss | 126:1f90756250fb | 47 | { |
klauss | 132:05cd37f7e007 | 48 | // if ( ! list_exception_ext -> find_value ( local_ext ) ) |
klauss | 132:05cd37f7e007 | 49 | { |
klauss | 133:3d136f5ffd66 | 50 | // checking master status |
klauss | 132:05cd37f7e007 | 51 | Call_Box * cb_master = find_CB ( v_cb, local_ext - 1 ); |
klauss | 132:05cd37f7e007 | 52 | |
klauss | 133:3d136f5ffd66 | 53 | if ( cb_master not_eq NULL ) |
klauss | 133:3d136f5ffd66 | 54 | { |
klauss | 133:3d136f5ffd66 | 55 | if ( cb_master -> get_status () not_eq cb_idle ) return ( 0x20 ); |
klauss | 133:3d136f5ffd66 | 56 | |
klauss | 133:3d136f5ffd66 | 57 | else if ( cb_master -> get_timer () >= cm -> get_acceptable_delay () ) return ( 0x40 ); |
klauss | 133:3d136f5ffd66 | 58 | } |
klauss | 132:05cd37f7e007 | 59 | } |
klauss | 119:ee6a53069455 | 60 | } |
klauss | 126:1f90756250fb | 61 | else |
klauss | 126:1f90756250fb | 62 | { |
klauss | 132:05cd37f7e007 | 63 | // if ( ! list_exception_ext -> find_value ( local_ext ) ) |
klauss | 132:05cd37f7e007 | 64 | { |
klauss | 132:05cd37f7e007 | 65 | // checking slave status |
klauss | 132:05cd37f7e007 | 66 | Call_Box * cb_slave = find_CB ( v_cb, local_ext + 1 ); |
klauss | 119:ee6a53069455 | 67 | |
klauss | 133:3d136f5ffd66 | 68 | if ( cb_slave not_eq NULL ) |
klauss | 133:3d136f5ffd66 | 69 | { |
klauss | 133:3d136f5ffd66 | 70 | if ( cb_slave -> get_status () not_eq cb_idle ) return ( 0x21 ); |
klauss | 133:3d136f5ffd66 | 71 | |
klauss | 133:3d136f5ffd66 | 72 | else if ( cb_slave -> get_timer () >= cm -> get_acceptable_delay () ) return ( 0x41 ); |
klauss | 133:3d136f5ffd66 | 73 | } |
klauss | 132:05cd37f7e007 | 74 | } |
klauss | 132:05cd37f7e007 | 75 | } |
klauss | 133:3d136f5ffd66 | 76 | |
klauss | 132:05cd37f7e007 | 77 | /* |
klauss | 132:05cd37f7e007 | 78 | condição adicionada para minimizar o comportamento do cbx perder o tx, |
klauss | 132:05cd37f7e007 | 79 | pacotes vindo shiftados pra cabeceira, e não perdendo o rx, portando |
klauss | 132:05cd37f7e007 | 80 | o cbx não resetava sozinho, idéia, deixar ele rebotar pelo wdt dele. |
klauss | 132:05cd37f7e007 | 81 | */ |
klauss | 133:3d136f5ffd66 | 82 | if ( ( cb -> get_timer () < cm -> get_acceptable_delay () ) and ( cb -> get_overflow_times () == 0 ) ) |
klauss | 132:05cd37f7e007 | 83 | { |
klauss | 132:05cd37f7e007 | 84 | send2callboxes( build_cb_package( local_ext, cb->get_port(), REGISTRY, |
klauss | 132:05cd37f7e007 | 85 | ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) ); |
klauss | 132:05cd37f7e007 | 86 | |
klauss | 132:05cd37f7e007 | 87 | if( debug_refresh ) vz_printf ("[%d] Refresh", local_ext ); |
klauss | 132:05cd37f7e007 | 88 | } else { |
klauss | 132:05cd37f7e007 | 89 | if ( debug_dont_refresh ) vz_debug ("Nao pedindo registro para o [%d]( %d, %d )", |
klauss | 132:05cd37f7e007 | 90 | local_ext, cb -> get_timer (), cb -> get_overflow_times () ); |
klauss | 132:05cd37f7e007 | 91 | } |
klauss | 119:ee6a53069455 | 92 | return( 0 ); |
klauss | 119:ee6a53069455 | 93 | }else return( -5 ); |
klauss | 119:ee6a53069455 | 94 | }else return( -3 ); |
klauss | 89:0fe315117b00 | 95 | }else return( -1 ); |
klauss | 80:61d61c9eb75c | 96 | } |
klauss | 80:61d61c9eb75c | 97 | |
klauss | 126:1f90756250fb | 98 | void wake_all_up ( Vector * v_cb ) |
klauss | 119:ee6a53069455 | 99 | { |
klauss | 119:ee6a53069455 | 100 | /*** |
klauss | 119:ee6a53069455 | 101 | [ Principio ] |
klauss | 119:ee6a53069455 | 102 | -- Procurar por CBx que constem sem seus pares registrados na lógica. |
klauss | 119:ee6a53069455 | 103 | -- Mandar uma mensagem do tipo prompt-ping para este elemento |
klauss | 119:ee6a53069455 | 104 | |
klauss | 119:ee6a53069455 | 105 | -- Fluxo -- |
klauss | 119:ee6a53069455 | 106 | - Verificar se possui uma lista ordenada de CBx atualizada |
klauss | 119:ee6a53069455 | 107 | - Caso nao possua, gerar esta lista. |
klauss | 119:ee6a53069455 | 108 | - Para cada iteraçao |
klauss | 119:ee6a53069455 | 109 | - Verifica se o CBx é master ou slave |
klauss | 119:ee6a53069455 | 110 | - Verifica se o par deste esta registrado |
klauss | 119:ee6a53069455 | 111 | - Caso contratio |
klauss | 119:ee6a53069455 | 112 | - Verifica se o CBx encontrado esta em ligaçao |
klauss | 119:ee6a53069455 | 113 | - Caso nao esteja, encaminha mensagem de ping para o CBx ausente da dupla. |
klauss | 119:ee6a53069455 | 114 | ***/ |
klauss | 119:ee6a53069455 | 115 | |
klauss | 119:ee6a53069455 | 116 | uint8_t data[ CB_BUFFER_SIZE ], write_buffer[ CB_BUFFER_SIZE ]; |
klauss | 126:1f90756250fb | 117 | if( ( v_cb not_eq NULL ) ) |
klauss | 126:1f90756250fb | 118 | { |
klauss | 119:ee6a53069455 | 119 | static int ext_list[ MAX_CB_IN_A_BRANCH + 1 ]; |
klauss | 119:ee6a53069455 | 120 | static uint8_t last_size = 0; |
klauss | 117:e9facba9db27 | 121 | uint8_t size = v_cb->size(); |
klauss | 119:ee6a53069455 | 122 | static int wake_all_up_index = 0; |
klauss | 116:39a41ebb675c | 123 | |
klauss | 126:1f90756250fb | 124 | if( last_size not_eq size ) |
klauss | 119:ee6a53069455 | 125 | { |
klauss | 78:1353744f01e1 | 126 | for( register int i = 0; i < size; i++ ){ |
klauss | 78:1353744f01e1 | 127 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 78:1353744f01e1 | 128 | } |
klauss | 78:1353744f01e1 | 129 | |
klauss | 124:c1b6c893e1c3 | 130 | qsort( ext_list, v_cb->size(), sizeof( int ), wake_comp ); |
klauss | 119:ee6a53069455 | 131 | } |
klauss | 119:ee6a53069455 | 132 | |
klauss | 119:ee6a53069455 | 133 | if( size > 0 ) |
klauss | 119:ee6a53069455 | 134 | { |
klauss | 119:ee6a53069455 | 135 | // testa se eh o master |
klauss | 119:ee6a53069455 | 136 | if( ( ext_list[ wake_all_up_index ] % 2 ) == 0 ) |
klauss | 119:ee6a53069455 | 137 | { |
klauss | 119:ee6a53069455 | 138 | //testa se o slave nao esta registrado |
klauss | 126:1f90756250fb | 139 | 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 | 140 | { |
klauss | 119:ee6a53069455 | 141 | strcpy( ( char * )data, "ping\r" ); |
klauss | 121:ee02790d00b7 | 142 | Call_Box * cb = find_CB( v_cb, ext_list[ wake_all_up_index ] ); |
klauss | 119:ee6a53069455 | 143 | |
klauss | 119:ee6a53069455 | 144 | // verifica se o master esta em chamada. |
klauss | 126:1f90756250fb | 145 | if ( ( cb not_eq NULL ) and ( cb -> get_status () == cb_idle ) ) |
klauss | 126:1f90756250fb | 146 | { |
klauss | 119:ee6a53069455 | 147 | send2callboxes( build_cb_package( ( cb->get_ext() + 1 ), ( cb->get_port() + 1 ), PROMPT, |
klauss | 126:1f90756250fb | 148 | ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) ); |
klauss | 119:ee6a53069455 | 149 | |
klauss | 132:05cd37f7e007 | 150 | if( debug_wake ) vz_printf ("%d without %d - ping sent to %d", |
klauss | 119:ee6a53069455 | 151 | ext_list[ wake_all_up_index ], ext_list[ wake_all_up_index ] + 1, cb->get_ext() + 1 ); |
klauss | 119:ee6a53069455 | 152 | } |
klauss | 119:ee6a53069455 | 153 | } |
klauss | 117:e9facba9db27 | 154 | |
klauss | 119:ee6a53069455 | 155 | }else{ |
klauss | 126:1f90756250fb | 156 | 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 | 157 | { |
klauss | 119:ee6a53069455 | 158 | strcpy( ( char * )data, "ping\r" ); |
klauss | 121:ee02790d00b7 | 159 | Call_Box * cb = find_CB( v_cb, ( ext_list[ wake_all_up_index ] ) ); |
klauss | 119:ee6a53069455 | 160 | |
klauss | 119:ee6a53069455 | 161 | // verifica se o slave esta em chamada. |
klauss | 126:1f90756250fb | 162 | if ( ( cb not_eq NULL ) and ( cb -> get_status () == cb_idle ) ) |
klauss | 119:ee6a53069455 | 163 | { |
klauss | 119:ee6a53069455 | 164 | send2callboxes( build_cb_package( ( cb->get_ext() - 1 ), ( cb->get_port() - 1 ), PROMPT, |
klauss | 126:1f90756250fb | 165 | ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) ); |
klauss | 119:ee6a53069455 | 166 | |
klauss | 132:05cd37f7e007 | 167 | if ( debug_wake ) vz_printf ("%d without %d - ping sent to %d", |
klauss | 126:1f90756250fb | 168 | ext_list [ wake_all_up_index ], ext_list [ wake_all_up_index ] - 1, cb -> get_ext () - 1 ); |
klauss | 78:1353744f01e1 | 169 | } |
klauss | 74:81c47fff88a5 | 170 | } |
klauss | 74:81c47fff88a5 | 171 | } |
klauss | 74:81c47fff88a5 | 172 | } |
klauss | 126:1f90756250fb | 173 | if ( ++wake_all_up_index >= size ) wake_all_up_index = 0; |
klauss | 74:81c47fff88a5 | 174 | } |
klauss | 74:81c47fff88a5 | 175 | } |