Просмотр исходного кода

save success ping TODO: normalize code

karasevIA 2 лет назад
Родитель
Сommit
b90babffe6
7 измененных файлов с 217 добавлено и 214 удалено
  1. 1 33
      application.fam
  2. 5 10
      eth_view_process.c
  3. 53 19
      eth_worker.c
  4. 5 1
      eth_worker.h
  5. 15 0
      eth_worker_ping.c
  6. 122 135
      lib/ioLibrary_Driver/Internet/ICMP/ping.c
  7. 16 16
      lib/ioLibrary_Driver/Internet/ICMP/ping.h

+ 1 - 33
application.fam

@@ -32,43 +32,11 @@ App(
 				"Internet/httpServer",
                 "Internet/ICMP",
 				"Internet/MQTT",
-                "MQTTPacket/src",
+                "Internet/MQTT/MQTTPacket/src",
 				"Internet/SNMP",
 				"Internet/SNTP",
                 "Internet/TFTP",
 			],
-			sources=[
-				"w5500.c",
-                "socket.c",
-                "wizchip_conf.c",
-                "dhcp.c",
-				"dns.c",
-				"ftpc.c",
-                "ftpf.c",
-				"httpParser.c",
-                "httpServer.c",
-                "httpUtil.c",
-                "ping.c",
-				"mqtt_interface.c",
-				"MQTTClient.c",
-                "MQTTConnectClient.c",
-                "MQTTConnectServer.c",
-                "MQTTDeserializePublish.c",
-                "MQTTFormat.c",
-                "MQTTPacket.c",
-                "MQTTSerializePublish.c",
-				"MQTTSubscribeClient.c",
-				"MQTTSubscribeServer.c",
-				"MQTTUnsubscribeClient.c",
-				"MQTTUnsubscribeServer.c",
-				"mqtt_interface.c",
-                "MQTTClient.c",
-				"snmp_custom.c",
-                "snmp.c",
-				"sntp.c",
-                "Internet/TFTP/netutil.c",
-                "Internet/TFTP/tftp.c",
-			],
 		),
 	],
 )

+ 5 - 10
eth_view_process.c

@@ -205,19 +205,14 @@ void ethernet_view_process_draw(EthViewProcess* process, Canvas* canvas) {
             }
         }
     } else if(process->type == EthWorkerProcessPing) {
-        canvas_draw_box(canvas, 26, 8, 5, 13);
-        canvas_draw_box(canvas, 52, 8, 3, 13);
-        canvas_draw_box(canvas, 76, 8, 3, 13);
-        canvas_draw_box(canvas, 100, 8, 3, 13);
-        canvas_draw_box(canvas, 124, 8, 4, 13);
-        canvas_draw_frame(canvas, 31, 8, 21, 13);
-        canvas_draw_frame(canvas, 55, 8, 21, 13);
-        canvas_draw_frame(canvas, 79, 8, 21, 13);
-        canvas_draw_frame(canvas, 103, 8, 21, 13);
+        canvas_draw_frame(canvas, 35, 8, 21, 13);
+        canvas_draw_frame(canvas, 59, 8, 21, 13);
+        canvas_draw_frame(canvas, 83, 8, 21, 13);
+        canvas_draw_frame(canvas, 107, 8, 21, 13);
         uint8_t current_digit = ((EthViewDrawPing*)process->draw_struct)->current_digit;
         uint8_t* adress = ((EthViewDrawPing*)process->draw_struct)->ip;
         for(uint8_t i = 0; i < 4; ++i) {
-            uint8_t x = 33 + i * 24;
+            uint8_t x = 37 + i * 24;
             draw_dec_number(canvas, x, 17, adress[i]);
             if(process->editing && (current_digit / 3 == i)) {
                 uint8_t x1 = x + 6 * (current_digit % 3);

+ 53 - 19
eth_worker.c

@@ -4,10 +4,9 @@
 
 #include <furi_hal.h>
 #include "dhcp.h"
-#include "ping.h"
 #include "socket.h"
 #include "stm32wbxx_hal_gpio.h"
-#include "wizchip_conf.h"
+#include <wizchip_conf.h>
 
 #define TAG "EthWorker"
 
@@ -158,7 +157,6 @@ void eth_run(EthWorker* worker, EthWorkerProcess process) {
             break;
         }
         worker->next_state = EthWorkerStatePing;
-        eth_log(EthWorkerProcessPing, "Fuck you");
         break;
     case EthWorkerProcessReset:
         worker->next_state = EthWorkerStateNotInited;
@@ -170,6 +168,7 @@ void eth_run(EthWorker* worker, EthWorkerProcess process) {
     case EthWorkerProcessExit:
         if(worker->state != EthWorkerStateNotAllocated) {
             worker->next_state = EthWorkerStateStop;
+            worker->state = EthWorkerStateStop;
             furi_thread_join(worker->thread);
             furi_thread_free(worker->thread);
             worker->state = EthWorkerStateNotAllocated;
@@ -245,6 +244,37 @@ void update_WIZNETINFO(uint8_t is_dhcp) {
     }
 }
 
+int check_phylink(EthWorker* worker, EthWorkerState state, EthWorkerProcess proc, int timeout) {
+    uint32_t start_time = furi_get_tick();
+    uint32_t last_log_time = start_time;
+    eth_log(proc, "phy link check 0");
+    for(;;) {
+        if(furi_get_tick() > start_time + timeout) {
+            eth_log(proc, "phy link timeout");
+            break;
+        }
+        if(worker->state != state) {
+            eth_log(proc, "state changed");
+            break;
+        }
+        uint8_t link = PHY_LINK_OFF;
+        if(ctlwizchip(CW_GET_PHYLINK, (void*)&link) == -1) {
+            eth_log(proc, "Unknown PHY link status");
+            break;
+        }
+        if(link != PHY_LINK_OFF) {
+            eth_log(proc, "phy link on");
+            return 1;
+        }
+        furi_delay_ms(20);
+        if(furi_get_tick() > last_log_time + 1000) {
+            eth_log(proc, "phy link check %d", (last_log_time - start_time) / 1000);
+            last_log_time = furi_get_tick();
+        }
+    }
+    return 0;
+}
+
 #define DHCP_SOCKET 0
 uint8_t ping_auto(uint8_t s, uint8_t* addr);
 
@@ -322,22 +352,16 @@ int32_t eth_worker_task(void* context) {
         } else if(worker->state == EthWorkerStateInited) {
             if(worker->next_state == EthWorkerStateDHCP) {
                 worker->state = EthWorkerStateDHCP;
-                uint8_t temp = PHY_LINK_OFF;
-                while(temp == PHY_LINK_OFF && worker->state == EthWorkerStateDHCP) {
-                    if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
-                        eth_log(EthWorkerProcessDHCP, "Unknown PHY link status");
-                    }
-                    furi_delay_ms(1);
-                }
-                if(worker->state != EthWorkerStateDHCP) {
-                    break;
+                if(!check_phylink(worker, EthWorkerStateDHCP, EthWorkerProcessDHCP, 5000)) {
+                    worker->state = EthWorkerStateInited;
+                    continue;
                 }
                 reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
                 DHCP_init(DHCP_SOCKET, dhcp_buffer);
-                uint8_t dhcp_ret = DHCP_STOPPED;
                 uint8_t next_cycle = 1;
+                uint8_t divider = 0;
                 while(next_cycle && worker->state == EthWorkerStateDHCP) {
-                    dhcp_ret = DHCP_run();
+                    uint8_t dhcp_ret = DHCP_run();
                     switch(dhcp_ret) {
                     case DHCP_IP_ASSIGN:
                     case DHCP_IP_CHANGED:
@@ -357,7 +381,16 @@ int32_t eth_worker_task(void* context) {
                         eth_log(EthWorkerProcessDHCP, "DHCP Failed");
                         break;
                     }
-                    furi_delay_ms(1000);
+                    furi_delay_ms(100);
+                    if(divider++ % 10 == 0) {
+                        eth_log(EthWorkerProcessDHCP, "DHCP process %d", divider / 10);
+                        if(divider > 250) {
+                            DHCP_stop();
+                            eth_log(EthWorkerProcessDHCP, "DHCP Stop by timer");
+                            worker->state = EthWorkerStateInited;
+                            break;
+                        }
+                    }
                     next_cycle = (dhcp_ret == DHCP_RUNNING);
                 }
                 if(worker->state != EthWorkerStateDHCP) {
@@ -399,7 +432,7 @@ int32_t eth_worker_task(void* context) {
                 worker->state = EthWorkerStatePing;
                 uint8_t* adress = static_worker->config->ping_ip;
                 eth_log(
-                    EthWorkerProcessDHCP,
+                    EthWorkerProcessPing,
                     "ping %d.%d.%d.%d",
                     adress[0],
                     adress[1],
@@ -410,14 +443,14 @@ int32_t eth_worker_task(void* context) {
                 while(try < tryes && worker->state == EthWorkerStatePing) {
                     try++;
                     uint32_t start_time = furi_get_tick();
-                    uint8_t res = 3; //ping_auto(1, adress);
+                    uint8_t res = ping_auto_interface(adress);
                     uint32_t res_time = furi_get_tick();
                     if(res == 3) {
                         eth_log(
-                            EthWorkerProcessDHCP, "%d success %d ms", try, res_time - start_time);
+                            EthWorkerProcessPing, "%d success %d ms", try, res_time - start_time);
                     } else {
                         eth_log(
-                            EthWorkerProcessDHCP,
+                            EthWorkerProcessPing,
                             "%d error %d, %d",
                             try,
                             res,
@@ -429,6 +462,7 @@ int32_t eth_worker_task(void* context) {
                     break;
                 }
                 worker->state = EthWorkerStateOnline;
+                worker->next_state = EthWorkerStateDefaultNext;
             } else {
             }
         }

+ 5 - 1
eth_worker.h

@@ -8,6 +8,7 @@ typedef struct EthViewProcess EthViewProcess;
 typedef enum {
     EthWorkerStateNotAllocated = 0,
     EthWorkerStateNotInited,
+    EthWorkerStateDefaultNext,
     EthWorkerStateInited,
     EthWorkerStateInit,
     EthWorkerStateModulePowerOn,
@@ -69,4 +70,7 @@ void eth_worker_start(
 void eth_worker_stop(EthWorker* eth_worker);
 void eth_worker_dhcp(EthWorker* eth_worker);
 void eth_worker_w5500(EthWorker* eth_worker);
-void eth_worker_init_process(EthWorker* eth_worker);
+void eth_worker_init_process(EthWorker* eth_worker);
+
+#define PING_SOCKET 1
+uint8_t ping_auto_interface(uint8_t* adress);

+ 15 - 0
eth_worker_ping.c

@@ -0,0 +1,15 @@
+#include "eth_worker.h"
+#include <furi_hal.h>
+#include <socket.h>
+#include <dhcp.h>
+#include <ping.h>
+
+uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t* addr);
+
+void ping_wait_ms(int ms) {
+    furi_delay_ms(ms);
+}
+
+uint8_t ping_auto_interface(uint8_t* adress) {
+    return ping_count(PING_SOCKET, 20, adress);
+}

+ 122 - 135
lib/ioLibrary_Driver/Internet/ICMP/ping.c

@@ -2,93 +2,83 @@
 #include "socket.h"
 #include "w5500.h"
 
-PINGMSGR PingRequest;	 // Variable for Ping Request
-PINGMSGR PingReply;	     // Variable for Ping Reply
-static uint16_t RandomID = 0x1234; 
+PINGMSGR PingRequest; // Variable for Ping Request
+PINGMSGR PingReply; // Variable for Ping Reply
+static uint16_t RandomID = 0x1234;
 static uint16_t RandomSeqNum = 0x4321;
-uint8_t ping_reply_received = 0; 
+uint8_t ping_reply_received = 0;
 uint8_t req = 0;
 uint8_t rep = 0;
 
+extern void ping_wait_ms(int ms);
 
-extern void wait_ms(int ms);
-
-uint8_t ping_auto(uint8_t s, uint8_t *addr)
-{
-	uint8_t i;
-	int32_t len = 0;
-	uint8_t cnt = 0;
-    int ret = 0; 
+uint8_t ping_auto(uint8_t s, uint8_t* addr) {
+    uint8_t i;
+    int32_t len = 0;
+    uint8_t cnt = 0;
+    int ret = 0;
     for(i = 0; i <= 3; i++) {
         uint8_t sr = getSn_SR(s);
         //logger("SR: %02X\r\n", sr);
-		switch(sr)
-		{
+        switch(sr) {
         case SOCK_CLOSED:
             close(s);
-            IINCHIP_WRITE(Sn_PROTO(s), IPPROTO_ICMP);     // set ICMP Protocol
-            if ((ret = socket(s, Sn_MR_IPRAW, 3000, 0)) != s) {       // open the SOCKET with IPRAW mode, if fail then Error
+            IINCHIP_WRITE(Sn_PROTO(s), IPPROTO_ICMP); // set ICMP Protocol
+            if((ret = socket(s, Sn_MR_IPRAW, 3000, 0)) !=
+               s) { // open the SOCKET with IPRAW mode, if fail then Error
                 //logger( "\r\n socket %d fail %d\r\n", s, ret);
 #ifdef PING_DEBUG
                 return SOCKET_ERROR;
 #endif
             }
             /* Check socket register */
-            while(getSn_SR(s) != SOCK_IPRAW);
-            wait_ms(1000); // wait 1000ms
-            wait_ms(1000); // wait 1000ms
+            while(getSn_SR(s) != SOCK_IPRAW)
+                ;
+            ping_wait_ms(1000); // wait 1000ms
+            ping_wait_ms(1000); // wait 1000ms
             break;
         case SOCK_IPRAW:
             ping_request(s, addr);
             req++;
             while(1) {
-                if ( (len = getSn_RX_RSR(s) ) > 0)
-                {
+                if((len = getSn_RX_RSR(s)) > 0) {
                     ping_reply(s, addr, len);
                     rep++;
                     break;
-                }
-                else if(cnt > 100)
-                {
+                } else if(cnt > 100) {
                     //logger("Request Time out. \r\n");
                     cnt = 0;
                     break;
-                }
-                else
-                {
+                } else {
                     cnt++;
-                    wait_ms(50); // wait 50ms
+                    ping_wait_ms(50); // wait 50ms
                 }
                 // wait_time for 2 seconds, Break on fail
             }
             break;
         default:
             break;
-		}
+        }
 #ifdef PING_DEBUG
-		if(req >= 3)
-	   	{
+        if(req >= 3) {
             //logger("Ping Request = %d, PING_Reply = %d\r\n", req, rep);
 
-	  		if(rep == req)
-	  			return SUCCESS;
-	  		else
-	  			return REPLY_ERROR;
-	  	}
+            if(rep == req)
+                return PING_SUCCESS;
+            else
+                return REPLY_ERROR;
+        }
 #endif
-	}
+    }
     return FUNCTION_ERROR;
 }
 
+uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t* addr) {
+    uint16_t rlen, cnt, i;
+    cnt = 0;
 
-uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t *addr)
-{
-	uint16_t rlen, cnt, i;
-	cnt = 0;
-	 	 
-	for(i=0; i < pCount + 1; i++) {
-
-        if (i != 0) {
+    for(i = 0; i < pCount + 1; i++) {
+        if(i != 0) {
             /* Output count number */
             //logger( "\r\nNo.%d\r\n", (i-1));
         }
@@ -97,50 +87,51 @@ uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t *addr)
         //logger("SR: %d\r\n", sr);
         switch(sr) {
         case SOCK_CLOSED:
-            close(s);  
+            close(s);
             // close the SOCKET
-            /* Create Socket */  
-            IINCHIP_WRITE(Sn_PROTO(s), IPPROTO_ICMP);              // set ICMP Protocol
-            if (socket(s, Sn_MR_IPRAW, 3000, 0) != s) {       // open the SOCKET with IPRAW mode, if fail then Error
+            /* Create Socket */
+            IINCHIP_WRITE(Sn_PROTO(s), IPPROTO_ICMP); // set ICMP Protocol
+            if(socket(s, Sn_MR_IPRAW, 3000, 0) !=
+               s) { // open the SOCKET with IPRAW mode, if fail then Error
                 //logger( "\r\n socket %d fail r\n", (s));
 #ifdef PING_DEBUG
                 return SOCKET_ERROR;
 #endif
             }
             /* Check socket register */
-            while(getSn_SR(s) != SOCK_IPRAW);
-            wait_ms(1000); // wait 1000ms
-            wait_ms(1000); // wait 1000ms
+            while(getSn_SR(s) != SOCK_IPRAW)
+                ;
+            ping_wait_ms(1000); // wait 1000ms
+            ping_wait_ms(1000); // wait 1000ms
             break;
         case SOCK_IPRAW:
             ping_request(s, addr);
             req++;
             while(1) {
-                if ((rlen = getSn_RX_RSR(s)) > 0) {
+                if((rlen = getSn_RX_RSR(s)) > 0) {
                     ping_reply(s, addr, rlen);
                     rep++;
-                    if (ping_reply_received)  break;               
+                    if(ping_reply_received) break;
                 }
                 /* wait_time for 2 seconds, Break on fail*/
-                if ( (cnt > 100) ) {
+                if((cnt > 100)) {
                     //logger( "\r\nRequest Time out. \r\n") ;
                     cnt = 0;
                     break;
-                } else { 
-                    cnt++; 	
-                    wait_ms(50); // wait 50ms
+                } else {
+                    cnt++;
+                    ping_wait_ms(50); // wait 50ms
                 }
             }
             break;
-        default:		
-            break;    
+        default:
+            break;
         }
 #ifdef PING_DEBUG
-  		if(req >= pCount)
-        {
+        if(req >= pCount) {
             //logger("Ping Request = %d, PING_Reply = %d\r\n", req, rep);
             if(rep == req)
-                return SUCCESS;
+                return PING_SUCCESS;
             else
                 return REPLY_ERROR;
         }
@@ -149,63 +140,63 @@ uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t *addr)
     return FUNCTION_ERROR;
 }
 
-uint8_t ping_request(uint8_t s, uint8_t *addr)
-{
+uint8_t ping_request(uint8_t s, uint8_t* addr) {
     uint16_t i;
 
-	//Initailize flag for ping reply
-	ping_reply_received = 0;
-	/* make header of the ping-request  */
-	PingRequest.Type = PING_REQUEST;                   // Ping-Request
-	PingRequest.Code = CODE_ZERO;	                   // Always '0'
-	PingRequest.ID = htons(RandomID++);	       // set ping-request's ID to random integer value
-	PingRequest.SeqNum =htons(RandomSeqNum++);// set ping-request's sequence number to ramdom integer value
-	//size = 32;                                 // set Data size
+    //Initailize flag for ping reply
+    ping_reply_received = 0;
+    /* make header of the ping-request  */
+    PingRequest.Type = PING_REQUEST; // Ping-Request
+    PingRequest.Code = CODE_ZERO; // Always '0'
+    PingRequest.ID = htons(RandomID++); // set ping-request's ID to random integer value
+    PingRequest.SeqNum =
+        htons(RandomSeqNum++); // set ping-request's sequence number to ramdom integer value
+    //size = 32;                                 // set Data size
 
-	/* Fill in Data[]  as size of BIF_LEN (Default = 32)*/
-  	for(i = 0 ; i < BUF_LEN; i++) {	                                
-		PingRequest.Data[i] = (i) % 8;		  //'0'~'8' number into ping-request's data 	
-	}
-	 /* Do checksum of Ping Request */
-	PingRequest.CheckSum = 0;		               // value of checksum before calucating checksum of ping-request packet
-	PingRequest.CheckSum = htons(checksum((uint8_t*)&PingRequest,sizeof(PingRequest)));  // Calculate checksum
-	
-     /* sendto ping_request to destination */
-	if (sendto(s,(uint8_t *)&PingRequest, sizeof(PingRequest), addr, 3000) == 0) {  // Send Ping-Request to the specified peer.
-        //logger( "\r\n Fail to send ping-reply packet  r\n") ;					
-	} else {
-        //logger( "Send Ping Request  to Destination (") ;					
+    /* Fill in Data[]  as size of BIF_LEN (Default = 32)*/
+    for(i = 0; i < BUF_LEN; i++) {
+        PingRequest.Data[i] = (i) % 8; //'0'~'8' number into ping-request's data
+    }
+    /* Do checksum of Ping Request */
+    PingRequest.CheckSum =
+        0; // value of checksum before calucating checksum of ping-request packet
+    PingRequest.CheckSum =
+        htons(checksum((uint8_t*)&PingRequest, sizeof(PingRequest))); // Calculate checksum
+
+    /* sendto ping_request to destination */
+    if(sendto(s, (uint8_t*)&PingRequest, sizeof(PingRequest), addr, 3000) ==
+       0) { // Send Ping-Request to the specified peer.
+        //logger( "\r\n Fail to send ping-reply packet  r\n") ;
+    } else {
+        //logger( "Send Ping Request  to Destination (") ;
         //logger( "%d.%d.%d.%d )",   (addr[0]),  (addr[1]),  (addr[2]),  (addr[3])) ;
         //logger( " ID:%x  SeqNum:%x CheckSum:%x\r\n",   htons(PingRequest.ID),  htons(PingRequest.SeqNum),  htons(PingRequest.CheckSum)) ;
-	}
-	return 0;
+    }
+    return 0;
 } // ping request
 
-
-
-uint8_t ping_reply(uint8_t s, uint8_t *addr,  uint16_t rlen)
-{
-    uint16_t tmp_checksum;	
+uint8_t ping_reply(uint8_t s, uint8_t* addr, uint16_t rlen) {
+    uint16_t tmp_checksum;
     uint16_t len;
     uint16_t i;
     uint8_t data_buf[128];
     uint16_t port = 3000;
     PINGMSGR PingReply;
     /* receive data from a destination */
-    len = recvfrom(s, (uint8_t *)data_buf, rlen, addr, &port);
-    if (data_buf[0] == PING_REPLY) {
-        PingReply.Type 		 = data_buf[0];
-        PingReply.Code 		 = data_buf[1];
-        PingReply.CheckSum   = (data_buf[3]<<8) + data_buf[2];
-        PingReply.ID 		 = (data_buf[5]<<8) + data_buf[4];
-        PingReply.SeqNum 	 = (data_buf[7]<<8) + data_buf[6];
+    len = recvfrom(s, (uint8_t*)data_buf, rlen, addr, &port);
+    if(data_buf[0] == PING_REPLY) {
+        PingReply.Type = data_buf[0];
+        PingReply.Code = data_buf[1];
+        PingReply.CheckSum = (data_buf[3] << 8) + data_buf[2];
+        PingReply.ID = (data_buf[5] << 8) + data_buf[4];
+        PingReply.SeqNum = (data_buf[7] << 8) + data_buf[6];
 
-        for(i = 0; i < len - 8 ; ++i) {
-            PingReply.Data[i] = data_buf[8+i];
+        for(i = 0; i < len - 8; ++i) {
+            PingReply.Data[i] = data_buf[8 + i];
         }
         /* check Checksum of Ping Reply */
         tmp_checksum = ~checksum(data_buf, len);
-        if (tmp_checksum != 0xffff) {
+        if(tmp_checksum != 0xffff) {
             //logger("tmp_checksum = %x\r\n",tmp_checksum);
         } else {
             /*  Output the Destination IP and the size of the Ping Reply Message*/
@@ -213,58 +204,55 @@ uint8_t ping_reply(uint8_t s, uint8_t *addr,  uint16_t rlen)
             //  (addr[0]),  (addr[1]),  (addr[2]),  (addr[3]),  htons(PingReply.ID),  htons(PingReply.SeqNum),  (rlen+6) );
             //logger("\r\n");
             /*  SET ping_reply_receiver to '1' and go out the while_loop (waitting for ping reply)*/
-            ping_reply_received =1;
+            ping_reply_received = 1;
         }
-    } else if (data_buf[0] == PING_REQUEST) {
-        PingReply.Code 	 = data_buf[1];
-        PingReply.Type 	 = data_buf[2];
-        PingReply.CheckSum  = (data_buf[3]<<8) + data_buf[2];
-        PingReply.ID 		 = (data_buf[5]<<8) + data_buf[4];
-        PingReply.SeqNum 	 = (data_buf[7]<<8) + data_buf[6];
+    } else if(data_buf[0] == PING_REQUEST) {
+        PingReply.Code = data_buf[1];
+        PingReply.Type = data_buf[2];
+        PingReply.CheckSum = (data_buf[3] << 8) + data_buf[2];
+        PingReply.ID = (data_buf[5] << 8) + data_buf[4];
+        PingReply.SeqNum = (data_buf[7] << 8) + data_buf[6];
 
-        for(i=0; i < len - 8; ++i) {
-            PingReply.Data[i] = data_buf[8+i];
+        for(i = 0; i < len - 8; ++i) {
+            PingReply.Data[i] = data_buf[8 + i];
         }
         /* check Checksum of Ping Reply */
         tmp_checksum = PingReply.CheckSum;
         PingReply.CheckSum = 0;
         PingReply.CheckSum = htons(checksum((uint8_t*)&PingReply, len));
 
-        if (tmp_checksum != PingReply.CheckSum){
+        if(tmp_checksum != PingReply.CheckSum) {
             //logger( " \n CheckSum is in correct %x shold be %x \n", (tmp_checksum), htons(PingReply.CheckSum));
         } else {
-            //logger( "\r\n Checksum is correct  \r\n") ;					
+            //logger( "\r\n Checksum is correct  \r\n") ;
         }
-		
+
         /*  Output the Destination IP and the size of the Ping Reply Message*/
         //logger("Request from %d.%d.%d.%d  ID:%x SeqNum:%x  :data size %d bytes\r\n",
         //    (addr[0]),  (addr[1]),  (addr[2]),  (addr[3]),  (PingReply.ID),  (PingReply.SeqNum),  (rlen+6) );
-        /*  SET ping_reply_receiver to '1' and go out the while_loop (waitting for ping reply)*/		   
+        /*  SET ping_reply_receiver to '1' and go out the while_loop (waitting for ping reply)*/
         ping_reply_received = 1;
-        } else {      
-            //logger(" Unkonwn msg. \n");
-        }
-
-        return 0;
-}// ping_reply
+    } else {
+        //logger(" Unkonwn msg. \n");
+    }
 
+    return 0;
+} // ping_reply
 
-uint16_t checksum(uint8_t * data_buf, uint16_t len)
-{
+uint16_t checksum(uint8_t* data_buf, uint16_t len) {
     uint16_t sum, tsum, i, j;
     uint32_t lsum;
 
     j = len >> 1;
     lsum = 0;
     tsum = 0;
-    for (i = 0; i < j; ++i) {
+    for(i = 0; i < j; ++i) {
         tsum = data_buf[i * 2];
         tsum = tsum << 8;
         tsum += data_buf[i * 2 + 1];
         lsum += tsum;
     }
-    if (len % 2)
-    {
+    if(len % 2) {
         tsum = data_buf[i * 2];
         lsum += (tsum << 8);
     }
@@ -273,15 +261,14 @@ uint16_t checksum(uint8_t * data_buf, uint16_t len)
     return sum;
 }
 
-uint16_t htons( uint16_t hostshort)
-{
+uint16_t htons(uint16_t hostshort) {
 #if 1
-  //#ifdef LITTLE_ENDIAN
-	uint16_t netshort=0;
-	netshort = (hostshort & 0xFF) << 8;
-	netshort |= ((hostshort >> 8)& 0xFF);
-	return netshort;
+    //#ifdef LITTLE_ENDIAN
+    uint16_t netshort = 0;
+    netshort = (hostshort & 0xFF) << 8;
+    netshort |= ((hostshort >> 8) & 0xFF);
+    return netshort;
 #else
-	return hostshort;
+    return hostshort;
 #endif
 }

+ 16 - 16
lib/ioLibrary_Driver/Internet/ICMP/ping.h

@@ -10,27 +10,27 @@
 
 #define SOCKET_ERROR 1
 #define TIMEOUT_ERROR 2
-#define SUCCESS 3
+#define PING_SUCCESS 3
 #define REPLY_ERROR 4
 #define FUNCTION_ERROR 5
 #define PING_DEBUG
 
-typedef struct pingmsg
-{
-  uint8_t  Type; 		// 0 - Ping Reply, 8 - Ping Request
-  uint8_t  Code;		// Always 0
-  int16_t  CheckSum;	// Check sum
-  int16_t  ID;	            // Identification
-  int16_t  SeqNum; 	// Sequence Number
-  int8_t   Data[BUF_LEN];// Ping Data  : 1452 = IP RAW MTU - sizeof(Type+Code+CheckSum+ID+SeqNum)
+typedef struct pingmsg {
+    uint8_t Type; // 0 - Ping Reply, 8 - Ping Request
+    uint8_t Code; // Always 0
+    int16_t CheckSum; // Check sum
+    int16_t ID; // Identification
+    int16_t SeqNum; // Sequence Number
+    int8_t Data[BUF_LEN]; // Ping Data  : 1452 = IP RAW MTU - sizeof(Type+Code+CheckSum+ID+SeqNum)
 } PINGMSGR;
 
-
-uint8_t ping_auto(uint8_t s, uint8_t *addr);
-uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t *addr);
-uint8_t ping_request(uint8_t s, uint8_t *addr);
-uint8_t ping_reply(uint8_t s,  uint8_t *addr, uint16_t rlen);
-uint16_t checksum(uint8_t * data_buf, uint16_t len);
-uint16_t htons( uint16_t  hostshort);	/* htons function converts a unsigned short from host to TCP/IP network byte order (which is big-endian).*/
+uint8_t ping_auto(uint8_t s, uint8_t* addr);
+uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t* addr);
+uint8_t ping_request(uint8_t s, uint8_t* addr);
+uint8_t ping_reply(uint8_t s, uint8_t* addr, uint16_t rlen);
+uint16_t checksum(uint8_t* data_buf, uint16_t len);
+uint16_t htons(
+    uint16_t
+        hostshort); /* htons function converts a unsigned short from host to TCP/IP network byte order (which is big-endian).*/
 
 #endif /* PING_H */