From patchwork Thu Aug 25 20:39:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Haws X-Patchwork-Id: 16668 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from open-mesh.org (localhost [IPv6:::1]) by open-mesh.org (Postfix) with ESMTP id A7D4A82EA5; Thu, 25 Aug 2016 22:40:07 +0200 (CEST) Authentication-Results: open-mesh.org; dmarc=none header.from=sdl.usu.edu Received: from Perses.usurf.usu.edu (unknown [129.123.197.145]) by open-mesh.org (Postfix) with ESMTPS id 0585B82CD8 for ; Thu, 25 Aug 2016 22:40:02 +0200 (CEST) Authentication-Results: open-mesh.org; dmarc=none header.from=sdl.usu.edu Received: from EK.usurf.usu.edu (172.31.35.73) by Perses.usurf.usu.edu (172.31.35.68) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Thu, 25 Aug 2016 14:40:00 -0600 Received: from PERSES.usurf.usu.edu (172.31.35.68) by Ek.usurf.usu.edu (172.31.35.73) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Thu, 25 Aug 2016 14:39:59 -0600 Received: from localhost (172.31.22.119) by PERSES.usurf.usu.edu (172.31.35.68) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Thu, 25 Aug 2016 14:39:59 -0600 From: Jonathan Haws To: Date: Thu, 25 Aug 2016 14:39:56 -0600 Message-ID: <1472157596-19774-2-git-send-email-jhaws@sdl.usu.edu> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1472157596-19774-1-git-send-email-jhaws@sdl.usu.edu> References: <1472157596-19774-1-git-send-email-jhaws@sdl.usu.edu> MIME-Version: 1.0 Cc: Jonathan Haws Subject: [B.A.T.M.A.N.] [PATCH 2/2] alfred: Adding time_subtract utility function X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking Errors-To: b.a.t.m.a.n-bounces@lists.open-mesh.org Sender: "B.A.T.M.A.N" Signed-off-by: Jonathan Haws Added time_subtract() utility function to use for simply subtracting one timespec from another. --- alfred.h | 2 ++ server.c | 5 ++++- util.c | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/alfred.h b/alfred.h index 5b7e965..194c62a 100644 --- a/alfred.h +++ b/alfred.h @@ -196,6 +196,8 @@ int netsock_own_address(const struct globals *globals, /* util.c */ int time_diff(struct timespec *tv1, struct timespec *tv2, struct timespec *tvdiff); +void time_subtract(struct timespec *tv1, struct timespec *tv2, + struct timespec *tvout); void time_random_seed(void); uint16_t get_random_id(void); bool is_valid_ether_addr(uint8_t *addr); diff --git a/server.c b/server.c index c5df945..884a1a7 100644 --- a/server.c +++ b/server.c @@ -372,7 +372,10 @@ int alfred_server(struct globals *globals) while (1) { clock_gettime(CLOCK_MONOTONIC, &now); - time_diff(&now, &globals->sync_period, &now); + + /* subtract the synchronization period from the current time */ + time_subtract(&now, &globals->sync_period, &now); + if (!time_diff(&last_check, &now, &tv)) { tv.tv_sec = 0; tv.tv_nsec = 0; diff --git a/util.c b/util.c index c7e11cc..1b67f78 100644 --- a/util.c +++ b/util.c @@ -41,6 +41,19 @@ int time_diff(struct timespec *tv1, struct timespec *tv2, return (tvdiff->tv_sec >= 0); } +void time_subtract(struct timespec *tv1, struct timespec *tv2, + struct timespec *tvout) { + tvout->tv_sec = tv1->tv_sec - tv2->tv_sec; + if (tv1->tv_nsec < tv2->tv_nsec) { + tvout->tv_nsec = 1000000000 + tv1->tv_nsec - tv2->tv_nsec; + tvout->tv_sec -= 1; + } else { + tvout->tv_nsec = tv1->tv_nsec - tv2->tv_nsec; + } + + return; +} + void time_random_seed(void) { struct timespec now;