From patchwork Mon Nov 21 08:42:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xuewenqian X-Patchwork-Id: 9439005 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4EE84600BA for ; Mon, 21 Nov 2016 08:43:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 427D328A0C for ; Mon, 21 Nov 2016 08:43:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 35EE228A19; Mon, 21 Nov 2016 08:43:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 187C028A0C for ; Mon, 21 Nov 2016 08:43:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752849AbcKUInk (ORCPT ); Mon, 21 Nov 2016 03:43:40 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:31105 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752318AbcKUInk (ORCPT ); Mon, 21 Nov 2016 03:43:40 -0500 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="13162907" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 21 Nov 2016 16:43:21 +0800 Received: from email.frdc.cn.fujitsu.com (unknown [10.167.174.30]) by cn.fujitsu.com (Postfix) with ESMTP id E46C047A8683; Mon, 21 Nov 2016 16:43:18 +0800 (CST) Received: from localhost.localdomain (unknown [10.167.175.108]) by email.frdc.cn.fujitsu.com (Postfix) with ESMTP id A0BC11C0B9C; Mon, 21 Nov 2016 16:43:18 +0800 (CST) From: wsn.iot.xwq@cn.fujitsu.com To: linux-wpan@vger.kernel.org Cc: xuewenqian Subject: [PATCH wpan-tools v2] Add the support to set wpan-ping interval Date: Mon, 21 Nov 2016 08:42:54 +0000 Message-Id: <1479717774-4485-1-git-send-email-wsn.iot.xwq@cn.fujitsu.com> X-Mailer: git-send-email 1.7.10.4 MIME-Version: 1.0 X-yoursite-MailScanner-ID: E46C047A8683.A4F8B X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: wsn.iot.xwq@cn.fujitsu.com Sender: linux-wpan-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wpan@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: xuewenqian This patch adds the support to set wpan-ping interval, you can use the command `-I $interval(ms)` to set the value (default 500ms). Signed-off-by: xuewenqian Signed-off-by: Xue Wenqian --- I have added the from line, and fixed the trailing whitespace problem. Also I made some revision about the curly braces of added function block. To be note, this is my first commit on kernel. If there is any wrong with my commit, please forgive me and point it out, thank you all! wpan-ping/wpan-ping.c | 52 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/wpan-ping/wpan-ping.c b/wpan-ping/wpan-ping.c index 2c63758..9544ad0 100644 --- a/wpan-ping/wpan-ping.c +++ b/wpan-ping/wpan-ping.c @@ -49,6 +49,7 @@ #define IEEE802154_ADDR_LEN 8 /* Set the dispatch header to not 6lowpan for compat */ #define NOT_A_6LOWPAN_FRAME 0x00 +#define DEFAULT_INTERVAL 500 #define DEBUG 0 @@ -96,6 +97,7 @@ struct config { int nl802154_id; struct sockaddr_ieee802154 src; struct sockaddr_ieee802154 dst; + unsigned short interval; }; extern char *optarg; @@ -109,6 +111,7 @@ static void usage(const char *name) { "--count | -c number of packets\n" "--size | -s packet length\n" "--interface | -i listen on this interface (default wpan0)\n" + "--interval | -I wait interval in milliseconds between sending packets (default 500ms)\n" "--version | -v print out version\n" "--help | -h this usage text\n", name); } @@ -232,9 +235,29 @@ static int print_address(char *addr, uint8_t dst_extended[IEEE802154_ADDR_LEN]) return 0; } +static void sleeping(struct timeval ping_start_time, struct timeval timeout) +{ + struct timeval curr_time; + long sec, usec, interval_usec, timeout_usec; + long sleep_usec = 0; + gettimeofday(&curr_time, NULL); + sec = curr_time.tv_sec - ping_start_time.tv_sec; + usec = curr_time.tv_usec - ping_start_time.tv_usec; + if (usec < 0) { + usec += 1000000; + sec--; + } + interval_usec = sec * 1000000 + usec; + timeout_usec = timeout.tv_sec * 1000000 + timeout.tv_usec; + if (interval_usec < timeout_usec) { + sleep_usec = timeout_usec - interval_usec; + usleep(sleep_usec); + } +} + static int measure_roundtrip(struct config *conf, int sd) { unsigned char *buf; - struct timeval start_time, end_time, timeout; + struct timeval ping_start_time, start_time, end_time, timeout; long sec = 0, usec = 0; long sec_max = 0, usec_max = 0; long sec_min = 2147483647, usec_min = 2147483647; @@ -256,9 +279,15 @@ static int measure_roundtrip(struct config *conf, int sd) { conf->dst.addr.short_addr, conf->dst.addr.pan_id, conf->packet_len); buf = (unsigned char *)malloc(MAX_PAYLOAD_LEN); - /* 500ms seconds packet receive timeout */ - timeout.tv_sec = 0; - timeout.tv_usec = 500000; + /* default 500ms seconds packet receive timeout */ + if (conf->interval >= 1000) { /* when interval is larger than 1s */ + double d_interval = (conf->interval * 1.0) / 1000; + timeout.tv_sec = (int)d_interval; + timeout.tv_usec = (int)((d_interval - timeout.tv_sec) * 1000000); + } else { + timeout.tv_sec = 0; + timeout.tv_usec = conf->interval * 1000; + } ret = setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&timeout,sizeof(struct timeval)); if (ret < 0) { perror("setsockopt receive timeout"); @@ -266,6 +295,7 @@ static int measure_roundtrip(struct config *conf, int sd) { count = 0; for (i = 0; i < conf->packets; i++) { + gettimeofday(&ping_start_time, NULL); generate_packet(buf, conf, i); seq_num = (buf[2] << 8)| buf[3]; ret = sendto(sd, buf, conf->packet_len, 0, (struct sockaddr *)&conf->dst, sizeof(conf->dst)); @@ -309,7 +339,9 @@ static int measure_roundtrip(struct config *conf, int sd) { fprintf(stdout, "%i bytes from 0x%04x seq=%i time=%.1f ms\n", ret, conf->dst.addr.short_addr, (int)seq_num, (float)usec/1000); } else - fprintf(stderr, "Hit 500 ms packet timeout\n"); + fprintf(stderr, "Hit %i ms packet timeout\n", conf->interval); + /* sleeping */ + sleeping(ping_start_time, timeout); } if (count) @@ -456,6 +488,9 @@ int main(int argc, char *argv[]) { /* Default to 65535 packets being sent */ conf->packets = USHRT_MAX; + /* Default to 500ms for interval value */ + conf->interval = DEFAULT_INTERVAL; + if (argc < 2) { usage(argv[0]); exit(1); @@ -464,9 +499,9 @@ int main(int argc, char *argv[]) { while (1) { #ifdef _GNU_SOURCE int opt_idx = -1; - c = getopt_long(argc, argv, "a:ec:s:i:dvh", perf_long_opts, &opt_idx); + c = getopt_long(argc, argv, "a:ec:s:i:dvhI:", perf_long_opts, &opt_idx); #else - c = getopt(argc, argv, "a:ec:s:i:dvh"); + c = getopt(argc, argv, "a:ec:s:i:dvhI:"); #endif if (c == -1) break; @@ -495,6 +530,9 @@ int main(int argc, char *argv[]) { case 'i': conf->interface = optarg; break; + case 'I': + conf->interval = atoi(optarg); + break; case 'v': fprintf(stdout, "wpan-ping " PACKAGE_VERSION "\n"); free(conf);