![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: clock.cpp
- Revision:
- 120:770f00554b1e
- Parent:
- 119:ee6a53069455
- Child:
- 121:ee02790d00b7
--- a/clock.cpp Thu Apr 30 15:42:41 2015 +0000 +++ b/clock.cpp Mon May 04 17:25:29 2015 +0000 @@ -10,9 +10,14 @@ int request_clock_to_server ( void ) { - char clock_msg[] = "request time"; + char clock_msg[ TIME_MSG_SIZE ]; - int send = clock_sock.sendTo( clock_server, clock_msg, strlen( clock_msg ) ); + for ( register uint8_t i = 0; i < TIME_MSG_SIZE; i++ ) clock_msg [ i ] = 0x00; + + clock_msg [ 0 ] = 0x23; + + //int send = clock_sock.sendTo( clock_server, clock_msg, strlen( clock_msg ) ); + int send = clock_sock.sendTo( clock_server, clock_msg, sizeof ( clock_msg ) ); if ( debug_clock ) debug_msg("::%d::%s::%s::%d::", send, clock_msg, clock_server.get_address (), clock_server.get_port () ); @@ -43,7 +48,9 @@ int update_clock ( void ) { - char time_msg[ 16 ]; + + char time_msg[ TIME_MSG_SIZE ]; + Endpoint local_clock_server; int time_msg_rcv = clock_sock.receiveFrom( local_clock_server, time_msg, sizeof( time_msg ) ); @@ -52,19 +59,48 @@ { if( debug_reconnect ) send_msg("Reconnect clock socket"); clock_sock_reconnect (); + } else { + if ( time_msg [ 0 ] != 0x24 ) + { + debug_msg("Error: time_msg [ 0 ] != 0x24"); + return time_msg [ 0 ]; + } else if ( time_msg [ 1 ] == 0x00 ) + { + debug_msg("Error: time_msg [ 0 ] == 0x00"); + return time_msg [ 1 ]; + } else if ( time_msg [ 1 ] > 0x0f ) + { + debug_msg("Error: time_msg [ 0 ] > 0x0f"); + return time_msg [ 1 ]; + } } - current_time = atoi( time_msg ); + time_t local_current_time = 0; + local_current_time = 0; - if ( debug_clock ) debug_msg("Clock.rcv ( %d )", current_time ); + local_current_time |= ( ( uint32_t ) time_msg [ 40 ] ) << 24; + local_current_time |= ( ( uint32_t ) time_msg [ 41 ] ) << 16; + local_current_time |= ( ( uint32_t ) time_msg [ 42 ] ) << 8; + local_current_time |= ( ( uint32_t ) time_msg [ 43 ] ); + /* + Como o valor recebido nos bytes [40 41 42 43] correspondem aos segundos decorridos desde 1/1/1900, + e como queremos trabalhar com unixtime, convertemos fazendo a subtracao de 70 anos = 2208988800 + */ + local_current_time -= 2208988800; + + int diff_time = ( local_current_time > current_time ) ? local_current_time - current_time : current_time - local_current_time; + + if ( diff_time > 2 ) current_time = local_current_time; + + if ( debug_clock ) debug_msg("Clock.rcv ( %d ), diff [ %d ] ", local_current_time, diff_time ); return 0; } int init_clock ( void ) { - const char host[] = "192.168.120.7"; - int port = 7475; + const char host[] = "192.168.120.120"; + int port = 123; int clock_server_set_address_ret = clock_server.set_address( host , port );