Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
call_box_manager.cpp@137:32dd35a6dbc9, 2015-11-24 (annotated)
- Committer:
- klauss
- Date:
- Tue Nov 24 14:06:22 2015 +0000
- Revision:
- 137:32dd35a6dbc9
- Parent:
- 135:2f4290590e51
core source of the .bin (09/21/2015) in the field
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 | 135:2f4290590e51 | 9 | if ( v_cb == NULL ) return NULL; |
klauss | 135:2f4290590e51 | 10 | |
klauss | 126:1f90756250fb | 11 | for ( register int i = 0; i < v_cb -> size (); i++ ) |
klauss | 119:ee6a53069455 | 12 | { |
klauss | 135:2f4290590e51 | 13 | Call_Box * cb = NULL; |
klauss | 135:2f4290590e51 | 14 | |
klauss | 126:1f90756250fb | 15 | cb = ( Call_Box * ) v_cb -> get_element ( i ); |
klauss | 126:1f90756250fb | 16 | if ( cb not_eq NULL ) |
klauss | 119:ee6a53069455 | 17 | { |
klauss | 126:1f90756250fb | 18 | if ( cb -> get_ext () == ext ) return ( cb ); |
klauss | 119:ee6a53069455 | 19 | } |
klauss | 74:81c47fff88a5 | 20 | } |
klauss | 126:1f90756250fb | 21 | return ( NULL ); |
klauss | 74:81c47fff88a5 | 22 | } |
klauss | 74:81c47fff88a5 | 23 | |
klauss | 135:2f4290590e51 | 24 | int |
klauss | 135:2f4290590e51 | 25 | refresh ( Vector * v_cb ) |
klauss | 119:ee6a53069455 | 26 | { |
klauss | 133:3d136f5ffd66 | 27 | if ( v_cb not_eq NULL ) |
klauss | 135:2f4290590e51 | 28 | { |
klauss | 126:1f90756250fb | 29 | if ( v_cb -> size () > 0 ) |
klauss | 119:ee6a53069455 | 30 | { |
klauss | 135:2f4290590e51 | 31 | static int list_index = 0; |
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 | 135:2f4290590e51 | 43 | Call_Box * pair = cb -> get_pair_cbx (); |
klauss | 135:2f4290590e51 | 44 | |
klauss | 135:2f4290590e51 | 45 | if ( pair not_eq NULL ) |
klauss | 126:1f90756250fb | 46 | { |
klauss | 135:2f4290590e51 | 47 | if ( pair -> get_status () not_eq cb_idle ) return ( local_ext % 2 == 1 ) ? ( 0x21 ) : ( 0x20 ); |
klauss | 135:2f4290590e51 | 48 | |
klauss | 135:2f4290590e51 | 49 | else if ( ( not ( cm -> get_cbx_detach_mode () ) ) and ( pair -> get_timer () >= cm -> get_acceptable_delay () ) ) return ( local_ext % 2 == 1 ) ? ( 0x41 ) : ( 0x40 ); |
klauss | 132:05cd37f7e007 | 50 | } |
klauss | 133:3d136f5ffd66 | 51 | |
klauss | 133:3d136f5ffd66 | 52 | if ( ( cb -> get_timer () < cm -> get_acceptable_delay () ) and ( cb -> get_overflow_times () == 0 ) ) |
klauss | 132:05cd37f7e007 | 53 | { |
klauss | 135:2f4290590e51 | 54 | static uint8_t data [ CB_BUFFER_SIZE ], write_buffer [ CB_BUFFER_SIZE ]; |
klauss | 135:2f4290590e51 | 55 | |
klauss | 132:05cd37f7e007 | 56 | send2callboxes( build_cb_package( local_ext, cb->get_port(), REGISTRY, |
klauss | 132:05cd37f7e007 | 57 | ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) ); |
klauss | 132:05cd37f7e007 | 58 | |
klauss | 135:2f4290590e51 | 59 | if ( debug_refresh ) vz_printf ("[%d] Refresh", local_ext ); |
klauss | 132:05cd37f7e007 | 60 | } else { |
klauss | 132:05cd37f7e007 | 61 | if ( debug_dont_refresh ) vz_debug ("Nao pedindo registro para o [%d]( %d, %d )", |
klauss | 132:05cd37f7e007 | 62 | local_ext, cb -> get_timer (), cb -> get_overflow_times () ); |
klauss | 132:05cd37f7e007 | 63 | } |
klauss | 119:ee6a53069455 | 64 | return( 0 ); |
klauss | 119:ee6a53069455 | 65 | }else return( -5 ); |
klauss | 119:ee6a53069455 | 66 | }else return( -3 ); |
klauss | 89:0fe315117b00 | 67 | }else return( -1 ); |
klauss | 80:61d61c9eb75c | 68 | } |
klauss | 80:61d61c9eb75c | 69 | |
klauss | 126:1f90756250fb | 70 | void wake_all_up ( Vector * v_cb ) |
klauss | 119:ee6a53069455 | 71 | { |
klauss | 119:ee6a53069455 | 72 | /*** |
klauss | 119:ee6a53069455 | 73 | [ Principio ] |
klauss | 119:ee6a53069455 | 74 | -- Procurar por CBx que constem sem seus pares registrados na lógica. |
klauss | 119:ee6a53069455 | 75 | -- Mandar uma mensagem do tipo prompt-ping para este elemento |
klauss | 119:ee6a53069455 | 76 | |
klauss | 119:ee6a53069455 | 77 | -- Fluxo -- |
klauss | 119:ee6a53069455 | 78 | - Verificar se possui uma lista ordenada de CBx atualizada |
klauss | 119:ee6a53069455 | 79 | - Caso nao possua, gerar esta lista. |
klauss | 119:ee6a53069455 | 80 | - Para cada iteraçao |
klauss | 119:ee6a53069455 | 81 | - Verifica se o CBx é master ou slave |
klauss | 119:ee6a53069455 | 82 | - Verifica se o par deste esta registrado |
klauss | 119:ee6a53069455 | 83 | - Caso contratio |
klauss | 119:ee6a53069455 | 84 | - Verifica se o CBx encontrado esta em ligaçao |
klauss | 119:ee6a53069455 | 85 | - Caso nao esteja, encaminha mensagem de ping para o CBx ausente da dupla. |
klauss | 119:ee6a53069455 | 86 | ***/ |
klauss | 119:ee6a53069455 | 87 | |
klauss | 135:2f4290590e51 | 88 | if ( ( v_cb not_eq NULL ) ) |
klauss | 126:1f90756250fb | 89 | { |
klauss | 135:2f4290590e51 | 90 | static int ext_list [ u8_MAX_CB_IN_A_BRANCH + 1 ]; |
klauss | 119:ee6a53069455 | 91 | static uint8_t last_size = 0; |
klauss | 135:2f4290590e51 | 92 | uint8_t size = v_cb -> size (); |
klauss | 119:ee6a53069455 | 93 | static int wake_all_up_index = 0; |
klauss | 116:39a41ebb675c | 94 | |
klauss | 135:2f4290590e51 | 95 | if ( last_size not_eq size ) { |
klauss | 135:2f4290590e51 | 96 | for ( register int i = 0; i < size; i++ ) |
klauss | 135:2f4290590e51 | 97 | ext_list [ i ] = ( ( Call_Box * ) v_cb -> get_element ( i ) ) -> get_ext (); |
klauss | 78:1353744f01e1 | 98 | |
klauss | 135:2f4290590e51 | 99 | qsort ( ext_list, v_cb -> size (), sizeof ( int ), wake_comp ); |
klauss | 119:ee6a53069455 | 100 | } |
klauss | 119:ee6a53069455 | 101 | |
klauss | 135:2f4290590e51 | 102 | if ( size > 0 ) |
klauss | 119:ee6a53069455 | 103 | { |
klauss | 135:2f4290590e51 | 104 | Call_Box * cb = find_CB ( v_cb, ext_list [ wake_all_up_index ] ); |
klauss | 135:2f4290590e51 | 105 | |
klauss | 135:2f4290590e51 | 106 | if ( cb not_eq NULL ) |
klauss | 119:ee6a53069455 | 107 | { |
klauss | 135:2f4290590e51 | 108 | Call_Box * pair = cb -> get_pair_cbx (); |
klauss | 135:2f4290590e51 | 109 | |
klauss | 135:2f4290590e51 | 110 | if ( ( cb -> get_status () == cb_idle ) and ( pair == NULL ) ) |
klauss | 135:2f4290590e51 | 111 | { |
klauss | 135:2f4290590e51 | 112 | uint8_t data [ CB_BUFFER_SIZE ], write_buffer [ CB_BUFFER_SIZE ]; |
klauss | 135:2f4290590e51 | 113 | |
klauss | 135:2f4290590e51 | 114 | strcpy ( ( char * )data, "ping\r" ); |
klauss | 135:2f4290590e51 | 115 | |
klauss | 135:2f4290590e51 | 116 | int cb_ext = cb -> get_ext (); |
klauss | 135:2f4290590e51 | 117 | |
klauss | 135:2f4290590e51 | 118 | int ext_to_ping = ( ( cb_ext % 2 ) == 0 ) ? cb_ext + 1 : cb_ext - 1; |
klauss | 135:2f4290590e51 | 119 | |
klauss | 135:2f4290590e51 | 120 | send2callboxes ( build_cb_package ( ext_to_ping, ext_to_ping, PROMPT, ( char * )data, |
klauss | 135:2f4290590e51 | 121 | cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) ); |
klauss | 135:2f4290590e51 | 122 | |
klauss | 135:2f4290590e51 | 123 | if ( debug_wake ) vz_printf ("%d without %d - ping sent to %d", |
klauss | 135:2f4290590e51 | 124 | cb_ext, ext_to_ping, ext_to_ping ); |
klauss | 135:2f4290590e51 | 125 | } |
klauss | 135:2f4290590e51 | 126 | } |
klauss | 135:2f4290590e51 | 127 | } |
klauss | 119:ee6a53069455 | 128 | |
klauss | 126:1f90756250fb | 129 | if ( ++wake_all_up_index >= size ) wake_all_up_index = 0; |
klauss | 74:81c47fff88a5 | 130 | } |
klauss | 74:81c47fff88a5 | 131 | } |