Ping¶
About¶
Ping a remote host periodically once every second.
Source code¶
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* 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(1);
}
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).