From 6f43ba411da9466a54e27fa405c87b2e078a339a Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 21 Oct 2013 14:20:32 +0100 Subject: [PATCH] [cmdline] Add "ping" command Signed-off-by: Michael Brown --- src/config/config.c | 3 ++ src/config/general.h | 1 + src/hci/commands/ping_cmd.c | 100 ++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/hci/commands/ping_cmd.c diff --git a/src/config/config.c b/src/config/config.c index 13fa7db4..6d19370a 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -272,6 +272,9 @@ REQUIRE_OBJECT ( param_cmd ); #ifdef NEIGHBOUR_CMD REQUIRE_OBJECT ( neighbour_cmd ); #endif +#ifdef PING_CMD +REQUIRE_OBJECT ( ping_cmd ); +#endif /* * Drag in miscellaneous objects diff --git a/src/config/general.h b/src/config/general.h index 00a327f2..2af521e2 100644 --- a/src/config/general.h +++ b/src/config/general.h @@ -133,6 +133,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); //#define PCI_CMD /* PCI commands */ //#define PARAM_CMD /* Form parameter commands */ //#define NEIGHBOUR_CMD /* Neighbour management commands */ +//#define PING_CMD /* Ping command */ /* * ROM-specific options diff --git a/src/hci/commands/ping_cmd.c b/src/hci/commands/ping_cmd.c new file mode 100644 index 00000000..a042b8fa --- /dev/null +++ b/src/hci/commands/ping_cmd.c @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2013 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** @file + * + * Ping command + * + */ + +/** Default payload length */ +#define PING_DEFAULT_SIZE 64 + +/** Default timeout */ +#define PING_DEFAULT_TIMEOUT 1000 + +/** "ping" options */ +struct ping_options { + /** Payload length */ + unsigned int size; + /** Timeout (in ms) */ + unsigned int timeout; +}; + +/** "ping" option list */ +static struct option_descriptor ping_opts[] = { + OPTION_DESC ( "size", 's', required_argument, + struct ping_options, size, parse_integer ), + OPTION_DESC ( "timeout", 't', required_argument, + struct ping_options, timeout, parse_integer ), +}; + +/** "ping" command descriptor */ +static struct command_descriptor ping_cmd = + COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, + "[--size ] [--timeout ] " ); + +/** + * The "ping" command + * + * @v argc Argument count + * @v argv Argument list + * @ret rc Return status code + */ +static int ping_exec ( int argc, char **argv ) { + struct ping_options opts; + const char *hostname; + int rc; + + /* Initialise options */ + memset ( &opts, 0, sizeof ( opts ) ); + opts.size = PING_DEFAULT_SIZE; + opts.timeout = PING_DEFAULT_TIMEOUT; + + /* Parse options */ + if ( ( rc = reparse_options ( argc, argv, &ping_cmd, &opts ) ) != 0 ) + return rc; + + /* Parse hostname */ + hostname = argv[optind]; + + /* Ping */ + if ( ( rc = ping ( hostname, opts.timeout, opts.size ) ) != 0 ) + return rc; + + return 0; +} + +/** Ping command */ +struct command ping_command __command = { + .name = "ping", + .exec = ping_exec, +};