![](/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@78:1353744f01e1, 2014-12-16 (annotated)
- Committer:
- klauss
- Date:
- Tue Dec 16 12:13:01 2014 +0000
- Revision:
- 78:1353744f01e1
- Parent:
- 74:81c47fff88a5
- Child:
- 80:61d61c9eb75c
before meeting
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 | 78:1353744f01e1 | 3 | int max_ext = 0; |
klauss | 78:1353744f01e1 | 4 | int min_ext = 0; |
klauss | 78:1353744f01e1 | 5 | Timer timer_aging; |
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 | 74:81c47fff88a5 | 9 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 74:81c47fff88a5 | 10 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 74:81c47fff88a5 | 11 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 74:81c47fff88a5 | 12 | } |
klauss | 74:81c47fff88a5 | 13 | return( NULL ); |
klauss | 74:81c47fff88a5 | 14 | } |
klauss | 74:81c47fff88a5 | 15 | |
klauss | 78:1353744f01e1 | 16 | int init_aging( void ){ |
klauss | 78:1353744f01e1 | 17 | timer_aging.start(); |
klauss | 78:1353744f01e1 | 18 | return( 0 ); |
klauss | 78:1353744f01e1 | 19 | } |
klauss | 74:81c47fff88a5 | 20 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 78:1353744f01e1 | 21 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 78:1353744f01e1 | 22 | uint8_t enough = 0; |
klauss | 78:1353744f01e1 | 23 | |
klauss | 78:1353744f01e1 | 24 | static int lst_ext = 0; |
klauss | 78:1353744f01e1 | 25 | |
klauss | 78:1353744f01e1 | 26 | if( timer_aging.read_ms() > 200 ){ |
klauss | 78:1353744f01e1 | 27 | timer_aging.reset(); |
klauss | 78:1353744f01e1 | 28 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 78:1353744f01e1 | 29 | Call_Box * cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 78:1353744f01e1 | 30 | if( cb->is_timeout() ){ |
klauss | 78:1353744f01e1 | 31 | if( cb->has_next_try() == false ){ |
klauss | 78:1353744f01e1 | 32 | if( debug_aging ) debug_msg( "time to really go " ); |
klauss | 78:1353744f01e1 | 33 | if( debug_aging ) debug_msg("( ext %d removed )", cb->get_ext() ); |
klauss | 78:1353744f01e1 | 34 | v_cb->remove_element( i ); |
klauss | 78:1353744f01e1 | 35 | delete( cb ); |
klauss | 78:1353744f01e1 | 36 | break; |
klauss | 78:1353744f01e1 | 37 | }else{ |
klauss | 78:1353744f01e1 | 38 | /* |
klauss | 78:1353744f01e1 | 39 | if( enough++ == 2 ){ |
klauss | 78:1353744f01e1 | 40 | if( debug_aging ) debug_msg( "Not enough in this cycle" ); |
klauss | 78:1353744f01e1 | 41 | return; |
klauss | 78:1353744f01e1 | 42 | } |
klauss | 78:1353744f01e1 | 43 | */ |
klauss | 78:1353744f01e1 | 44 | |
klauss | 78:1353744f01e1 | 45 | if( lst_ext == 0 ) lst_ext = cb->get_ext(); |
klauss | 78:1353744f01e1 | 46 | |
klauss | 78:1353744f01e1 | 47 | if( lst_ext == max_ext ) lst_ext = 0; |
klauss | 78:1353744f01e1 | 48 | |
klauss | 78:1353744f01e1 | 49 | if( cb->get_ext() < lst_ext ) continue; |
klauss | 78:1353744f01e1 | 50 | |
klauss | 78:1353744f01e1 | 51 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 78:1353744f01e1 | 52 | |
klauss | 78:1353744f01e1 | 53 | int8_t type = cb->get_next_aging_type(); |
klauss | 78:1353744f01e1 | 54 | |
klauss | 78:1353744f01e1 | 55 | uint8_t msg_type = 0x00; |
klauss | 78:1353744f01e1 | 56 | |
klauss | 78:1353744f01e1 | 57 | if( type == REQUEST_REGISTRY ){ |
klauss | 78:1353744f01e1 | 58 | msg_type = __REGISTRY__; |
klauss | 78:1353744f01e1 | 59 | if( debug_aging ) debug_msg("Registry request Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); |
klauss | 78:1353744f01e1 | 60 | }else if( type == REQUEST_PEND ){ |
klauss | 78:1353744f01e1 | 61 | msg_type = __PROMPT__; |
klauss | 78:1353744f01e1 | 62 | strcpy( ( char * )data, "pend\r\r\r\n" ); |
klauss | 78:1353744f01e1 | 63 | if( debug_aging ) debug_msg("Pend Request Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); |
klauss | 78:1353744f01e1 | 64 | }else if( ( type == REQUEST_PING ) ){ |
klauss | 78:1353744f01e1 | 65 | msg_type = __PROMPT__; |
klauss | 78:1353744f01e1 | 66 | strcpy( ( char * )data, "ping\r\r\r\n" ); |
klauss | 78:1353744f01e1 | 67 | if( debug_aging ) debug_msg("Ping on Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); |
klauss | 78:1353744f01e1 | 68 | } |
klauss | 78:1353744f01e1 | 69 | |
klauss | 78:1353744f01e1 | 70 | send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), msg_type, |
klauss | 78:1353744f01e1 | 71 | ( char * )data, 0x19, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 78:1353744f01e1 | 72 | |
klauss | 78:1353744f01e1 | 73 | return; |
klauss | 78:1353744f01e1 | 74 | } |
klauss | 78:1353744f01e1 | 75 | } |
klauss | 78:1353744f01e1 | 76 | } |
klauss | 78:1353744f01e1 | 77 | if( debug_aging ) if( enough > 1 ) debug_msg( " out( %d ) ", enough ); |
klauss | 78:1353744f01e1 | 78 | } |
klauss | 78:1353744f01e1 | 79 | } |
klauss | 78:1353744f01e1 | 80 | } |
klauss | 78:1353744f01e1 | 81 | |
klauss | 78:1353744f01e1 | 82 | void wake_all_up( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 78:1353744f01e1 | 83 | if( v_cb != NULL && data != NULL && write_buffer != NULL ){ |
klauss | 78:1353744f01e1 | 84 | int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; |
klauss | 78:1353744f01e1 | 85 | uint8_t size = v_cb->size(); |
klauss | 78:1353744f01e1 | 86 | |
klauss | 78:1353744f01e1 | 87 | bool once = true; |
klauss | 78:1353744f01e1 | 88 | |
klauss | 78:1353744f01e1 | 89 | if( size > 0 ){ |
klauss | 78:1353744f01e1 | 90 | for( register int i = 0; i < size; i++ ){ |
klauss | 78:1353744f01e1 | 91 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 78:1353744f01e1 | 92 | } |
klauss | 78:1353744f01e1 | 93 | |
klauss | 78:1353744f01e1 | 94 | qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); |
klauss | 78:1353744f01e1 | 95 | |
klauss | 78:1353744f01e1 | 96 | for( register int i = 0; i < size; i++ ){ |
klauss | 78:1353744f01e1 | 97 | // testa se eh o master |
klauss | 78:1353744f01e1 | 98 | if( ( ext_list[ i ] % 2 ) == 0 ){ |
klauss | 78:1353744f01e1 | 99 | //testa se o slave nao esta registrado |
klauss | 78:1353744f01e1 | 100 | if( ( ( i + 1 ) < size ) && ( ext_list[ i + 1 ] != ( ext_list[ i ] + 1 ) ) || ( size == 1 ) ){ |
klauss | 78:1353744f01e1 | 101 | if( debug_wake ) debug_msg("Mestre %d encontrado sem slave %d", ext_list[ i ], ext_list[ i ] + 1 ); |
klauss | 78:1353744f01e1 | 102 | if( once ){ |
klauss | 78:1353744f01e1 | 103 | strcpy( ( char * )data, "ping\r\r\r\n" ); |
klauss | 78:1353744f01e1 | 104 | Call_Box * cb = __find_CB__( v_cb, ext_list[ i ] ); |
klauss | 78:1353744f01e1 | 105 | if( cb != NULL ){ |
klauss | 78:1353744f01e1 | 106 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 78:1353744f01e1 | 107 | |
klauss | 78:1353744f01e1 | 108 | send2callboxes( __build_cb_package__( ( cb->get_ext() + 1 ), ( cb->get_port() + 1 ), __PROMPT__, |
klauss | 78:1353744f01e1 | 109 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 78:1353744f01e1 | 110 | |
klauss | 78:1353744f01e1 | 111 | if( debug_wake ) debug_msg("Slave %d pingado", cb->get_ext() + 1 ); |
klauss | 78:1353744f01e1 | 112 | } |
klauss | 78:1353744f01e1 | 113 | once = false; |
klauss | 78:1353744f01e1 | 114 | } |
klauss | 78:1353744f01e1 | 115 | } |
klauss | 74:81c47fff88a5 | 116 | }else{ |
klauss | 78:1353744f01e1 | 117 | if( ( ( i - 1 ) > 0 ) && ( ext_list[ i - 1 ] != ( ext_list[ i ] - 1 ) ) || ( size == 1 ) ){ |
klauss | 78:1353744f01e1 | 118 | if( debug_wake ) debug_msg("Escravo revolucionario %d encontrado sem master %d", ext_list[ i ], ext_list[ i ] - 1 ); |
klauss | 78:1353744f01e1 | 119 | if( once ){ |
klauss | 78:1353744f01e1 | 120 | strcpy( ( char * )data, "ping\r\r\r\n" ); |
klauss | 78:1353744f01e1 | 121 | Call_Box * cb = __find_CB__( v_cb, ( ext_list[ i ] ) ); |
klauss | 78:1353744f01e1 | 122 | if( cb != NULL ){ |
klauss | 78:1353744f01e1 | 123 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); |
klauss | 78:1353744f01e1 | 124 | |
klauss | 78:1353744f01e1 | 125 | send2callboxes( __build_cb_package__( ( cb->get_ext() - 1 ), ( cb->get_port() - 1 ), __PROMPT__, |
klauss | 78:1353744f01e1 | 126 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 78:1353744f01e1 | 127 | |
klauss | 78:1353744f01e1 | 128 | if( debug_wake ) debug_msg("Master %d pingado", cb->get_ext() - 1 ); |
klauss | 78:1353744f01e1 | 129 | } |
klauss | 78:1353744f01e1 | 130 | once = false; |
klauss | 78:1353744f01e1 | 131 | } |
klauss | 78:1353744f01e1 | 132 | } |
klauss | 74:81c47fff88a5 | 133 | } |
klauss | 74:81c47fff88a5 | 134 | } |
klauss | 74:81c47fff88a5 | 135 | } |
klauss | 74:81c47fff88a5 | 136 | } |
klauss | 74:81c47fff88a5 | 137 | } |