Project Embedded Systems E-ict Denayer

Dependencies:   BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed

main.cpp

Committer:
Ayrton_L
Date:
2017-07-17
Revision:
3:7aef6c427f97
Parent:
2:1a5565ee8219
Child:
4:860566e5814e

File content as of revision 3:7aef6c427f97:

#include "main.h"

int32_t l_ResetDisplay( void )
{
    display.Clear( LCD_COLOR_WHITE );
    display.SetBackColor( LCD_COLOR_WHITE );
    display.SetTextColor( LCD_COLOR_BLACK );
    return 0;
}

/*-----------------------------------------------------------*/

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];
        Eth.connect( );
        strncpy( c_NewIP, Eth.getIPAddress( ), 16 );
        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 );
            queue.put(x_DynNetwork);
        }
        ul_MyReturn = 0;
    }
    else
    {
            ul_MyReturn =1;
    }
    return ul_MyReturn;
}

/*-----------------------------------------------------------*/

int32_t l_CreateMenu( void )
{
    //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;
}

/*-----------------------------------------------------------*/

int32_t l_ShowSettings( void )
{
    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 )
    {
        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 );        
        
        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;
}

/*-----------------------------------------------------------*/

Serial pc(USBTX, USBRX); // tx, rx

int32_t l_sendNTPpacket(void)
{
    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];
    
    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();
    
    if( l_ErrorCatch == -1 )
    {
        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 )
        {
            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 )
{   
    l_ShowSettings( );
    Eth.disconnect();
}