Project Embedded Systems E-ict Denayer
Dependencies: BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed
Revision 5:0518cef07365, committed 2017-08-17
- Comitter:
- Ayrton_L
- Date:
- Thu Aug 17 14:36:34 2017 +0000
- Parent:
- 4:860566e5814e
- Commit message:
- Final publish; Wrong branch was added last time
Changed in this revision
diff -r 860566e5814e -r 0518cef07365 README.md --- a/README.md Thu Aug 17 13:32:25 2017 +0000 +++ b/README.md Thu Aug 17 14:36:34 2017 +0000 @@ -62,3 +62,10 @@ deze controller nog niet volledig ondersteund wordt. Een mooi voorbeeld hiervan is het gebruik van LWIP / F7_Ethernet en niet de standaard library van mbed. + +== Bronnen / gebruikte informatie == +- functie ul_IpStringToNumber (main.cpp): + https://stackoverflow.com/questions/15653695/how-to-convert-ip-address-in-char-to-uint32-t-in-c + +-ping: + https://github.com/goertzenator/lwip/tree/master/contrib-1.4.0/apps/ping \ No newline at end of file
diff -r 860566e5814e -r 0518cef07365 main.cpp --- a/main.cpp Thu Aug 17 13:32:25 2017 +0000 +++ b/main.cpp Thu Aug 17 14:36:34 2017 +0000 @@ -10,6 +10,60 @@ /*-----------------------------------------------------------*/ +int32_t ul_IpStringToNumber( char* pux_IpAddr, uint32_t * pul_IpAddr ) +{ + uint8_t uc_Byte3; + uint8_t uc_Byte2; + uint8_t uc_Byte1; + uint8_t uc_Byte0; + char ux_DummyString[2]; //dummy -> als er na %ls nog een char komt -> geen correct adres + + if ( sscanf( pux_IpAddr, "%u.%u.%u.%u%1s", &uc_Byte3, &uc_Byte2, &uc_Byte1, &uc_Byte0, ux_DummyString ) == 4 ) + { + if( ( uc_Byte3 < 256 ) && ( uc_Byte2 < 256 ) && ( uc_Byte1 < 256 ) && ( uc_Byte0 < 256 ) ) + { + *pul_IpAddr = ( uc_Byte3 << 24 ) + ( uc_Byte2 << 16 ) + ( uc_Byte1 << 8 ) + uc_Byte0; + return 1; + } + } + + return 0; +} + +/*-----------------------------------------------------------*/ + +int32_t ul_ShowDevices( void ) +{ + uint32_t ul_GenIP = 0; + uint32_t ul_MyIP = 0; + uint32_t ul_Counter =0; + uint32_t ul_DevCounter = 0; + + ul_IpStringToNumber(Eth.getIPAddress( ) , &ul_MyIP); + + 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 + + ul_GenIP = ul_MyIP & 0xFFFFFF00; + display.SetFont( &Font16 ); + display.DisplayStringAt( 10, 120, ( uint8_t * )"PING: ", LEFT_MODE ); + + for( ul_Counter = 1; ul_Counter < 255; ul_Counter++) + { + ul_GenIP++; + if( ul_Ping( ( ip_addr_t * ) ul_GenIP ) == 0 ) + { + ip_addr_t ux_ConIP = { ul_GenIP }; + x_ActDev[ul_DevCounter] = ux_ConIP; //voor een of andere reden is dit de enige manier dat werkt + display.DisplayStringAt( 10, ( ul_DevCounter * 10 ) + 120 , ( uint8_t * )ul_GenIP, LEFT_MODE ); + ul_DevCounter++; + } + } + + return 0; +} + +/*-----------------------------------------------------------*/ + int32_t l_ShowSettings( void ) { l_ResetDisplay( ); @@ -114,5 +168,6 @@ int main( void ) { l_ShowSettings( ); + ul_ShowDevices( ); Eth.disconnect(); } \ No newline at end of file
diff -r 860566e5814e -r 0518cef07365 main.h --- a/main.h Thu Aug 17 13:32:25 2017 +0000 +++ b/main.h Thu Aug 17 14:36:34 2017 +0000 @@ -11,6 +11,7 @@ #include "TS_DISCO_F746NG.h" #include "LCD_DISCO_F746NG.h" +#include "ping.h" #ifndef NTP_SERVER_ADDRESS @@ -47,13 +48,6 @@ TS_DISCO_F746NG Touch; - -Serial pc(USBTX, USBRX); // tx, rx - -MemoryPool<Network, 64> mpool; -Queue<Network, 64> queue; - - /*-----------------------------------------------------------*/ int32_t l_ResetDisplay( void );
diff -r 860566e5814e -r 0518cef07365 ping.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ping.cpp Thu Aug 17 14:36:34 2017 +0000 @@ -0,0 +1,120 @@ +#include "ping.h" + +void v_MaakPingRequest( struct icmp_echo_hdr *x_ICMPEchoHdr, uint16_t us_Lenght ) +{ + int32_t ul_I = 0; + int32_t ul_DataLenght = 0; + ul_DataLenght = us_Lenght - sizeof( struct icmp_echo_hdr ); + + ICMPH_TYPE_SET( x_ICMPEchoHdr, ICMP_ECHO ); + ICMPH_CODE_SET( x_ICMPEchoHdr, 0 ); + x_ICMPEchoHdr->chksum = 0; + x_ICMPEchoHdr->id = PING_ID; + x_ICMPEchoHdr->seqno = htons( ++us_PingSequenceNummer ); //byte order to netwerk byte order + + for( ul_I = 0; ul_I < ul_DataLenght; ul_I++ ) //beetje "random" data erin gooien + { + ( ( char* ) x_ICMPEchoHdr )[sizeof( struct icmp_echo_hdr ) + ul_I] = ( char )ul_I; + } + + x_ICMPEchoHdr->chksum = inet_chksum( x_ICMPEchoHdr, us_Lenght ); +} + +/*-----------------------------------------------------------*/ + +err_t ux_Ping( int32_t l_SocketReturn, ip_addr_t *x_IPAddr ) //pind maken +{ + int32_t l_Err = 0; + int32_t l_PingGrootte = 0; + struct icmp_echo_hdr *x_ICMPEchoHdr; + struct sockaddr_in x_VerzendAddr; + + /* NOTE + Ik doe een malloc omdat: +Ik voor mijn fucntie om een ping request samen te stellen een pointer nodig heb, +doordat een pointer geen grootte heeft en slechts enkel een geheugenplaats kan aanduiden +gebruik ik dus malloc om deze pointer zijn geheugenplaats toe te wijzen waar nadien +al mijn data van mijn ICMP-struct inkom*/ + + l_PingGrootte = sizeof( struct icmp_echo_hdr ) + PING_DATA_SIZE; + x_ICMPEchoHdr = ( struct icmp_echo_hdr * ) mem_malloc( ( mem_size_t ) l_PingGrootte ); //mallocske doen, is helaas niet toegelaten volgens mistra C :( + //mem_size_t afhankelijk van hoe groot, uint16_t of uint32_t is van type size_t => malloc heeft verwacht size_t => unsigned type + if( !x_ICMPEchoHdr ) //heeft ni veel nut dat functie verdergaat en error gooit als Echo failed + { + return ERR_MEM; //Out Of memory errorke geven + } + + v_MaakPingRequest( x_ICMPEchoHdr, ( uint16_t ) l_PingGrootte ); + + x_VerzendAddr.sin_len = sizeof( x_VerzendAddr ); + x_VerzendAddr.sin_family = AF_INET; + inet_addr_from_ipaddr( &x_VerzendAddr.sin_addr, x_IPAddr ); + l_Err = lwip_sendto( l_SocketReturn, x_ICMPEchoHdr, l_PingGrootte, 0, ( struct sockaddr* ) &x_VerzendAddr, sizeof( x_VerzendAddr ) ); + + mem_free( x_ICMPEchoHdr ); + + return ( l_Err ? ERR_OK : ERR_VAL ); //geen error = 0 => ERR_OK anders ERR_VAL zelfs als if(l_Err =0) {return ERR_OK;} else {return ERR_VAL;} +} + +/*-----------------------------------------------------------*/ + +void v_PingOntvang( int32_t l_SocketReturn ) +{ + char c_Buffer[64]; + uint32_t ul_FromLen = 0; + uint32_t ul_RecvLen = 0; + struct sockaddr_in x_OntvangAdres; + struct ip_hdr *px_IPHdr; + struct icmp_echo_hdr *pux_Echo; + ip_addr_t x_RecvAddr; + + ul_RecvLen = lwip_recvfrom( l_SocketReturn, c_Buffer, sizeof( c_Buffer ), 0, ( struct sockaddr* ) &x_OntvangAdres, ( socklen_t* ) &ul_FromLen); //word gebruikt om te kijken vanwaar de data komt, na oproep heeft deze dus een waarde + + while( ul_RecvLen > 0 ) + { + if( ul_RecvLen >= sizeof( struct ip_hdr ) + sizeof( struct icmp_echo_hdr ) ); + { + inet_addr_to_ipaddr(&x_RecvAddr, &x_OntvangAdres.sin_addr); + + px_IPHdr = ( struct ip_hdr * ) c_Buffer; + pux_Echo = ( struct icmp_echo_hdr * ) ( c_Buffer + ( IPH_HL( px_IPHdr ) * 4)); + + if( ( pux_Echo->id == PING_ID ) && ( pux_Echo->seqno == htons( us_PingSequenceNummer ) ) ) //matchen van data, byte order naar netwerk byte order + { + PING_RESULT( ( ICMPH_TYPE( iecho ) == ICMP_ER ) ); //ping resultaat processen + return; + } + } + + ul_RecvLen = lwip_recvfrom( l_SocketReturn, c_Buffer, sizeof( c_Buffer ), 0, ( struct sockaddr* ) &x_OntvangAdres, ( socklen_t* ) &ul_FromLen ); + } + + PING_RESULT( 0 ); //ping resultaat processen +} + +/*-----------------------------------------------------------*/ + +uint32_t ul_Ping( ip_addr_t *x_PingTarget ) +{ + int32_t l_SocketReturn = 0; //vooral om te kunnen debuggen binnen LWIP, sockets.c ... -1 on fail + uint32_t ul_TimeOut = 0; + + l_SocketReturn = lwip_socket( AF_INET, SOCK_RAW, IP_PROTO_ICMP ); + ul_TimeOut = PING_RCV_TIMEO; + + if( l_SocketReturn > -1 ) + { + lwip_setsockopt( l_SocketReturn, SOL_SOCKET, SO_RCVTIMEO, &ul_TimeOut, sizeof( ul_TimeOut ) ); + + if ( ux_Ping( l_SocketReturn, x_PingTarget ) == ERR_OK ) + { + v_PingOntvang( l_SocketReturn ); + } + sys_msleep( PING_DELAY ); + return 0; + } + else + { + return 1; + } +}
diff -r 860566e5814e -r 0518cef07365 ping.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ping.h Thu Aug 17 14:36:34 2017 +0000 @@ -0,0 +1,46 @@ +#ifndef _PING_H_ +#define _PING_H_ + +#include "lwip/opt.h" +#include "lwip/mem.h" +#include "lwip/raw.h" +#include "lwip/icmp.h" +#include "lwip/netif.h" +#include "lwip/sys.h" +#include "lwip/timers.h" +#include "lwip/inet_chksum.h" +#include "lwip/sockets.h" +#include "lwip/inet.h" + +#ifndef PING_RCV_TIMEO //ping timeout... geen antwoord binnen deze tijd => timeout request +#define PING_RCV_TIMEO 1000 +#endif + +#ifndef PING_ID +#define PING_ID 0x2601 //identifier om te matchen (antwoord matched hiermee. Linux => uniek, Windows => vast, maar wel verschillend per versie) +#endif + +#ifndef PING_DATA_SIZE //grootte payload: "random" data die ook gematched moet worden in het antwoord +#define PING_DATA_SIZE 32 +#endif + +#ifndef PING_DELAY +#define PING_DELAY 500 +#endif + +#ifndef PING_RESULT +#define PING_RESULT(ping_ok) +#endif + +/*-----------------------------------------------------------*/ + +uint32_t ul_Ping( ip_addr_t *x_PingTarget ); +void v_MaakPingRequest( struct icmp_echo_hdr *x_ICMPEchoHdr, uint16_t us_Lenght ); +void v_PingOntvang( int32_t l_SocketReturn ); +err_t ux_Ping( int32_t l_SocketReturn, ip_addr_t *x_IPAddr ); + +static uint16_t us_PingSequenceNummer; + +/*-----------------------------------------------------------*/ + +#endif