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

clock.cpp

00001 #include "clock.h"
00002 
00003 time_t current_time = 0;
00004 
00005 UDPSocket clock_sock;
00006 
00007 Endpoint clock_server;
00008 
00009 Timer server_clock_timer;
00010 
00011 int 
00012 request_clock_to_server ( void )
00013 {
00014     char clock_msg [ TIME_MSG_SIZE ];
00015     
00016     for ( register uint8_t i = 0; i < TIME_MSG_SIZE; i++ ) clock_msg [ i ] = 0x00;
00017      
00018     clock_msg [ 0 ] = 0x23;
00019     
00020     int send = clock_sock.sendTo ( clock_server, clock_msg, TIME_MSG_SIZE );
00021     
00022     if ( debug_clock ) vz_debug ("::%d::%s::%s::%d::", send, clock_msg, clock_server.get_address (), clock_server.get_port () );
00023     
00024     if ( send not_eq TIME_MSG_SIZE )
00025     {
00026         if ( debug_reconnect ) vz_debug ("Reconnect clock request" );
00027         int clock_sock_reconnect_ret = clock_sock_reconnect ();
00028         
00029         if ( debug_reconnect ) vz_debug ("Valor de retorno no reconnect :: %d", clock_sock_reconnect_ret );
00030         
00031         miss_clock_send_pkg  ++;
00032     }
00033     
00034     if ( debug_clock ) vz_debug ("send :: %d", send );
00035     
00036     return ( send );
00037 }
00038 
00039 int check_clock ( void )
00040 {
00041     if( server_clock_timer.read() > EXTERNAL_TIME_REQUEST_WAIT_SECONDS )
00042     {
00043         server_clock_timer.reset();
00044         return request_clock_to_server ();
00045     }
00046     else
00047     {
00048         // nro arbitrario maior que strlen( request_time )
00049         return 0x30;
00050     }
00051 }
00052 
00053 int update_clock ( void )
00054 {   
00055 
00056     char time_msg[ TIME_MSG_SIZE ];
00057     
00058     Endpoint local_clock_server;
00059     
00060     int time_msg_rcv = clock_sock.receiveFrom ( local_clock_server, time_msg, sizeof( time_msg ) );
00061     
00062     if( time_msg_rcv == -1 )
00063     {
00064         if( debug_reconnect ) vz_printf ("Reconnect clock socket");
00065         clock_sock_reconnect ();
00066     } else {
00067         if ( time_msg [ 0 ] != 0x24 )
00068         {
00069             vz_debug ("Error: time_msg [ 0 ] != 0x24");
00070             return time_msg [ 0 ];
00071         } else if ( time_msg [ 1 ] == 0x00 ) 
00072         {
00073             vz_debug ("Error: time_msg [ 0 ] == 0x00");
00074             return time_msg [ 1 ];
00075         } else if ( time_msg [ 1 ] > 0x0f )
00076         { 
00077             vz_debug ("Error: time_msg [ 0 ] > 0x0f");
00078             return time_msg [ 1 ];
00079         }
00080     }
00081     
00082     time_t local_current_time = 0;
00083     local_current_time = 0;
00084     
00085     local_current_time |= ( ( uint32_t ) time_msg [ 40 ] ) << 24;
00086     local_current_time |= ( ( uint32_t ) time_msg [ 41 ] ) << 16;
00087     local_current_time |= ( ( uint32_t ) time_msg [ 42 ] ) << 8;
00088     local_current_time |= ( ( uint32_t ) time_msg [ 43 ] );
00089     /*
00090         Como o valor recebido nos bytes [40 41 42 43] correspondem aos segundos decorridos desde 1/1/1900,
00091         e como queremos trabalhar com unixtime, convertemos fazendo a subtracao de 70 anos = 2208988800
00092     */
00093     local_current_time -= 2208988800;
00094     
00095     int diff_time  = ( local_current_time > current_time ) ? local_current_time - current_time : current_time - local_current_time;
00096     
00097     if ( diff_time > 2 ) current_time = local_current_time;
00098     
00099     if ( debug_clock ) vz_debug ("Clock.rcv ( %d ), diff [ %d ] ", local_current_time, diff_time );
00100     
00101     return ( local_current_time - current_time );
00102 }
00103 
00104 int init_clock ( void )
00105 {   
00106     char server_ip [ 16 ];
00107     
00108     cm -> get_clock_server_ip ( server_ip );
00109 
00110     int clock_server_set_address_ret = clock_server.set_address ( server_ip, CLOCK_SERVER_PORT );
00111     
00112     if ( debug_clock ) vz_debug ("clock_server_set_address_ret :: %d", clock_server_set_address_ret );
00113     
00114     int clock_sock_bind_ret = clock_sock.bind ( CLOCK_HEADER_PORT );
00115     
00116     clock_sock.set_blocking ( false, 0 );
00117     
00118     if ( debug_clock ) vz_debug ("clock_sock_bind_ret ::%d", clock_sock_bind_ret );
00119     
00120     server_clock_timer.start ();
00121     
00122     request_clock_to_server ();
00123     
00124     return ( clock_server_set_address_ret | clock_sock_bind_ret );
00125 }
00126 
00127 int 
00128 clock_sock_reconnect ( void )
00129 {
00130     clock_sock.close ();
00131     
00132     int clock_sock_bind_ret = clock_sock.bind ( CLOCK_HEADER_PORT );
00133     
00134     clock_sock.set_blocking ( false, 0 );
00135     
00136     return ( clock_sock_bind_ret );    
00137 }
00138 
00139 int show_clock ( void )
00140 {   
00141     char buffer[ 512 ];
00142     struct tm * result_tm;
00143     if ( sizeof( time_t ) != sizeof( long ) )
00144     {
00145         vz_printf ("sizeof( time_t ) : %lu -- sizeof( long int ) : %lu\n", sizeof( time_t ), sizeof( long ) );
00146     }
00147                                    
00148     int filled = snprintf( buffer, 512, "current_time : %lu\tclock() : ", current_time );
00149     int available_bytes = 512 - filled - 1;
00150                                                                                                                                         
00151     if ( current_time != 0 )
00152     {
00153         result_tm = localtime( ( const time_t *)&current_time );
00154         if ( result_tm )
00155         {
00156             char formated_time[ 16 ];
00157             
00158             /* Correcao "manual" do (GMT -3:00) */
00159             result_tm->tm_hour -= 3;
00160             if ( result_tm->tm_hour < 0 ) result_tm->tm_hour = 24 + result_tm->tm_hour;
00161             
00162             strftime ( formated_time, sizeof( formated_time ), "%Y%m%d%H%M%S", result_tm );
00163             
00164             strncat ( buffer, formated_time, available_bytes );
00165         }
00166     }
00167         else
00168     {
00169         strncat ( buffer, "???", available_bytes );
00170     }
00171     
00172     vz_printf ( "%s", buffer );
00173     
00174     return ( strlen ( buffer ) );
00175 }