Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split dtls_time into platform-specifc (WIP) #124

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 8 additions & 58 deletions dtls_time.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
*
* Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
* Copyright (c) 2011-2022 Olaf Bergmann (TZI) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
Expand All @@ -20,67 +20,17 @@
*/

#include "tinydtls.h"
#include "dtls_time.h"

#ifdef WITH_CONTIKI
clock_time_t dtls_clock_offset;
#if defined (WITH_CONTIKI)
#include "platform-specific/dtls_time_contiki.c"

void
dtls_clock_init(void) {
clock_init();
dtls_clock_offset = clock_time();
}
#elif defined (RIOT_VERSION)
#include "platform-specific/dtls_time_riot.c"

void
dtls_ticks(dtls_tick_t *t) {
*t = clock_time();
}
#elif defined (WITH_POSIX)
#include "platform-specific/dtls_time_posix.c"

#endif /* WITH_CONTIKI */

#ifdef RIOT_VERSION
dtls_tick_t dtls_clock_offset;

void
dtls_clock_init(void) {
dtls_clock_offset = xtimer_now64().ticks64;
}

void
dtls_ticks(dtls_tick_t *t) {
*t = xtimer_now64().ticks64 -dtls_clock_offset;
}

#endif /* RIOT_VERSION */

#ifdef WITH_POSIX
time_t dtls_clock_offset;

void
dtls_clock_init(void) {
#ifdef HAVE_TIME_H
dtls_clock_offset = time(NULL);
#else
# ifdef __GNUC__
/* Issue a warning when using gcc. Other prepropressors do
* not seem to have a similar feature. */
# warning "cannot initialize clock"
# endif
dtls_clock_offset = 0;
#endif
}
#error platform specific time functions not defined

void dtls_ticks(dtls_tick_t *t) {
#ifdef HAVE_SYS_TIME_H
struct timeval tv;
gettimeofday(&tv, NULL);
*t = (tv.tv_sec - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
+ (tv.tv_usec * DTLS_TICKS_PER_SECOND / 1000000);
#else
#error "clock not implemented"
#endif
}

#endif /* WITH_POSIX */


31 changes: 31 additions & 0 deletions platform-specific/dtls_time_contiki.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
*
* Copyright (c) 2011-2022 Olaf Bergmann (TZI) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Olaf Bergmann - initial API and implementation
*
*******************************************************************************/

#include "tinydtls.h"
#include "dtls_time.h"

clock_time_t dtls_clock_offset;

void
dtls_clock_init(void) {
clock_init();
dtls_clock_offset = clock_time();
}

void
dtls_ticks(dtls_tick_t *t) {
*t = clock_time();
}
47 changes: 47 additions & 0 deletions platform-specific/dtls_time_posix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
*
* Copyright (c) 2011-2022 Olaf Bergmann (TZI) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Olaf Bergmann - initial API and implementation
*
*******************************************************************************/

#include "tinydtls.h"
#include "dtls_time.h"

time_t dtls_clock_offset;

void
dtls_clock_init(void) {
#ifdef HAVE_TIME_H
dtls_clock_offset = time(NULL);
#else
# ifdef __GNUC__
/* Issue a warning when using gcc. Other prepropressors do
* not seem to have a similar feature. */
# warning "cannot initialize clock"
# endif
dtls_clock_offset = 0;
#endif
}

void dtls_ticks(dtls_tick_t *t) {
#ifdef HAVE_SYS_TIME_H
struct timeval tv;
gettimeofday(&tv, NULL);
*t = (tv.tv_sec - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
+ (tv.tv_usec * DTLS_TICKS_PER_SECOND / 1000000);
#else
#error "clock not implemented"
#endif
}


30 changes: 30 additions & 0 deletions platform-specific/dtls_time_riot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
*
* Copyright (c) 2011-2022 Olaf Bergmann (TZI) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Olaf Bergmann - initial API and implementation
*
*******************************************************************************/

#include "tinydtls.h"
#include "dtls_time.h"

dtls_tick_t dtls_clock_offset;

void
dtls_clock_init(void) {
dtls_clock_offset = xtimer_now64().ticks64;
}

void
dtls_ticks(dtls_tick_t *t) {
*t = xtimer_now64().ticks64 -dtls_clock_offset;
}