Project Embedded Systems E-ict Denayer

Dependencies:   BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed

Committer:
Ayrton_L
Date:
Thu Aug 17 14:36:34 2017 +0000
Revision:
5:0518cef07365
Final publish; Wrong branch was added last time

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ayrton_L 5:0518cef07365 1 #include "ping.h"
Ayrton_L 5:0518cef07365 2
Ayrton_L 5:0518cef07365 3 void v_MaakPingRequest( struct icmp_echo_hdr *x_ICMPEchoHdr, uint16_t us_Lenght )
Ayrton_L 5:0518cef07365 4 {
Ayrton_L 5:0518cef07365 5 int32_t ul_I = 0;
Ayrton_L 5:0518cef07365 6 int32_t ul_DataLenght = 0;
Ayrton_L 5:0518cef07365 7 ul_DataLenght = us_Lenght - sizeof( struct icmp_echo_hdr );
Ayrton_L 5:0518cef07365 8
Ayrton_L 5:0518cef07365 9 ICMPH_TYPE_SET( x_ICMPEchoHdr, ICMP_ECHO );
Ayrton_L 5:0518cef07365 10 ICMPH_CODE_SET( x_ICMPEchoHdr, 0 );
Ayrton_L 5:0518cef07365 11 x_ICMPEchoHdr->chksum = 0;
Ayrton_L 5:0518cef07365 12 x_ICMPEchoHdr->id = PING_ID;
Ayrton_L 5:0518cef07365 13 x_ICMPEchoHdr->seqno = htons( ++us_PingSequenceNummer ); //byte order to netwerk byte order
Ayrton_L 5:0518cef07365 14
Ayrton_L 5:0518cef07365 15 for( ul_I = 0; ul_I < ul_DataLenght; ul_I++ ) //beetje "random" data erin gooien
Ayrton_L 5:0518cef07365 16 {
Ayrton_L 5:0518cef07365 17 ( ( char* ) x_ICMPEchoHdr )[sizeof( struct icmp_echo_hdr ) + ul_I] = ( char )ul_I;
Ayrton_L 5:0518cef07365 18 }
Ayrton_L 5:0518cef07365 19
Ayrton_L 5:0518cef07365 20 x_ICMPEchoHdr->chksum = inet_chksum( x_ICMPEchoHdr, us_Lenght );
Ayrton_L 5:0518cef07365 21 }
Ayrton_L 5:0518cef07365 22
Ayrton_L 5:0518cef07365 23 /*-----------------------------------------------------------*/
Ayrton_L 5:0518cef07365 24
Ayrton_L 5:0518cef07365 25 err_t ux_Ping( int32_t l_SocketReturn, ip_addr_t *x_IPAddr ) //pind maken
Ayrton_L 5:0518cef07365 26 {
Ayrton_L 5:0518cef07365 27 int32_t l_Err = 0;
Ayrton_L 5:0518cef07365 28 int32_t l_PingGrootte = 0;
Ayrton_L 5:0518cef07365 29 struct icmp_echo_hdr *x_ICMPEchoHdr;
Ayrton_L 5:0518cef07365 30 struct sockaddr_in x_VerzendAddr;
Ayrton_L 5:0518cef07365 31
Ayrton_L 5:0518cef07365 32 /* NOTE
Ayrton_L 5:0518cef07365 33 Ik doe een malloc omdat:
Ayrton_L 5:0518cef07365 34 Ik voor mijn fucntie om een ping request samen te stellen een pointer nodig heb,
Ayrton_L 5:0518cef07365 35 doordat een pointer geen grootte heeft en slechts enkel een geheugenplaats kan aanduiden
Ayrton_L 5:0518cef07365 36 gebruik ik dus malloc om deze pointer zijn geheugenplaats toe te wijzen waar nadien
Ayrton_L 5:0518cef07365 37 al mijn data van mijn ICMP-struct inkom*/
Ayrton_L 5:0518cef07365 38
Ayrton_L 5:0518cef07365 39 l_PingGrootte = sizeof( struct icmp_echo_hdr ) + PING_DATA_SIZE;
Ayrton_L 5:0518cef07365 40 x_ICMPEchoHdr = ( struct icmp_echo_hdr * ) mem_malloc( ( mem_size_t ) l_PingGrootte ); //mallocske doen, is helaas niet toegelaten volgens mistra C :(
Ayrton_L 5:0518cef07365 41 //mem_size_t afhankelijk van hoe groot, uint16_t of uint32_t is van type size_t => malloc heeft verwacht size_t => unsigned type
Ayrton_L 5:0518cef07365 42 if( !x_ICMPEchoHdr ) //heeft ni veel nut dat functie verdergaat en error gooit als Echo failed
Ayrton_L 5:0518cef07365 43 {
Ayrton_L 5:0518cef07365 44 return ERR_MEM; //Out Of memory errorke geven
Ayrton_L 5:0518cef07365 45 }
Ayrton_L 5:0518cef07365 46
Ayrton_L 5:0518cef07365 47 v_MaakPingRequest( x_ICMPEchoHdr, ( uint16_t ) l_PingGrootte );
Ayrton_L 5:0518cef07365 48
Ayrton_L 5:0518cef07365 49 x_VerzendAddr.sin_len = sizeof( x_VerzendAddr );
Ayrton_L 5:0518cef07365 50 x_VerzendAddr.sin_family = AF_INET;
Ayrton_L 5:0518cef07365 51 inet_addr_from_ipaddr( &x_VerzendAddr.sin_addr, x_IPAddr );
Ayrton_L 5:0518cef07365 52 l_Err = lwip_sendto( l_SocketReturn, x_ICMPEchoHdr, l_PingGrootte, 0, ( struct sockaddr* ) &x_VerzendAddr, sizeof( x_VerzendAddr ) );
Ayrton_L 5:0518cef07365 53
Ayrton_L 5:0518cef07365 54 mem_free( x_ICMPEchoHdr );
Ayrton_L 5:0518cef07365 55
Ayrton_L 5:0518cef07365 56 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;}
Ayrton_L 5:0518cef07365 57 }
Ayrton_L 5:0518cef07365 58
Ayrton_L 5:0518cef07365 59 /*-----------------------------------------------------------*/
Ayrton_L 5:0518cef07365 60
Ayrton_L 5:0518cef07365 61 void v_PingOntvang( int32_t l_SocketReturn )
Ayrton_L 5:0518cef07365 62 {
Ayrton_L 5:0518cef07365 63 char c_Buffer[64];
Ayrton_L 5:0518cef07365 64 uint32_t ul_FromLen = 0;
Ayrton_L 5:0518cef07365 65 uint32_t ul_RecvLen = 0;
Ayrton_L 5:0518cef07365 66 struct sockaddr_in x_OntvangAdres;
Ayrton_L 5:0518cef07365 67 struct ip_hdr *px_IPHdr;
Ayrton_L 5:0518cef07365 68 struct icmp_echo_hdr *pux_Echo;
Ayrton_L 5:0518cef07365 69 ip_addr_t x_RecvAddr;
Ayrton_L 5:0518cef07365 70
Ayrton_L 5:0518cef07365 71 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
Ayrton_L 5:0518cef07365 72
Ayrton_L 5:0518cef07365 73 while( ul_RecvLen > 0 )
Ayrton_L 5:0518cef07365 74 {
Ayrton_L 5:0518cef07365 75 if( ul_RecvLen >= sizeof( struct ip_hdr ) + sizeof( struct icmp_echo_hdr ) );
Ayrton_L 5:0518cef07365 76 {
Ayrton_L 5:0518cef07365 77 inet_addr_to_ipaddr(&x_RecvAddr, &x_OntvangAdres.sin_addr);
Ayrton_L 5:0518cef07365 78
Ayrton_L 5:0518cef07365 79 px_IPHdr = ( struct ip_hdr * ) c_Buffer;
Ayrton_L 5:0518cef07365 80 pux_Echo = ( struct icmp_echo_hdr * ) ( c_Buffer + ( IPH_HL( px_IPHdr ) * 4));
Ayrton_L 5:0518cef07365 81
Ayrton_L 5:0518cef07365 82 if( ( pux_Echo->id == PING_ID ) && ( pux_Echo->seqno == htons( us_PingSequenceNummer ) ) ) //matchen van data, byte order naar netwerk byte order
Ayrton_L 5:0518cef07365 83 {
Ayrton_L 5:0518cef07365 84 PING_RESULT( ( ICMPH_TYPE( iecho ) == ICMP_ER ) ); //ping resultaat processen
Ayrton_L 5:0518cef07365 85 return;
Ayrton_L 5:0518cef07365 86 }
Ayrton_L 5:0518cef07365 87 }
Ayrton_L 5:0518cef07365 88
Ayrton_L 5:0518cef07365 89 ul_RecvLen = lwip_recvfrom( l_SocketReturn, c_Buffer, sizeof( c_Buffer ), 0, ( struct sockaddr* ) &x_OntvangAdres, ( socklen_t* ) &ul_FromLen );
Ayrton_L 5:0518cef07365 90 }
Ayrton_L 5:0518cef07365 91
Ayrton_L 5:0518cef07365 92 PING_RESULT( 0 ); //ping resultaat processen
Ayrton_L 5:0518cef07365 93 }
Ayrton_L 5:0518cef07365 94
Ayrton_L 5:0518cef07365 95 /*-----------------------------------------------------------*/
Ayrton_L 5:0518cef07365 96
Ayrton_L 5:0518cef07365 97 uint32_t ul_Ping( ip_addr_t *x_PingTarget )
Ayrton_L 5:0518cef07365 98 {
Ayrton_L 5:0518cef07365 99 int32_t l_SocketReturn = 0; //vooral om te kunnen debuggen binnen LWIP, sockets.c ... -1 on fail
Ayrton_L 5:0518cef07365 100 uint32_t ul_TimeOut = 0;
Ayrton_L 5:0518cef07365 101
Ayrton_L 5:0518cef07365 102 l_SocketReturn = lwip_socket( AF_INET, SOCK_RAW, IP_PROTO_ICMP );
Ayrton_L 5:0518cef07365 103 ul_TimeOut = PING_RCV_TIMEO;
Ayrton_L 5:0518cef07365 104
Ayrton_L 5:0518cef07365 105 if( l_SocketReturn > -1 )
Ayrton_L 5:0518cef07365 106 {
Ayrton_L 5:0518cef07365 107 lwip_setsockopt( l_SocketReturn, SOL_SOCKET, SO_RCVTIMEO, &ul_TimeOut, sizeof( ul_TimeOut ) );
Ayrton_L 5:0518cef07365 108
Ayrton_L 5:0518cef07365 109 if ( ux_Ping( l_SocketReturn, x_PingTarget ) == ERR_OK )
Ayrton_L 5:0518cef07365 110 {
Ayrton_L 5:0518cef07365 111 v_PingOntvang( l_SocketReturn );
Ayrton_L 5:0518cef07365 112 }
Ayrton_L 5:0518cef07365 113 sys_msleep( PING_DELAY );
Ayrton_L 5:0518cef07365 114 return 0;
Ayrton_L 5:0518cef07365 115 }
Ayrton_L 5:0518cef07365 116 else
Ayrton_L 5:0518cef07365 117 {
Ayrton_L 5:0518cef07365 118 return 1;
Ayrton_L 5:0518cef07365 119 }
Ayrton_L 5:0518cef07365 120 }