Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

call_box_manager.cpp

Committer:
klauss
Date:
2015-09-01
Revision:
134:395678c651d8
Parent:
133:3d136f5ffd66
Child:
135:2f4290590e51

File content as of revision 134:395678c651d8:

#include "call_box_manager.h"

Timer timer_refresh;
Timer timer_aging;
Timer timer_sync_refresh;

Call_Box * find_CB ( Vector * v_cb, const int ext )
{
    //fixme
    // if ( v_cb == NULL ) return NULL;
    Call_Box * cb = NULL;
    for ( register int i = 0; i < v_cb -> size (); i++ )
    {
        cb = ( Call_Box * ) v_cb -> get_element ( i );
        if ( cb not_eq NULL )
        {
            if ( cb -> get_ext () == ext ) return ( cb );
        }
    }
    return ( NULL );
}

int refresh ( Vector * v_cb )
{
    if ( v_cb not_eq NULL )
    {
        static int list_index = 0;
            
        if ( v_cb -> size () > 0 )
        {
            static uint8_t data [ CB_BUFFER_SIZE ], write_buffer [ CB_BUFFER_SIZE ];
            
            led4 = 0;
            
            if ( list_index >= v_cb -> size () ) list_index = 0;
            
            Call_Box * cb = ( Call_Box * ) v_cb -> get_element ( list_index++ );
            
            if ( ( cb not_eq NULL ) and ( cb -> get_status () == cb_idle ) )
            {
                int local_ext = cb -> get_ext ();
                
                if ( local_ext % 2 == 1 )
                { 
                    // if ( ! list_exception_ext -> find_value ( local_ext ) )
                    {
                        // checking master status
                        Call_Box * cb_master = find_CB ( v_cb, local_ext - 1 );
                        
                        if ( cb_master not_eq NULL ) 
                        {
                            if ( cb_master -> get_status () not_eq cb_idle ) return ( 0x20 );
                        
                            else if ( cb_master -> get_timer () >= cm -> get_acceptable_delay () ) return ( 0x40 );
                        }
                    }
                }
                    else
                {
                    // if ( ! list_exception_ext -> find_value ( local_ext ) )
                    {
                        // checking slave status
                        Call_Box * cb_slave = find_CB ( v_cb, local_ext + 1 );
                        
                        if ( cb_slave not_eq NULL )
                        {
                            if ( cb_slave -> get_status () not_eq cb_idle ) return ( 0x21 );
                            
                            else if ( cb_slave -> get_timer () >= cm -> get_acceptable_delay () ) return ( 0x41 );
                        }
                    }
                }
                
                /*
                    condição adicionada para minimizar o comportamento do cbx perder o tx, 
                    pacotes vindo shiftados pra cabeceira, e não perdendo o rx, portando
                    o cbx não resetava sozinho, idéia, deixar ele rebotar pelo wdt dele.
                */
                if ( ( cb -> get_timer () < cm -> get_acceptable_delay () ) and ( cb -> get_overflow_times () == 0 ) )
                {
                    send2callboxes( build_cb_package( local_ext, cb->get_port(), REGISTRY, 
                        ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
                        
                    if( debug_refresh ) vz_printf ("[%d] Refresh", local_ext );
                } else {
                    if ( debug_dont_refresh ) vz_debug ("Nao pedindo registro para o [%d]( %d, %d )", 
                        local_ext, cb -> get_timer (), cb -> get_overflow_times () );    
                }
                return( 0 );
            }else return( -5 );
        }else return( -3 );
    }else return( -1 );
}

void wake_all_up ( Vector * v_cb )
{
 /***
    [ Principio ]
        -- Procurar por CBx que constem sem seus pares registrados na lógica.
        -- Mandar uma mensagem do tipo prompt-ping para este elemento
        
    -- Fluxo --
        - Verificar se possui uma lista ordenada de CBx atualizada
            - Caso nao possua, gerar esta lista.
        - Para cada iteraçao
            - Verifica se o CBx é master ou slave
            - Verifica se o par deste esta registrado
                - Caso contratio
                    - Verifica se o CBx encontrado esta em ligaçao
                        - Caso nao esteja, encaminha mensagem de ping para o CBx ausente da dupla.
 ***/
 
    uint8_t data[ CB_BUFFER_SIZE ], write_buffer[ CB_BUFFER_SIZE ];
    if( ( v_cb not_eq NULL ) )
    {
        static int ext_list[ MAX_CB_IN_A_BRANCH + 1 ];
        static uint8_t last_size = 0;
        uint8_t size = v_cb->size();
        static int wake_all_up_index = 0;
        
        if( last_size not_eq size )
        {
            for( register int i = 0; i < size; i++ ){
                ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext();
            }
            
            qsort( ext_list, v_cb->size(), sizeof( int ), wake_comp );
        }
        
        if( size > 0 )
        {   
            // testa se eh o master
            if( ( ext_list[ wake_all_up_index ] % 2 ) == 0 )
            {
                //testa se o slave nao esta registrado
                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 ) ) ) )
                {
                    strcpy( ( char * )data, "ping\r" );
                    Call_Box * cb = find_CB( v_cb, ext_list[ wake_all_up_index ] );
                 
                    // verifica se o master esta em chamada.   
                    if ( ( cb not_eq NULL ) and ( cb -> get_status () == cb_idle ) )
                    {                         
                        send2callboxes( build_cb_package( ( cb->get_ext() + 1 ), ( cb->get_port() + 1 ), PROMPT, 
                            ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
        
                        if( debug_wake ) vz_printf ("%d without %d - ping sent to %d", 
                            ext_list[ wake_all_up_index ], ext_list[ wake_all_up_index ] + 1, cb->get_ext() + 1 );
                    }
                }
                
            }else{
                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 ) ) ) )
                {
                    strcpy( ( char * )data, "ping\r" );
                    Call_Box * cb = find_CB( v_cb, ( ext_list[ wake_all_up_index ] ) );
                    
                    // verifica se o slave esta em chamada.
                    if ( ( cb not_eq NULL ) and ( cb -> get_status () == cb_idle ) )
                    { 
                        send2callboxes( build_cb_package( ( cb->get_ext() - 1 ), ( cb->get_port() - 1 ), PROMPT, 
                            ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
                        
                        if ( debug_wake ) vz_printf ("%d without %d - ping sent to %d", 
                            ext_list [ wake_all_up_index ], ext_list [ wake_all_up_index ] - 1, cb -> get_ext () - 1 );
                    }
                }
            }
        }
        if ( ++wake_all_up_index >= size ) wake_all_up_index = 0;
    }
}