Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
101:162c28286c29
Parent:
100:09a23fcd3bdf
Child:
102:98c7155e8bea
--- a/vz_protocol.cpp	Thu Feb 12 16:42:35 2015 +0000
+++ b/vz_protocol.cpp	Fri Feb 13 18:44:45 2015 +0000
@@ -5,6 +5,10 @@
 uint32_t pkg_cksok = 0;
 int begin = 0;
 int end = 0;
+time_t current_time = 0;
+UDPSocket clock_sock;
+Endpoint clock_server;
+Timer external_time;
 
 void init_ranges( void ){
     FILE * begin_ext = fopen( "/qspi/minext.txt", "r" );
@@ -166,36 +170,36 @@
         fill = length + __VZ_HEADER_OFFSET__;
     }else if( type == __INVITE__ ){
         pkg[ 7 ] = seq_num;
-        __print_clock__( pkg + 8 );
+        print_clock( pkg + 8 );
         
         pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
         fill = __TIMESLICE_PLACE__ + 1;
     }else if( type == __REGISTRY__ || type == __BOOT__ ){
         pkg[ 7 ] = seq_num;
-        __print_clock__( pkg + 8 );
+        print_clock( pkg + 8 );
         
         fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__;
     }else if( type == __CB_BYE__ ){
         pkg[ 7 ] = seq_num;
-        __print_clock__( pkg + 8 );
+        print_clock( pkg + 8 );
         
         pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
         fill = __TIMESLICE_PLACE__ + 1;
     }else if( type == __PROMPT__ ){
-        __print_clock__( pkg + 8 );
+        print_clock( pkg + 8 );
         
         xmemcpy((pkg+7), (uint8_t*)cb_buffer, 293);
         //FIXME isso forca qualquer cmd ter < 100 chars
         fill = 100;
     }else if( type == __TELEMETRY__ ){
         pkg[ 7 ] = seq_num;
-        __print_clock__( pkg + 8 );
+        print_clock( pkg + 8 );
         
         pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
         fill = __TIMESLICE_PLACE__ + 1;
     }else if( type == __BOOTLOADER_CBX__ ){
         pkg[ 7 ] = seq_num;
-        __print_clock__( pkg + 8 );
+        print_clock( pkg + 8 );
         
         fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__;
     }else{
@@ -231,47 +235,64 @@
     return cc; 
 }
 
-void __print_clock__( uint8_t * buffer ){
-    //if( !buffer ) return;
-    
-    //int ntp_result = ntp.setTime( "200.192.232.8", 123, 3 );
-    //led2 = !led2;
-    //struct tm  ts;
-    //int ntp_result = ntp.setTime( "200.192.232.8" );
-    //int ntp_result = ntp.setTime( "200.192.232.8", 123, 3 );
-    //if( ntp_result == 0 ){
-    //    time_t seconds;
-        // seconds = time(NULL);
-    //    time( &seconds );
-    //    ts = *localtime( &seconds );
-   //}
-    
-    //int ano = ts.tm_year + 1900;
-    //int mes = ts.tm_mon + 1;
-    //int dia = ts.tm_mday; 
-    //int hora = ts.tm_hour - 3;
-    //int min = ts.tm_min; 
-    //int sec = ts.tm_sec; 
+void print_clock( uint8_t * buffer )
+{
+    if( buffer != NULL )
+    {   
+        struct tm tmp_tm, * result_tm;
     
-    //buffer[ 0 ] = ano / 1000;
-    //ano -= buffer[ 0 ] * 1000;
-    //buffer[ 1 ] = ano / 100;
-    //ano -= buffer[ 1 ]* 100;
-    //buffer[ 2 ] = ano / 10;
-    //ano -= buffer[ 2 ] * 10;
-    //buffer[ 3 ] = ano;
-    //buffer[ 4 ] = mes / 10;
-    //buffer[ 5 ] = mes % 10;
-    //buffer[ 6 ] = dia / 10;
-    //buffer[ 7 ] = dia % 10;
-    //buffer[ 8 ] = hora / 10;
-    //buffer[ 9 ] = hora % 10;
-    //buffer[ 10 ] = min / 10;
-    //buffer[ 11 ] = min % 10;
-    //buffer[ 12 ] = sec / 10;
-    //buffer[ 13 ] = sec % 10;
-    
-    /* convertendo pro ascii do nro */
-    for( register int i = 0; i < 14; i++ ) buffer[ i ] = 0xab;
-    //led4 = !led4;
+        if( sizeof( time_t ) != sizeof( long ) )
+            debug_msg("sizeof( time_t ) : %lu -- sizeof( long int ) : %lu\n", sizeof( time_t ), sizeof( long ) );
+        
+        debug_msg("current_time : %lu\t", current_time );
+        
+        if( current_time != 0 )
+        {
+            result_tm = localtime_r( ( const time_t *)&current_time, ( struct tm * )&tmp_tm );
+            if (result_tm )
+            {
+                char formated_time[ 16 ];
+                size_t formated_nbytes = strftime( formated_time, sizeof( formated_time ), "%Y%m%d%H%M%S", result_tm );
+                debug_msg("clock() : %s\n", formated_time );
+                if( formated_nbytes != __CLOCK_SYNC_SIZE__ ) debug_msg("( %lu )\n", formated_nbytes );
+                for( register int i = 0; i < __CLOCK_SYNC_SIZE__; i++ ) buffer[ i ] = formated_time[ i ];
+            }
+                else
+            {
+                for( register int i = 0; i < __CLOCK_SYNC_SIZE__; i++ ) buffer[ i ] = 0xfa;
+            }
+        }
+            else
+        {
+            for( register int i = 0; i < __CLOCK_SYNC_SIZE__; i++ ) buffer[ i ] = 0xab;
+        }
+    }
+}
+
+int request_clock_to_server( void )
+{
+    char clock_msg[ 16 ];
+    strcpy( clock_msg, "request_time" );
+    int send = clock_sock.sendTo( clock_server, clock_msg, strlen( clock_msg ) );
+    return send;
+}
+
+int check_clock( void )
+{
+    if( external_time.read() > EXTERNAL_TIME_REQUEST_WAIT_SECONDS )
+    {
+        external_time.reset();
+        return request_clock_to_server();
+    }
+        else
+    {
+        // nro arbitrario maior que strlen( request_time )
+        return 0x30;
+    }
+}
+
+int update_clock( void )
+{
+    // rcv    
+    return 0;
 }
\ No newline at end of file