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_manager.cpp Source File

call_manager.cpp

00001 #include "call_manager.h"
00002 
00003 Timer invite_pkg_retry_timer;
00004 Timer bye_pkg_retry_timer;
00005 
00006 VZ_call * 
00007 find_Call ( Vector  * v_call, const int ext )
00008 {
00009     for ( register int i = 0; i < v_call -> size(); i ++ )
00010     {
00011         VZ_call * call = NULL;
00012         
00013         call = ( VZ_call * )v_call -> get_element ( i );
00014         if ( call != NULL )
00015         {
00016             if ( call -> get_cb_ext () == ext ) return ( call );
00017         }
00018     }
00019     return( NULL );
00020 }
00021 
00022 /* remove calls por timeout */
00023 void call_manager ( Vector  * v_call, Vector  * v_cb, Timeslice * ts )
00024 {
00025     //FIXME precisa tratar o caso dos parametros NULL ?
00026     static uint8_t data[ CB_BUFFER_SIZE ], write_buffer[ CB_BUFFER_SIZE ];
00027     
00028     for ( register uint8_t i = 0; i < v_call->size(); i++ )
00029     {
00030         bool should_remove = false;
00031         
00032         VZ_call * call = ( VZ_call * )v_call->get_element( i );
00033         
00034         if ( call == NULL ) continue;
00035         
00036         if ( end_call )
00037         {
00038             if ( end_call_ext == call -> get_cb_ext () )
00039             {
00040                 end_call = false;
00041                 end_call_ext = 0;
00042                 should_remove = true;
00043             }
00044         }
00045         
00046         if ( call->is_timetofinish() || call -> cbx_pkg_is_idle() || should_remove )
00047         {
00048             if ( debug_invite && !should_remove )
00049             {
00050                 if ( call -> cbx_pkg_is_idle () ) { 
00051                     if( debug_invite ) vz_debug ("[%d] Call timeout: no pkg", call -> get_cb_ext () );
00052                 } else { 
00053                     if( debug_invite ) vz_debug ("[%d] Call timeout", call->get_cb_ext () ); 
00054                 }
00055             }
00056             
00057             v_call -> remove_element ( i );
00058             Call_Box * cb = find_CB ( v_cb, call -> get_cb_ext () );
00059             if ( cb != NULL )
00060             { 
00061                 data [ TIMESLICE_PLACE ] = 0;
00062                 
00063                 send2callboxes ( build_cb_package ( cb -> get_ext (), cb -> get_port (), CB_BYE, 
00064                     ( char * )data, cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
00065                 
00066                 ts -> return_timeslice ( cb -> call_end () );
00067             }
00068             delete( call );    
00069         }
00070     }    
00071     if ( end_call_ext != 0 )
00072     {
00073         if ( debug_invite ) vz_debug ( "Call from %i not foud", end_call_ext );
00074         end_call = false;
00075         end_call_ext = 0;
00076     }
00077 }
00078 
00079 void 
00080 invite_handler ( Vector  * v_call, Vector  * v_cb, Timeslice * ts, Call_Box * inviting_cb )
00081 {
00082     
00083     if ( ( v_call != NULL ) and ( v_cb != NULL ) and ( ts != NULL ) )
00084     {
00085         uint8_t data [ CB_BUFFER_SIZE ];
00086         uint8_t write_buffer [ CB_BUFFER_SIZE ];
00087     
00088         if ( inviting_cb != NULL )
00089         {
00090             if ( inviting_cb -> get_status () == cb_idle )
00091             {
00092                 inviting_cb -> cb_set_status ( cb_trying );
00093                 if ( debug_invite ) vz_debug ("[%d] Request invite <cb_idle,cb_trying>", inviting_cb->get_ext ()  );
00094             }
00095         }
00096         
00097         for ( register uint8_t i = 0; i < v_cb->size (); i++ )
00098         {
00099             Call_Box * cb = ( Call_Box * ) v_cb->get_element ( i );
00100             
00101             if ( cb == NULL ) continue;
00102             
00103             if ( ( cb -> get_status () == cb_idle ) or ( cb -> get_status () == cb_on_call ) ) continue;
00104             
00105             int ext = cb->get_ext ();
00106             int port = cb->get_port ();
00107     
00108             switch ( cb -> get_status () )
00109             {            
00110                 case cb_trying : {                
00111                     uint8_t timeslice = ts -> get_timeslice ();
00112                     
00113                     if ( timeslice == 0 )
00114                     {
00115                         
00116                         data [ TIMESLICE_PLACE ] = 0x00;
00117           
00118                         // validar troca de mensagens com nros "nao previstos"
00119                         send2callboxes ( build_cb_package ( ext, port, INVITE, 
00120                             ( char * )data, ( cb->get_msg_id () | BIT7 ), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
00121                          
00122                         cb -> call_end ();
00123                          
00124                         if ( debug_invite ) vz_debug ( "[%d] Trying without TS <cb_trying,cb_idle>", ext );
00125                         
00126                         return;
00127                     }
00128                         else
00129                     {
00130                         
00131                         data [ TIMESLICE_PLACE ] = timeslice;
00132                         
00133                         cb -> call_init ( timeslice );
00134                         
00135                         // validar troca de mensagens com nros "nao previstos"    
00136                         send2callboxes( build_cb_package( ext, port, INVITE, 
00137                             ( char * )data, cb->get_msg_id (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
00138                         
00139                         if ( debug_invite ) vz_debug ("[%d] TS::%d <cb_trying,cb_ringing>", ext, data [ TIMESLICE_PLACE ] );
00140                     }
00141                 }break;
00142                 case cb_ringing : {
00143                     VZ_call * call = cb -> invite ();
00144                     
00145                     if ( cb -> get_sip_status () == sip_busy ){
00146                         cb->cb_set_status( cb_busy );
00147                     } 
00148                         else if ( call != NULL )
00149                     {
00150                         if ( find_Call ( v_call, call -> get_cb_ext () ) == NULL ) v_call -> add ( call );
00151                         
00152                         call -> init_cbx_pkg_idle_timer ();
00153                         
00154                         if ( cb -> get_sip_status () == sip_on_call )
00155                         {
00156                         
00157                             data [ TIMESLICE_PLACE ] = cb -> get_timeslice ();  
00158                             
00159                             // confirmar a msg_id se sempre é bem aceita pelo cbx
00160                             send2callboxes ( build_cb_package( ext, port, INVITE, ( char * )data, 
00161                                 cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
00162                             
00163                             cb -> call_confirmed ();    
00164                     
00165                             if ( debug_invite ) vz_debug ( "[%d] Ringing call ok <cb_ringing,cb_on_call>", ext );
00166                         }
00167                     } else {
00168                         if ( cb -> get_sip_status () == sip_denied )
00169                         {
00170                             cb -> cb_set_status ( cb_denied );
00171                             if ( debug_invite ) vz_debug ( "[%d] call denied <sip_denied>::<cb_ringing,cb_denied>", ext );
00172                         } else {
00173                             if ( cb -> time_to_retry () )
00174                             {                                
00175                                 if ( cb -> get_sip_status () == sip_waiting_trying )
00176                                 {
00177                                     if ( debug_invite ) vz_debug ("[%4i %3i] Resend invite to *", ext, cb -> get_invite_try_number ()  );
00178                                     cb -> retry_send_invite_pkg_to_ast ();    
00179                                 }
00180                             }    
00181                         }
00182                     }
00183                 }break;
00184                 case cb_busy : {
00185                     data [ TIMESLICE_PLACE ] = 0;
00186                     
00187                     send2callboxes ( build_cb_package( ext, port, CB_BYE, 
00188                         ( char * )data, cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
00189                     
00190                     for ( register uint8_t i = 0; i < v_call -> size (); i++ )
00191                     {
00192                         VZ_call * call = ( VZ_call * ) v_call->get_element ( i );
00193     
00194                         if ( call -> get_cb_ext () == cb -> get_ext () )
00195                         { 
00196                             v_call -> remove_element ( i );
00197                             if ( call != NULL ) delete ( call );
00198                             break;
00199                         }
00200                     }
00201                         
00202                     ts -> return_timeslice ( cb -> call_end () );
00203                     
00204                     if ( debug_invite ) vz_debug ( "[%d] Busy Here <cb_busy,cb_idle>", ext );
00205                 }break;
00206                 
00207                 case cb_denied : {
00208                     data [ TIMESLICE_PLACE ] = 0;
00209                     
00210                     send2callboxes ( build_cb_package( ext, port, CB_BYE, 
00211                         ( char * )data, cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
00212                         
00213                     for ( register uint8_t i = 0; i < v_call -> size (); i++ )
00214                     {
00215                         VZ_call * call = ( VZ_call * ) v_call->get_element ( i );
00216                         if ( call -> get_cb_ext () == cb->get_ext () )
00217                         {
00218                             v_call -> remove_element( i );
00219                             if ( call != NULL ) delete ( call );
00220                             break;
00221                         }
00222                     }
00223                     ts -> return_timeslice ( cb -> call_end () );
00224     
00225                     if ( debug_invite ) vz_debug ( "[%d] call denied <cb_denied,cb_idle>", ext );
00226                 }break;
00227             }
00228         }
00229     }
00230 }