Ping¶
About¶
Ping a remote host periodically once every second.
Source code¶
/**
* @file main.c
*
* @section License
* Copyright (C) 2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
/* The ip address of the host to ping. */
#define REMOTE_HOST_IP 216.58.211.142
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_ms(1000);
}
return (0);
}
The source code can also be found on Github in the examples/ping folder.
Build and run¶
Build and run the application.
$ cd examples/ping
$ make -s BOARD=<board> run
Successfully pinged '192.168.1.100' in 20 ms (#1).
Successfully pinged '192.168.1.100' in 20 ms (#2).
Successfully pinged '192.168.1.100' in 20 ms (#3).