VZTECH / Mbed 2 deprecated main_src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers call_box_manager.cpp Source File

call_box_manager.cpp

00001 #include "call_box_manager.h"
00002 
00003 Timer timer_refresh;
00004 Timer timer_aging;
00005 Timer timer_sync_refresh;
00006 
00007 Call_Box * find_CB ( Vector  * v_cb, const int ext )
00008 {
00009     if ( v_cb == NULL ) return NULL;
00010     
00011     for ( register int i = 0; i < v_cb -> size (); i++ )
00012     {
00013         Call_Box * cb = NULL;
00014         
00015         cb = ( Call_Box * ) v_cb -> get_element ( i );
00016         if ( cb not_eq NULL )
00017         {
00018             if ( cb -> get_ext () == ext ) return ( cb );
00019         }
00020     }
00021     return ( NULL );
00022 }
00023 
00024 int 
00025 refresh ( Vector  * v_cb )
00026 {
00027     if ( v_cb not_eq NULL )
00028     {    
00029         if ( v_cb -> size () > 0 )
00030         {
00031             static int list_index = 0;
00032             
00033             led4 = 0;
00034             
00035             if ( list_index >= v_cb -> size () ) list_index = 0;
00036             
00037             Call_Box * cb = ( Call_Box * ) v_cb -> get_element ( list_index++ );
00038             
00039             if ( ( cb not_eq NULL ) and ( cb -> get_status () == cb_idle ) )
00040             {
00041                 int local_ext = cb -> get_ext ();
00042                 
00043                 Call_Box * pair = cb -> get_pair_cbx ();
00044                     
00045                 if ( pair not_eq NULL )
00046                 {
00047                     if ( pair -> get_status () not_eq cb_idle ) return ( local_ext % 2 == 1 ) ? ( 0x21 ) : ( 0x20 );
00048                     
00049                     else if ( ( not ( cm -> get_cbx_detach_mode () ) ) and ( pair -> get_timer () >= cm -> get_acceptable_delay () ) ) return ( local_ext % 2 == 1 ) ? ( 0x41 ) : ( 0x40 );
00050                 }
00051                 
00052                 if ( ( cb -> get_timer () < cm -> get_acceptable_delay () ) and ( cb -> get_overflow_times () == 0 ) )
00053                 {
00054                     static uint8_t data [ CB_BUFFER_SIZE ], write_buffer [ CB_BUFFER_SIZE ];
00055                     
00056                     send2callboxes( build_cb_package( local_ext, cb->get_port(), REGISTRY, 
00057                         ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
00058                         
00059                     if ( debug_refresh ) vz_printf ("[%d] Refresh", local_ext );
00060                 } else {
00061                     if ( debug_dont_refresh ) vz_debug ("Nao pedindo registro para o [%d]( %d, %d )", 
00062                         local_ext, cb -> get_timer (), cb -> get_overflow_times () );    
00063                 }
00064                 return( 0 );
00065             }else return( -5 );
00066         }else return( -3 );
00067     }else return( -1 );
00068 }
00069 
00070 void wake_all_up ( Vector  * v_cb )
00071 {
00072  /***
00073     [ Principio ]
00074         -- Procurar por CBx que constem sem seus pares registrados na lógica.
00075         -- Mandar uma mensagem do tipo prompt-ping para este elemento
00076         
00077     -- Fluxo --
00078         - Verificar se possui uma lista ordenada de CBx atualizada
00079             - Caso nao possua, gerar esta lista.
00080         - Para cada iteraçao
00081             - Verifica se o CBx é master ou slave
00082             - Verifica se o par deste esta registrado
00083                 - Caso contratio
00084                     - Verifica se o CBx encontrado esta em ligaçao
00085                         - Caso nao esteja, encaminha mensagem de ping para o CBx ausente da dupla.
00086  ***/
00087  
00088     if ( ( v_cb not_eq NULL ) )
00089     {
00090         static int ext_list [ u8_MAX_CB_IN_A_BRANCH + 1 ];
00091         static uint8_t last_size = 0;
00092         uint8_t size = v_cb -> size ();
00093         static int wake_all_up_index = 0;
00094         
00095         if ( last_size not_eq size ) {
00096             for ( register int i = 0; i < size; i++ )
00097                 ext_list [ i ] = ( ( Call_Box * ) v_cb -> get_element ( i ) ) -> get_ext ();
00098             
00099             qsort ( ext_list, v_cb -> size (), sizeof ( int ), wake_comp );
00100         }
00101         
00102         if ( size > 0 )
00103         {   
00104             Call_Box * cb = find_CB ( v_cb, ext_list [ wake_all_up_index ] );
00105             
00106             if ( cb not_eq NULL )
00107             {
00108                 Call_Box * pair = cb -> get_pair_cbx ();
00109                 
00110                 if ( ( cb -> get_status () == cb_idle ) and ( pair == NULL ) )
00111                 {    
00112                     uint8_t data [ CB_BUFFER_SIZE ], write_buffer [ CB_BUFFER_SIZE ];
00113                     
00114                     strcpy ( ( char * )data, "ping\r" );
00115                     
00116                     int cb_ext = cb -> get_ext ();
00117                     
00118                     int ext_to_ping = ( ( cb_ext % 2 ) == 0 ) ? cb_ext + 1 : cb_ext - 1;
00119                     
00120                     send2callboxes ( build_cb_package ( ext_to_ping, ext_to_ping, PROMPT, ( char * )data, 
00121                                     cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
00122                             
00123                     if ( debug_wake ) vz_printf ("%d without %d - ping sent to %d", 
00124                             cb_ext, ext_to_ping, ext_to_ping );
00125                 }
00126             }    
00127         }
00128         
00129         if ( ++wake_all_up_index >= size ) wake_all_up_index = 0;
00130     }
00131 }