Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

call_box.cpp

Committer:
klauss
Date:
2015-03-13
Revision:
106:a34fcf9f0e02
Parent:
100:09a23fcd3bdf
Child:
108:18a3702650f3

File content as of revision 106:a34fcf9f0e02:

#include "call_box.h"

Call_Box::Call_Box( int ext, int port ){
    this->ext = ext;
    this->port = port;
    t.start();
    remaining_attempts = __MAX_ATTEMPTS__;
    timeout = __TIMEOUT__;
    msg_id = 0x10;
    timeslice = 0x00;
    status = cb_idle;
    
    {
        FILE * fp = NULL;
        char inner_tmp[ 20 ];
        
        fp = fopen( "/qspi/shift_port.txt", "r" );
        if( fp == NULL )
        {
            if( debug_cb ) debug_msg("Failed to open /qspi/shift_port.txt" );
                shift_port = SHIFT_PORT;
            }else{
                if( fread( (void *)inner_tmp, 1, 20, fp ) > 0 ){
                shift_port = atoi( inner_tmp );
                if( debug_cb ) debug_msg("Shift port %d", shift_port );
            }else{
                if( debug_cb ) debug_msg("Failed to read /qspi/shift_port.txt" );
                shift_port = SHIFT_PORT;
            }
        }
        
        fclose( fp );
    }
    
    shift_port = SHIFT_PORT;
    sip = new Sip( ext, ext + shift_port );
    sip_socket_fd = sip->get_socket_fd();
    if( sip == NULL )
    
    {
        memory_is_over = true;
        if( debug_memory ) debug_msg("Sip allocation fail");
    }
    
    next_aging_type = __CYCLES__;
    seconds.start();
    invite_response = true;
    bye_response = true;
    invite_retry_count = MAX_INVITE_RETRY;
    cb_new_counter++;
}

int Call_Box::get_status( void ){ return this->status; }

void Call_Box::cb_set_status( uint8_t status ){ this->status = status; }

int Call_Box::get_sip_status( void ){
    if( sip == NULL ) return -1;
    
    else return this->sip->get_status();
}

int Call_Box::set_sip_status( uint8_t status ){
    if( sip == NULL ) return( -1 );
    
    sip->sip_set_status( status );
    
    return( 0 );
}

Call_Box::~Call_Box( void ){
    if( sip != NULL ) delete( sip );
    cb_delete_counter++;
}

int Call_Box::get_port( void ){ return( this->port ); }
int Call_Box::get_ext( void ){ return( this->ext ); }

float Call_Box::get_elapsed_time( void ){ return( this->t.read_ms() ); }

void Call_Box::reset_elapsed_time( void ){ 
    this->t.reset(); 
    timeout = __TIMEOUT__;
    next_aging_type = __CYCLES__;
}

bool Call_Box::is_timeout( void ){
    if( seconds.read() > __RESQUEST_TIME__ ){
        seconds.reset();
        return( t.read() > __TIME_TO_REGISTER__ ) ? true : false;
    }else return false;
}

bool Call_Box::has_next_try( void ){
    return( ( t.read() < __TIMEOUT__ ) ? true : false );
}

void Call_Box::registry( void ){
    if( this->sip == NULL ){
        this->sip = new Sip( ext, ext + shift_port );
        deleted_sip++;
        if( sip == NULL ){
            memory_is_over = true;
            if( debug_memory ) debug_msg("Call_Box cb allocation fail");
            return;
        }
    
        if( sip->registry() > 0x00 ) reset_elapsed_time();
        if( debug_aging ) debug_msg( "%d Registered - ( %f )", ext, __TIMEOUT__ - t.read() );
        
        delete( sip );
        this->sip = NULL;
    }else{
        if( sip->registry() > 0x00 ) reset_elapsed_time();
        if( debug_aging ) debug_msg( "%d Registered - ( %f )", ext, __TIMEOUT__ - t.read() );
    }
}

void Call_Box::unregistry( void ){
    if( this->sip == NULL ){ 
        this->sip = new Sip( ext, ext + shift_port );
        deleted_sip++;
        if( sip == NULL ){
            memory_is_over = true;
            if( debug_memory ) debug_msg("Call_Box cb allocation fail");
            return;    
        }
        sip->send_unregistry_pkg();
    
        delete( sip );
        this->sip = NULL;
    }else{
        sip->send_unregistry_pkg();
    }
}

VZ_call * Call_Box::invite( void ){
    //t.stop();
    VZ_call * call = NULL;
    
    if( this->sip == NULL ){
        this->sip = new Sip( ext, ext + shift_port );
        deleted_sip++;
        
        if( sip != NULL ){
            memory_is_over = true;
            if( debug_memory ) debug_msg("Call_Box cb allocation fail");
            return( NULL );
        }else{
            call = sip->invite();
            
            delete( sip );
            this->sip = NULL;
        }
    }else{   
        call = sip->invite();
    }
    
    if( debug_cb ) debug_msg("Call returned value :: %p ", (void * )call );
    if( call == NULL ) t.start();
    return( call );
}

/*  Retorna 
    = 0 :: ok
    < 0 :: tive problemas
    > 0 :: devo remover essa call do vetor de calls 
*/
int Call_Box::listen_SIP_server( void ){
    if( this->sip == NULL ){
        if( status == cb_on_call || status == cb_idle ){
            this->sip = new Sip( ext, ext + shift_port );
            deleted_sip++;
            
            if( sip == NULL ){
                memory_is_over = true;
                if( debug_memory ) debug_msg("Call_Box cb allocation fail");
                return( -1 );
            }else{
                int out_read = sip->listen_SIP_server();
                
                delete( sip );
                this->sip = NULL;
                return( out_read );
            }
        }else return( 0 );
    }else{
        if( status == cb_on_call || status == cb_idle ){
            return ( sip->listen_SIP_server() );
        } else return( 0 );
    }
}

void Call_Box::set_msg_id( uint8_t msg_id ){
    this->msg_id = ( msg_id > 0x10 ) ? msg_id : 0x11;
}
uint8_t Call_Box::get_msg_id( void ){ return( this->msg_id ); }

void Call_Box::set_timeslice( uint8_t timeslice ){
    this->timeslice = timeslice;
}
uint8_t Call_Box::get_timeslice( void ){ return( this->timeslice ); }

//void Call_Box::set_sip( Sip * sip ){
//    this->sip = sip;    
//}

void Call_Box::send_bye( void ){ 
    if( this->sip == NULL ){
        this->sip = new Sip( ext, ext + shift_port );
        deleted_sip++;
        
        if( sip == NULL ){
            memory_is_over = true;
            if( debug_memory ) debug_msg("Call_Box cb allocation fail");
            return;
        }else{
        
            sip->send_bye();
            
            delete( sip );
            sip = NULL;
        }
    }else{
        sip->send_bye();
    }
}

void Call_Box::reset_sip( void ){
    if( this->sip != NULL ) delete( this->sip );
    
    this->sip = new Sip( ext, ext + shift_port );
    deleted_sip++;
    
    if( sip == NULL ) memory_is_over = true;
}

void Call_Box::set_sip_status( int status ){
    if( this->sip != NULL ){
        this->sip->status = status;
    }else{
        if( debug_cb) debug_msg("Sip equals NULL o.O");
    }        
}

void Call_Box::re_start_timer( void ){
    t.start();    
}

int8_t Call_Box::get_next_aging_type( void ){
    next_aging_type--;
    
    if( ( next_aging_type % 2 ) == 0  || ( next_aging_type > __CYCLES__ - 15 ) ) return( REQUEST_REGISTRY );
    
    else if( ( next_aging_type % 7 ) == 0 ) return( REQUEST_PEND );
    
    else return( REQUEST_PING );
}

void Call_Box::set_invite_response_ok( void ){ this->invite_response = true; }

//void Call_Box::set_first_invite_response_ok( void ){ this->invite_first_response = true; 
//    debug_msg("");
//}

void Call_Box::set_invite_response_pending( void ){ this->invite_response = false; }
bool Call_Box::get_invite_response( void ){ return( this->invite_response ); }

//void Call_Box::set_first_invite_response_pending( void ){ this->first_invite_response = false; }
//bool Call_Box::get_first_invite_response( void ){ return( this->first_invite_response ); }


void Call_Box::invite_retry_count_reset( void ){ invite_retry_count = MAX_INVITE_RETRY; }
uint8_t Call_Box::get_invite_retry_count( void ){ 
    return ( invite_retry_count ) ? invite_retry_count-- : 0;
}

void Call_Box::set_bye_response_ok( void ){ this->bye_response = true; }
void Call_Box::set_bye_response_pending( void ){ this->bye_response = false; }
bool Call_Box::get_bye_response( void ){ return( this->bye_response ); }


int Call_Box::get_sip_socket_fd( void ){
    return( sip_socket_fd );
}

int Call_Box::sip_udp_incomming_pkg( void ){
    return sip->udp_incomming_pkg();
}

void Call_Box::reset_cb_status( void ){
    if( sip != NULL ) sip->reset_call();
}

int Call_Box::get_sip_ext( void ){ return this->sip->get_ext(); }
int Call_Box::get_sip_port( void ){ return this->sip->get_port(); }


int Call_Box::get_timer( void ){ return t.read(); }