static int test_host_by_ip_address(struct harness_t *harness_p) { struct inet_ip_addr_t address; struct time_t round_trip_time, timeout; uint8_t request[8]; uint8_t reply[28]; timeout.seconds = 1; timeout.nanoseconds = 0; /* Prepare the socket stub with the reply packet. The first 20 bytes in the reply are the IP header. */ reply[20] = 0; reply[21] = 0; reply[22] = 0xff; reply[23] = 0xfe; reply[24] = 0; reply[25] = 0; reply[26] = 0; reply[27] = 1; socket_stub_input(reply, sizeof(reply)); /* Perform the ping. */ address.number = 0x1; BTASSERT(ping_host_by_ip_address(&address, &timeout, &round_trip_time) == 0); /* Check the request send by the ping module. */ socket_stub_output(request, sizeof(request)); BTASSERT(request[0] == 8); BTASSERT(request[1] == 0); BTASSERT(request[2] == 0xf7); BTASSERT(request[3] == 0xfe); BTASSERT(request[4] == 0); BTASSERT(request[5] == 0); BTASSERT(request[6] == 0); BTASSERT(request[7] == 1); std_printf(FSTR("round trip time: %lu s %lu ns\r\n"), round_trip_time.seconds, round_trip_time.nanoseconds); return (0); }
int main() { int res, attempt; char remote_host_ip[] = STRINGIFY(REMOTE_HOST_IP); struct inet_ip_addr_t remote_host_ip_address; struct time_t round_trip_time, timeout; sys_start(); if (inet_aton(remote_host_ip, &remote_host_ip_address) != 0) { std_printf(FSTR("Bad ip address '%s'.\r\n"), remote_host_ip); return (-1); } timeout.seconds = 3; timeout.nanoseconds = 0; attempt = 1; /* Ping the remote host once every second. */ while (1) { res = ping_host_by_ip_address(&remote_host_ip_address, &timeout, &round_trip_time); if (res == 0) { std_printf(FSTR("Successfully pinged '%s' (#%d).\r\n"), remote_host_ip, attempt); } else { std_printf(FSTR("Failed to ping '%s' (#%d).\r\n"), remote_host_ip, attempt); } attempt++; thrd_sleep(1); } return (0); }