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

main_app_functions.cpp

00001 #include "main_app_functions.h"
00002 
00003 inline void reset_leds() { led1 = led2 = 1; led3 = led4 = 0; }
00004 
00005 int header_app_init ( void )
00006 {
00007  
00008     vz_printf ( "\r\n" );
00009  
00010     if ( debug_app_init ) vz_printf ( "init_wdt() :: %s", ( init_wdt () == 0 ) ? "Ok" : "Failure" );
00011     
00012     if ( debug_app_init ) vz_printf ( "config_lpc() :: %s", ( config_lpc () == 0 ) ? "Ok" : "Failure" );
00013     
00014     if ( debug_app_init ) vz_printf ( "start_cpld() :: %s", ( start_cpld () == 0 ) ? "Ok" : "Failure" );
00015     
00016     if( sdram_init() == 1 ) {
00017         vz_printf ("******* Failed to initialize SDRAM *******");
00018         return 1;
00019     } else {
00020         vz_printf ("******* Success to initialize SDRAM *******");
00021     }
00022     
00023     int init_fsystem_ret = init_fsystem();
00024     if( init_fsystem_ret & BIT0 )
00025     {
00026         vz_printf ("******* File system configured!!*******");
00027     }
00028     
00029     if( init_fsystem_ret & BIT1 )
00030     {
00031         vz_printf ("******* File System Ready *******");
00032     }
00033     
00034     {
00035         char s[ 32 ];
00036         mbed_mac_address( s );
00037         vz_printf ( "::Mac::%02x:%02x:%02x:%02x:%02x:%02x:: ", s[0],s[1],s[2],s[3],s[4],s[5] );
00038     }
00039     
00040     eth_status = __init_eth__ ();
00041     if ( eth_status )
00042     {
00043         vz_printf ("******* Cannot connect to eth *******");
00044     } else {
00045         vz_printf ("******* Connection eth - ok *******");
00046         int vz_io_eth_init_ret = vz_io_eth_init ();
00047         
00048         if ( vz_io_eth_init_ret == BIT0 ) vz_printf ("******* Prompt eth UDP Ready *******");
00049         
00050         if ( vz_io_eth_init_ret == BIT1 ) vz_printf ("******* Prompt eth TCP Ready *******");
00051         
00052         if ( debug_app_init ) vz_printf ( "vz_io_eth_init () :: %s", ( vz_io_eth_init_ret == ( BIT0 bitor BIT1 ) ) ? "Ok" : "Failure" );
00053         
00054         if ( debug_app_init ) vz_printf ( "init_external_wdt () :: %s", ( init_external_wdt () == 0 ) ? "Ok" : "Failure" );
00055         
00056         if ( debug_app_init ) vz_printf ( "init_bl () :: %s", ( init_bl() == 0 ) ? "Ok" : "Failure" );
00057         
00058         if ( debug_app_init ) vz_printf ( "init_clock () :: %s", ( init_clock () == 0 ) ? "Ok" : "Failure" );
00059         
00060         if ( debug_app_init ) vz_printf ( "init_pwr_src () :: %s", ( init_power_source () == 0 ) ? "Ok" : "Failure" );
00061     }
00062     
00063     sync_timer.start();
00064     
00065     led_sync_timer.start();
00066     
00067     ext = 0;
00068 
00069     port = 0;
00070     
00071     buffer_from_cb_ptr = ( uint8_t * )RXBuffer;
00072     
00073     data = NULL;
00074     
00075     type = DO_NOTHING;
00076     
00077     v_cb = new Vector ();
00078     
00079     if ( debug_app_init ) vz_printf ( "v_cb :: %s", ( v_cb not_eq NULL ) ? "Ok" : "Failure" );
00080     
00081     v_call = new Vector ();
00082     
00083     if ( debug_app_init ) vz_printf ( "v_call :: %s", ( v_call not_eq NULL ) ? "Ok" : "Failure" );
00084     
00085     ts = new Timeslice();
00086     if( ts == NULL ) {
00087         memory_is_over = true;
00088         if( debug_memory ) vz_printf ("TS allocation fail");
00089     }
00090     
00091     if ( debug_app_init ) vz_printf ( "TS :: %s", ( ts not_eq NULL ) ? "Ok" : "Failure" );
00092     
00093     for( register uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) {
00094         cb_rx_buffer[ i ] = 0;
00095         cb_tx_buffer[ i ] = 0;
00096     }
00097     
00098     reset_leds();
00099     
00100     if ( debug_app_init ) vz_printf ( "init_fw_handler() :: %s", ( init_fw_handler() == 0 ) ? "Ok" : "Failure" );
00101     
00102     if ( debug_app_init ) vz_printf ( "init_ranges() :: %s", ( init_ranges() == 0 ) ? "Ok" : "Failure" );
00103     
00104     if ( debug_app_init ) vz_printf ( "init_refresh() :: %s", ( init_refresh() == 0 ) ? "Ok" : "Failure" );
00105     
00106     if ( debug_app_init ) vz_printf ( "init_sync_refresh() :: %s", ( init_sync_refresh() == 0 ) ? "Ok" : "Failure" );
00107     
00108     count = 0;
00109     
00110     max_registered_cbx = 0;
00111     
00112     wake_all = false;
00113     
00114     pkg_wdt = RX_CB_IDLE;
00115     
00116     eth_wdt = ETH_CONNECT_TIMEOUT;
00117     
00118     invite_retry_timer.start();
00119     
00120     if ( debug_app_init ) vz_printf ( "hex_init() :: %s", ( hex_init() == 0 ) ? "Ok" : "Failure" );
00121     
00122     reset_leds();
00123     
00124     lpc_low_level_input_counter = 0;
00125     
00126     if ( debug_app_init ) vz_printf ( "init_hello () :: %s", ( init_hello ()  == 0 ) ? "Ok" : "Failure" );
00127     
00128     test_ts_timer.start ();
00129     
00130     vz_printf ( "\r\n" );
00131     
00132     return ( 0 );   
00133 }
00134 
00135 
00136 int try_reconnect_with_eth ( void )
00137 {
00138     eth_status = __init_eth__();
00139     
00140     if ( eth_status )
00141     {
00142         vz_printf ("******* Cannot connect to eth *******");
00143     } else {
00144         vz_printf ("******* Connection eth - ok *******");
00145         int vz_io_eth_init_ret = vz_io_eth_init ();
00146         
00147         if ( vz_io_eth_init_ret == BIT0 ) vz_printf ("******* Prompt eth UDP Ready *******");
00148         
00149         if ( vz_io_eth_init_ret == BIT1 ) vz_printf ("******* Prompt eth TCP Ready *******");
00150         
00151         if ( debug_app_init ) vz_printf ( "vz_io_eth_init () :: %s", ( vz_io_eth_init_ret == ( BIT0 bitor BIT1 ) ) ? "Ok" : "Failure" );
00152         
00153         if ( debug_app_init ) vz_printf ( "init_external_wdt() :: %s", ( init_external_wdt() == 0 ) ? "Ok" : "Failure" );
00154         
00155         if ( debug_app_init ) vz_printf ( "init_bl() :: %s", ( init_bl() == 0 ) ? "Ok" : "Failure" );
00156         
00157         if ( debug_app_init ) vz_printf ( "init_clock() :: %s", ( init_clock() == 0 ) ? "Ok" : "Failure" );
00158         
00159         if ( debug_app_init ) vz_printf ( "init_pwr_src () :: %s", ( init_power_source () == 0 ) ? "Ok" : "Failure" );
00160     }
00161     
00162     return eth_status;
00163 }
00164 
00165 
00166 int show_stats ( void )
00167 {
00168     char str [ 256 ];
00169     int snprintf_ret = snprintf( str, sizeof ( str ) - 1,
00170             "\n\rReceived Pkgs::\n\r "
00171             "Boot :: %u\n\r "
00172             "Registry :: %u\n\r "
00173             "Invite :: %u\n\r "
00174             "Audio :: %u\n\r "
00175             "Telemetry :: %u\n\r "
00176             "CB_stats :: %u\n\r "
00177             "CB_bye :: %u\n\r "
00178             "Prompt :: %u\n\r "
00179             "Flood :: %u\n\r "
00180             "Bootloader_cbx :: %u\n\r",
00181             boot_counter, 
00182             registry_counter, 
00183             invite_counter,
00184             audio_counter, 
00185             telemetry_counter, 
00186             cb_stats_counter, 
00187             cb_bye_counter, 
00188             prompt_counter, 
00189             flood_counter,
00190             bootloader_cbx_counter
00191     );
00192     
00193     vz_printf ( "%s", str );
00194     
00195     if ( debug_string_length ) vz_debug ( "str.length ( %d )", snprintf_ret );
00196     
00197     return ( snprintf_ret );
00198 }
00199 
00200 
00201 int reset_stats ( void )
00202 {
00203     boot_counter = 0;
00204     registry_counter = 0;
00205     invite_counter = 0;
00206     audio_counter = 0;
00207     telemetry_counter = 0;
00208     cb_bye_counter = 0;
00209     prompt_counter = 0;
00210     flood_counter = 0;
00211     bootloader_cbx_counter = 0;
00212     cb_stats_counter = 0;
00213     fw_counter = 0; 
00214 
00215     return ( 0 );
00216 }
00217 
00218 int 
00219 show_cb_list ( Vector  * v_cb )
00220 {
00221     vz_printf (
00222         "Registered %d[ %d ] CBx ( %d - %d ) -- "
00223         "Remain_timeslices :: %d :: v_call->size() :: %d", 
00224         v_cb->size(), max_registered_cbx, min_ext, max_ext, 
00225         ts->remain_timeslices(), v_call->size() 
00226     );
00227         
00228     if( v_cb->size() == 1 ) {
00229         vz_printf (" %d ", ( ( Call_Box * )v_cb -> get_element( 0 ) ) -> get_ext () );
00230     } else if( v_cb -> size() > 1 ) {
00231         char str [ 1024 ];
00232         int ext_list [ u8_MAX_CB_IN_A_BRANCH ];
00233         register int i = 0;
00234         for( ; i < v_cb -> size(); i++ ) {
00235             ext_list [ i ] = ( ( Call_Box * )v_cb->get_element ( i ) ) -> get_ext ();
00236         }
00237 
00238         qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp );
00239 
00240         char aux[ 16 ];
00241         strcpy ( str, "\r\n> " );
00242         for ( i = 0; i < v_cb -> size () - 1; i ++ )
00243         {
00244             sprintf ( aux, "%i, ", ext_list [ i ] );
00245             strcat ( str, aux );
00246             if ( ( i not_eq 0 ) and not ( ( i + 1 ) % 16 ) ) strcat ( str, "\r\n> " );
00247         }
00248         sprintf ( aux, "%i ", ext_list [ i ] );
00249         strcat ( str, aux );
00250         vz_printf ( "%s", str );
00251     }
00252     
00253     return ( 0 );
00254 }
00255 
00256 int 
00257 show_cb_long_list ( Vector  * v_cb, bool show_time, bool show_invite )
00258 {
00259     {
00260         int ext_list [ u8_MAX_CB_IN_A_BRANCH ];
00261         
00262         if ( v_cb -> size () >= 1 )
00263         {
00264             for ( register int i = 0; i < v_cb -> size (); i ++ )
00265             {
00266                 ext_list [ i ] = ( ( Call_Box * )v_cb -> get_element ( i ) ) -> get_ext ();
00267             }
00268             qsort ( ext_list, v_cb -> size (), sizeof ( int ), ls_comp );
00269         }   
00270         
00271         vz_printf ("Registered %d[ %d ] CBx ( %d - %d ) -- "
00272             "Remain_timeslices :: %d :: v_call->size() :: %d", 
00273             v_cb->size(), max_registered_cbx, min_ext, max_ext,
00274             ts->remain_timeslices(), v_call->size() 
00275         );
00276         
00277         for ( register uint8_t i = 0; i < v_cb -> size (); i ++ )
00278         {    
00279             Call_Box * cb = find_CB ( v_cb, ext_list [ i ] );
00280     
00281             if ( cb not_eq NULL )
00282             {
00283                 char cb_status [ 32 ];
00284                 char cb_sip_status [ 32 ];
00285                 switch ( cb -> get_status () )
00286                 {
00287                     case cb_idle : {
00288                         strcpy ( cb_status, "cb_idle" );
00289                         break;
00290                     }
00291                     case cb_ringing : {
00292                         strcpy ( cb_status, "cb_ringing" );
00293                         break;
00294                     }
00295                     case cb_trying : {
00296                         strcpy ( cb_status,"cb_trying" );
00297                         break;
00298                     }
00299                     case cb_on_call : {
00300                         strcpy ( cb_status, "cb_on_call" );
00301                         break;
00302                     }
00303                     case cb_busy : {
00304                         strcpy ( cb_status, "cb_busy" );
00305                         break;
00306                     }
00307                     case cb_denied : {
00308                         strcpy ( cb_status, "cb_denied" );
00309                         break;
00310                     }
00311                     case cb_bootloader : {               
00312                         strcpy ( cb_status, "cb_bootloader" );
00313                         break;
00314                     }
00315                 }
00316                 
00317                 switch( cb -> get_sip_status () ) 
00318                 {
00319                     case sip_idle : {
00320                         strcpy ( cb_sip_status, "sip_idle" );
00321                         break;
00322                     }
00323                     case sip_waiting_trying : {
00324                         strcpy ( cb_sip_status, "sip_waiting_trying" );
00325                         break;
00326                     }
00327                     case sip_trying : {
00328                         strcpy ( cb_sip_status, "sip_trying" );
00329                         break;
00330                     }
00331                     case sip_ringing : {
00332                         strcpy ( cb_sip_status, "sip_ringing" );
00333                         break;
00334                     }
00335                     case sip_busy : {
00336                         strcpy ( cb_sip_status, "sip_busy" );
00337                         break;
00338                     }
00339                     case sip_ok : {
00340                         strcpy ( cb_sip_status, "sip_ok" );
00341                         break;
00342                     }
00343                     case sip_on_call : {
00344                         strcpy ( cb_sip_status, "sip_on_call" );
00345                         break;
00346                     }
00347                     case sip_denied : {
00348                         strcpy ( cb_sip_status, "sip_denied" );
00349                         break;
00350                     }
00351                 }
00352                 
00353                 char cbx_to_string [ 254 ];
00354                 snprintf ( cbx_to_string, sizeof ( cbx_to_string ), 
00355                     "Ext :: %5i :: Port :: %5i :: Status -- %s - %s",
00356                     cb->get_ext(), cb->get_port(), cb_status, cb_sip_status
00357                 );
00358                 
00359                 if( cb -> get_timeslice () not_eq 0 )
00360                 {
00361                     char aux [ 32 ];
00362                     snprintf ( aux, sizeof ( aux ), " -- on TimeSlice :: %i",  cb->get_timeslice () );
00363                     strcat( cbx_to_string, aux );
00364                 }
00365                 
00366                 if ( show_time )
00367                 {
00368                     char get_timer_msg [ 128 ];
00369                     
00370                     snprintf ( get_timer_msg, sizeof ( get_timer_msg ) -1, " -- timer %4i [ %s ",
00371                         cb -> get_timer (), 
00372                         ( cb -> get_overflow_flag () ) ? "Overflow" : "Ok"
00373                     );
00374                 
00375                     int get_overflow_times_tmp = cb -> get_overflow_times ();
00376                 
00377                     if ( get_overflow_times_tmp == 0 ) strcat ( get_timer_msg, "]" );
00378                 
00379                     else {
00380                         char strcat_tmp [ 16 ];
00381                         if ( get_overflow_times_tmp == 1 ) snprintf ( strcat_tmp, sizeof ( strcat_tmp ) - 1, "1 time ]" );
00382                         
00383                         else snprintf ( strcat_tmp, sizeof ( strcat_tmp ) - 1, "%d times ]", get_overflow_times_tmp );
00384                         
00385                         strcat ( get_timer_msg, strcat_tmp );
00386                     }
00387                     
00388                     strcat ( cbx_to_string, get_timer_msg );
00389                 }
00390                 
00391                 if ( show_invites )
00392                 {
00393                     char get_invites_msg [ 32 ];
00394                     
00395                     snprintf ( get_invites_msg, 
00396                         sizeof ( get_invites_msg ) -1, 
00397                             " :: invites :: %u ", cb -> get_invite_counter () );
00398                     
00399                     strcat ( cbx_to_string, get_invites_msg );
00400                 }
00401                 
00402                 vz_printf ( cbx_to_string );
00403             }
00404         }   
00405     }
00406     
00407     return ( 0 );
00408 }
00409 
00410 int show_cb ( Vector  * v_cb )
00411 {
00412     vz_printf ("Registered %d ( of %d ) CBx ( %d - %d ) -- Remain_timeslices :: %d :: v_call->size() :: %d", 
00413         v_cb->size(), max_registered_cbx, min_ext, max_ext, ts->remain_timeslices(), v_call->size() 
00414     );
00415     
00416     for( register uint8_t i = 0; i < v_cb -> size (); i++ )
00417     {
00418         Call_Box * cb = ( Call_Box * )v_cb -> get_element ( i );
00419         char cb_status [ 32 ];
00420         char cb_sip_status [ 32 ];
00421         switch ( cb -> get_status () )
00422         {
00423             case cb_idle : {
00424                 strcpy ( cb_status, "cb_idle" );
00425                 break;
00426             }
00427             case cb_ringing : {
00428                 strcpy ( cb_status, "cb_ringing" );
00429                 break;
00430             }
00431             case cb_trying : {
00432                 strcpy ( cb_status,"cb_trying" );
00433                 break;
00434             }
00435             case cb_on_call : {
00436                 strcpy ( cb_status, "cb_on_call" );
00437                 break;
00438             }
00439             case cb_busy : {
00440                 strcpy ( cb_status, "cb_busy" );
00441                 break;
00442             }
00443             case cb_denied : {
00444                 strcpy ( cb_status, "cb_denied" );
00445                 break;
00446             }
00447             case cb_bootloader : {               
00448                 strcpy ( cb_status, "cb_bootloader" );
00449                 break;
00450             }
00451         }
00452         switch( cb -> get_sip_status () ) {
00453             case sip_idle : {
00454                 strcpy ( cb_sip_status, "sip_idle" );
00455                 break;
00456             }
00457             case sip_waiting_trying : {
00458                 strcpy ( cb_sip_status, "sip_waiting_trying" );
00459                 break;
00460             }
00461             case sip_trying : {
00462                 strcpy ( cb_sip_status, "sip_trying" );
00463                 break;
00464             }
00465             case sip_ringing : {
00466                 strcpy ( cb_sip_status, "sip_ringing" );
00467                 break;
00468             }
00469             case sip_busy : {
00470                 strcpy ( cb_sip_status, "sip_busy" );
00471                 break;
00472             }
00473             case sip_ok : {
00474                 strcpy ( cb_sip_status, "sip_ok" );
00475                 break;
00476             }
00477             case sip_on_call : {
00478                 strcpy ( cb_sip_status, "sip_on_call" );
00479                 break;
00480             }
00481             case sip_denied : {
00482                 strcpy ( cb_sip_status, "sip_denied" );
00483                 break;
00484             }
00485         }
00486         
00487         char cbx_to_string [ 254 ];
00488         snprintf ( cbx_to_string, sizeof ( cbx_to_string ), 
00489             "Ext :: %5i :: Port :: %5i :: Status -- %s - %s",
00490             cb -> get_ext (), cb -> get_port (), cb_status, cb_sip_status
00491         );
00492         
00493         if ( cb -> get_timeslice () not_eq 0 )
00494         {
00495             char aux [ 32 ];
00496             snprintf ( aux, sizeof ( aux ), " -- on TimeSlice :: %i",  cb -> get_timeslice () );
00497             strcat ( cbx_to_string, aux );
00498         }
00499         
00500         
00501         vz_printf ( cbx_to_string );
00502     }
00503     
00504     return ( 0 );
00505 }
00506 
00507 int show_cb_sip ( Vector  * v_cb )
00508 {
00509     vz_printf (":: Sip :: %u", v_cb -> size () );
00510     for ( register uint8_t i = 0; i < v_cb -> size (); i++ )
00511     {
00512         Call_Box * cb = ( Call_Box * ) v_cb -> get_element ( i );
00513         
00514         char tmp_send_msg [ 256 ];
00515         
00516         snprintf ( tmp_send_msg, sizeof ( tmp_send_msg ) - 1, 
00517             "ext :: %5i -- port :: %5i -- timer %4i [ %s ", 
00518             cb -> get_sip_ext (), 
00519             cb -> get_sip_port (), 
00520             cb -> get_timer (), 
00521             ( cb -> get_overflow_flag () ) ? "Overflow" : "Ok"
00522         );
00523         
00524         int get_overflow_times_tmp = cb -> get_overflow_times ();
00525         
00526         if ( get_overflow_times_tmp == 0 ) strcat ( tmp_send_msg, "]" );
00527         
00528         else {
00529             char strcat_tmp [ 16 ];
00530             if ( get_overflow_times_tmp == 1 ) snprintf( strcat_tmp, sizeof ( strcat_tmp ) - 1, "1 time ]" );
00531             
00532             else snprintf( strcat_tmp, sizeof ( strcat_tmp ) - 1, "%d times ]", get_overflow_times_tmp );
00533             
00534             strcat ( tmp_send_msg, strcat_tmp );
00535         }
00536         
00537         vz_printf ( tmp_send_msg );
00538     }    
00539     
00540     return ( 0 );
00541 }
00542 
00543 int show_sizes ( void )
00544 {
00545     sizes = false;
00546     vz_printf ( "CB_New (%u) -- CB_Delete (%u)", cb_new_counter, cb_delete_counter );
00547     vz_printf ( "SIP_New (%u) -- SIP_Delete (%u)", sip_new_counter, sip_delete_counter );
00548     vz_printf ( "RTP_header_New (%u) -- RTP_header_Delete (%u)", rtp_header_new_counter, rtp_header_delete_counter );
00549     vz_printf ( "RTP_body_New (%u) -- RTP_body_Delete (%u)", rtp_body_new_counter, rtp_body_delete_counter );
00550     vz_printf ( "Call_New (%u) -- Call_Delete (%u)", call_new_counter, call_delete_counter );
00551     vz_printf ( "lpc_low_level_input_counter :: %d", lpc_low_level_input_counter );
00552     vz_printf ( "Memory is %s", ( memory_is_over ) ? "Over" : "Ok" );
00553     vz_printf ( "Missed_Pkg :: %d ::", missed_pkg );
00554     vz_printf ( "Sizeof Sip :: %u", sizeof ( Sip ) );
00555     vz_printf ( "Sizeof Call_Box :: %u", sizeof ( Call_Box ) );
00556     vz_printf ( "Sizeof VZ_call :: %u", sizeof ( VZ_call ) );
00557     vz_printf ( "Sizeof RTP :: %u", sizeof ( RTP ) );
00558     vz_printf ( "Sizeof RTP_Header :: %u", sizeof ( RTP_Header ) );
00559     vz_printf ( "Sizeof RTP_Body :: %u", sizeof ( RTP_Body ) );
00560     vz_printf ( "Sizeof Vector :: %u", sizeof ( Vector  ) );
00561     vz_printf ("Sizeof Timeslice :: %u", sizeof ( Timeslice ) );
00562     vz_printf ( "Sizeof Watchdog :: %u", sizeof ( Watchdog ) );
00563     
00564     return ( 0 );
00565 }
00566 
00567 int check_udp_packages_pending ( Vector  * v_cb )
00568 {
00569     fd_set fdSet;
00570     FD_ZERO(&fdSet);
00571     
00572     udp_query = false;
00573     from_eth = false;
00574 
00575     for( register uint8_t i = 0; i < v_cb->size(); i++ ) {
00576         Call_Box * cb = (Call_Box *)v_cb->get_element( i );
00577         FD_SET( cb->get_sip_socket_fd(), &fdSet);
00578     }
00579     
00580     /* Recepcao de pacotes UDP para atualizacao de callboxes */
00581     FD_SET( udp_bl_client.get_fd (), &fdSet);
00582     
00583     /* Recepcao de pacotes UDP para "tickagem" do watchdog */
00584     FD_SET( udp_wdt_client.get_fd (), &fdSet);
00585     
00586     // adiciona o socket de comandos prompt-UDP-ETH
00587     FD_SET( udp_client.get_fd (), &fdSet );
00588     
00589     // adiciona o socket de pedido de clock para o servidor
00590     FD_SET( clock_sock.get_fd (), &fdSet );
00591     
00592     struct timeval t;
00593     t.tv_sec = 0;
00594     t.tv_usec = 0;
00595     int ret = lwip_select ( FD_SETSIZE, &fdSet, NULL, NULL, &t );
00596     
00597     if ( ( udp_bl_timer.read() > 30) and (bl_start_flag) ) 
00598     {
00599         udp_bl_timer.stop();
00600         udp_bl_timer.reset();
00601         bl_start_flag = 0;
00602         if (bl_ts not_eq 0) {
00603             ts->return_timeslice( bl_ts );
00604             bl_ts = 0;
00605         }
00606         if (bl_cb not_eq NULL) {
00607             bl_cb -> set_timeslice( 0 );
00608             bl_cb -> cb_set_status ( cb_idle );
00609             bl_cb = NULL;
00610         }
00611     }
00612 
00613     if ( ret > 0 )
00614     {  
00615         for ( register uint8_t i = 0; i < v_cb -> size (); i++ ) 
00616         {
00617             Call_Box * cb = ( Call_Box * )v_cb -> get_element ( i );
00618             int fd =  cb -> get_sip_socket_fd ();
00619             if ( FD_ISSET ( fd, &fdSet ) ) 
00620             {
00621                 cb -> sip_udp_incomming_pkg ();
00622             }
00623         }
00624         
00625         /* Tratamento dos pacotes de bootloader vindo do servidor */
00626         if( FD_ISSET( udp_bl_client.get_fd(), &fdSet ) ) {
00627             if( udp_bl_client.receiveFrom( udp_bl_server, bl_recv_buffer, sizeof( bl_recv_buffer ) ) > 0 ) {
00628                 uint16_t cnt = 0;
00629                 if (debug_bootloader) {
00630                     pc.printf("\r\nPACOTE SRV->HDR {");
00631                     for (cnt = 0;cnt < UDP_BL_SIZE;cnt++) {
00632                         if ((cnt % 30) == 0) {
00633                             pc.printf("\r\n  ");
00634                             pc.printf(hex16(cnt));
00635                             pc.printf(" : ");
00636                         }
00637                         pc.printf(hex8(bl_recv_buffer[cnt]));
00638                         pc.printf(", ");
00639                     }
00640                     pc.printf("\r\n}");
00641                 }
00642                 bl_peer = ((uint8_t)bl_recv_buffer[0])*256 + (uint8_t)bl_recv_buffer[1];
00643                 bl_send_buffer[0] = bl_recv_buffer[0];
00644                 bl_send_buffer[1] = bl_recv_buffer[1];
00645                 for (cnt = 2;cnt < UDP_BL_SIZE;cnt++) {
00646                     bl_send_buffer[cnt] = 0;
00647                 }
00648                 
00649                 if ((bl_ts == 0) and !(bl_start_flag)) {
00650                     bl_ts = ts->get_timeslice();
00651                 }
00652                 
00653                 /* caso nao haja timeslice disponivel informar servidor */
00654                 if ((bl_ts == 0) and !(bl_start_flag)) {
00655                     strncpy(bl_send_buffer + 2,"cbxdead\x00",8);
00656                     int udp_bl_client_ret = udp_bl_client.sendTo ( udp_bl_server, bl_send_buffer, strlen ( bl_send_buffer ) );
00657                     
00658                     if ( udp_bl_client_ret not_eq strlen( bl_send_buffer ) )
00659                     {
00660                             reconnect_bl ();
00661                             miss_bl_udp_send_pkg ++;
00662                             if ( debug_reconnect ) vz_printf ( "[%d] Reconnect BL - %d", bl_peer, udp_bl_client_ret );
00663                     }
00664                 } else {
00665                     
00666                     /* pacote para verificar se o callbox esta disponivel */
00667                     if (!(strncmp(bl_recv_buffer + 2,"avaiable?",9))) {
00668                         bl_start_flag = 1;
00669                         udp_bl_timer.start();
00670                         udp_bl_timer.reset();
00671                         strncpy(bl_send_buffer + 2,"cbxalive\x00",9);
00672                         if (debug_bootloader) {
00673                             pc.printf("\r\nPACOTE HDR->SRV {");
00674                             for (cnt = 0;cnt < UDP_BL_SIZE;cnt++) {
00675                                 if ((cnt % 30) == 0) {
00676                                     pc.printf("\r\n  ");
00677                                     pc.printf(hex16(cnt));
00678                                     pc.printf(" : ");
00679                                 }
00680                                 pc.printf(hex8(bl_send_buffer[cnt]));
00681                                 pc.printf(", ");
00682                             }
00683                             pc.printf("\r\n}");
00684                         }
00685                         int udp_bl_client_ret = udp_bl_client.sendTo ( udp_bl_server, bl_send_buffer, strlen ( bl_send_buffer ) );
00686                         
00687                         if ( udp_bl_client_ret not_eq strlen( bl_send_buffer ) )
00688                         {
00689                                 reconnect_bl ();
00690                                 miss_bl_udp_send_pkg ++;
00691                                 if ( debug_reconnect ) vz_printf ( "[%d] Reconnect BL - %d", bl_peer, udp_bl_client_ret );
00692                         }
00693                     }
00694                     
00695                     /* pacote para indicar o termino do processo de atualizacao */
00696                     else if (!(strncmp(bl_recv_buffer + 2,"finished",8))) {
00697                         bl_start_flag = 0;
00698                         if (bl_ts not_eq 0) {
00699                             ts->return_timeslice( bl_ts );
00700                             bl_ts = 0;
00701                         }
00702                         if (bl_cb not_eq NULL) {
00703                             bl_cb->set_timeslice( 0 );
00704                             bl_cb -> cb_set_status( cb_idle );
00705                             bl_cb = NULL;
00706                         }
00707                     }
00708                     
00709                     /* pacotes a serem repassados para o callbox */
00710                     else if (bl_start_flag) {
00711                         uint16_t bl_cnt = 0;
00712                         udp_bl_timer.reset();
00713                         /* pacote de ERASE enviado para o cbx */
00714                         /* neste momento a cabeceira sabe que o servidor se comunicou com o callbox */
00715                         if (!(strncmp(bl_recv_buffer + 2,"\x45",1))) {
00716                             bl_cb = find_CB( v_cb, bl_peer );
00717                             if (bl_cb not_eq NULL) {
00718                                 bl_cb->set_timeslice( bl_ts );
00719                                 bl_cb -> cb_set_status ( cb_bootloader );
00720                             }
00721                         }
00722                         /* pacote de OK enviado para o cbx */
00723                         /* neste momento a cabeceira desaloca o timeslice do callbox  */
00724                         if (!(strncmp(bl_recv_buffer + 2,"\x4f\x00\x00\x00",4))) {
00725                             ts->return_timeslice( bl_ts );
00726                             bl_ts = 0;
00727                             if (bl_cb not_eq NULL) {
00728                                 bl_cb->set_timeslice( 0 );
00729                             }
00730                         }
00731                         bl_cbx_buffer[0] = bl_ts;
00732                         for (bl_cnt = 0; bl_cnt < BL_SIZE; bl_cnt++) {
00733                             bl_cbx_buffer[bl_cnt + 1] = bl_recv_buffer[bl_cnt + 2];
00734                         }
00735                         if (debug_bootloader) {
00736                             pc.printf("\r\nPACOTE HDR->CBX {");
00737                             for (cnt = 0;cnt < BL_SIZE + 1;cnt++) {
00738                                 if ((cnt % 30) == 0) {
00739                                     pc.printf("\r\n  ");
00740                                     pc.printf(hex16(cnt));
00741                                     pc.printf(" : ");
00742                                 }
00743                                 pc.printf(hex8(bl_cbx_buffer[cnt]));
00744                                 pc.printf(", ");
00745                             }
00746                             pc.printf("\r\n}");
00747                         }
00748                         if (bl_cb not_eq NULL) {
00749                             bl_port = bl_cb->get_port();
00750                         } else {
00751                             bl_port = bl_peer;
00752                         }
00753                         send2callboxes( build_cb_package( bl_peer, bl_port, BOOTLOADER_CBX,bl_cbx_buffer, 0, BL_SIZE + 1, write_buffer) );
00754                     }
00755                 }
00756             }
00757         }
00758         
00759         // verifica o socket do prompt-UDP-ETH                
00760         if( FD_ISSET( udp_client.get_fd(), &fdSet ) ) {
00761             char to_prompt_process [ PROMPT_UDP_COMMAND_SIZE ];
00762             for( register int i = 0; i < PROMPT_UDP_COMMAND_SIZE; i++ ) to_prompt_process[ i ] = 0;
00763             
00764             int prompt_process_msg_rcv = udp_client.receiveFrom ( udp_server, to_prompt_process, ( PROMPT_UDP_COMMAND_SIZE - 1 ) );
00765             
00766             if ( prompt_process_msg_rcv == -1 )
00767             {
00768                 if ( debug_reconnect ) vz_printf ("Reconnect Prompt Process");
00769                 reconnect_udp_prompt_process ();
00770                 miss_prompt_udp_rcv_pkg ++;
00771             } 
00772                 else if ( prompt_process_msg_rcv > 0 )
00773             {
00774                 udp_query = true;
00775                 from_eth = true;
00776                 prompt_process ( to_prompt_process, prompt_process_msg_rcv );
00777             }
00778         } 
00779         
00780         // verifica o socket do watchdog
00781         if ( FD_ISSET ( udp_wdt_client.get_fd(), &fdSet ) )
00782         {
00783             const uint16_t WAKE_MSG_SIZE = 768;
00784             static char wake_msg [ WAKE_MSG_SIZE ];
00785             
00786             for ( register uint16_t i = 0; i < WAKE_MSG_SIZE; i ++ ) wake_msg [ i ] = 0;
00787             
00788             Endpoint udp_wdt_server;
00789             
00790             int wake_msg_rcv = udp_wdt_client.receiveFrom ( udp_wdt_server, wake_msg, sizeof ( wake_msg ) );
00791             
00792             if ( dmissed_wdt ) vz_debug ("wake_msg_rcv :: %d -- wake_msg :: %s", wake_msg_rcv, wake_msg );
00793             
00794             if( wake_msg_rcv == -1 )
00795             {
00796                 if( debug_reconnect ) vz_printf ("Reconnect Extern wdt");
00797                 reconnect_extern_wdt_socket ();
00798                 miss_wdt_send_pkg ++;
00799             } 
00800                 else if( wake_msg_rcv > 0 )
00801             {
00802                 if( !( strncmp( wake_msg, "alive", 5 ) ) ) {
00803                     // Just ckeck but not set 'alive?'
00804                     // 'alive*' - force wdt tick right now
00805                     // Ckecking and set 'alive'
00806                     bool question_alive = ( wake_msg[ 5 ] == '?' );
00807                     if( wake_msg[ 5 ] == '*' ) wdt.kick();
00808                     
00809                     if ( dmissed_wdt ) if ( wake_msg [ 5 ] == '?' ) vz_debug ("Recebi o alive ???");
00810                     
00811                     build_wdt_string ( wake_msg, WAKE_MSG_SIZE - 1 );
00812                     
00813                     wake_msg[ WAKE_MSG_SIZE - 1 ] = 0;
00814                     
00815                     int send = udp_wdt_client.sendTo ( udp_wdt_server, wake_msg, strlen ( wake_msg ) );
00816                     if( send not_eq strlen ( wake_msg ) )
00817                     {
00818                         if ( debug_reconnect ) vz_debug ("Reconnect Extern wdt (%d, %d)", send, strlen ( wake_msg ) );
00819                         reconnect_extern_wdt_socket ();
00820                         miss_wdt_send_pkg ++;
00821                     }
00822                     
00823                     if ( ( 
00824                             (!question_alive) 
00825                             && ( cb_new_counter <= u8_MAX_CB_IN_A_BRANCH ) 
00826                             && ( ( cb_new_counter >=  MIN_CBX_IN_A_BRANCH ) || ( disable_wdt_from_cbx ) ) 
00827                     ) ) 
00828                     {
00829                         external_wdt = EXTERN_WDT_IDLE;
00830                         if ( dmissed_wdt ) vz_debug ("kickando");
00831                     }
00832                     
00833                     if ( dmissed_wdt ) vz_debug ("Mandei send = %d[::%s::] to [%s:%i]", send, wake_msg, udp_wdt_server.get_address (), udp_wdt_server.get_port () );
00834                     
00835                 } else if( !( strncmp( wake_msg, "reset", 5 ) ) ) {
00836                     external_wdt = 0;
00837 
00838                     sprintf( wake_msg, "rst:%u:", uptime );
00839                     int send = udp_wdt_client.sendTo ( udp_wdt_server, wake_msg, strlen( wake_msg ) );
00840                     if( send not_eq strlen( wake_msg ) )
00841                     {
00842                         if( debug_reconnect ) vz_printf ("Reconnect Extern wdt");
00843                         reconnect_extern_wdt_socket ();
00844                         miss_wdt_send_pkg ++;
00845                     }
00846                 }
00847             }
00848         }
00849         
00850         if( FD_ISSET( clock_sock.get_fd(), &fdSet ) )
00851         {
00852             update_clock ();
00853         }
00854     }
00855     
00856     return ( ret );    
00857 }
00858 
00859 int show_cb_content ( void )
00860 {
00861     Call_Box * cb = find_CB ( v_cb, print_this_cb );
00862     if ( cb not_eq NULL ) {
00863         cb -> print_yourself ();
00864     } else {
00865         if ( print_values ) {
00866             vz_debug ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
00867         } else {
00868             vz_printf ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
00869         }
00870     }
00871     
00872     return ( 0 );
00873 }
00874 
00875 int show_cb_content_all ( void )
00876 {
00877     if ( v_cb -> size () == 0 ) {
00878         vz_printf ("known CBx :: 0");
00879     }
00880     
00881     for ( register uint8_t i = 0; i < v_cb->size(); i++ )
00882     {
00883         Call_Box * cb = (Call_Box *)v_cb->get_element( i );
00884                 
00885         if ( cb not_eq NULL ) {
00886             cb -> print_yourself ();
00887         } else {
00888             if ( print_values ) vz_debug ("Objeto CBx ( %d ) nao encontrado", i );
00889         }
00890     }
00891     
00892     return ( 0 );
00893 }
00894 
00895 int show_rtp ( void )
00896 {
00897     vz_printf (":: RTP :: %u", v_cb->size() );
00898     
00899     int ext_list[ u8_MAX_CB_IN_A_BRANCH ];
00900         
00901     if( v_cb->size() >= 1 ) {
00902         for( register int i = 0; i < v_cb->size(); i++ )
00903         {
00904             ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext();
00905         }
00906         qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp );
00907     }   
00908     
00909     for ( register uint8_t i = 0; i < v_cb->size(); i++ )
00910     {
00911         Call_Box * cb = find_CB( v_cb, ext_list[ i ] );
00912         if( cb not_eq NULL )
00913         {
00914             vz_printf ("CBX ( %d,  %d ) - SIP  ( %d,  %d ) - RTP ( %d )", 
00915                 cb -> get_ext (),
00916                 cb -> get_port (),
00917                 cb -> get_sip_ext (), 
00918                 cb -> get_sip_port (), 
00919                 cb -> get_rtp_port ()
00920             );
00921         }
00922     }
00923     
00924     return ( 0 );
00925 }
00926 
00927 int 
00928 fuck_rtp ( Vector  * v_cb )
00929 {
00930     Call_Box * cb = find_CB( v_cb, frtp_target );
00931     
00932     if ( cb not_eq NULL ) {
00933         cb -> set_rtp_port ( -1008789032 );
00934     } else {
00935         vz_debug ("frtp fail");
00936     }
00937     
00938     return ( 0 );
00939 }
00940 
00941 int 
00942 show_hex_cb_content ( void )
00943 {
00944     uint8_t * ptr = ( uint8_t * ) find_CB ( v_cb, print_hex_this_cb );    
00945     
00946     if ( ptr not_eq NULL )
00947     {
00948         vz_printf ( "Values :: %p\r\n", ( void *) ptr );            
00949         
00950         for ( register int i = 0; i < sizeof( Call_Box ); i++ )
00951         {
00952             if( debug_uart3 ) pc.printf("%x", *ptr++ );
00953             
00954             if ( ( i % 32 ) == 0 )
00955             {
00956                 if ( i not_eq 0 ) {
00957                     if( debug_uart3 ) pc.printf("\n\r> ");
00958                 } else {
00959                     if( debug_uart3 ) pc.printf(" ");
00960                 }
00961             } else {
00962                 if( debug_uart3 ) pc.printf(" ");
00963             }
00964         }
00965         
00966         if ( debug_uart3 ) pc.printf("\n\r> ");
00967         
00968         if ( tcp_session ) {
00969             char aux[ ( sizeof( Call_Box ) * 3 ) + 3 ];
00970             
00971             for ( register int i = 0; i < ( sizeof( Call_Box ) * 3 ) + 3 ; i++ ) aux [ i ] = 0;
00972             
00973             uint8_t * ptr = ( uint8_t * ) find_CB ( v_cb, print_hex_this_cb );
00974             
00975             for ( register int i = 0; i < sizeof( Call_Box ); i++ )
00976             {
00977                 char tmp[ 16 ];
00978                 sprintf( tmp, "%x ", *ptr++ );  
00979                 strcat( aux, tmp );
00980             }
00981             
00982             strcat( aux, "\n\r\0" );
00983             tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) );
00984             tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) );
00985         }
00986     }
00987         else
00988     {
00989         if ( print_values ) vz_debug ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
00990     }
00991     
00992     return ( 0 );
00993 }
00994 
00995 int show_hex_cb_content_all ()
00996 {
00997     if ( v_cb -> size () == 0 )
00998     {
00999         vz_printf ("known CBx :: 0");    
01000     }
01001     
01002     for ( register uint8_t j = 0; j < v_cb->size(); j++ )
01003     {
01004         uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j );
01005                     
01006         if ( ptr not_eq NULL )
01007         {
01008             vz_printf ("Values :: %p\r\n", ( void *) ptr );
01009             for ( register int i = 0; i < sizeof( Call_Box ); i++ )
01010             {
01011                 if( debug_uart3 ) pc.printf("%x", *ptr++ );
01012                 
01013                 if ( ( i % 32 ) == 0 )
01014                 {
01015                     if( i not_eq 0 ) {
01016                         if( debug_uart3 ) pc.printf("\n\r> ");
01017                     } else {
01018                         if( debug_uart3 ) pc.printf(" ");
01019                     }
01020                 } else {
01021                     if( debug_uart3 ) pc.printf(" ");
01022                 }
01023             }
01024             
01025             if ( debug_uart3 ) pc.printf("\n\r> ");
01026             
01027             if ( tcp_session )
01028             {
01029                 char aux[ ( sizeof( Call_Box ) * 3 ) + 3 ];
01030                 
01031                 for ( register int i = 0; i < ( sizeof( Call_Box ) * 3 ) + 3 ; i++ ) aux [ i ] = 0;
01032                 
01033                 uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j );
01034                 
01035                 for ( register int i = 0; i < sizeof( Call_Box ); i++ )
01036                 {
01037                     char tmp[ 16 ];
01038                     sprintf( tmp, "%x ", *ptr++ );  
01039                     strcat( aux, tmp );
01040                 }
01041                 
01042                 strcat( aux, "\n\r\0" );
01043                 tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) );
01044                 tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) );
01045             }
01046         } else {
01047             if ( print_values ) {
01048                 vz_debug ("Objeto CBx ( %d ) nao encontrado", j );
01049             } else {
01050                 vz_printf ("Objeto CBx ( %d ) nao encontrado", j );
01051             }
01052         }
01053     }
01054     
01055     return ( 0 );    
01056 }
01057 
01058 
01059 int show_cb_sip ( void )
01060 {
01061     Call_Box * cb = find_CB ( v_cb, print_this_sip );
01062     
01063     if ( cb not_eq NULL )
01064     {
01065         if( cb -> sip_print_yourself () == -1 )
01066         {
01067             if( print_values ) {
01068                 vz_debug ("Sip param of %d equals NULL", print_this_sip );
01069             } else {
01070                 vz_printf ("Sip param of %d equals NULL", print_this_sip );
01071             }
01072         }
01073     } else {
01074         if ( print_values ) {
01075             vz_debug ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
01076         } else {
01077             vz_printf ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
01078         }                
01079     }
01080     
01081     return ( 0 );    
01082 }
01083 int show_cb_sip_all ( void )
01084 {    
01085     if ( v_cb -> size () == 0 )
01086     {
01087         vz_printf ("known CBx :: 0");
01088     }
01089     
01090     for ( register uint8_t i = 0; i < v_cb->size(); i++ )
01091     {
01092         Call_Box * cb = (Call_Box *)v_cb->get_element( i );
01093                 
01094         if ( cb not_eq NULL )
01095         {
01096             if ( cb -> sip_print_yourself () == -1 )
01097             {
01098                 if ( print_values ) {
01099                     vz_debug ("Sip param of %d equals NULL", print_this_sip );
01100                 } else {
01101                     vz_printf ("Sip param of %d equals NULL", print_this_sip );
01102                 }
01103             }
01104         } else {
01105             if( print_values ) {
01106                 vz_debug ("CBx %d not found", print_this_sip );
01107             } else {
01108                 vz_printf ("CBx %d not found", print_this_sip );
01109             }
01110         }
01111     }
01112     return ( 0 );    
01113 }
01114 
01115 int show_cb_hex_sip ( void )
01116 {
01117     uint8_t * ptr = NULL;
01118     
01119     Call_Box * cb = find_CB ( v_cb, print_hex_this_sip );
01120     
01121     if( cb not_eq NULL )
01122     {
01123         ptr = ( uint8_t * ) cb -> get_sip ();
01124     }
01125 
01126     if ( ptr not_eq NULL )
01127     {
01128         vz_printf ("Values :: %p\r\n", ( void *) ptr );
01129         for ( register int i = 0; i < sizeof( Sip ); i++ )
01130         {
01131             if( debug_uart3 ) pc.printf("%x", *ptr++ );
01132             
01133             if ( ( i % 32 ) == 0 )
01134             {
01135                 if( i not_eq 0 ) {
01136                     if( debug_uart3 ) pc.printf("\n\r> ");
01137                 } else {
01138                     if( debug_uart3 ) pc.printf(" ");
01139                 }
01140             } else {
01141                 if( debug_uart3 ) pc.printf(" ");
01142             }
01143         }
01144         
01145         if ( debug_uart3 ) pc.printf("\n\r> ");
01146         
01147         if ( tcp_session ) {
01148             char aux[ ( 32 * 3 ) + 5 ];
01149             
01150             for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0;
01151             
01152             ptr = NULL;
01153     
01154             print_hex_sip_var = false;
01155             Call_Box * cb = find_CB ( v_cb, print_hex_this_sip );
01156             if( cb not_eq NULL )
01157             {
01158                 ptr = ( uint8_t * ) cb -> get_sip ();
01159             }
01160     
01161             if ( ptr not_eq NULL )
01162             {
01163                 bool finished = false;
01164                 volatile int i = 0;
01165                 int count = 0;
01166                 Timer dont_overwrite_tcp_buffer;
01167                 dont_overwrite_tcp_buffer.reset ();
01168                 dont_overwrite_tcp_buffer.start ();
01169                 
01170                 while ( !finished )
01171                 {
01172                     if( dont_overwrite_tcp_buffer.read_ms () >= 500 )
01173                     {
01174                         dont_overwrite_tcp_buffer.reset();
01175                         for ( ; i < sizeof( Sip ); i++ )
01176                         {
01177                             char tmp[ 16 ];
01178                             sprintf( tmp, "%x ", *ptr++ );  
01179                             strcat( aux, tmp );
01180                             count++;
01181                             
01182                             if ( count >= 32  )
01183                             {
01184                                 count = 0;
01185                                 i++;
01186                                 break;
01187                             }
01188                         }                                
01189                         strcat ( aux, "\n\r\0" );
01190                         tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) );
01191                         if ( i >= sizeof ( Sip ) ) finished = true;
01192                         strcpy ( aux, "\0" );
01193                     }
01194                 }
01195                 dont_overwrite_tcp_buffer.reset();
01196                 dont_overwrite_tcp_buffer.stop();
01197                 tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) );
01198             }
01199         }
01200     }
01201         else
01202     {
01203         if ( print_values ) vz_debug ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
01204     }
01205     
01206     
01207     return ( 0 );    
01208 }
01209 
01210 int show_rtp_on_call ( void )
01211 {
01212     vz_printf (":: CAll RTP :: %u", v_call->size() );
01213     
01214     for( register uint8_t i = 0; i < v_call->size(); i++ ) {
01215         VZ_call * call = ( VZ_call * )v_call->get_element( i );
01216         if( call not_eq NULL )
01217         {
01218             vz_printf ("CBX ( %d,  %d ) - Server ( %d,  %d )", 
01219                 call->get_cb_ext(),
01220                 call->get_cb_port(), 
01221                 call->get_rtp_server_ext(), 
01222                 call->get_rtp_server_port()
01223             );
01224         }
01225     }
01226     
01227     return ( 0 );    
01228 }
01229 
01230 int show_call ( void )
01231 {
01232     VZ_call * call = find_Call ( v_call, print_this_call );
01233     
01234     if ( call not_eq NULL )
01235     {
01236         vz_printf ("Values :: %p\r\n", ( void *) call );
01237         call -> print_yourself ();
01238     }
01239         else
01240     {            
01241         if( print_values ) {
01242             vz_debug ("Call %d not found", print_this_call );
01243         } else {
01244             vz_printf ("Call %d not found", print_this_call );
01245         }
01246     }
01247     
01248     return ( 0 );
01249 }
01250 
01251 int show_hex_call ( void )
01252 {
01253     VZ_call * call = find_Call ( v_call, print_hex_this_call );
01254             
01255     uint8_t * ptr = NULL;
01256     if( call not_eq NULL )
01257     {
01258         ptr = ( uint8_t * ) call;
01259     }
01260 
01261     if ( ptr not_eq NULL )
01262     {
01263         vz_printf ("Values :: %p\r\n", ( void *) ptr );
01264         for ( register int i = 0; i < sizeof( VZ_call ); i++ )
01265         {
01266             if( debug_uart3 ) pc.printf("%x", *ptr++ );
01267             
01268             if ( ( i % 32 ) == 0 )
01269             {
01270                 if( i not_eq 0 ) {
01271                     if( debug_uart3 ) pc.printf("\n\r> ");
01272                 } else {
01273                     if( debug_uart3 ) pc.printf(" ");
01274                 }
01275             }        
01276                 else
01277             {
01278                 if( debug_uart3 ) pc.printf(" ");
01279             }
01280         }
01281         if ( debug_uart3 ) pc.printf("\n\r> ");
01282         
01283         if ( tcp_session ) {
01284             char aux[ ( 32 * 3 ) + 5 ];
01285             
01286             for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0;
01287             
01288             ptr = NULL;
01289     
01290             VZ_call * call = find_Call ( v_call, print_hex_this_call );
01291             
01292             uint8_t * ptr = NULL;
01293             if( call not_eq NULL )
01294             {
01295                 ptr = ( uint8_t * ) call;
01296             }
01297     
01298             if ( ptr not_eq NULL )
01299             {
01300                 bool finished = false;
01301                 volatile int i = 0;
01302                 int count = 0;
01303                 Timer dont_overwrite_tcp_buffer;
01304                 dont_overwrite_tcp_buffer.reset ();
01305                 dont_overwrite_tcp_buffer.start ();
01306                 
01307                 while ( !finished )
01308                 {
01309                     if( dont_overwrite_tcp_buffer.read_ms () >= 20 )
01310                     {
01311                         dont_overwrite_tcp_buffer.reset();
01312                         for ( ; i < sizeof( VZ_call ); i++ )
01313                         {
01314                             char tmp[ 16 ];
01315                             sprintf( tmp, "%x ", *ptr++ );  
01316                             strcat( aux, tmp );
01317                             count++;
01318                             
01319                             if ( count >= 32  )
01320                             {
01321                                 count = 0;
01322                                 i++;
01323                                 break;
01324                             }
01325                         }                                
01326                         strcat ( aux, "\n\r\0" );
01327                         tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) );
01328                         if ( i >= sizeof ( VZ_call ) ) finished = true;
01329                         strcpy ( aux, "\0" );
01330                     }
01331                 }
01332                 dont_overwrite_tcp_buffer.reset();
01333                 dont_overwrite_tcp_buffer.stop();
01334                 tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) );
01335             }
01336         }
01337     }
01338         else
01339     {   
01340         if( print_values )
01341         {
01342             vz_debug ("Call %d not found", print_this_call );
01343         }
01344             else 
01345         {
01346             vz_printf ("Call %d not found", print_this_call );
01347         }
01348     }
01349     
01350     return ( 0 );    
01351 }
01352 
01353 
01354 int show_cb_rtp ( void )
01355 {
01356     VZ_call * call = find_Call ( v_call, print_hex_this_rtp );
01357             
01358     uint8_t * ptr = NULL;
01359     if( call not_eq NULL )
01360     {
01361         ptr = ( uint8_t * ) call -> check_rtp ();
01362     }
01363 
01364     if ( ptr not_eq NULL )
01365     {
01366         vz_printf ("Values :: %p\r\n", ( void *) ptr );
01367         for ( register int i = 0; i < sizeof( RTP ); i++ )
01368         {
01369             if( debug_uart3 ) pc.printf("%x", *ptr++ );
01370             
01371             if ( ( i % 32 ) == 0 )
01372             {
01373                 if( i not_eq 0 )
01374                 {
01375                     if( debug_uart3 ) pc.printf("\n\r> ");
01376                 }  
01377                     else
01378                 {
01379                     if( debug_uart3 ) pc.printf(" ");
01380                 }
01381             }        
01382                 else
01383             {
01384                 if( debug_uart3 ) pc.printf(" ");
01385             }
01386         }
01387         if ( debug_uart3 ) pc.printf("\n\r> ");
01388         
01389         if ( tcp_session ) {
01390             char aux[ ( 32 * 3 ) + 5 ];
01391             
01392             for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0;
01393             
01394             ptr = NULL;
01395     
01396             VZ_call * call = find_Call ( v_call, print_hex_this_rtp );
01397             
01398             uint8_t * ptr = NULL;
01399             if( call not_eq NULL )
01400             {
01401                 ptr = ( uint8_t * ) call -> check_rtp ();
01402             }
01403     
01404             if ( ptr not_eq NULL )
01405             {
01406                 bool finished = false;
01407                 volatile int i = 0;
01408                 int count = 0;
01409                 Timer dont_overwrite_tcp_buffer;
01410                 dont_overwrite_tcp_buffer.reset ();
01411                 dont_overwrite_tcp_buffer.start ();
01412                 
01413                 while ( !finished )
01414                 {
01415                     if( dont_overwrite_tcp_buffer.read_ms () >= 20 )
01416                     {
01417                         dont_overwrite_tcp_buffer.reset();
01418                         for ( ; i < sizeof( RTP ); i++ )
01419                         {
01420                             char tmp[ 16 ];
01421                             sprintf( tmp, "%x ", *ptr++ );  
01422                             strcat( aux, tmp );
01423                             count++;
01424                             
01425                             if ( count >= 32  )
01426                             {
01427                                 count = 0;
01428                                 i++;
01429                                 break;
01430                             }
01431                         }                                
01432                         strcat ( aux, "\n\r\0" );
01433                         tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) );
01434                         if ( i >= sizeof ( RTP ) ) finished = true;
01435                         strcpy ( aux, "\0" );
01436                     }
01437                 }
01438                 dont_overwrite_tcp_buffer.reset();
01439                 dont_overwrite_tcp_buffer.stop();
01440                 tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) );
01441             }
01442         }
01443     }
01444         else
01445     {   
01446         if( print_values )
01447         {
01448             vz_debug ("Call %d not found", print_this_rtp );
01449         }
01450             else 
01451         {
01452             vz_printf ("Call %d not found", print_this_rtp );
01453         }
01454     }
01455     
01456     return ( 0 );    
01457 }
01458 
01459 int show_cb_hex_rtp ( void )
01460 {
01461     VZ_call * call = find_Call ( v_call, print_this_rtp );
01462     
01463     if ( call not_eq NULL )
01464     {
01465         if ( call -> check_rtp () not_eq NULL )
01466         {
01467             vz_printf ("Values :: %p\r\n", ( void *) call -> check_rtp () );
01468             call -> rtp_print_yourself ();
01469         }
01470             else
01471         {
01472             if( print_values )
01473             {
01474                 vz_debug ("Call->RTP %d not found", print_this_rtp );
01475             }
01476                 else 
01477             {
01478                 vz_printf ("Call->RTP %d not found", print_this_rtp );
01479             }       
01480         }
01481     }
01482         else
01483     {            
01484         if( print_values )
01485         {
01486             vz_debug ("Call->RTP %d not found", print_this_rtp );
01487         }
01488             else 
01489         {
01490             vz_printf ("Call->RTP %d not found", print_this_rtp );
01491         }
01492     }
01493     
01494     return ( 0 );    
01495 }
01496 
01497 
01498 int reset_stats_cks ( void )
01499 {
01500     pkg_cksok = 0;
01501     pkg_ckserr = 0;
01502     pkg_zero = 0;
01503     out_of_range = 0;
01504     missed_pkg = 0;
01505     delayed_pkg_to_cb = 0;
01506     cpld_pkg_tx_counter = 0;
01507     cpld_pkg_rx_counter = 0;
01508     
01509     return ( 0 );
01510 }
01511 
01512 int show_cb_stats ( void )
01513 {
01514     vz_printf ( 
01515         "\r\n"
01516         "   PKG_CKS OK: %d ( %003.2f )\r\n"
01517         "   PKG_CKS ERR: %d ( %003.2f )\r\n"
01518         "   PKG_0: %d ( %003.2f )\r\n"
01519         "   Out_of_range: %d ( %003.2f )\r\n"
01520         "   Miss_Pkg: %d\r\n"
01521         "   TX_delayed: %d\n\r"
01522         "   TX_Counter: %d\n\r"
01523         "   RX_Counter: %d\r\n",
01524         pkg_cksok, ( cpld_pkg_rx_counter  == 0 ) ? 0.00 : ( (  double ) pkg_cksok / cpld_pkg_rx_counter ) * 100, 
01525         pkg_ckserr, ( cpld_pkg_rx_counter  == 0 ) ? 0.00 : ( ( double ) pkg_ckserr / cpld_pkg_rx_counter ) * 100, 
01526         pkg_zero, ( cpld_pkg_rx_counter  == 0 ) ? 0.00 : ( ( double ) pkg_zero / cpld_pkg_rx_counter ) * 100, 
01527         out_of_range, ( cpld_pkg_rx_counter  == 0 ) ? 0.00 : ( ( double ) out_of_range / cpld_pkg_rx_counter ) * 100, 
01528         missed_pkg, 
01529         delayed_pkg_to_cb, 
01530         cpld_pkg_tx_counter, 
01531         cpld_pkg_rx_counter 
01532     );
01533     
01534     return ( 0 );   
01535 }
01536 
01537 
01538 int reset_missed_send_udp ( void )
01539 {
01540     miss_fw_send_pkg = 0;
01541     miss_prompt_udp_send_pkg = 0;
01542     miss_sip_registry_send_pkg = 0;
01543     miss_sip_invite_send_pkg = 0;
01544     miss_sip_bye_send_pkg = 0;
01545     miss_sip_ok_send_pkg = 0;
01546     miss_sip_rcv_bye_send_pkg = 0;
01547     miss_wdt_send_pkg = 0;
01548     miss_prompt_udp_send_pkg = 0;
01549     miss_rtp_udp_send_pkg = 0;
01550     miss_prompt_udp_rcv_pkg = 0;
01551     
01552     return ( 0 );
01553 }
01554 
01555 int show_missed_send_udp_pkg ( void )
01556 {
01557     vz_printf (   "\r\nMissed pkgs ::\r\n "
01558                 "FW: %d\r\n "
01559                 "Prompt UDP %d\r\n "
01560                 "Registry %d\r\n "
01561                 "Invite %d\r\n "
01562                 "Bye %d\r\n "
01563                 "UDP incoming ( invite ans ) %d\r\n "
01564                 "UDP incoming ( bye from * ) %d\r\n "
01565                 "Wdt [ alive | rst ] %d\r\n "
01566                 "Rcv prompt %d\r\n "
01567                 "[ debug | send ]_msg %d\r\n "
01568                 "RTP %d\r\n", 
01569         miss_fw_send_pkg, 
01570         miss_prompt_udp_send_pkg, 
01571         miss_sip_registry_send_pkg, 
01572         miss_sip_invite_send_pkg, 
01573         miss_sip_bye_send_pkg, 
01574         miss_sip_ok_send_pkg, 
01575         miss_sip_rcv_bye_send_pkg, 
01576         miss_wdt_send_pkg, 
01577         miss_prompt_udp_send_pkg,
01578         miss_prompt_udp_rcv_pkg,
01579         miss_rtp_udp_send_pkg 
01580     );
01581     
01582     return ( 0 );
01583 }
01584 
01585 int 
01586 invite_ack_to_cb_handler ( Vector  * v_cb, Vector  * v_call )
01587 {    
01588     static int retry_invite_pkg = 0;
01589     bool need_retry = false;
01590     Call_Box * cb = NULL;
01591 
01592     for ( register uint8_t i = 0; i < v_cb -> size (); i++ )
01593     {
01594         retry_invite_pkg ++;
01595         if ( retry_invite_pkg >= v_cb -> size () ) retry_invite_pkg = 0;
01596         
01597         cb = (Call_Box * )v_cb -> get_element ( i );
01598         
01599         if ( cb not_eq NULL )
01600         {
01601             switch ( cb -> get_status () )
01602             {
01603                 case cb_ringing :
01604                 case cb_trying :
01605                 case cb_on_call :
01606                     need_retry = true;
01607                     break;
01608             }
01609             
01610             if ( need_retry ) break;
01611         }
01612     }
01613     
01614     if ( need_retry )
01615     {
01616         uint16_t ext = cb -> get_ext ();
01617         uint16_t port = cb -> get_port ();
01618         
01619         if ( cb -> get_invite_response () == false )
01620         {
01621             buffer [ TIMESLICE_PLACE ] = cb -> get_timeslice ();
01622             
01623             do_not_show_this_invite_pkg = true;
01624             
01625             send2callboxes ( build_cb_package ( ext, port, INVITE, ( char * )buffer,
01626                cb -> msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
01627 
01628             if ( debug_resend_invite ) vz_debug ("[%d] resend invite OK to Cbx : ( %d,  %d )", ext, ext, port );
01629         } 
01630         
01631         if ( find_Call ( v_call, ext ) == NULL )
01632         {
01633             uint8_t cb_status = cb -> get_status ();
01634             
01635             if ( ( ( cb_status == cb_ringing ) or ( cb_status == cb_trying ) ) )
01636             {
01637                     char rtp_src_tmp_write_buffer [ CB_BUFFER_SIZE ];
01638                     
01639                     uint8_t rtp_dst_tmp_write_buffer [ CB_BUFFER_SIZE ];
01640                     
01641                     for ( register int i = 0; i < RTP_MSG_SIZE; i++ ) rtp_src_tmp_write_buffer [ i ] = 0x00;
01642                     
01643                     send2callboxes ( build_cb_package ( ext, port, AUDIO, 
01644                         rtp_src_tmp_write_buffer, AUDIO, RTP_MSG_SIZE, rtp_dst_tmp_write_buffer ) );
01645             } else {
01646                 if ( cb -> get_invite_retry_count () == 0 )
01647                 {
01648                     cb -> set_msg_id ( ( cb->get_msg_id () + 1 ) & ~BIT7 );
01649 
01650                     for ( register uint8_t i = 0; i < v_call -> size(); i++ )
01651                     {
01652                         VZ_call * call = ( VZ_call * )v_call -> get_element ( i );
01653                         if ( call -> get_cb_ext () == ext )
01654                         {
01655                             v_call -> remove_element ( i );
01656                             if ( call not_eq NULL ) delete( call );
01657                             break;
01658                         }
01659                     }
01660                     
01661                     ts -> return_timeslice ( cb -> call_end () );
01662                     
01663                     if( debug_invite or debug_resend_invite ) vz_debug ( "[%d] No audio pkgs from cbx", ext );
01664                 }
01665             }
01666         }
01667     }
01668     
01669     return ( 0 );    
01670 }
01671 
01672 int check_audio_from_ast ( Vector  * v_cb, Vector  * v_call )
01673 {
01674     for( register uint8_t i = 0; i < v_call->size(); i++ )
01675     {
01676         VZ_call * call = ( VZ_call * ) v_call -> get_element ( i );
01677         if ( call not_eq NULL )
01678         {
01679             int length = 0;
01680             char * tmp = call -> get_eth_message ( &length );
01681             if( tmp not_eq NULL )
01682             {
01683                 Call_Box * cb = find_CB ( v_cb, call -> get_cb_ext () );
01684 
01685                 if( cb not_eq NULL ) 
01686                 {
01687                     int cb_port = cb -> get_port ();
01688                     
01689                     if ( drop_rtp_from_ast_pkg )
01690                     {
01691                         led1 = !led1;
01692                     }
01693                         else
01694                     {
01695                         uint8_t * pkg2cb = build_cb_package ( call -> get_cb_ext (), cb_port, AUDIO,
01696                             tmp, AUDIO, length, write_buffer );
01697                             
01698                         send2callboxes( pkg2cb );
01699                     }
01700                     
01701                 } else if ( debug_main ) vz_debug ("[%d] received missed package -- Type :: %i", ext, type );
01702             }
01703         }
01704     }
01705 
01706     return ( 0 );    
01707 }
01708 
01709 int 
01710 wake_up_or_refresh_handler ( Vector  * v_cb )
01711 {
01712     int return_value = 0;
01713     
01714     if( timer_sync_refresh.read_ms () > 250 * 2 )
01715     {
01716         timer_sync_refresh.reset ();
01717     
01718         static uint8_t time_to_mode = TIME_TO_REFRESH;
01719     
01720         if ( time_to_mode == TIME_TO_REFRESH )
01721         {
01722             time_to_mode = TIME_TO_WAKE_UP;
01723             if ( !do_not_refresh ) return_value = refresh ( v_cb );
01724         } else {
01725             time_to_mode = TIME_TO_REFRESH;
01726             if ( wake_all and ( v_call -> size() == 0 ) and cm -> get_cbx_wake_mode () ) wake_all_up ( v_cb );
01727         }
01728     }
01729     
01730     return ( return_value );
01731 }
01732 
01733 int check_sip_messages_from_ast ( Vector  * v_cb, Vector  * v_call )
01734 {
01735     // check sip messages only for cbx in call ?
01736     int ext_to_be_removed = sip_manager ( v_cb );
01737     if ( ext_to_be_removed > 0 )
01738     {
01739         Call_Box * cb = find_CB ( v_cb, ext_to_be_removed );
01740         if ( cb not_eq NULL )
01741         {
01742             if ( cb -> get_status () == cb_on_call )
01743             {
01744                 buffer [ TIMESLICE_PLACE ] = 0;
01745 
01746                 for ( register uint8_t i = 0; i < v_call -> size(); i++ )
01747                 {
01748                     VZ_call * call = ( VZ_call * )v_call -> get_element ( i );
01749                     if ( call -> get_cb_ext () == ext_to_be_removed )
01750                     {
01751                         v_call -> remove_element ( i );
01752                         delete ( call );
01753                     }
01754                 }
01755                 
01756                 ts -> return_timeslice ( cb -> call_end ( false ) );
01757 
01758                 send2callboxes ( build_cb_package ( cb -> get_ext (), cb -> get_port (), CB_BYE,
01759                     ( char * )buffer, cb -> get_msg_id(), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
01760                 
01761                 if( debug_invite ) vz_debug ("[%d] Received Bye from *", cb -> get_ext () );
01762             }
01763         } else if( debug_main ) vz_debug ("[%d] Missed bye request", ext_to_be_removed );
01764     }
01765     
01766     return ( 0 );    
01767 }
01768 
01769 int check_for_runaways_ts ( Vector  * v_cb, Timeslice * ts )
01770 {
01771     if( ( v_call->size() == 0 ) && ( ts->remain_timeslices() not_eq MAX_TIMESLICES ) ) {
01772         bool ts_reset = true;
01773         for( register uint8_t i = 0; i < v_cb->size(); i++ ) {
01774             uint16_t cb_status = ((Call_Box *)v_cb->get_element( i ))->get_status();
01775             uint16_t cb_sip_status = ((Call_Box *)v_cb->get_element( i ))->get_sip_status();
01776             
01777             if  ((cb_status not_eq cb_idle) and (cb_sip_status not_eq sip_idle)) {
01778                 ts_reset = false;
01779                 break;
01780             }
01781             
01782         }
01783         if( ts_reset && !bl_start_flag )
01784         {
01785             if( debug_invite ) vz_debug ("Resetando TS");
01786             ts->reset();
01787         }
01788     }
01789 
01790     return ( 0 );    
01791 }
01792 
01793 int show_wdt_status ( void )
01794 {
01795     if( eth_status == 0 ) {
01796         vz_printf ( "Wdt last reset: %s - status_eth :: Connected - Extern Wdt idle for :: %3d sec ( %3d ) - Rx from CBx idle for :: %3d sec ( %3d )",
01797                   ( wdt.WatchdogCausedReset() ) ? "true" : "false",
01798                   EXTERN_WDT_IDLE - external_wdt,
01799                   EXTERN_WDT_IDLE,
01800                   RX_CB_IDLE - pkg_wdt,
01801                   RX_CB_IDLE
01802                 );
01803     } else {
01804         vz_printf ( "Wdt last reset: %s - status_eth :: Disconnected :: since %3d sec - Extern Wdt idle for :: %3d sec ( %3d ) - Rx from CBx idle for :: %3d sec ( %3d )",
01805                   ( wdt.WatchdogCausedReset() ) ? "true" : "false",
01806                   ETH_CONNECT_TIMEOUT - eth_wdt,
01807                   EXTERN_WDT_IDLE - external_wdt,
01808                   EXTERN_WDT_IDLE,
01809                   RX_CB_IDLE - pkg_wdt,
01810                   RX_CB_IDLE
01811                 );
01812     }
01813 
01814     return ( 0 );    
01815 }
01816 
01817 
01818 int wdt_update ( void )
01819 {
01820     uptime++;
01821             
01822     current_time++;
01823 
01824     if ( wdt_show ) debug_wdt = true;
01825 
01826     if ( external_wdt ) external_wdt--;
01827     if ( pkg_wdt ) pkg_wdt--;
01828     if ( eth_wdt ) eth_wdt--;
01829 
01830     if ( eth_wdt && external_wdt && ( pkg_wdt || disable_wdt_from_cbx ) ){
01831         wdt.kick();
01832         return ( 0 );
01833     }
01834     
01835     return ( 1 );
01836 }
01837 
01838 int check_for_unwanted_rtp_ports ( Vector  * v_cb )
01839 {
01840     for ( register uint8_t i = 0; i < v_cb->size(); i++ )
01841     {
01842         Call_Box * cb = (Call_Box *) v_cb->get_element (i);
01843         if ( cb not_eq NULL )
01844         {
01845             if (  ( cb->get_status () == cb_idle ) && ( cb->get_sip_status () == sip_idle ) && ( cb -> is_rtp_timer_timeout () ) )
01846             {
01847                 if ( debug_reset_rtp ) vz_debug ( "[%d] rtp reset", cb -> get_ext () );
01848                 cb -> cb_set_status( cb_idle );
01849                 cb -> set_sip_status ( sip_idle );
01850                 cb -> reset_rtp_timer ();
01851                 cb -> set_rtp_port ( 0 );           
01852             }
01853         }
01854     }
01855     return ( 0 );    
01856 }
01857 
01858 
01859 int process_received_pkg_from_cbx ( void )
01860 {   
01861     // short path
01862     if ( buffer_from_cb_ptr [ TYPE_PLACE ] == AUDIO )
01863     {
01864         xmemcpy ( buffer, buffer_from_cb_ptr, CB_BUFFER_SIZE );
01865         status = WAITING;
01866         missed_pkg --;
01867     }
01868         else
01869     // long path
01870     {
01871         xmemcpy ( cb_rx_buffer, buffer_from_cb_ptr, CB_BUFFER_SIZE );
01872         status = WAITING;
01873         missed_pkg --;
01874         xmemcpy ( buffer, cb_rx_buffer, CB_BUFFER_SIZE );
01875   
01876         if ( debug_show_rx_cpld )
01877         {
01878             char str [ 256 ];
01879             strcpy ( str, "RX :: \n\r" );
01880             
01881             for ( register uint16_t i = 0; i < 32; i++ )
01882             {
01883                 char tmp [ 8 ];
01884                 sprintf ( tmp, "%02x ", cb_rx_buffer [ i ] );
01885                 strcat ( str, tmp );
01886             }
01887             
01888             strcat ( str, "\n\r " );
01889             
01890             vz_printf ( "%s", str );
01891         }            
01892     }
01893 
01894     data = parse_vz_pkg ( &ext, &port, &type, buffer );
01895 
01896     if ( data not_eq NULL )
01897     {   
01898         if ( min_ext == 0 ) min_ext = ext;
01899 
01900         if ( ext > max_ext ) max_ext = ext;
01901 
01902         if ( ext < min_ext ) min_ext = ext;
01903         
01904         if ( debug_fw ) { fw_cbx_pkg ( ext, ( char *)buffer ); }
01905         
01906         if ( type == TELEMETRY )
01907         {
01908             telemetry_counter ++;
01909             if ( debug_telemetry ) show_last_rx = true;
01910         }
01911 
01912         if( type not_eq AUDIO )
01913         {
01914             if ( debug_cb_rx ) { vz_printf ("[%d %d] -- Type :: %d", ext, port, type ); }
01915             
01916             if( 
01917                 type == TELEMETRY or
01918                 type == CB_STATS or
01919                 type == FW1 or
01920                 type == FW2 or
01921                 type == FW3 or
01922                 type == FW4 or
01923                 type == FW5 or
01924                 type == FW6
01925              ) type = FW;
01926             
01927             if ( debug_sqn ) { vz_printf ("[%d %d] -- Type :: %d -- seq_num :: %u", ext, port, type, cb_tx_buffer [ SEQ_NUM_PLACE ] ); }
01928 
01929             Call_Box * cb = find_CB ( v_cb, ext );
01930             if ( cb not_eq NULL ) 
01931             {
01932                 if ( data[ 0 ] bitand BIT7 ) 
01933                 {
01934                     if ( type == BOOT )
01935                     {
01936                         send2callboxes( build_cb_package( ext, port, REGISTRY,
01937                             ( char * )data, cb->get_msg_id(), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
01938                     } else {
01939                         if ( debug_main ) vz_debug ("[%d] Received ack pkg with seq_num %d", ext, data[ 0 ] );
01940 
01941                         switch ( type )
01942                         {
01943                             case INVITE : {
01944                                 if ( debug_main or debug_invite ) vz_debug ("[%d] Invite ACK - msg_id %d -- Cbx seqNum %d", ext, cb->get_msg_id(), data[ 0 ] );
01945                                 break;
01946                             }
01947                             case CB_BYE : {
01948                                 if ( debug_main or debug_invite ) vz_debug ("[%d] BYE ACK - msg_id %d", ext, cb->get_msg_id() );
01949                                 cb -> set_bye_response_ok ();
01950                                 break;
01951                             }
01952                             case REGISTRY : {
01953                                 if ( debug_main or debug_aging ) vz_debug ("[%d] Registry ACK - msg_id %d", ext, cb->get_msg_id() );
01954                                 break;
01955                             }
01956                             default : {
01957                                 if ( debug_main or debug_aging ) vz_debug ("[%d] ACK msg_id :: %d :: type %d", ext, cb->get_msg_id(), type );
01958                             }
01959                         }
01960                         if ( type not_eq REGISTRY and type not_eq CB_BYE and type not_eq INVITE ) type = DO_NOTHING;
01961                         if ( type == CB_BYE )
01962                         {
01963                             VZ_call * call = find_Call ( v_call, ext );
01964                             if ( call not_eq NULL )
01965                             {
01966                                 if ( call -> get_elapsed_time () < 120000 )
01967                                 {
01968                                     if ( debug_invite ) vz_debug ("[%d] ack bye ignored", ext );
01969                                     type = DO_NOTHING;
01970                                 }
01971                             }
01972                         }
01973                     }
01974                 }
01975             }
01976         }
01977     } else type = DO_NOTHING;
01978     
01979     return 0;
01980 }
01981 
01982 Call_Box * 
01983 try_add_new_cbx ( Vector  * v_cb, const int ext )
01984 {
01985     if ( v_cb not_eq NULL )
01986     {
01987         if ( v_cb -> size () < u8_MAX_CB_IN_A_BRANCH )
01988         {
01989             if ( debug_main ) vz_debug ( "[%d] Adding Cbx", ext );
01990             
01991             Call_Box * cb = new Call_Box ( ext, ext );
01992     
01993             if ( cb == NULL ) { memory_is_over = true; } 
01994             
01995             else
01996             {
01997                 int pair_ext = ( ( ext % 2 ) == 0 ) ? ext + 1 : ext - 1;
01998                 
01999                 Call_Box * pair_cb = find_CB ( v_cb, pair_ext );
02000                 
02001                 if ( pair_cb not_eq NULL )
02002                 {
02003                     int pair_ret = cb -> set_pair_cbx ( pair_cb );
02004                     
02005                     if ( pair_ret == 0 ) {
02006                         if ( debug_pair ) vz_debug ( "[%d] vinculado com [%d]", ext, pair_ext );
02007                         
02008                         pair_cb -> set_pair_cbx ( cb );
02009                     } else if ( pair_ret == -3 ) { 
02010                         if ( debug_pair ) vz_debug ( "[%d] par sobrescrito com [%d]", ext, pair_ext );
02011                     }
02012 
02013                 } else if ( debug_pair ) vz_debug ( "[%d] Par nao encontrado", ext )
02014                                             
02015                 v_cb -> add ( cb );
02016                 
02017                 if ( debug_main ) vz_debug ( "[%d] Added CBx", ext );
02018                 
02019                 return ( cb );
02020             }
02021         }
02022     }
02023     
02024     return ( NULL );
02025 }
02026 
02027 void update_all_cb_timer ( Vector  * v_cb )
02028 {
02029     if ( v_cb == NULL ) return;
02030     
02031     for( register uint8_t i = 0; i < v_cb->size(); i++ )
02032     {
02033         Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
02034         if ( cb not_eq NULL ) cb -> update_time ();
02035     }
02036 }
02037 
02038 int init_hello ( void )
02039 {
02040     hello_sync.start ();
02041     hello_times = 0;
02042     return ( 0 );
02043 }
02044 
02045 void send_hello_to_cbx ( void )
02046 {   
02047     if ( hello_times >= 3 ){
02048         hello_sync.stop ();
02049         hello_sync.reset ();
02050         dont_say_hello_again = true;
02051     }
02052     
02053     if ( ( !dont_say_hello_again ) && ( hello_sync.read () > 3 ) && ( hello_times < 3 ) )
02054     {
02055         hello_times++;
02056         
02057         char cmd_msg [ CB_BUFFER_SIZE ] = "pend\r";
02058         
02059         if ( debug_hello ) vz_debug ("Enviando pend_all nro [ %i ]", hello_times ); 
02060         
02061         send2callboxes( build_cb_package( BROADCAST_EXT, BROADCAST_EXT, PROMPT, cmd_msg, ( 0x11 + hello_times ), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
02062         
02063         hello_sync.reset ();
02064     }
02065 }
02066 
02067 void show_hello_status_function ( void )
02068 {
02069     vz_printf ("hello_sync.read :: %d -- hello_times :: %u", ( int ) hello_sync.read (), hello_times );        
02070 }
02071 
02072 void show_last_rx_pkg_from_cbx ( void )
02073 {
02074     char str [ 256 ] = "";
02075     
02076     for ( register uint16_t i = 0; i < CB_BUFFER_SIZE; i++ )
02077     {
02078         char tmp[ 8 ];
02079     
02080         sprintf ( tmp, "%02x", cb_rx_buffer [ i ] );
02081         
02082         strcat ( str, tmp );
02083         
02084         if ( ( i not_eq 0 ) && !( ( i + 1 ) % 25 ) )
02085         { 
02086             vz_printf ( "%s", str );
02087             strcpy ( str, "" );
02088         } 
02089         
02090         else { strcat ( str, " " ); }
02091     }
02092     
02093     vz_printf ( "%s", str );
02094 }
02095 
02096 void show_last_tx_pkg_from_cbx ( void )
02097 {   
02098     char str [ 256 ] = "";
02099     
02100     for ( register uint16_t i = 0; i < CB_BUFFER_SIZE; i++ )
02101     {
02102         char tmp[ 8 ];
02103     
02104         sprintf ( tmp, "%02x", cb_tx_buffer [ i ] );
02105         
02106         strcat ( str, tmp );
02107         
02108         if ( ( i not_eq 0 ) && !( ( i + 1 ) % 25 ) )
02109         { 
02110             vz_printf ( "%s", str );
02111             strcpy ( str, "" );
02112         } 
02113         
02114         else { strcat ( str, " " ); }
02115     }
02116     
02117     vz_printf ( "%s", str );
02118 }
02119 
02120 /* Funcao para leitura da fonte de alimentacao e aviso ao servidor */
02121 void check_power_source ( void )
02122 {
02123     static uint8_t samples = 0, warn_back = 0, wait_to_warn = 0; 
02124 
02125     power_source_timer.stop ();
02126     power_source_timer.reset ();
02127 
02128     if ( pwr_src_in )
02129     {
02130         if ( samples < 8 ) samples ++;
02131     } else {
02132         if ( samples > 0 ) samples --;
02133     }
02134     
02135     if ( wait_to_warn ) wait_to_warn --;
02136     
02137     if ( samples >= 8 )
02138     {
02139         power_source_status = 1;
02140         if (!wait_to_warn )
02141         {
02142             wait_to_warn = u8_WAIT_TO_WARN; 
02143             warn_back = 1;
02144             int power_source_client_ret = power_source_client.sendTo ( pwr_src_server, "mainpowerisdown", u8_POWER_MSG_LENGTH );
02145             
02146             if ( power_source_client_ret not_eq u8_POWER_MSG_LENGTH )
02147             {
02148                 reconnect_power_source ();
02149                 miss_power_source_send_pkg ++;
02150                 if ( debug_reconnect ) vz_debug (
02151                     "Reconnect power_source_client_ret [ %d - %d ]", 
02152                     power_source_client_ret, u8_POWER_MSG_LENGTH 
02153                 );
02154             }
02155                 
02156             vz_debug ( "Main power is DOWN!");
02157         } else {
02158             wait_to_warn--;
02159         }
02160     }
02161     
02162     if ( ( samples == 0 ) and ( warn_back ) )
02163     {
02164         power_source_status = 0;
02165         warn_back = 0;
02166         wait_to_warn = 0;
02167         int power_source_client_ret = power_source_client.sendTo ( pwr_src_server, "mainpowerisback", u8_POWER_MSG_LENGTH );
02168         if ( power_source_client_ret not_eq u8_POWER_MSG_LENGTH );
02169         {
02170             reconnect_power_source ();
02171             miss_power_source_send_pkg ++;
02172             if ( debug_reconnect ) vz_debug (
02173                 "Reconnect power_source_client_ret [ %d - %d ]", 
02174                 power_source_client_ret, u8_POWER_MSG_LENGTH 
02175             );
02176         }
02177         vz_debug ( "Main power is BACK!" );
02178     }
02179     
02180     power_source_timer.start ();
02181 }
02182 
02183 void update_config_values ( void )
02184 {
02185     if ( cm -> min_ext_was_modified () or cm -> max_ext_was_modified () ) init_ranges ();
02186     
02187     if ( cm -> header_ip_was_modified () or 
02188             cm -> net_mask_was_modified () or
02189                 cm -> gateway_was_modified () ) 
02190     {
02191         __reconnect ();    
02192     }
02193     
02194     if  ( cm -> header_ip_was_modified () or 
02195             cm -> shift_port_was_modified () or
02196                 cm -> server_ext_was_modified () or
02197                     cm -> server_port_was_modified () or
02198                         cm -> server_ip_was_modified ()
02199         ) 
02200     {
02201         for ( register uint8_t i = 0; i < v_cb -> size (); i++ )
02202         {
02203             Call_Box * cb = ( Call_Box * ) v_cb -> get_element ( i );
02204             
02205             if  ( cb not_eq NULL ) {
02206                 cb -> update ();
02207             }
02208         }                            
02209     }
02210     
02211     if ( cm -> server_ip_was_modified () ) reconnect_power_source ();
02212     
02213     if ( cm -> fw_server_ip_was_modified () or cm -> fw_server_port_was_modified () ) {
02214         re_start_fw ();
02215     }
02216 }
02217 
02218 char * build_wdt_string ( char * wake_msg, const size_t length )
02219 {
02220     if ( wake_msg == NULL ) return ( NULL );
02221     
02222     snprintf ( wake_msg, length,"wdt:%u,%u,%u,%c,%u,%d,%u,%u,%d,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u:", 
02223         uptime, 
02224         invite_counter, 
02225         external_wdt, 
02226         ( wdt.WatchdogCausedReset() ) ? '1' : '0', 
02227         cb_new_counter, 
02228         v_cb -> size (),
02229         ts->remain_timeslices(), 
02230         sip_socket_send_failure, 
02231         v_call -> size (),
02232         pkg_cksok,
02233         pkg_ckserr,
02234         pkg_zero,
02235         out_of_range,
02236         missed_pkg,
02237         delayed_pkg_to_cb,
02238         cpld_pkg_tx_counter,
02239         cpld_pkg_rx_counter,
02240         eth_wdt,
02241         pkg_wdt,
02242         miss_fw_send_pkg,
02243         miss_prompt_udp_send_pkg,
02244         miss_sip_registry_send_pkg,
02245         miss_sip_invite_send_pkg,
02246         miss_sip_bye_send_pkg,
02247         miss_sip_ok_send_pkg,
02248         miss_sip_rcv_bye_send_pkg,
02249         miss_wdt_send_pkg,
02250         miss_rtp_udp_send_pkg,
02251         miss_prompt_udp_rcv_pkg,
02252         miss_clock_send_pkg,
02253         miss_sip_inc_pkg,
02254         miss_power_source_send_pkg,
02255         miss_bl_udp_send_pkg,
02256         miss_rtp_udp_rcv_pkg
02257     );
02258     
02259     return ( wake_msg );
02260 }