Project Embedded Systems E-ict Denayer

Dependencies:   BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed

Revision:
3:7aef6c427f97
Parent:
2:1a5565ee8219
Child:
4:860566e5814e
--- a/main.cpp	Thu Jan 26 10:40:35 2017 +0000
+++ b/main.cpp	Mon Jul 17 01:00:39 2017 +0000
@@ -1,6 +1,6 @@
 #include "main.h"
 
-uint32_t ul_ResetDisplay( void )
+int32_t l_ResetDisplay( void )
 {
     display.Clear( LCD_COLOR_WHITE );
     display.SetBackColor( LCD_COLOR_WHITE );
@@ -10,11 +10,12 @@
 
 /*-----------------------------------------------------------*/
 
-uint32_t ul_CheckDHCP( void )                                     
+int32_t l_CheckDHCP( void )                                     
 {
     uint32_t ul_MyReturn = 2;
     Network *x_DynNetwork = mpool.alloc();                                      //waarschijnlijk loopt het hier mis met een memory pool
-     
+    //Network *x_DynNetwork;
+    
     if( Eth.init( ) == 0 )
     {
         char c_NewIP[16];
@@ -23,6 +24,8 @@
         if( strcmp( x_DynNetwork->c_IPAddress, c_NewIP ) == false )
         {
             strncpy( x_DynNetwork->c_IPAddress, c_NewIP, 16 );
+            //display.DisplayStringAt( 270, 10, ( uint8_t * )x_DynNetwork->c_IPAddress, LEFT_MODE );
+            //display.DisplayStringAt( 270, 50, ( uint8_t * )Eth.getIPAddress( ), LEFT_MODE ); 
             strncpy( x_DynNetwork->c_MACAddress, Eth.getMACAddress( ), 16 );
             strncpy( x_DynNetwork->c_Netmask, Eth.getNetworkMask( ), 16 );
             strncpy( x_DynNetwork->c_Gateway, Eth.getGateway( ), 16 );
@@ -39,176 +42,125 @@
 
 /*-----------------------------------------------------------*/
 
-uint32_t ul_CheckInternet( void )
+int32_t l_CreateMenu( void )
 {
-    uint32_t ul_Counter = 0;
-    uint32_t ul_PingCounter = 0;
-    ip_addr_t x_PingTarget = { 0x08080808 }; 
-    for( ul_Counter = 0; ul_Counter < 4; ul_Counter++)
-    {    
-        if( ul_Ping( &x_PingTarget ) == 0 )
-        {
-            ul_PingCounter++;
-        }
-    }
-    if( ul_PingCounter >= 3 )
-    {
-        return 0; 
-    }
-    else
-    {
-        return 1;
-    }
-      
+    //480x272
+    display.DrawRect( 0, 0, 105, 68);
+    display.DisplayStringAt( 2, 34, ( uint8_t * )"DHCP", LEFT_MODE );
+    display.DrawRect( 0, 68, 105, 68);
+    display.DisplayStringAt( 2, 102, ( uint8_t * )"STATIC", LEFT_MODE );
+    return 0;
 }
 
 /*-----------------------------------------------------------*/
 
-uint32_t ul_ShowDevices( void )
+int32_t l_ShowSettings( void )
 {
-    uint32_t ul_GenIP = 0;
-    uint32_t ul_MyIP = 0;
-    uint32_t ul_Counter =0;
-    uint32_t ul_DevCounter = 0;
-    
-    ip_addr_t x_ActDev[255];                     //max 255 adressen binnen sub, beter is malloc gebruik en redefine van array/vector => max IP adressen haalbaar: 4228250625
-    
-    osEvent x_Evt = queue.get();
-    if (x_Evt.status == osEventMessage) 
+    l_ResetDisplay( );
+    display.SetFont( &Font16 );
+    display.DisplayStringAt( 10, 10, ( uint8_t * )"Starting application", LEFT_MODE ); 
+    display.DisplayStringAt( 10, 30, ( uint8_t * )"Initiating EthernetInterface", LEFT_MODE ); 
+    if( Eth.init( ) == 0 )
     {
-        Network *x_DynNetwork = (Network*)x_Evt.value.p;
-        ul_MyIP = ( uint32_t ) x_DynNetwork->c_IPAddress ;
-        mpool.free(x_DynNetwork);
+        display.DisplayStringAt( 10, 50, ( uint8_t * )"Initiating EthernetInterface: done", LEFT_MODE );
+        display.DisplayStringAt( 10, 70, ( uint8_t * )"Connecting to EthernetInterface", LEFT_MODE );
+        
+        char c_NewIP[16];
+        Eth.connect( );
+        display.DisplayStringAt( 10, 90, ( uint8_t * )"Initiating EthernetInterface: done", LEFT_MODE );
+        display.DisplayStringAt( 10, 110, ( uint8_t * )"Receiving network information", LEFT_MODE );        
         
-        ul_GenIP = ul_MyIP & 0xFFFFFF00;
-       
-        for( ul_Counter = 1; ul_Counter < 255; ul_Counter++)
-        {
-            ul_GenIP++;
-            if( ul_Ping( ( ip_addr_t * ) ul_GenIP ) == 0 )
-            {
-                ip_addr_t x_ConIP = { ul_GenIP };
-                x_ActDev[ul_DevCounter] = x_ConIP;                          //voor een of andere reden is dit de enige manier dat werkt
-                ul_DevCounter++;
-            }
-        }  
+        strncpy( c_NewIP, Eth.getIPAddress( ), 16 );
+        l_ResetDisplay( );
+        l_CreateMenu( );
+        
+        display.DisplayStringAt( 130, 10, ( uint8_t * )"IP address: ", LEFT_MODE ); 
+        display.DisplayStringAt( 270, 10, ( uint8_t * )c_NewIP, LEFT_MODE );  
+        display.DisplayStringAt( 130, 30, ( uint8_t * )"Netmask: ", LEFT_MODE ); 
+        display.DisplayStringAt( 270, 30, ( uint8_t * )Eth.getNetworkMask( ), LEFT_MODE ); 
+        display.DisplayStringAt( 130, 50, ( uint8_t * )"Gateway: ", LEFT_MODE ); 
+        display.DisplayStringAt( 270, 50, ( uint8_t * )Eth.getGateway( ), LEFT_MODE );
+        display.DisplayStringAt( 130, 70, ( uint8_t * )"Internet: ", LEFT_MODE ); 
+        l_sendNTPpacket();
     }
     
     return 0;
 }
 
-uint32_t ul_CreateMenu( void )
-{
-    //480x272
-    display.DrawRect( 0, 0, 125, 68);
-    display.DisplayStringAt( 2, 34, ( uint8_t * )"Settings", LEFT_MODE );
-    display.DrawRect( 0, 68, 125, 68);
-    display.DisplayStringAt( 2, 102, ( uint8_t * )"PING", LEFT_MODE );
-    return 0;
-}
-
 /*-----------------------------------------------------------*/
 
-uint32_t ul_ShowSettings( void )
-{
-    osEvent x_Evt = queue.get();
-    if (x_Evt.status == osEventMessage) 
-    {
-        Network *x_DynNetwork = (Network*)x_Evt.value.p;
-        ul_ResetDisplay( );
-        ul_CreateMenu( );
-        display.SetFont( &Font16 );
-        display.DisplayStringAt( 130, 10, ( uint8_t * )"IP address: ", LEFT_MODE ); 
-        display.DisplayStringAt( 270, 10, ( uint8_t * )x_DynNetwork->c_IPAddress, LEFT_MODE );
-        display.DisplayStringAt( 130, 30, ( uint8_t * )"MAC Address: ", LEFT_MODE ); 
-        display.DisplayStringAt( 270, 30, ( uint8_t * )x_DynNetwork->c_MACAddress, LEFT_MODE );  
-        display.DisplayStringAt( 130, 50, ( uint8_t * )"Netmask: ", LEFT_MODE ); 
-        display.DisplayStringAt( 270, 50, ( uint8_t * )x_DynNetwork->c_Netmask, LEFT_MODE ); 
-        display.DisplayStringAt( 130, 70, ( uint8_t * )"Gateway: ", LEFT_MODE ); 
-        display.DisplayStringAt( 270, 70, ( uint8_t * )x_DynNetwork->c_Gateway, LEFT_MODE );
-        display.DisplayStringAt( 130, 90, ( uint8_t * )"Internet: ", LEFT_MODE ); 
-        if( ul_CheckInternet == 0 )
-        {
-            display.DisplayStringAt( 270, 90, ( uint8_t * )"connected", LEFT_MODE );
-        }
-        else
-        {
-            display.DisplayStringAt( 270, 90, ( uint8_t * )"not connected", LEFT_MODE );
-        }
-        mpool.free(x_DynNetwork);
-    }
-    return 0;
-}
+Serial pc(USBTX, USBRX); // tx, rx
 
-/*-----------------------------------------------------------*/
-
-void v_TouchThread( )
+int32_t l_sendNTPpacket(void)
 {
-     Timer x_Timer1;
-    uint32_t ul_Time =0;
-    uint32_t ul_Counter =0;
-    TS_StateTypeDef Touch_State; 
-    ip_addr_t x_PingTarget = { 0x08080808 }; 
+    UDPSocket x_Sock;
+    int32_t l_ErrorCatch;
+    uint32_t ul_Epoch;  
+    uint16_t us_LowWord;
+    uint16_t us_HighWord; 
+    uint8_t uc_PacketBuffer[NTP_PACKET_SIZE];
     
-    Touch.Init( 420, 272 ); 
+    uc_PacketBuffer[0] = 0b11100011;
+    uc_PacketBuffer[1] = 0;
+    uc_PacketBuffer[2] = 6;
+    uc_PacketBuffer[3] = 0xEC;
+    uc_PacketBuffer[12] = 49;
+    uc_PacketBuffer[13] = 0x4E;
+    uc_PacketBuffer[14] = 49;
+    uc_PacketBuffer[15] = 52;
+    
+    l_ErrorCatch = x_Sock.init();
     
-    while( 1 ) 
+    if( l_ErrorCatch == -1 )
     {
-        Touch.GetState( &Touch_State );
-        if( ( Touch_State.touchDetected )&& ( Touch_State.touchX[0] < 125 )&& ( Touch_State.touchY[0] < 69 ) ) 
+        display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE );
+        return -1;
+    }
+    
+    Endpoint x_NTPServer;
+    l_ErrorCatch = x_NTPServer.set_address(NTP_SERVER_ADDRESS, NTP_SERVER_PORT);
+    if( l_ErrorCatch == -1 )
+    {
+        l_ErrorCatch = x_NTPServer.set_address(NTP_SERVER_ADDRESS_IP, NTP_SERVER_PORT);
+        if( l_ErrorCatch == -1 )
         {
-            ul_ResetDisplay( );
-            ul_CreateMenu( );
-            ul_ShowSettings( );
-        } 
-        else if( ( Touch_State.touchDetected )&& ( Touch_State.touchX[0] < 125 )&& ( Touch_State.touchY[0] > 68 )&& ( Touch_State.touchY[0] < 137 ) ) 
-        { 
-            for( ul_Counter = 0; ul_Counter < 4; ul_Counter++)
-            {
-                x_Timer1.start( );
-                ul_Ping( &x_PingTarget );
-                x_Timer1.stop( );
-                ul_Time = ul_Time + x_Timer1;
-            }
-            ul_ResetDisplay( );
-            ul_CreateMenu( );
-            ul_Time = ul_Time/4;
-            display.DisplayStringAt( 130, 10, ( uint8_t * )"Internet: ", LEFT_MODE );
-            if( ul_CheckInternet == 0 )
-            {
-                display.DisplayStringAt( 270, 90, ( uint8_t * )"connected", LEFT_MODE );
-            }
-            else
-            {
-                display.DisplayStringAt( 270, 90, ( uint8_t * )"not connected", LEFT_MODE );
-            }
-            display.DisplayStringAt( 130, 30, ( uint8_t * )"PING: ", LEFT_MODE ); 
-            display.DisplayStringAt( 270, 30, ( uint8_t * )ul_Time, LEFT_MODE );
-        }
-        else
-        {
-            //niks doen
+            display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE );
+            return -2;
         }
     }
+    
+    l_ErrorCatch= x_Sock.sendTo(x_NTPServer, ( char* )uc_PacketBuffer, NTP_PACKET_SIZE);
+    
+    if( l_ErrorCatch == -1 )
+    {
+        display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE );
+        return -1;
+    }
+    
+    l_ErrorCatch = x_Sock.receiveFrom(x_NTPServer, ( char* ) uc_PacketBuffer, NTP_PACKET_SIZE);
+
+    if( l_ErrorCatch == -1 )
+    {
+        display.DisplayStringAt( 270, 70, ( uint8_t * )"Not Connected" , LEFT_MODE );
+        return -1;
+    }
+    
+    us_HighWord = (uc_PacketBuffer[40] << 8 ) | uc_PacketBuffer[41];
+    us_LowWord = (uc_PacketBuffer[42] << 8 ) | uc_PacketBuffer[43];
+    ul_Epoch = ((us_HighWord << 16) | us_LowWord );
+    ul_Epoch = ul_Epoch - 2208988800;
+    
+    display.SetFont( &Font16 );
+    display.DisplayStringAt( 270, 70, ( uint8_t * )"Connected" , LEFT_MODE );
+    
+    x_Sock.close();
+    return 0;
 }
 
 /*-----------------------------------------------------------*/
 
 int main( void )
 {   
-    Thread x_T1(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL);
-    x_T1.start( v_TouchThread );
-    while( ul_CheckDHCP( ) != 0 )
-    {
-        ul_ResetDisplay( );
-        ul_CreateMenu( );
-        //Thread x_T1(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL);
-        //x_T1.start( v_TouchThread );
-
-        ul_ShowSettings( );
-        ul_CheckInternet( );
-        
-        wait_ms(1000);                                           //kleinde delay => geen 1000 keer uitvoeren van checkdhcp => efficienter maar misschien iets trager
-    }
-    return 0;
-}
+    l_ShowSettings( );
+    Eth.disconnect();
+}
\ No newline at end of file