Code for PING ethshield

just tested…

void EthernetDriver::send_ping_to_device(uint8_t device_id){

struct MGS
{
uint8_t Type; // 0 - Ping Reply, 8 - Ping Request
uint8_t Code; // Always 0
uint16_t CheckSum; // Check sum
uint16_t ID; // Identification
uint16_t SeqNum; // Sequence Number
}msg;

int s = 0;//solo s0 puo fare raw
if(transaction>6000)transaction=1;

uint8_t ip = { 0,0,0,0 };
W5100.getIPAddress(ip);

msg.ID = ip[3];
msg.Type = 8;//PING_REQUEST
msg.Code = 0;//CODE_ZERO
msg.SeqNum = hton(transaction);
msg.CheckSum = 0;

ip[3] = device_id;

if(W5100.readSnSR(s) != SnSR::IPRAW){
if(W5100.readSnSR(s) != SnSR::CLOSED)return;
//W5100.writeSnMR(s,SnMR::IPRAW);
W5100.writeSnPROTO(s,IPPROTO::ICMP);
//W5100.execCmdSn(s,SockCMD::Sock_OPEN);
socket(s,SnMR::IPRAW,3000,0);
}

//cksum
uint16_t* sum_id = (uint16_t*)&msg;
uint32_t sum = 0;
uint16_t last = 0;

int count = sizeof(msg);

while( count > 1 ) {
sum += *sum_id;
sum_id++;
count -= 2;
}
if(count>0){
last = ((unsigned char)sum_id);
last = last << 8;
sum +=last;
}

sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);

msg.CheckSum = ~(sum);
sendto(s,(uint8_t*)&msg,sizeof(msg),ip,3000);
transaction++;
}

uint16_t EthernetDriver::wait_recive_ping_from_device(){

struct MSG
{
uint8_t Type; // 0 - Ping Reply, 8 - Ping Request
uint8_t Code; // Always 0
uint16_t CheckSum; // Check sum
uint16_t ID; // Identification
uint16_t SeqNum; // Sequence Number
}msg;

int s =0;

if(W5100.readSnSR(s) != SnSR::IPRAW)return 0xffff;

uint8_t remote_ip = { 0,0,0,0 };
uint16_t remote_port = 0;

uint8_t srv_ip = { 0,0,0,0 };
W5100.getIPAddress(srv_ip);

if(W5100.getRXReceivedSize(0)<sizeof(msg))return 0xffff;
recvfrom(s,(uint8_t*)&msg,sizeof(msg),remote_ip,&remote_port);

if(msg.ID == srv_ip[3] && remote_ip[2] == srv_ip[2] && remote_ip[1] == srv_ip[1] && remote_ip[0] == srv_ip[0]){
if(msg.Type == 0){
return remote_ip[3];
}
}

return 0xffff;
}